stringutil_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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 TestQuoteCLIArgs(t *testing.T) {
  266. if res := QuoteCLIArgs([]string{"-i"}); res != "-i" {
  267. t.Error("Unexpected result:", res)
  268. return
  269. }
  270. if res := QuoteCLIArgs([]string{"-i test"}); res != "'-i test'" {
  271. t.Error("Unexpected result:", res)
  272. return
  273. }
  274. if res := QuoteCLIArgs([]string{"-i", "--TEST&test"}); res != "-i '--TEST&test'" {
  275. t.Error("Unexpected result:", res)
  276. return
  277. }
  278. }
  279. func TestConvertToString(t *testing.T) {
  280. if res := ConvertToString(""); res != "" {
  281. t.Error("Unexpected result:", res)
  282. return
  283. }
  284. if res := ConvertToString("test"); res != "test" {
  285. t.Error("Unexpected result:", res)
  286. return
  287. }
  288. if res := ConvertToString(4.123); res != "4.123" {
  289. t.Error("Unexpected result:", res)
  290. return
  291. }
  292. if res := ConvertToString(6); res != "6" {
  293. t.Error("Unexpected result:", res)
  294. return
  295. }
  296. if res := ConvertToString(map[string]int{"z": 1, "d": 2, "a": 4}); res != `{"a":4,"d":2,"z":1}` {
  297. t.Error("Unexpected result:", res)
  298. return
  299. }
  300. if res := ConvertToString([]int{1, 2, 3}); res != "[1,2,3]" {
  301. t.Error("Unexpected result:", res)
  302. return
  303. }
  304. if res := ConvertToString(map[interface{}]interface{}{"z": 1, "d": 2, "a": 4}); res != `{"a":4,"d":2,"z":1}` {
  305. t.Error("Unexpected result:", res)
  306. return
  307. }
  308. if res := ConvertToString(map[interface{}]interface{}{"z": []interface{}{1, 2, 3}, "d": 2, "a": 4}); res != `{"a":4,"d":2,"z":[1,2,3]}` {
  309. t.Error("Unexpected result:", res)
  310. return
  311. }
  312. if res := ConvertToString([]interface{}{1, sync.Mutex{}, 3}); res != `[1,{},3]` {
  313. t.Error("Unexpected result:", res)
  314. return
  315. }
  316. if res := ConvertToString([]interface{}{1, map[interface{}]interface{}{1: 2}, 3}); res != `[1,{"1":2},3]` {
  317. t.Error("Unexpected result:", res)
  318. return
  319. }
  320. if res := ConvertToString(&bytes.Buffer{}); res != "" {
  321. t.Error("Unexpected result:", res)
  322. return
  323. }
  324. // Not much to do with such a construct but we shouldn't fail!
  325. type foo struct{ i int }
  326. x := make(map[foo]foo)
  327. x[foo{1}] = foo{2}
  328. if res := ConvertToString(x); res != "map[{1}:{2}]" {
  329. t.Error("Unexpected result:", res)
  330. return
  331. }
  332. }
  333. func TestConvertToPrettyString(t *testing.T) {
  334. if res := ConvertToPrettyString(""); res != `""` {
  335. t.Error("Unexpected result:", res)
  336. return
  337. }
  338. if res := ConvertToPrettyString("test"); res != `"test"` {
  339. t.Error("Unexpected result:", res)
  340. return
  341. }
  342. if res := ConvertToPrettyString(4.123); res != "4.123" {
  343. t.Error("Unexpected result:", res)
  344. return
  345. }
  346. if res := ConvertToString(6); res != "6" {
  347. t.Error("Unexpected result:", res)
  348. return
  349. }
  350. if res := ConvertToPrettyString(map[string]int{"z": 1, "d": 2, "a": 4}); res != `{
  351. "a": 4,
  352. "d": 2,
  353. "z": 1
  354. }` {
  355. t.Error("Unexpected result:", res)
  356. return
  357. }
  358. if res := ConvertToPrettyString([]int{1, 2, 3}); res != `[
  359. 1,
  360. 2,
  361. 3
  362. ]` {
  363. t.Error("Unexpected result:", res)
  364. return
  365. }
  366. if res := ConvertToPrettyString(map[interface{}]interface{}{"z": 1, "d": 2, "a": 4}); res != `{
  367. "a": 4,
  368. "d": 2,
  369. "z": 1
  370. }` {
  371. t.Error("Unexpected result:", res)
  372. return
  373. }
  374. if res := ConvertToPrettyString(map[interface{}]interface{}{"z": []interface{}{1, 2, 3}, "d": 2, "a": 4}); res != `{
  375. "a": 4,
  376. "d": 2,
  377. "z": [
  378. 1,
  379. 2,
  380. 3
  381. ]
  382. }` {
  383. t.Error("Unexpected result:", res)
  384. return
  385. }
  386. if res := ConvertToPrettyString([]interface{}{1, sync.Mutex{}, 3}); res != `[
  387. 1,
  388. {},
  389. 3
  390. ]` {
  391. t.Error("Unexpected result:", res)
  392. return
  393. }
  394. if res := ConvertToPrettyString([]interface{}{1, map[interface{}]interface{}{1: 2}, 3}); res != `[
  395. 1,
  396. {
  397. "1": 2
  398. },
  399. 3
  400. ]` {
  401. t.Error("Unexpected result:", res)
  402. return
  403. }
  404. if res := ConvertToPrettyString(&bytes.Buffer{}); res != "{}" {
  405. t.Error("Unexpected result:", res)
  406. return
  407. }
  408. // Not much to do with such a construct but we shouldn't fail!
  409. type foo struct{ i int }
  410. x := make(map[foo]foo)
  411. x[foo{1}] = foo{2}
  412. if res := ConvertToPrettyString(x); res != "map[{1}:{2}]" {
  413. t.Error("Unexpected result:", res)
  414. return
  415. }
  416. }
  417. func TestMD5HexString(t *testing.T) {
  418. res := MD5HexString("This is a test")
  419. if res != "ce114e4501d2f4e2dcea3e17b546f339" {
  420. t.Error("Unexpected md5 hex result", res)
  421. }
  422. }
  423. func TestLengthConstantEquals(t *testing.T) {
  424. if !LengthConstantEquals([]byte("test1"), []byte("test1")) {
  425. t.Error("Unexpected result")
  426. return
  427. }
  428. if LengthConstantEquals([]byte("test1"), []byte("test2")) {
  429. t.Error("Unexpected result")
  430. return
  431. }
  432. if LengthConstantEquals([]byte("test1"), []byte("test2test123")) {
  433. t.Error("Unexpected result")
  434. return
  435. }
  436. }
  437. func TestPrintGraphicStringTable(t *testing.T) {
  438. if res := PrintGraphicStringTable(nil, 0, 5, nil); res != "" {
  439. t.Error("Unexpected result:\n", "#\n"+res+"#")
  440. return
  441. }
  442. if res := PrintGraphicStringTable([]string{}, 4, 5, SingleLineTable); res != `
  443. ┌┐
  444. └┘
  445. `[1:] {
  446. t.Error("Unexpected result:\n", "#\n"+res+"#")
  447. return
  448. }
  449. if res := PrintCSVTable([]string{}, 4); res != "" {
  450. t.Error("Unexpected result:\n", "#\n"+res+"#")
  451. return
  452. }
  453. test1 := []string{"foo", "bar", "tester", "1", "xxx", "test", "te", "foo",
  454. "bar", "tester", "1"}
  455. if res := PrintGraphicStringTable(test1, 4, 5, SingleLineTable); res != `
  456. ┌────┬───────┬───────┬────┐
  457. │foo │bar │tester │1 │
  458. │xxx │test │te │foo │
  459. │bar │tester │1 │ │
  460. └────┴───────┴───────┴────┘
  461. `[1:] {
  462. t.Error("Unexpected result:\n", "#\n"+res+"#")
  463. return
  464. }
  465. if res := PrintCSVTable(test1, 4); res != `
  466. foo, bar, tester, 1
  467. xxx, test, te, foo
  468. bar, tester, 1
  469. `[1:] {
  470. t.Error("Unexpected result:\n", "#\n"+res+"#")
  471. return
  472. }
  473. test1 = []string{"foo", "bar", "tester", "1", "xxx", "test", "te", "foo",
  474. "bar"}
  475. if res := PrintGraphicStringTable(test1, 4, 5, nil); res != `
  476. #########################
  477. #foo #bar #tester #1 #
  478. #xxx #test #te #foo #
  479. #bar # # # #
  480. #########################
  481. `[1:] {
  482. t.Error("Unexpected result:\n", "#\n"+res+"#")
  483. return
  484. }
  485. test1 = []string{"foo", "bar", "tester", "1", "xxx", "test", "te", "foo"}
  486. if res := PrintGraphicStringTable(test1, 4, 5, nil); res != `
  487. #########################
  488. #foo #bar #tester #1 #
  489. #xxx #test #te #foo #
  490. #########################
  491. `[1:] {
  492. t.Error("Unexpected result:\n", "#\n"+res+"#")
  493. return
  494. }
  495. test1 = []string{"foo", "bar", "tester", "1", "xxx", "test", "te", "foo"}
  496. if res := PrintGraphicStringTable(test1, 1, 2, SingleLineTable); res != `
  497. ┌───────┐
  498. │foo │
  499. │bar │
  500. ├───────┤
  501. │tester │
  502. │1 │
  503. │xxx │
  504. │test │
  505. │te │
  506. │foo │
  507. └───────┘
  508. `[1:] {
  509. t.Error("Unexpected result:\n", "#\n"+res+"#")
  510. return
  511. }
  512. if res := PrintCSVTable(test1, 1); res != `
  513. foo
  514. bar
  515. tester
  516. 1
  517. xxx
  518. test
  519. te
  520. foo
  521. `[1:] {
  522. t.Error("Unexpected result:\n", "#\n"+res+"#")
  523. return
  524. }
  525. if res := PrintGraphicStringTable(test1, 100, 0, nil); res != `
  526. ##########################################
  527. #foo #bar #tester #1 #xxx #test #te #foo #
  528. ##########################################
  529. `[1:] {
  530. t.Error("Unexpected result:\n", "#\n"+res+"#")
  531. return
  532. }
  533. test1 = []string{"foo", "bar", "tester", "1", "xxx", "test", "te", "foo"}
  534. if res := PrintGraphicStringTable(test1, 4, 5, SingleLineTable); res != `
  535. ┌────┬─────┬───────┬────┐
  536. │foo │bar │tester │1 │
  537. │xxx │test │te │foo │
  538. └────┴─────┴───────┴────┘
  539. `[1:] {
  540. t.Error("Unexpected result:\n", "#\n"+res+"#")
  541. return
  542. }
  543. test1 = []string{"foo", "bar", "tester", "1", "xxx", "test", "te", "foo"}
  544. if res := PrintGraphicStringTable(test1, 1, 2, SingleDoubleLineTable); res != `
  545. ╒═══════╕
  546. │foo │
  547. │bar │
  548. ╞═══════╡
  549. │tester │
  550. │1 │
  551. │xxx │
  552. │test │
  553. │te │
  554. │foo │
  555. ╘═══════╛
  556. `[1:] {
  557. t.Error("Unexpected result:\n", "#\n"+res+"#")
  558. return
  559. }
  560. if res := PrintGraphicStringTable(test1, 1, 2, DoubleSingleLineTable); res != `
  561. ╓───────╖
  562. ║foo ║
  563. ║bar ║
  564. ╟───────╢
  565. ║tester ║
  566. ║1 ║
  567. ║xxx ║
  568. ║test ║
  569. ║te ║
  570. ║foo ║
  571. ╙───────╜
  572. `[1:] {
  573. t.Error("Unexpected result:\n", "#\n"+res+"#")
  574. return
  575. }
  576. if res := PrintGraphicStringTable(test1, 1, 2, DoubleLineTable); res != `
  577. ╔═══════╗
  578. ║foo ║
  579. ║bar ║
  580. ╠═══════╣
  581. ║tester ║
  582. ║1 ║
  583. ║xxx ║
  584. ║test ║
  585. ║te ║
  586. ║foo ║
  587. ╚═══════╝
  588. `[1:] {
  589. t.Error("Unexpected result:\n", "#\n"+res+"#")
  590. return
  591. }
  592. if res := PrintGraphicStringTable(test1, 100, 0, SingleLineTable); res != `
  593. ┌────┬────┬───────┬──┬────┬─────┬───┬────┐
  594. │foo │bar │tester │1 │xxx │test │te │foo │
  595. └────┴────┴───────┴──┴────┴─────┴───┴────┘
  596. `[1:] {
  597. t.Error("Unexpected result:\n", "#\n"+res+"#")
  598. return
  599. }
  600. }
  601. func TestCamelCaseSplit(t *testing.T) {
  602. if res := fmt.Sprint(CamelCaseSplit("FooBar")); res != "[Foo Bar]" {
  603. t.Error("Unexpected result:", res)
  604. return
  605. }
  606. if res := fmt.Sprint(CamelCaseSplit("FooB#ar")); res != "[Foo B # ar]" {
  607. t.Error("Unexpected result:", res)
  608. return
  609. }
  610. if res := fmt.Sprint(CamelCaseSplit("fOObAR")); res != "[f O Ob AR]" {
  611. t.Error("Unexpected result:", res)
  612. return
  613. }
  614. if res := fmt.Sprint(CamelCaseSplit("lower")); res != "[lower]" {
  615. t.Error("Unexpected result:", res)
  616. return
  617. }
  618. if res := fmt.Sprint(CamelCaseSplit("foo1bar")); res != "[foo 1 bar]" {
  619. t.Error("Unexpected result:", res)
  620. return
  621. }
  622. if res := fmt.Sprint(CamelCaseSplit("Low\xf2\xe6Er1")); res != "[Low\xf2\xe6Er1]" {
  623. t.Error("Unexpected result:", res)
  624. return
  625. }
  626. if res := fmt.Sprint(CamelCaseSplit("ROCKHard")); res != "[ROCK Hard]" {
  627. t.Error("Unexpected result:", res)
  628. return
  629. }
  630. }
  631. func TestChunkSplit(t *testing.T) {
  632. if res := fmt.Sprint(ChunkSplit("Foobar tester fooooo", 4, false)); res != "[Foob ar t este r fo oooo]" {
  633. t.Error("Unexpected result:", res)
  634. return
  635. }
  636. resSplit := ChunkSplit("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", 15, true)
  637. if res := strings.Join(resSplit, "\n"); res != `
  638. Lorem ipsum
  639. dolor sit
  640. amet,
  641. consectetur
  642. adipiscing
  643. elit, sed do
  644. eiusmod tempor
  645. incididunt ut
  646. labore et
  647. dolore magna
  648. aliqua.`[1:] {
  649. t.Errorf("Unexpected result:\n===============\n#%v#", res)
  650. return
  651. }
  652. resSplit = ChunkSplit("Loremipsumdolorsitamet,consecteturadipiscingelit,seddoeiusmodtemporincididunt ut labore etdoloremagnaaliqua.", 15, true)
  653. if res := strings.Join(resSplit, "\n"); res != `
  654. Loremipsumdolor
  655. sitamet,consect
  656. eturadipiscinge
  657. lit,seddoeiusmo
  658. dtemporincididu
  659. nt ut labore
  660. etdoloremagnaal
  661. iqua.`[1:] {
  662. t.Errorf("Unexpected result:\n===============\n#%v#", res)
  663. return
  664. }
  665. resSplit = ChunkSplit("Lor", 15, true)
  666. if res := strings.Join(resSplit, "\n"); res != `
  667. Lor`[1:] {
  668. t.Errorf("Unexpected result:\n===============\n#%v#", res)
  669. return
  670. }
  671. }