guile-reflow/tests/test-ansi.scm
2024-02-29 11:40:40 -05:00

76 lines
2 KiB
Scheme

(define-module (tests test-ansi)
#:use-module (reflow ansi)
#:use-module (tests utils)
#:use-module (srfi srfi-64)
#:use-module (rnrs io ports))
(test-begin "test-ansi")
(define pink-text-then-reset "\x1b[38;2;249;38;114m你好reflow\x1b[0m")
(test-equal
"Text should be forwarded to the port properly"
pink-text-then-reset
(call-with-output-string
(λ (str-port)
(define-values (ansi-port cur-seq reset restore)
(make-ansi-port-tuple str-port))
(display pink-text-then-reset ansi-port)
(reset)
(restore)
(test-equal "Current sequence should be #f" #f (cur-seq)))))
(test-equal
"Text should contain a bold sequence with only one reset sequence"
"\x1b[1mhello\x1b[0m你好"
(call-with-output-string
(λ (str-port)
(define-values (ansi-port cur-seq reset restore)
(make-ansi-port-tuple str-port))
;; no-op reset
(reset)
(display "\x1b[1mhello" ansi-port)
(test-equal "cur-seq should have bold"
"\x1b[1m"
(cur-seq))
;; op reset
(reset)
(display "你好" ansi-port))))
(test-equal
"Text should contain a bold sequence, reset, and then another bold sequence"
"\x1b[1mhello\x1b[0m你好\x1b[1mworld"
(call-with-output-string
(λ (str-port)
(define-values (ansi-port cur-seq reset restore)
(make-ansi-port-tuple str-port))
(display "\x1b[1mhello" ansi-port)
(reset)
(display "你好" ansi-port)
(restore)
(display "world" ansi-port))))
(test-equal
"Text should contain a bold sequence, text, then italic sequence, reset and restore"
"\x1b[1mhello\x1b[3m你好\x1b[0mworld! \x1b[1m\x1b[3mit's me!"
(call-with-output-string
(λ (str-port)
(define-values (ansi-port cur-seq reset restore)
(make-ansi-port-tuple str-port))
(display "\x1b[1mhello" ansi-port)
(display "\x1b[3m你好" ansi-port)
(reset)
(display "world! " ansi-port)
(restore)
(display "it's me!" ansi-port))))
(test-end "test-ansi")