stop_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package runner
  2. import (
  3. "testing"
  4. )
  5. func TestTruncateStop(t *testing.T) {
  6. tests := []struct {
  7. name string
  8. sequence string
  9. stop string
  10. expected string
  11. expectedTrunc bool
  12. }{
  13. {
  14. name: "Single word",
  15. sequence: "helloworld",
  16. stop: "world",
  17. expected: "hello",
  18. expectedTrunc: true,
  19. },
  20. {
  21. name: "Partial",
  22. sequence: "hellowor",
  23. stop: "or",
  24. expected: "hellow",
  25. expectedTrunc: true,
  26. },
  27. {
  28. name: "Suffix",
  29. sequence: "Hello there!",
  30. stop: "!",
  31. expected: "Hello there",
  32. expectedTrunc: true,
  33. },
  34. {
  35. name: "Middle",
  36. sequence: "hello wor",
  37. stop: "llo w",
  38. expected: "he",
  39. expectedTrunc: true,
  40. },
  41. {
  42. name: "No stop found",
  43. sequence: "hello world",
  44. stop: "xyz",
  45. expected: "hello world",
  46. expectedTrunc: false,
  47. },
  48. }
  49. for _, tt := range tests {
  50. t.Run(tt.name, func(t *testing.T) {
  51. result, truncated := truncateStop(tt.sequence, tt.stop)
  52. if result != tt.expected || truncated != tt.expectedTrunc {
  53. t.Errorf("truncateStop(%q, %q): have %q (%v); want %q (%v)",
  54. tt.sequence, tt.stop, result, truncated, 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. }