stringutil_test.go 18 KB

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