table_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package table
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "strings"
  8. "testing"
  9. "github.com/mattn/go-runewidth"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestFormatter(t *testing.T) {
  13. t.Parallel()
  14. var formatter Formatter
  15. fn := func(a string, b ...interface{}) string { return "" }
  16. f := Formatter(fn)
  17. assert.IsType(t, formatter, f)
  18. }
  19. func TestTable_New(t *testing.T) {
  20. t.Parallel()
  21. buf := bytes.Buffer{}
  22. New("foo", "bar").WithWriter(&buf).Print()
  23. out := buf.String()
  24. assert.Contains(t, out, "foo")
  25. assert.Contains(t, out, "bar")
  26. buf.Reset()
  27. New().WithWriter(&buf).Print()
  28. out = buf.String()
  29. assert.Empty(t, strings.TrimSpace(out))
  30. }
  31. func TestTable_WithHeaderFormatter(t *testing.T) {
  32. t.Parallel()
  33. uppercase := func(f string, v ...interface{}) string {
  34. return strings.ToUpper(fmt.Sprintf(f, v...))
  35. }
  36. buf := bytes.Buffer{}
  37. tbl := New("foo", "bar").WithWriter(&buf).WithHeaderFormatter(uppercase)
  38. tbl.Print()
  39. out := buf.String()
  40. assert.Contains(t, out, "FOO")
  41. assert.Contains(t, out, "BAR")
  42. buf.Reset()
  43. tbl.WithHeaderFormatter(nil).Print()
  44. out = buf.String()
  45. assert.Contains(t, out, "foo")
  46. assert.Contains(t, out, "bar")
  47. }
  48. func TestTable_WithFirstColumnFormatter(t *testing.T) {
  49. t.Parallel()
  50. uppercase := func(f string, v ...interface{}) string {
  51. return strings.ToUpper(fmt.Sprintf(f, v...))
  52. }
  53. buf := bytes.Buffer{}
  54. tbl := New("foo", "bar").WithWriter(&buf).WithFirstColumnFormatter(uppercase).AddRow("fizz", "buzz")
  55. tbl.Print()
  56. out := buf.String()
  57. assert.Contains(t, out, "foo")
  58. assert.Contains(t, out, "bar")
  59. assert.Contains(t, out, "FIZZ")
  60. assert.Contains(t, out, "buzz")
  61. buf.Reset()
  62. tbl.WithFirstColumnFormatter(nil).Print()
  63. out = buf.String()
  64. assert.Contains(t, out, "fizz")
  65. assert.Contains(t, out, "buzz")
  66. }
  67. func TestTable_WithPadding(t *testing.T) {
  68. t.Parallel()
  69. // zero value
  70. buf := bytes.Buffer{}
  71. tbl := New("foo", "bar").WithWriter(&buf).WithPadding(0)
  72. tbl.Print()
  73. out := buf.String()
  74. assert.Contains(t, out, "foobar")
  75. // positive value
  76. buf.Reset()
  77. tbl.WithPadding(4).Print()
  78. out = buf.String()
  79. assert.Contains(t, out, "foo bar ")
  80. // negative value
  81. buf.Reset()
  82. tbl.WithPadding(-1).Print()
  83. out = buf.String()
  84. assert.Contains(t, out, "foobar")
  85. }
  86. func TestTable_WithWriter(t *testing.T) {
  87. t.Parallel()
  88. // not that we haven't been using it in all these tests but:
  89. buf := bytes.Buffer{}
  90. New("foo", "bar").WithWriter(&buf).Print()
  91. assert.NotEmpty(t, buf.String())
  92. stdout := os.Stdout
  93. temp, _ := ioutil.TempFile("", "")
  94. os.Stdout = temp
  95. defer func() {
  96. os.Stdout = stdout
  97. temp.Close()
  98. }()
  99. New("foo", "bar").WithWriter(nil).Print()
  100. temp.Seek(0, 0)
  101. out, _ := ioutil.ReadAll(temp)
  102. assert.NotEmpty(t, out)
  103. }
  104. func TestTable_AddRow(t *testing.T) {
  105. t.Parallel()
  106. buf := bytes.Buffer{}
  107. tbl := New("foo", "bar").WithWriter(&buf).AddRow("fizz", "buzz")
  108. tbl.Print()
  109. out := buf.String()
  110. assert.Contains(t, out, "fizz")
  111. assert.Contains(t, out, "buzz")
  112. lines := strings.Count(out, "\n")
  113. // empty should add empty line
  114. buf.Reset()
  115. tbl.AddRow().Print()
  116. assert.Equal(t, lines+1, strings.Count(buf.String(), "\n"))
  117. // less than one will fill left-to-right
  118. buf.Reset()
  119. tbl.AddRow("cat").Print()
  120. assert.Contains(t, buf.String(), "\ncat")
  121. // more than initial length are truncated
  122. buf.Reset()
  123. tbl.AddRow("bippity", "boppity", "boo").Print()
  124. assert.NotContains(t, buf.String(), "boo")
  125. }
  126. func TestTable_WithWidthFunc(t *testing.T) {
  127. t.Parallel()
  128. buf := bytes.Buffer{}
  129. New("", "").
  130. WithWriter(&buf).
  131. WithPadding(1).
  132. WithWidthFunc(runewidth.StringWidth).
  133. AddRow("请求", "alpha").
  134. AddRow("abc", "beta").
  135. Print()
  136. actual := buf.String()
  137. assert.Contains(t, actual, "请求 alpha")
  138. assert.Contains(t, actual, "abc beta")
  139. }