test.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. (use-modules
  2. ;; SRFI 64 for unit testing facilities
  3. (srfi srfi-64)
  4. ;; utils - the code to be tested
  5. ((grid-printer) #:prefix grid:))
  6. (test-begin "grid-printer-test")
  7. (test-group
  8. "col-content-width"
  9. (test-equal "col-content-width-1"
  10. 6
  11. (grid:col-content-width 4
  12. (grid:make-grid-config
  13. #|col-sep|#
  14. "|"
  15. #|row-sep|#
  16. "-"
  17. #|intersection|#
  18. "+"
  19. #|empty|#
  20. " "
  21. #|col-pad|#
  22. 1
  23. #|row-pad|#
  24. 1
  25. #|pad-direction|#
  26. 'left))))
  27. (test-group
  28. "find-longest-string-length*"
  29. (test-equal "find-longest-string-length*-1"
  30. 3
  31. (grid:find-longest-string-length* '("b"
  32. "aa"
  33. "c"
  34. "aaa"
  35. "d"))))
  36. (test-group
  37. "print-segmented-line"
  38. (test-equal "print-segmented-line-1"
  39. "| | | |\n"
  40. (call-with-output-string
  41. (lambda (string-port)
  42. (grid:print-segmented-line 3 2 " " "|" string-port))))
  43. (test-equal "print-segmented-line-2 - can make zero width segments"
  44. "|||||\n"
  45. (call-with-output-string
  46. (lambda (string-port)
  47. (grid:print-segmented-line 4 0 " " "|" string-port))))
  48. (test-equal "print-segmented-line-3 - can print with non-default characters"
  49. "-@-@-@-@-@-\n"
  50. (call-with-output-string
  51. (lambda (string-port)
  52. (grid:print-segmented-line 5 1 "@" "-" string-port))))
  53. (test-equal "print-segmented-line-4 - works with equal characters"
  54. "|||||||||||\n"
  55. (call-with-output-string
  56. (lambda (string-port)
  57. (grid:print-segmented-line 5 1 "|" "|" string-port))))
  58. (test-equal "print-segmented-line-5 - works with equal characters"
  59. (string-append
  60. ;; 5 for the 5 segments
  61. "|||||"
  62. ;; 5 for the 5 segments -> width 2
  63. "|||||"
  64. ;; 6 for separators
  65. "||||||"
  66. ;; final newline
  67. "\n")
  68. (call-with-output-string
  69. (lambda (string-port)
  70. (grid:print-segmented-line 5 2 "|" "|" string-port))))
  71. (test-equal "print-segmented-line-6 - negative number of segments -> empty string"
  72. "|\n"
  73. (call-with-output-string
  74. (lambda (string-port)
  75. (grid:print-segmented-line -1 2 " " "|" string-port)))))
  76. (test-group
  77. "col-content-width"
  78. (test-equal "col-content-width-1"
  79. 9
  80. (grid:col-content-width 5 (grid:make-grid-config "|" "-" "+" "@" 2 1 'right)))
  81. (test-equal "col-content-width-2"
  82. 4
  83. (grid:col-content-width 2 (grid:make-grid-config "|" "-" "+" "@" 1 1 'right)))
  84. (test-equal "col-content-width-3"
  85. 13
  86. (grid:col-content-width 5 (grid:make-grid-config "|" "--" "+" "@@" 2 1 'left)))
  87. (test-equal "col-content-width-3"
  88. 13
  89. (grid:col-content-width 5 (grid:make-grid-config "|" "--" "+" "@" 2 1 'left)))
  90. (test-equal "col-content-width-3"
  91. 13
  92. (grid:col-content-width 5 (grid:make-grid-config "|" "--" "+" " " 2 1 'left))))
  93. (test-group
  94. "print-empty-line"
  95. (test-equal "print-empty-line-1"
  96. "|@@@@|@@@@|@@@@|\n"
  97. (call-with-output-string
  98. (lambda (string-port)
  99. (grid:print-empty-line
  100. #|fields|#
  101. 3
  102. #|data content width|#
  103. 2
  104. #|grid config|#
  105. (grid:make-grid-config "|" "-" "+" "@" 1 1 'left)
  106. #|output port|#
  107. string-port))))
  108. (test-equal "print-empty-line-2"
  109. "|@@@@@@|@@@@@@|@@@@@@|\n"
  110. (call-with-output-string
  111. (lambda (string-port)
  112. (grid:print-empty-line
  113. #|fields|#
  114. 3
  115. #|data content width|#
  116. 2
  117. #|grid config|#
  118. (grid:make-grid-config "|" "-" "+" "@" 2 1 'left)
  119. #|output port|#
  120. string-port)))))
  121. (test-group
  122. "print-content-line"
  123. (test-equal "print-content-line-1"
  124. "| 1 | 22 | |\n"
  125. (call-with-output-string
  126. (lambda (string-port)
  127. (grid:print-content-line
  128. #|minimum field count|#
  129. 3
  130. #|field contents|#
  131. '("1" "22")
  132. 2
  133. #|grid config|#
  134. (grid:make-grid-config "|" "-" "+" " " 1 1 'right)
  135. #|output port|#
  136. string-port))))
  137. (test-equal "print-content-line-2"
  138. "| 1 | 22 | |\n"
  139. (call-with-output-string
  140. (lambda (string-port)
  141. (grid:print-content-line
  142. #|minimum field count|#
  143. 3
  144. #|field contents|#
  145. '("1" "22")
  146. 2
  147. #|grid config|#
  148. (grid:make-grid-config "|" "-" "+" " " 1 1 'left)
  149. #|output port|#
  150. string-port)))))
  151. (test-group
  152. "print-separating-line"
  153. (test-equal "print-separating-line-1"
  154. "+---+---+---+---+\n"
  155. (call-with-output-string
  156. (lambda (string-port)
  157. (grid:print-separating-line
  158. #|fields count|#
  159. 4
  160. #|data content width|#
  161. 1
  162. #|grid config|#
  163. (grid:make-grid-config "|" "-" "+" " " 1 1 'right)
  164. #|output port|#
  165. string-port))))
  166. (test-equal "print-separating-line-2"
  167. "+----+----+----+----+\n"
  168. (call-with-output-string
  169. (lambda (string-port)
  170. (grid:print-separating-line
  171. #|fields count|#
  172. 4
  173. #|data content width|#
  174. 2
  175. #|grid config|#
  176. (grid:make-grid-config "|" "-" "+" " " 1 1 'right)
  177. #|output port|#
  178. string-port)))))
  179. ;; not testing output-padding-line as it is only an alias to print-empty-line
  180. (test-group
  181. "get-nth-cell-parts"
  182. (test-equal "get-nth-cell-parts-1"
  183. '("b" "e" "h")
  184. (grid:get-nth-cell-parts
  185. '(("a" "b" "c")
  186. ("d" "e" "f")
  187. ("g" "h" "i"))
  188. 1))
  189. (test-equal "get-nth-cell-parts-2"
  190. '("a" "d" "g")
  191. (grid:get-nth-cell-parts
  192. '(("a" "b" "c")
  193. ("d" "e" "f")
  194. ("g" "h" "i"))
  195. 0)))
  196. (test-group
  197. "equalize-lines-count-test"
  198. (let ([empty " "])
  199. (test-equal `(("1" "2" "3") ("a" ,empty ,empty) ("x" "y" ,empty))
  200. (grid:equalize-lines-count '(("1" "2" "3") ("a") ("x" "y")) empty))
  201. (test-equal `(("a" ,empty ,empty) ("1" "2" "3") ("x" "y" ,empty))
  202. (grid:equalize-lines-count '(("a") ("1" "2" "3") ("x" "y")) empty))
  203. (test-equal `(("a" ,empty ,empty) ("x" "y" ,empty) ("1" "2" "3"))
  204. (grid:equalize-lines-count '(("a") ("x" "y") ("1" "2" "3")) empty))))
  205. (test-group
  206. "print-grid-row"
  207. (test-equal "print-grid-row-1"
  208. (string-append
  209. "| a | d | g |\n"
  210. "| b | e | h |\n"
  211. "| c | f | i |\n")
  212. (call-with-output-string
  213. (lambda (string-port)
  214. (grid:print-grid-row
  215. #|row data|#
  216. ;; grid row contains 3 cells
  217. '(
  218. ;; cell contains "a" on one line, "b" on one line, "c" on one line
  219. ("a" "b" "c")
  220. ("d" "e" "f")
  221. ("g" "h" "i"))
  222. #|fields count|#
  223. 3
  224. #|data content width|#
  225. 1
  226. #|grid configuration|#
  227. (grid:make-grid-config "|" "-" "+" " " 1 0 'right)
  228. #|output port|#
  229. string-port)))))
  230. (test-group
  231. "print-grid"
  232. (test-equal "print-grid-1"
  233. (string-append
  234. "+---+---+---+\n"
  235. "| a | d | g |\n"
  236. "| b | e | h |\n"
  237. "| c | f | i |\n"
  238. "+---+---+---+\n"
  239. "| 1 | 4 | 7 |\n"
  240. "| 2 | 5 | 8 |\n"
  241. "| 3 | 6 | 9 |\n"
  242. "+---+---+---+\n")
  243. (call-with-output-string
  244. (lambda (string-port)
  245. (grid:print-grid
  246. ;; grid containing 2 rows
  247. '(
  248. ;; grid row contains 3 cells
  249. (
  250. ;; cell contains "a" on one line, "b" on one line, "c" on one line
  251. ("a" "b" "c")
  252. ("d" "e" "f")
  253. ("g" "h" "i"))
  254. (("1" "2" "3")
  255. ("4" "5" "6")
  256. ("7" "8" "9")))
  257. string-port
  258. #:grid-config (grid:make-grid-config "|" "-" "+" " " 1 0 'right)))))
  259. (test-equal "print-grid-2"
  260. (string-append
  261. "+---+---+\n"
  262. "| a | |\n"
  263. "| b | |\n"
  264. "| c | |\n"
  265. "+---+---+\n")
  266. (call-with-output-string
  267. (lambda (string-port)
  268. (grid:print-grid
  269. ;; grid
  270. '(
  271. ;; row
  272. (
  273. ;; col 1
  274. ("a" "b" "c")
  275. ;; col 2
  276. ("")))
  277. string-port
  278. #:grid-config (grid:make-grid-config "|" "-" "+" " " 1 0 'right)))))
  279. ;; (test-equal "print-grid--grid-in-grid-1"
  280. ;; (string-append
  281. ;; "+-------+-------+\n"
  282. ;; "| +---+ | |\n"
  283. ;; "| | a | | |\n"
  284. ;; "| +---+ | |\n"
  285. ;; "+-------+-------+\n")
  286. ;; (let ([inner-grid
  287. ;; (call-with-output-string
  288. ;; (lambda (string-port)
  289. ;; (grid:print-grid
  290. ;; '((("a")))
  291. ;; string-port
  292. ;; #:grid-config (grid:make-grid-config "|" "-" "+" " " 1 0 'right))))])
  293. ;; (call-with-output-string
  294. ;; (lambda (string-port)
  295. ;; (let ([inner-grid-as-list
  296. ;; (string-split (string-trim-right inner-grid)
  297. ;; (λ (char) (char=? char #\newline)))])
  298. ;; (display (simple-format #f "list inner grid: ~a\n" inner-grid-as-list))
  299. ;; (display (simple-format #f "length: ~a\n" (length inner-grid-as-list)))
  300. ;; (grid:print-grid
  301. ;; ;; grid
  302. ;; `(
  303. ;; ;; row
  304. ;; (
  305. ;; ;; col 1
  306. ;; ,inner-grid-as-list
  307. ;; ;; col 2
  308. ;; ("")))
  309. ;; string-port
  310. ;; #:grid-config (grid:make-grid-config "|" "-" "+" " " 1 0 'right)))))))
  311. ;; (test-equal "print-grid--grid-in-grid-2"
  312. ;; (string-append
  313. ;; "+---------------+---------------+\n"
  314. ;; "| +---+---+---+ | |\n"
  315. ;; "| | a | d | g | | |\n"
  316. ;; "| | b | e | h | | |\n"
  317. ;; "| | c | f | i | | |\n"
  318. ;; "| +---+---+---+ | |\n"
  319. ;; "| | 1 | 4 | 7 | | |\n"
  320. ;; "| | 2 | 5 | 8 | | |\n"
  321. ;; "| | 3 | 6 | 9 | | |\n"
  322. ;; "| +---+---+---+ | |\n"
  323. ;; "+---------------+---------------+\n")
  324. ;; (let ([inner-grid
  325. ;; (call-with-output-string
  326. ;; (lambda (string-port)
  327. ;; (grid:print-grid
  328. ;; ;; grid containing 2 rows
  329. ;; '(
  330. ;; ;; grid row contains 3 cells
  331. ;; (
  332. ;; ;; cell contains "a" on one line, "b" on one line, "c" on one line
  333. ;; ("a" "b" "c")
  334. ;; ("d" "e" "f")
  335. ;; ("g" "h" "i"))
  336. ;; (("1" "2" "3")
  337. ;; ("4" "5" "6")
  338. ;; ("7" "8" "9")))
  339. ;; string-port
  340. ;; #:grid-config (grid:make-grid-config "|" "-" "+" " " 1 0 'right))))])
  341. ;; (call-with-output-string
  342. ;; (lambda (string-port)
  343. ;; (display (simple-format #f "~a\n" (string-trim-right inner-grid)))
  344. ;; (grid:print-grid
  345. ;; ;; grid containing 1 row
  346. ;; `(
  347. ;; ;; the 1 row
  348. ;; (
  349. ;; ;; the 2 cells
  350. ;; ,(string-split (string-trim-right inner-grid)
  351. ;; (λ (char) (char=? char #\newline)))
  352. ;; ("")))
  353. ;; string-port
  354. ;; #:grid-config (grid:make-grid-config "|" "-" "+" " " 1 0 'right))))))
  355. )
  356. (test-end "grid-printer-test")