indent_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package text
  2. import (
  3. "bytes"
  4. "testing"
  5. )
  6. type T struct {
  7. inp, exp, pre string
  8. }
  9. var tests = []T{
  10. {
  11. "The quick brown fox\njumps over the lazy\ndog.\nBut not quickly.\n",
  12. "xxxThe quick brown fox\nxxxjumps over the lazy\nxxxdog.\nxxxBut not quickly.\n",
  13. "xxx",
  14. },
  15. {
  16. "The quick brown fox\njumps over the lazy\ndog.\n\nBut not quickly.",
  17. "xxxThe quick brown fox\nxxxjumps over the lazy\nxxxdog.\n\nxxxBut not quickly.",
  18. "xxx",
  19. },
  20. }
  21. func TestIndent(t *testing.T) {
  22. for _, test := range tests {
  23. got := Indent(test.inp, test.pre)
  24. if got != test.exp {
  25. t.Errorf("mismatch %q != %q", got, test.exp)
  26. }
  27. }
  28. }
  29. type IndentWriterTest struct {
  30. inp, exp string
  31. pre []string
  32. }
  33. var ts = []IndentWriterTest{
  34. {
  35. `
  36. The quick brown fox
  37. jumps over the lazy
  38. dog.
  39. But not quickly.
  40. `[1:],
  41. `
  42. xxxThe quick brown fox
  43. xxxjumps over the lazy
  44. xxxdog.
  45. xxxBut not quickly.
  46. `[1:],
  47. []string{"xxx"},
  48. },
  49. {
  50. `
  51. The quick brown fox
  52. jumps over the lazy
  53. dog.
  54. But not quickly.
  55. `[1:],
  56. `
  57. xxaThe quick brown fox
  58. xxxjumps over the lazy
  59. xxxdog.
  60. xxxBut not quickly.
  61. `[1:],
  62. []string{"xxa", "xxx"},
  63. },
  64. {
  65. `
  66. The quick brown fox
  67. jumps over the lazy
  68. dog.
  69. But not quickly.
  70. `[1:],
  71. `
  72. xxaThe quick brown fox
  73. xxbjumps over the lazy
  74. xxcdog.
  75. xxxBut not quickly.
  76. `[1:],
  77. []string{"xxa", "xxb", "xxc", "xxx"},
  78. },
  79. {
  80. `
  81. The quick brown fox
  82. jumps over the lazy
  83. dog.
  84. But not quickly.`[1:],
  85. `
  86. xxaThe quick brown fox
  87. xxxjumps over the lazy
  88. xxxdog.
  89. xxx
  90. xxxBut not quickly.`[1:],
  91. []string{"xxa", "xxx"},
  92. },
  93. }
  94. func TestIndentWriter(t *testing.T) {
  95. for _, test := range ts {
  96. b := new(bytes.Buffer)
  97. pre := make([][]byte, len(test.pre))
  98. for i := range test.pre {
  99. pre[i] = []byte(test.pre[i])
  100. }
  101. w := NewIndentWriter(b, pre...)
  102. if _, err := w.Write([]byte(test.inp)); err != nil {
  103. t.Error(err)
  104. }
  105. if got := b.String(); got != test.exp {
  106. t.Errorf("mismatch %q != %q", got, test.exp)
  107. t.Log(got)
  108. t.Log(test.exp)
  109. }
  110. }
  111. }