stop_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package common
  2. import (
  3. "reflect"
  4. "testing"
  5. )
  6. func TestTruncateStop(t *testing.T) {
  7. tests := []struct {
  8. name string
  9. pieces []string
  10. stop string
  11. expected []string
  12. expectedTrunc bool
  13. }{
  14. {
  15. name: "Single word",
  16. pieces: []string{"hello", "world"},
  17. stop: "world",
  18. expected: []string{"hello"},
  19. expectedTrunc: false,
  20. },
  21. {
  22. name: "Partial",
  23. pieces: []string{"hello", "wor"},
  24. stop: "or",
  25. expected: []string{"hello", "w"},
  26. expectedTrunc: true,
  27. },
  28. {
  29. name: "Suffix",
  30. pieces: []string{"Hello", " there", "!"},
  31. stop: "!",
  32. expected: []string{"Hello", " there"},
  33. expectedTrunc: false,
  34. },
  35. {
  36. name: "Suffix partial",
  37. pieces: []string{"Hello", " the", "re!"},
  38. stop: "there!",
  39. expected: []string{"Hello", " "},
  40. expectedTrunc: true,
  41. },
  42. {
  43. name: "Middle",
  44. pieces: []string{"hello", " wor"},
  45. stop: "llo w",
  46. expected: []string{"he"},
  47. expectedTrunc: true,
  48. },
  49. }
  50. for _, tt := range tests {
  51. t.Run(tt.name, func(t *testing.T) {
  52. result, resultTrunc := TruncateStop(tt.pieces, tt.stop)
  53. if !reflect.DeepEqual(result, tt.expected) || resultTrunc != tt.expectedTrunc {
  54. t.Errorf("truncateStop(%v, %s): have %v (%v); want %v (%v)", tt.pieces, tt.stop, result, resultTrunc, tt.expected, tt.expectedTrunc)
  55. }
  56. })
  57. }
  58. }
  59. func TestIncompleteUnicode(t *testing.T) {
  60. tests := []struct {
  61. name string
  62. input string
  63. expected bool
  64. }{
  65. {
  66. name: "Basic",
  67. input: "hi",
  68. expected: false,
  69. },
  70. {
  71. name: "Two byte",
  72. input: "hi" + string([]byte{0xc2, 0xa3}),
  73. expected: false,
  74. },
  75. {
  76. name: "Two byte - missing last",
  77. input: "hi" + string([]byte{0xc2}),
  78. expected: true,
  79. },
  80. {
  81. name: "Three byte",
  82. input: "hi" + string([]byte{0xe0, 0xA0, 0x80}),
  83. expected: false,
  84. },
  85. {
  86. name: "Three byte - missing last",
  87. input: "hi" + string([]byte{0xe0, 0xA0}),
  88. expected: true,
  89. },
  90. {
  91. name: "Three byte - missing last 2",
  92. input: "hi" + string([]byte{0xe0}),
  93. expected: true,
  94. },
  95. {
  96. name: "Four byte",
  97. input: "hi" + string([]byte{0xf0, 0x92, 0x8a, 0xb7}),
  98. expected: false,
  99. },
  100. {
  101. name: "Four byte - missing last",
  102. input: "hi" + string([]byte{0xf0, 0x92, 0x8a}),
  103. expected: true,
  104. },
  105. {
  106. name: "Four byte - missing last 2",
  107. input: "hi" + string([]byte{0xf0, 0x92}),
  108. expected: true,
  109. },
  110. {
  111. name: "Four byte - missing last 3",
  112. input: "hi" + string([]byte{0xf0}),
  113. expected: true,
  114. },
  115. }
  116. for _, tt := range tests {
  117. t.Run(tt.name, func(t *testing.T) {
  118. result := IncompleteUnicode(tt.input)
  119. if result != tt.expected {
  120. t.Errorf("incompleteUnicode(%s): have %v; want %v", tt.input, result, tt.expected)
  121. }
  122. })
  123. }
  124. }