Silly little cli celebration tool
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
5.3 KiB

5 years ago
  1. package runewidth
  2. import (
  3. "testing"
  4. )
  5. var runewidthtests = []struct {
  6. in rune
  7. out int
  8. }{
  9. {'世', 2},
  10. {'界', 2},
  11. {'セ', 1},
  12. {'カ', 1},
  13. {'イ', 1},
  14. {'☆', 2}, // double width in ambiguous
  15. {'\x00', 0},
  16. {'\x01', 1},
  17. {'\u0300', 0},
  18. }
  19. func TestRuneWidth(t *testing.T) {
  20. c := NewCondition()
  21. c.EastAsianWidth = true
  22. for _, tt := range runewidthtests {
  23. if out := c.RuneWidth(tt.in); out != tt.out {
  24. t.Errorf("Width(%q) = %q, want %q", tt.in, out, tt.out)
  25. }
  26. }
  27. }
  28. var isambiguouswidthtests = []struct {
  29. in rune
  30. out bool
  31. }{
  32. {'世', false},
  33. {'■', true},
  34. {'界', false},
  35. {'○', true},
  36. {'㈱', false},
  37. {'①', true},
  38. {'②', true},
  39. {'③', true},
  40. {'④', true},
  41. {'⑤', true},
  42. {'⑥', true},
  43. {'⑦', true},
  44. {'⑧', true},
  45. {'⑨', true},
  46. {'⑩', true},
  47. {'⑪', true},
  48. {'⑫', true},
  49. {'⑬', true},
  50. {'⑭', true},
  51. {'⑮', true},
  52. {'⑯', true},
  53. {'⑰', true},
  54. {'⑱', true},
  55. {'⑲', true},
  56. {'⑳', true},
  57. {'☆', true},
  58. }
  59. func TestIsAmbiguousWidth(t *testing.T) {
  60. for _, tt := range isambiguouswidthtests {
  61. if out := IsAmbiguousWidth(tt.in); out != tt.out {
  62. t.Errorf("IsAmbiguousWidth(%q) = %q, want %q", tt.in, out, tt.out)
  63. }
  64. }
  65. }
  66. var stringwidthtests = []struct {
  67. in string
  68. out int
  69. }{
  70. {"■㈱の世界①", 12},
  71. {"スター☆", 8},
  72. }
  73. func TestStringWidth(t *testing.T) {
  74. c := NewCondition()
  75. c.EastAsianWidth = true
  76. for _, tt := range stringwidthtests {
  77. if out := c.StringWidth(tt.in); out != tt.out {
  78. t.Errorf("StringWidth(%q) = %q, want %q", tt.in, out, tt.out)
  79. }
  80. }
  81. }
  82. func TestStringWidthInvalid(t *testing.T) {
  83. s := "こんにちわ\x00世界"
  84. if out := StringWidth(s); out != 14 {
  85. t.Errorf("StringWidth(%q) = %q, want %q", s, out, 14)
  86. }
  87. }
  88. func TestTruncateSmaller(t *testing.T) {
  89. s := "あいうえお"
  90. expected := "あいうえお"
  91. if out := Truncate(s, 10, "..."); out != expected {
  92. t.Errorf("Truncate(%q) = %q, want %q", s, out, expected)
  93. }
  94. }
  95. func TestTruncate(t *testing.T) {
  96. s := "あいうえおあいうえおえおおおおおおおおおおおおおおおおおおおおおおおおおおおおおお"
  97. expected := "あいうえおあいうえおえおおおおおおおおおおおおおおおおおおおおおおおおおおお..."
  98. out := Truncate(s, 80, "...")
  99. if out != expected {
  100. t.Errorf("Truncate(%q) = %q, want %q", s, out, expected)
  101. }
  102. width := StringWidth(out)
  103. if width != 79 {
  104. t.Errorf("width of Truncate(%q) should be %d, but %d", s, 79, width)
  105. }
  106. }
  107. func TestTruncateFit(t *testing.T) {
  108. s := "aあいうえおあいうえおえおおおおおおおおおおおおおおおおおおおおおおおおおおおおおお"
  109. expected := "aあいうえおあいうえおえおおおおおおおおおおおおおおおおおおおおおおおおおおお..."
  110. out := Truncate(s, 80, "...")
  111. if out != expected {
  112. t.Errorf("Truncate(%q) = %q, want %q", s, out, expected)
  113. }
  114. width := StringWidth(out)
  115. if width != 80 {
  116. t.Errorf("width of Truncate(%q) should be %d, but %d", s, 80, width)
  117. }
  118. }
  119. func TestTruncateJustFit(t *testing.T) {
  120. s := "あいうえおあいうえおえおおおおおおおおおおおおおおおおおおおおおおおおおおおおお"
  121. expected := "あいうえおあいうえおえおおおおおおおおおおおおおおおおおおおおおおおおおおおおお"
  122. out := Truncate(s, 80, "...")
  123. if out != expected {
  124. t.Errorf("Truncate(%q) = %q, want %q", s, out, expected)
  125. }
  126. width := StringWidth(out)
  127. if width != 80 {
  128. t.Errorf("width of Truncate(%q) should be %d, but %d", s, 80, width)
  129. }
  130. }
  131. func TestWrap(t *testing.T) {
  132. s := `東京特許許可局局長はよく柿喰う客だ/東京特許許可局局長はよく柿喰う客だ
  133. 123456789012345678901234567890
  134. END`
  135. expected := `東京特許許可局局長はよく柿喰う
  136. 客だ/東京特許許可局局長はよく
  137. 柿喰う客だ
  138. 123456789012345678901234567890
  139. END`
  140. if out := Wrap(s, 30); out != expected {
  141. t.Errorf("Wrap(%q) = %q, want %q", s, out, expected)
  142. }
  143. }
  144. func TestTruncateNoNeeded(t *testing.T) {
  145. s := "あいうえおあい"
  146. expected := "あいうえおあい"
  147. if out := Truncate(s, 80, "..."); out != expected {
  148. t.Errorf("Truncate(%q) = %q, want %q", s, out, expected)
  149. }
  150. }
  151. var isneutralwidthtests = []struct {
  152. in rune
  153. out bool
  154. }{
  155. {'→', false},
  156. {'┊', false},
  157. {'┈', false},
  158. {'~', false},
  159. {'└', false},
  160. {'⣀', true},
  161. {'⣀', true},
  162. }
  163. func TestIsNeutralWidth(t *testing.T) {
  164. for _, tt := range isneutralwidthtests {
  165. if out := IsNeutralWidth(tt.in); out != tt.out {
  166. t.Errorf("IsNeutralWidth(%q) = %q, want %q", tt.in, out, tt.out)
  167. }
  168. }
  169. }
  170. func TestFillLeft(t *testing.T) {
  171. s := "あxいうえお"
  172. expected := " あxいうえお"
  173. if out := FillLeft(s, 15); out != expected {
  174. t.Errorf("FillLeft(%q) = %q, want %q", s, out, expected)
  175. }
  176. }
  177. func TestFillLeftFit(t *testing.T) {
  178. s := "あいうえお"
  179. expected := "あいうえお"
  180. if out := FillLeft(s, 10); out != expected {
  181. t.Errorf("FillLeft(%q) = %q, want %q", s, out, expected)
  182. }
  183. }
  184. func TestFillRight(t *testing.T) {
  185. s := "あxいうえお"
  186. expected := "あxいうえお "
  187. if out := FillRight(s, 15); out != expected {
  188. t.Errorf("FillRight(%q) = %q, want %q", s, out, expected)
  189. }
  190. }
  191. func TestFillRightFit(t *testing.T) {
  192. s := "あいうえお"
  193. expected := "あいうえお"
  194. if out := FillRight(s, 10); out != expected {
  195. t.Errorf("FillRight(%q) = %q, want %q", s, out, expected)
  196. }
  197. }