stringutil_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package stringutil
  10. import (
  11. "bytes"
  12. "fmt"
  13. "regexp"
  14. "strings"
  15. "sync"
  16. "testing"
  17. )
  18. func TestLongestCommonPrefix(t *testing.T) {
  19. if res := LongestCommonPrefix([]string{}); res != "" {
  20. t.Error("Unexpected result:", res)
  21. return
  22. }
  23. if res := LongestCommonPrefix([]string{"test"}); res != "test" {
  24. t.Error("Unexpected result:", res)
  25. return
  26. }
  27. if res := LongestCommonPrefix([]string{"tester", "test"}); res != "test" {
  28. t.Error("Unexpected result:", res)
  29. return
  30. }
  31. if res := LongestCommonPrefix([]string{"foo", "test"}); res != "" {
  32. t.Error("Unexpected result:", res)
  33. return
  34. }
  35. if res := LongestCommonPrefix([]string{"foo", "test"}); res != "" {
  36. t.Error("Unexpected result:", res)
  37. return
  38. }
  39. if res := LongestCommonPrefix([]string{"foo2", "foo1", "footest"}); res != "foo" {
  40. t.Error("Unexpected result:", res)
  41. return
  42. }
  43. }
  44. func TestPrintStringTable(t *testing.T) {
  45. if res := PrintStringTable(nil, 0); res != "" {
  46. t.Error("Unexpected result:\n", "#\n"+res+"#")
  47. return
  48. }
  49. test1 := []string{"foo", "bar", "tester", "1", "xxx", "test", "te"}
  50. if res := PrintStringTable(test1, 4); res != `
  51. foo bar tester 1
  52. xxx test te
  53. `[1:] {
  54. t.Error("Unexpected result:\n", "#"+res+"#")
  55. return
  56. }
  57. if res := PrintStringTable(test1, 3); res != `
  58. foo bar tester
  59. 1 xxx test
  60. te
  61. `[1:] {
  62. t.Error("Unexpected result:\n", "#"+res+"#")
  63. return
  64. }
  65. }
  66. func TestRuneSlice(t *testing.T) {
  67. sl := StringToRuneSlice("test")
  68. if fmt.Sprint(sl) != "[116 101 115 116]" {
  69. t.Error("Unexpected result:", sl)
  70. return
  71. }
  72. if RuneSliceToString(sl) != "test" {
  73. t.Error("Unexpected result:", sl)
  74. return
  75. }
  76. }
  77. func TestPluralCompareByteArray(t *testing.T) {
  78. if fmt.Sprintf("There are 2 test%s", Plural(2)) != "There are 2 tests" {
  79. t.Error("2 items should have an 's'")
  80. return
  81. }
  82. if fmt.Sprintf("There is 1 test%s", Plural(1)) != "There is 1 test" {
  83. t.Error("1 item should have no 's'")
  84. return
  85. }
  86. if fmt.Sprintf("There are 0 test%s", Plural(0)) != "There are 0 tests" {
  87. t.Error("0 items should have an 's'")
  88. return
  89. }
  90. }
  91. func TestGlobToRegex(t *testing.T) {
  92. globMatch(t, true, "*", "^$", "foo", "bar")
  93. globMatch(t, true, "?", "?", "^", "[", "]", "$")
  94. globMatch(t, true, "foo*", "foo", "food", "fool")
  95. globMatch(t, true, "f*d", "fud", "food")
  96. globMatch(t, true, "*d", "good", "bad")
  97. globMatch(t, true, "\\*\\?\\[\\{\\\\", "*?[{\\")
  98. globMatch(t, true, "[]^-]", "]", "-", "^")
  99. globMatch(t, true, "]", "]")
  100. globMatch(t, true, "^.$()|+", "^.$()|+")
  101. globMatch(t, true, "[^^]", ".", "$", "[", "]")
  102. globMatch(t, false, "[^^]", "^")
  103. globMatch(t, true, "[!!-]", "^", "?")
  104. globMatch(t, false, "[!!-]", "!", "-")
  105. globMatch(t, true, "{[12]*,[45]*,[78]*}", "1", "2!", "4", "42", "7", "7$")
  106. globMatch(t, false, "{[12]*,[45]*,[78]*}", "3", "6", "9ß")
  107. globMatch(t, true, "}", "}")
  108. globMatch(t, true, "abc,", "abc,")
  109. globMatch(t, true, "myfile[^9]", "myfile1")
  110. globMatch(t, true, "myfile[!9]", "myfile1")
  111. globMatch(t, false, "myfile[^9]", "myfile9")
  112. globMatch(t, false, "myfile[!9]", "myfile9")
  113. globMatch(t, true, "*.*", "tester/bla.txt")
  114. globMatch(t, false, "*.tmp", "tester/bla.txt")
  115. testdata := []string{"foo*test", "f?t", "*d", "all"}
  116. expected := []string{"foo", "f", "", "all"}
  117. for i, str := range testdata {
  118. res := GlobStartingLiterals(str)
  119. if res != expected[i] {
  120. t.Error("Unexpected starting literal for glob:", res, "str:",
  121. str, "expected:", expected[i])
  122. }
  123. }
  124. testdata = []string{"[", "{", "\\", "*.*\\", "[["}
  125. expected = []string{"Unclosed character class at 1 of [",
  126. "Unclosed group at 1 of {",
  127. "Missing escaped character at 1 of \\",
  128. "Missing escaped character at 4 of *.*\\",
  129. "Unclosed character class at 1 of [["}
  130. for i, str := range testdata {
  131. _, err := GlobToRegex(str)
  132. if err.Error() != expected[i] {
  133. t.Error("Unexpected error for glob:", err, "str:",
  134. str, "expected error:", expected[i])
  135. }
  136. }
  137. if str, err := GlobToRegex("[][]"); str != "[][]" || err != nil {
  138. t.Error("Unecpected glob parsing result:", str, err)
  139. }
  140. if str, err := GlobToRegex(")"); str != "\\)" || err != nil {
  141. t.Error("Unecpected glob parsing result:", str, err)
  142. }
  143. }
  144. func globMatch(t *testing.T, expectedResult bool, glob string, testStrings ...string) {
  145. re, err := GlobToRegex(glob)
  146. if err != nil {
  147. t.Error("Glob parsing error:", err)
  148. }
  149. for _, testString := range testStrings {
  150. res, err := regexp.MatchString(re, testString)
  151. if err != nil {
  152. t.Error("Regexp", re, "parsing error:", err, "from glob", glob)
  153. }
  154. if res != expectedResult {
  155. t.Error("Unexpected evaluation result. Glob:", glob, "testString:",
  156. testString, "expectedResult:", expectedResult)
  157. }
  158. }
  159. }
  160. func TestLevenshteinDistance(t *testing.T) {
  161. testdata1 := []string{"", "a", "", "abc", "", "a", "abc", "a", "b", "ac",
  162. "abcdefg", "a", "ab", "example", "sturgeon", "levenshtein", "distance"}
  163. testdata2 := []string{"", "", "a", "", "abc", "a", "abc", "ab", "ab", "abc",
  164. "xabxcdxxefxgx", "b", "ac", "samples", "urgently", "frankenstein", "difference"}
  165. expected := []int{0, 1, 1, 3, 3, 0, 0, 1, 1, 1, 6, 1, 1,
  166. 3, 6, 6, 5}
  167. for i, str1 := range testdata1 {
  168. res := LevenshteinDistance(str1, testdata2[i])
  169. if res != expected[i] {
  170. t.Error("Unexpected Levenshtein distance result:", res, "str1:",
  171. str1, "str2:", testdata2[i], "expected:", expected[i])
  172. }
  173. }
  174. }
  175. func TestVersionStringCompare(t *testing.T) {
  176. testdata1 := []string{"1", "1.1", "1.1", "2.1", "5.4.3.2.1", "1.674.2.18",
  177. "1.674.2", "1.674.2.5", "2.4.18.14smp", "2.4.18.15smp", "1.2.3a1",
  178. "2.18.15smp"}
  179. testdata2 := []string{"2", "2.0", "1.1", "2.0", "6.5.4.3.2", "1.674.2.5",
  180. "1.674.2.5", "1.674.2", "2.4.18.14smp", "2.4.18.14smp", "1.2.3b1",
  181. "2.4.18.14smp"}
  182. expected := []int{-1, -1, 0, 1, -1, 1, -1, 1, 0, 1, -1, 1}
  183. for i, str1 := range testdata1 {
  184. res := VersionStringCompare(str1, testdata2[i])
  185. if res != expected[i] {
  186. t.Error("Unexpected version string compare result:", res, "str1:",
  187. str1, "str2:", testdata2[i])
  188. }
  189. }
  190. }
  191. func TestVersionStringPartCompare(t *testing.T) {
  192. testdata1 := []string{"", "", "1", "1", "a", "1a", "a", "1a", "1a", "1", "12a", "12a1",
  193. "12a1"}
  194. testdata2 := []string{"", "1", "", "2", "b", "b", "2b", "2b", "1", "1b", "12b", "12a2",
  195. "12b1"}
  196. expected := []int{0, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1}
  197. for i, str1 := range testdata1 {
  198. res := versionStringPartCompare(str1, testdata2[i])
  199. if res != expected[i] {
  200. t.Error("Unexpected version string compare result:", res, "str1:",
  201. str1, "str2:", testdata2[i])
  202. }
  203. }
  204. }
  205. func TestIsAlphaNumeric(t *testing.T) {
  206. testdata := []string{"test", "123test", "test1234_123", "test#", "test-"}
  207. expected := []bool{true, true, true, false, false}
  208. for i, str := range testdata {
  209. if IsAlphaNumeric(str) != expected[i] {
  210. t.Error("Unexpected result for alphanumeric test:", str)
  211. }
  212. }
  213. }
  214. func TestIsTrueValue(t *testing.T) {
  215. testdata := []string{"1", "ok", "1", "FaLse", "0"}
  216. expected := []bool{true, true, true, false, false}
  217. for i, str := range testdata {
  218. if IsTrueValue(str) != expected[i] {
  219. t.Error("Unexpected result for alphanumeric test:", str)
  220. }
  221. }
  222. }
  223. func TestIndexOf(t *testing.T) {
  224. slice := []string{"foo", "bar", "test"}
  225. if res := IndexOf("foo", slice); res != 0 {
  226. t.Error("Unexpected result", res)
  227. return
  228. }
  229. if res := IndexOf("bar", slice); res != 1 {
  230. t.Error("Unexpected result", res)
  231. return
  232. }
  233. if res := IndexOf("test", slice); res != 2 {
  234. t.Error("Unexpected result", res)
  235. return
  236. }
  237. if res := IndexOf("hans", slice); res != -1 {
  238. t.Error("Unexpected result", res)
  239. return
  240. }
  241. }
  242. func TestMapKeys(t *testing.T) {
  243. testMap := map[string]interface{}{
  244. "1": "2",
  245. "3": "4",
  246. "5": "6",
  247. }
  248. if res := MapKeys(testMap); fmt.Sprint(res) != "[1 3 5]" {
  249. t.Error("Unexpected result:", res)
  250. return
  251. }
  252. }
  253. func TestGenerateRollingString(t *testing.T) {
  254. testdata := []string{"_-=-_", "abc", "=", ""}
  255. testlen := []int{20, 4, 5, 100}
  256. expected := []string{"_-=-__-=-__-=-__-=-_", "abca", "=====", ""}
  257. for i, str := range testdata {
  258. res := GenerateRollingString(str, testlen[i])
  259. if res != expected[i] {
  260. t.Error("Unexpected result for creating a roling string from:", str,
  261. "result:", res, "expected:", expected[i])
  262. }
  263. }
  264. }
  265. func TestConvertToString(t *testing.T) {
  266. if res := ConvertToString(""); res != "" {
  267. t.Error("Unexpected result:", res)
  268. return
  269. }
  270. if res := ConvertToString("test"); res != "test" {
  271. t.Error("Unexpected result:", res)
  272. return
  273. }
  274. if res := ConvertToString(4.123); res != "4.123" {
  275. t.Error("Unexpected result:", res)
  276. return
  277. }
  278. if res := ConvertToString(6); res != "6" {
  279. t.Error("Unexpected result:", res)
  280. return
  281. }
  282. if res := ConvertToString(map[string]int{"z": 1, "d": 2, "a": 4}); res != `{"a":4,"d":2,"z":1}` {
  283. t.Error("Unexpected result:", res)
  284. return
  285. }
  286. if res := ConvertToString([]int{1, 2, 3}); res != "[1,2,3]" {
  287. t.Error("Unexpected result:", res)
  288. return
  289. }
  290. if res := ConvertToString(map[interface{}]interface{}{"z": 1, "d": 2, "a": 4}); res != `{"a":4,"d":2,"z":1}` {
  291. t.Error("Unexpected result:", res)
  292. return
  293. }
  294. if res := ConvertToString(map[interface{}]interface{}{"z": []interface{}{1, 2, 3}, "d": 2, "a": 4}); res != `{"a":4,"d":2,"z":[1,2,3]}` {
  295. t.Error("Unexpected result:", res)
  296. return
  297. }
  298. if res := ConvertToString([]interface{}{1, sync.Mutex{}, 3}); res != `[1,{},3]` {
  299. t.Error("Unexpected result:", res)
  300. return
  301. }
  302. if res := ConvertToString([]interface{}{1, map[interface{}]interface{}{1: 2}, 3}); res != `[1,{"1":2},3]` {
  303. t.Error("Unexpected result:", res)
  304. return
  305. }
  306. if res := ConvertToString(&bytes.Buffer{}); res != "" {
  307. t.Error("Unexpected result:", res)
  308. return
  309. }
  310. // Not much to do with such a construct but we shouldn't fail!
  311. type foo struct{ i int }
  312. x := make(map[foo]foo)
  313. x[foo{1}] = foo{2}
  314. if res := ConvertToString(x); res != "map[{1}:{2}]" {
  315. t.Error("Unexpected result:", res)
  316. return
  317. }
  318. }
  319. func TestConvertToPrettyString(t *testing.T) {
  320. if res := ConvertToPrettyString(""); res != `""` {
  321. t.Error("Unexpected result:", res)
  322. return
  323. }
  324. if res := ConvertToPrettyString("test"); res != `"test"` {
  325. t.Error("Unexpected result:", res)
  326. return
  327. }
  328. if res := ConvertToPrettyString(4.123); res != "4.123" {
  329. t.Error("Unexpected result:", res)
  330. return
  331. }
  332. if res := ConvertToString(6); res != "6" {
  333. t.Error("Unexpected result:", res)
  334. return
  335. }
  336. if res := ConvertToPrettyString(map[string]int{"z": 1, "d": 2, "a": 4}); res != `{
  337. "a": 4,
  338. "d": 2,
  339. "z": 1
  340. }` {
  341. t.Error("Unexpected result:", res)
  342. return
  343. }
  344. if res := ConvertToPrettyString([]int{1, 2, 3}); res != `[
  345. 1,
  346. 2,
  347. 3
  348. ]` {
  349. t.Error("Unexpected result:", res)
  350. return
  351. }
  352. if res := ConvertToPrettyString(map[interface{}]interface{}{"z": 1, "d": 2, "a": 4}); res != `{
  353. "a": 4,
  354. "d": 2,
  355. "z": 1
  356. }` {
  357. t.Error("Unexpected result:", res)
  358. return
  359. }
  360. if res := ConvertToPrettyString(map[interface{}]interface{}{"z": []interface{}{1, 2, 3}, "d": 2, "a": 4}); res != `{
  361. "a": 4,
  362. "d": 2,
  363. "z": [
  364. 1,
  365. 2,
  366. 3
  367. ]
  368. }` {
  369. t.Error("Unexpected result:", res)
  370. return
  371. }
  372. if res := ConvertToPrettyString([]interface{}{1, sync.Mutex{}, 3}); res != `[
  373. 1,
  374. {},
  375. 3
  376. ]` {
  377. t.Error("Unexpected result:", res)
  378. return
  379. }
  380. if res := ConvertToPrettyString([]interface{}{1, map[interface{}]interface{}{1: 2}, 3}); res != `[
  381. 1,
  382. {
  383. "1": 2
  384. },
  385. 3
  386. ]` {
  387. t.Error("Unexpected result:", res)
  388. return
  389. }
  390. if res := ConvertToPrettyString(&bytes.Buffer{}); res != "{}" {
  391. t.Error("Unexpected result:", res)
  392. return
  393. }
  394. // Not much to do with such a construct but we shouldn't fail!
  395. type foo struct{ i int }
  396. x := make(map[foo]foo)
  397. x[foo{1}] = foo{2}
  398. if res := ConvertToPrettyString(x); res != "map[{1}:{2}]" {
  399. t.Error("Unexpected result:", res)
  400. return
  401. }
  402. }
  403. func TestMD5HexString(t *testing.T) {
  404. res := MD5HexString("This is a test")
  405. if res != "ce114e4501d2f4e2dcea3e17b546f339" {
  406. t.Error("Unexpected md5 hex result", res)
  407. }
  408. }
  409. func TestLengthConstantEquals(t *testing.T) {
  410. if !LengthConstantEquals([]byte("test1"), []byte("test1")) {
  411. t.Error("Unexpected result")
  412. return
  413. }
  414. if LengthConstantEquals([]byte("test1"), []byte("test2")) {
  415. t.Error("Unexpected result")
  416. return
  417. }
  418. if LengthConstantEquals([]byte("test1"), []byte("test2test123")) {
  419. t.Error("Unexpected result")
  420. return
  421. }
  422. }
  423. func TestPrintGraphicStringTable(t *testing.T) {
  424. if res := PrintGraphicStringTable(nil, 0, 5, nil); res != "" {
  425. t.Error("Unexpected result:\n", "#\n"+res+"#")
  426. return
  427. }
  428. if res := PrintGraphicStringTable([]string{}, 4, 5, SingleLineTable); res != `
  429. ┌┐
  430. └┘
  431. `[1:] {
  432. t.Error("Unexpected result:\n", "#\n"+res+"#")
  433. return
  434. }
  435. if res := PrintCSVTable([]string{}, 4); res != "" {
  436. t.Error("Unexpected result:\n", "#\n"+res+"#")
  437. return
  438. }
  439. test1 := []string{"foo", "bar", "tester", "1", "xxx", "test", "te", "foo",
  440. "bar", "tester", "1"}
  441. if res := PrintGraphicStringTable(test1, 4, 5, SingleLineTable); res != `
  442. ┌────┬───────┬───────┬────┐
  443. │foo │bar │tester │1 │
  444. │xxx │test │te │foo │
  445. │bar │tester │1 │ │
  446. └────┴───────┴───────┴────┘
  447. `[1:] {
  448. t.Error("Unexpected result:\n", "#\n"+res+"#")
  449. return
  450. }
  451. if res := PrintCSVTable(test1, 4); res != `
  452. foo, bar, tester, 1
  453. xxx, test, te, foo
  454. bar, tester, 1
  455. `[1:] {
  456. t.Error("Unexpected result:\n", "#\n"+res+"#")
  457. return
  458. }
  459. test1 = []string{"foo", "bar", "tester", "1", "xxx", "test", "te", "foo",
  460. "bar"}
  461. if res := PrintGraphicStringTable(test1, 4, 5, nil); res != `
  462. #########################
  463. #foo #bar #tester #1 #
  464. #xxx #test #te #foo #
  465. #bar # # # #
  466. #########################
  467. `[1:] {
  468. t.Error("Unexpected result:\n", "#\n"+res+"#")
  469. return
  470. }
  471. test1 = []string{"foo", "bar", "tester", "1", "xxx", "test", "te", "foo"}
  472. if res := PrintGraphicStringTable(test1, 4, 5, nil); res != `
  473. #########################
  474. #foo #bar #tester #1 #
  475. #xxx #test #te #foo #
  476. #########################
  477. `[1:] {
  478. t.Error("Unexpected result:\n", "#\n"+res+"#")
  479. return
  480. }
  481. test1 = []string{"foo", "bar", "tester", "1", "xxx", "test", "te", "foo"}
  482. if res := PrintGraphicStringTable(test1, 1, 2, SingleLineTable); res != `
  483. ┌───────┐
  484. │foo │
  485. │bar │
  486. ├───────┤
  487. │tester │
  488. │1 │
  489. │xxx │
  490. │test │
  491. │te │
  492. │foo │
  493. └───────┘
  494. `[1:] {
  495. t.Error("Unexpected result:\n", "#\n"+res+"#")
  496. return
  497. }
  498. if res := PrintCSVTable(test1, 1); res != `
  499. foo
  500. bar
  501. tester
  502. 1
  503. xxx
  504. test
  505. te
  506. foo
  507. `[1:] {
  508. t.Error("Unexpected result:\n", "#\n"+res+"#")
  509. return
  510. }
  511. if res := PrintGraphicStringTable(test1, 100, 0, nil); res != `
  512. ##########################################
  513. #foo #bar #tester #1 #xxx #test #te #foo #
  514. ##########################################
  515. `[1:] {
  516. t.Error("Unexpected result:\n", "#\n"+res+"#")
  517. return
  518. }
  519. test1 = []string{"foo", "bar", "tester", "1", "xxx", "test", "te", "foo"}
  520. if res := PrintGraphicStringTable(test1, 4, 5, SingleLineTable); res != `
  521. ┌────┬─────┬───────┬────┐
  522. │foo │bar │tester │1 │
  523. │xxx │test │te │foo │
  524. └────┴─────┴───────┴────┘
  525. `[1:] {
  526. t.Error("Unexpected result:\n", "#\n"+res+"#")
  527. return
  528. }
  529. test1 = []string{"foo", "bar", "tester", "1", "xxx", "test", "te", "foo"}
  530. if res := PrintGraphicStringTable(test1, 1, 2, SingleDoubleLineTable); res != `
  531. ╒═══════╕
  532. │foo │
  533. │bar │
  534. ╞═══════╡
  535. │tester │
  536. │1 │
  537. │xxx │
  538. │test │
  539. │te │
  540. │foo │
  541. ╘═══════╛
  542. `[1:] {
  543. t.Error("Unexpected result:\n", "#\n"+res+"#")
  544. return
  545. }
  546. if res := PrintGraphicStringTable(test1, 1, 2, DoubleSingleLineTable); res != `
  547. ╓───────╖
  548. ║foo ║
  549. ║bar ║
  550. ╟───────╢
  551. ║tester ║
  552. ║1 ║
  553. ║xxx ║
  554. ║test ║
  555. ║te ║
  556. ║foo ║
  557. ╙───────╜
  558. `[1:] {
  559. t.Error("Unexpected result:\n", "#\n"+res+"#")
  560. return
  561. }
  562. if res := PrintGraphicStringTable(test1, 1, 2, DoubleLineTable); res != `
  563. ╔═══════╗
  564. ║foo ║
  565. ║bar ║
  566. ╠═══════╣
  567. ║tester ║
  568. ║1 ║
  569. ║xxx ║
  570. ║test ║
  571. ║te ║
  572. ║foo ║
  573. ╚═══════╝
  574. `[1:] {
  575. t.Error("Unexpected result:\n", "#\n"+res+"#")
  576. return
  577. }
  578. if res := PrintGraphicStringTable(test1, 100, 0, SingleLineTable); res != `
  579. ┌────┬────┬───────┬──┬────┬─────┬───┬────┐
  580. │foo │bar │tester │1 │xxx │test │te │foo │
  581. └────┴────┴───────┴──┴────┴─────┴───┴────┘
  582. `[1:] {
  583. t.Error("Unexpected result:\n", "#\n"+res+"#")
  584. return
  585. }
  586. }
  587. func TestCamelCaseSplit(t *testing.T) {
  588. if res := fmt.Sprint(CamelCaseSplit("FooBar")); res != "[Foo Bar]" {
  589. t.Error("Unexpected result:", res)
  590. return
  591. }
  592. if res := fmt.Sprint(CamelCaseSplit("FooB#ar")); res != "[Foo B # ar]" {
  593. t.Error("Unexpected result:", res)
  594. return
  595. }
  596. if res := fmt.Sprint(CamelCaseSplit("fOObAR")); res != "[f O Ob AR]" {
  597. t.Error("Unexpected result:", res)
  598. return
  599. }
  600. if res := fmt.Sprint(CamelCaseSplit("lower")); res != "[lower]" {
  601. t.Error("Unexpected result:", res)
  602. return
  603. }
  604. if res := fmt.Sprint(CamelCaseSplit("foo1bar")); res != "[foo 1 bar]" {
  605. t.Error("Unexpected result:", res)
  606. return
  607. }
  608. if res := fmt.Sprint(CamelCaseSplit("Low\xf2\xe6Er1")); res != "[Low\xf2\xe6Er1]" {
  609. t.Error("Unexpected result:", res)
  610. return
  611. }
  612. if res := fmt.Sprint(CamelCaseSplit("ROCKHard")); res != "[ROCK Hard]" {
  613. t.Error("Unexpected result:", res)
  614. return
  615. }
  616. }
  617. func TestChunkSplit(t *testing.T) {
  618. if res := fmt.Sprint(ChunkSplit("Foobar tester fooooo", 4, false)); res != "[Foob ar t este r fo oooo]" {
  619. t.Error("Unexpected result:", res)
  620. return
  621. }
  622. resSplit := ChunkSplit("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", 15, true)
  623. if res := strings.Join(resSplit, "\n"); res != `
  624. Lorem ipsum
  625. dolor sit
  626. amet,
  627. consectetur
  628. adipiscing
  629. elit, sed do
  630. eiusmod tempor
  631. incididunt ut
  632. labore et
  633. dolore magna
  634. aliqua.`[1:] {
  635. t.Errorf("Unexpected result:\n===============\n#%v#", res)
  636. return
  637. }
  638. resSplit = ChunkSplit("Loremipsumdolorsitamet,consecteturadipiscingelit,seddoeiusmodtemporincididunt ut labore etdoloremagnaaliqua.", 15, true)
  639. if res := strings.Join(resSplit, "\n"); res != `
  640. Loremipsumdolor
  641. sitamet,consect
  642. eturadipiscinge
  643. lit,seddoeiusmo
  644. dtemporincididu
  645. nt ut labore
  646. etdoloremagnaal
  647. iqua.`[1:] {
  648. t.Errorf("Unexpected result:\n===============\n#%v#", res)
  649. return
  650. }
  651. resSplit = ChunkSplit("Lor", 15, true)
  652. if res := strings.Join(resSplit, "\n"); res != `
  653. Lor`[1:] {
  654. t.Errorf("Unexpected result:\n===============\n#%v#", res)
  655. return
  656. }
  657. }