123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- (use-modules
- ;; SRFI 64 for unit testing facilities
- (srfi srfi-64)
- ;; utils - the code to be tested
- ((grid-printer) #:prefix grid:))
- (test-begin "grid-printer-test")
- (test-group
- "col-content-width"
- (test-equal "col-content-width-1"
- 6
- (grid:col-content-width 4
- (grid:make-grid-config
- #|col-sep|#
- "|"
- #|row-sep|#
- "-"
- #|intersection|#
- "+"
- #|empty|#
- " "
- #|col-pad|#
- 1
- #|row-pad|#
- 1
- #|pad-direction|#
- 'left))))
- (test-group
- "find-longest-string-length*"
- (test-equal "find-longest-string-length*-1"
- 3
- (grid:find-longest-string-length* '("b"
- "aa"
- "c"
- "aaa"
- "d"))))
- (test-group
- "print-segmented-line"
- (test-equal "print-segmented-line-1"
- "| | | |\n"
- (call-with-output-string
- (lambda (string-port)
- (grid:print-segmented-line 3 2 " " "|" string-port))))
- (test-equal "print-segmented-line-2 - can make zero width segments"
- "|||||\n"
- (call-with-output-string
- (lambda (string-port)
- (grid:print-segmented-line 4 0 " " "|" string-port))))
- (test-equal "print-segmented-line-3 - can print with non-default characters"
- "-@-@-@-@-@-\n"
- (call-with-output-string
- (lambda (string-port)
- (grid:print-segmented-line 5 1 "@" "-" string-port))))
- (test-equal "print-segmented-line-4 - works with equal characters"
- "|||||||||||\n"
- (call-with-output-string
- (lambda (string-port)
- (grid:print-segmented-line 5 1 "|" "|" string-port))))
- (test-equal "print-segmented-line-5 - works with equal characters"
- (string-append
- ;; 5 for the 5 segments
- "|||||"
- ;; 5 for the 5 segments -> width 2
- "|||||"
- ;; 6 for separators
- "||||||"
- ;; final newline
- "\n")
- (call-with-output-string
- (lambda (string-port)
- (grid:print-segmented-line 5 2 "|" "|" string-port))))
- (test-equal "print-segmented-line-6 - negative number of segments -> empty string"
- "|\n"
- (call-with-output-string
- (lambda (string-port)
- (grid:print-segmented-line -1 2 " " "|" string-port)))))
- (test-group
- "col-content-width"
- (test-equal "col-content-width-1"
- 9
- (grid:col-content-width 5 (grid:make-grid-config "|" "-" "+" "@" 2 1 'right)))
- (test-equal "col-content-width-2"
- 4
- (grid:col-content-width 2 (grid:make-grid-config "|" "-" "+" "@" 1 1 'right)))
- (test-equal "col-content-width-3"
- 13
- (grid:col-content-width 5 (grid:make-grid-config "|" "--" "+" "@@" 2 1 'left)))
- (test-equal "col-content-width-3"
- 13
- (grid:col-content-width 5 (grid:make-grid-config "|" "--" "+" "@" 2 1 'left)))
- (test-equal "col-content-width-3"
- 13
- (grid:col-content-width 5 (grid:make-grid-config "|" "--" "+" " " 2 1 'left))))
- (test-group
- "print-empty-line"
- (test-equal "print-empty-line-1"
- "|@@@@|@@@@|@@@@|\n"
- (call-with-output-string
- (lambda (string-port)
- (grid:print-empty-line
- #|fields|#
- 3
- #|data content width|#
- 2
- #|grid config|#
- (grid:make-grid-config "|" "-" "+" "@" 1 1 'left)
- #|output port|#
- string-port))))
- (test-equal "print-empty-line-2"
- "|@@@@@@|@@@@@@|@@@@@@|\n"
- (call-with-output-string
- (lambda (string-port)
- (grid:print-empty-line
- #|fields|#
- 3
- #|data content width|#
- 2
- #|grid config|#
- (grid:make-grid-config "|" "-" "+" "@" 2 1 'left)
- #|output port|#
- string-port)))))
- (test-group
- "print-content-line"
- (test-equal "print-content-line-1"
- "| 1 | 22 | |\n"
- (call-with-output-string
- (lambda (string-port)
- (grid:print-content-line
- #|minimum field count|#
- 3
- #|field contents|#
- '("1" "22")
- 2
- #|grid config|#
- (grid:make-grid-config "|" "-" "+" " " 1 1 'right)
- #|output port|#
- string-port))))
- (test-equal "print-content-line-2"
- "| 1 | 22 | |\n"
- (call-with-output-string
- (lambda (string-port)
- (grid:print-content-line
- #|minimum field count|#
- 3
- #|field contents|#
- '("1" "22")
- 2
- #|grid config|#
- (grid:make-grid-config "|" "-" "+" " " 1 1 'left)
- #|output port|#
- string-port)))))
- (test-group
- "print-separating-line"
- (test-equal "print-separating-line-1"
- "+---+---+---+---+\n"
- (call-with-output-string
- (lambda (string-port)
- (grid:print-separating-line
- #|fields count|#
- 4
- #|data content width|#
- 1
- #|grid config|#
- (grid:make-grid-config "|" "-" "+" " " 1 1 'right)
- #|output port|#
- string-port))))
- (test-equal "print-separating-line-2"
- "+----+----+----+----+\n"
- (call-with-output-string
- (lambda (string-port)
- (grid:print-separating-line
- #|fields count|#
- 4
- #|data content width|#
- 2
- #|grid config|#
- (grid:make-grid-config "|" "-" "+" " " 1 1 'right)
- #|output port|#
- string-port)))))
- ;; not testing output-padding-line as it is only an alias to print-empty-line
- (test-group
- "get-nth-cell-parts"
- (test-equal "get-nth-cell-parts-1"
- '("b" "e" "h")
- (grid:get-nth-cell-parts
- '(("a" "b" "c")
- ("d" "e" "f")
- ("g" "h" "i"))
- 1))
- (test-equal "get-nth-cell-parts-2"
- '("a" "d" "g")
- (grid:get-nth-cell-parts
- '(("a" "b" "c")
- ("d" "e" "f")
- ("g" "h" "i"))
- 0)))
- (test-group
- "equalize-lines-count-test"
- (let ([empty " "])
- (test-equal `(("1" "2" "3") ("a" ,empty ,empty) ("x" "y" ,empty))
- (grid:equalize-lines-count '(("1" "2" "3") ("a") ("x" "y")) empty))
- (test-equal `(("a" ,empty ,empty) ("1" "2" "3") ("x" "y" ,empty))
- (grid:equalize-lines-count '(("a") ("1" "2" "3") ("x" "y")) empty))
- (test-equal `(("a" ,empty ,empty) ("x" "y" ,empty) ("1" "2" "3"))
- (grid:equalize-lines-count '(("a") ("x" "y") ("1" "2" "3")) empty))))
- (test-group
- "print-grid-row"
- (test-equal "print-grid-row-1"
- (string-append
- "| a | d | g |\n"
- "| b | e | h |\n"
- "| c | f | i |\n")
- (call-with-output-string
- (lambda (string-port)
- (grid:print-grid-row
- #|row data|#
- ;; grid row contains 3 cells
- '(
- ;; cell contains "a" on one line, "b" on one line, "c" on one line
- ("a" "b" "c")
- ("d" "e" "f")
- ("g" "h" "i"))
- #|fields count|#
- 3
- #|data content width|#
- 1
- #|grid configuration|#
- (grid:make-grid-config "|" "-" "+" " " 1 0 'right)
- #|output port|#
- string-port)))))
- (test-group
- "print-grid"
- (test-equal "print-grid-1"
- (string-append
- "+---+---+---+\n"
- "| a | d | g |\n"
- "| b | e | h |\n"
- "| c | f | i |\n"
- "+---+---+---+\n"
- "| 1 | 4 | 7 |\n"
- "| 2 | 5 | 8 |\n"
- "| 3 | 6 | 9 |\n"
- "+---+---+---+\n")
- (call-with-output-string
- (lambda (string-port)
- (grid:print-grid
- ;; grid containing 2 rows
- '(
- ;; grid row contains 3 cells
- (
- ;; cell contains "a" on one line, "b" on one line, "c" on one line
- ("a" "b" "c")
- ("d" "e" "f")
- ("g" "h" "i"))
- (("1" "2" "3")
- ("4" "5" "6")
- ("7" "8" "9")))
- string-port
- #:grid-config (grid:make-grid-config "|" "-" "+" " " 1 0 'right)))))
- (test-equal "print-grid-2"
- (string-append
- "+---+---+\n"
- "| a | |\n"
- "| b | |\n"
- "| c | |\n"
- "+---+---+\n")
- (call-with-output-string
- (lambda (string-port)
- (grid:print-grid
- ;; grid
- '(
- ;; row
- (
- ;; col 1
- ("a" "b" "c")
- ;; col 2
- ("")))
- string-port
- #:grid-config (grid:make-grid-config "|" "-" "+" " " 1 0 'right)))))
- ;; (test-equal "print-grid--grid-in-grid-1"
- ;; (string-append
- ;; "+-------+-------+\n"
- ;; "| +---+ | |\n"
- ;; "| | a | | |\n"
- ;; "| +---+ | |\n"
- ;; "+-------+-------+\n")
- ;; (let ([inner-grid
- ;; (call-with-output-string
- ;; (lambda (string-port)
- ;; (grid:print-grid
- ;; '((("a")))
- ;; string-port
- ;; #:grid-config (grid:make-grid-config "|" "-" "+" " " 1 0 'right))))])
- ;; (call-with-output-string
- ;; (lambda (string-port)
- ;; (let ([inner-grid-as-list
- ;; (string-split (string-trim-right inner-grid)
- ;; (λ (char) (char=? char #\newline)))])
- ;; (display (simple-format #f "list inner grid: ~a\n" inner-grid-as-list))
- ;; (display (simple-format #f "length: ~a\n" (length inner-grid-as-list)))
- ;; (grid:print-grid
- ;; ;; grid
- ;; `(
- ;; ;; row
- ;; (
- ;; ;; col 1
- ;; ,inner-grid-as-list
- ;; ;; col 2
- ;; ("")))
- ;; string-port
- ;; #:grid-config (grid:make-grid-config "|" "-" "+" " " 1 0 'right)))))))
- ;; (test-equal "print-grid--grid-in-grid-2"
- ;; (string-append
- ;; "+---------------+---------------+\n"
- ;; "| +---+---+---+ | |\n"
- ;; "| | a | d | g | | |\n"
- ;; "| | b | e | h | | |\n"
- ;; "| | c | f | i | | |\n"
- ;; "| +---+---+---+ | |\n"
- ;; "| | 1 | 4 | 7 | | |\n"
- ;; "| | 2 | 5 | 8 | | |\n"
- ;; "| | 3 | 6 | 9 | | |\n"
- ;; "| +---+---+---+ | |\n"
- ;; "+---------------+---------------+\n")
- ;; (let ([inner-grid
- ;; (call-with-output-string
- ;; (lambda (string-port)
- ;; (grid:print-grid
- ;; ;; grid containing 2 rows
- ;; '(
- ;; ;; grid row contains 3 cells
- ;; (
- ;; ;; cell contains "a" on one line, "b" on one line, "c" on one line
- ;; ("a" "b" "c")
- ;; ("d" "e" "f")
- ;; ("g" "h" "i"))
- ;; (("1" "2" "3")
- ;; ("4" "5" "6")
- ;; ("7" "8" "9")))
- ;; string-port
- ;; #:grid-config (grid:make-grid-config "|" "-" "+" " " 1 0 'right))))])
- ;; (call-with-output-string
- ;; (lambda (string-port)
- ;; (display (simple-format #f "~a\n" (string-trim-right inner-grid)))
- ;; (grid:print-grid
- ;; ;; grid containing 1 row
- ;; `(
- ;; ;; the 1 row
- ;; (
- ;; ;; the 2 cells
- ;; ,(string-split (string-trim-right inner-grid)
- ;; (λ (char) (char=? char #\newline)))
- ;; ("")))
- ;; string-port
- ;; #:grid-config (grid:make-grid-config "|" "-" "+" " " 1 0 'right))))))
- )
- (test-end "grid-printer-test")
|