wrap_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package text
  2. import (
  3. "bytes"
  4. "testing"
  5. )
  6. var text = "The quick brown fox jumps over the lazy dog."
  7. func TestWrap(t *testing.T) {
  8. exp := [][]string{
  9. {"The", "quick", "brown", "fox"},
  10. {"jumps", "over", "the", "lazy", "dog."},
  11. }
  12. words := bytes.Split([]byte(text), sp)
  13. got := WrapWords(words, 1, 24, defaultPenalty)
  14. if len(exp) != len(got) {
  15. t.Fail()
  16. }
  17. for i := range exp {
  18. if len(exp[i]) != len(got[i]) {
  19. t.Fail()
  20. }
  21. for j := range exp[i] {
  22. if exp[i][j] != string(got[i][j]) {
  23. t.Fatal(i, exp[i][j], got[i][j])
  24. }
  25. }
  26. }
  27. }
  28. func TestWrapNarrow(t *testing.T) {
  29. exp := "The\nquick\nbrown\nfox\njumps\nover\nthe\nlazy\ndog."
  30. if Wrap(text, 5) != exp {
  31. t.Fail()
  32. }
  33. }
  34. func TestWrapOneLine(t *testing.T) {
  35. exp := "The quick brown fox jumps over the lazy dog."
  36. if Wrap(text, 500) != exp {
  37. t.Fail()
  38. }
  39. }
  40. func TestWrapBug1(t *testing.T) {
  41. cases := []struct {
  42. limit int
  43. text string
  44. want string
  45. }{
  46. {4, "aaaaa", "aaaaa"},
  47. {4, "a aaaaa", "a\naaaaa"},
  48. }
  49. for _, test := range cases {
  50. got := Wrap(test.text, test.limit)
  51. if got != test.want {
  52. t.Errorf("Wrap(%q, %d) = %q want %q", test.text, test.limit, got, test.want)
  53. }
  54. }
  55. }