Add failing unit tests, seems stream is not 100%

This commit is contained in:
Vivianne 2024-03-03 23:16:43 -05:00
parent 59c9347bea
commit fcc0f4b263
3 changed files with 62 additions and 4 deletions

View file

@ -31,7 +31,8 @@
(tests ((directory
"tests"
((scheme-file "test-eastasian-locale")
(scheme-file "test-uniseg")))))
(scheme-file "test-uniseg")
(scheme-file "test-graphemes-stream")))))
(programs
((directory
"scripts"

View file

@ -0,0 +1,54 @@
(define-module (tests test-graphemes-stream)
#:use-module (uniseg graphemes stream)
#:use-module (srfi srfi-41)
#:use-module (srfi srfi-64))
(test-begin "tests-graphemes-stream")
;; silly double-em dash
(define stream (string->grapheme-stream "Trans 🏳️‍⚧️ rights are human rights⸺and that's a promise!"))
(define* (advance-stream! #:optional (times 1))
(for-each
(λ (_)
(set! stream (stream-cdr stream)))
(make-list times)))
(advance-stream! 6)
(define trans-flag-grapheme (stream-car stream))
(test-equal "Trans flag stream is correct"
"🏳️‍⚧️" (grapheme-string trans-flag-grapheme))
(test-equal "Trans flag is width 2"
2 (grapheme-width trans-flag-grapheme))
(advance-stream! 2)
(define r-grapheme (stream-car stream))
(test-equal "r in rights is in correct place"
"r" (grapheme-string r-grapheme))
(test-equal "r is width 1"
1 (grapheme-width r-grapheme))
(advance-stream! 23)
(define double-em-dash-grapheme (stream-car stream))
(test-equal "double-em-dash is in right place"
"⸺" (grapheme-string double-em-dash-grapheme))
(test-equal "double-em-dash is double-width"
2 (grapheme-width double-em-dash-grapheme))
(advance-stream!)
(define a-grapheme (stream-car stream))
(test-equal "advancing one goes to the a"
"a" (grapheme-string a-grapheme))
(test-end "tests-graphemes-stream")

View file

@ -8,19 +8,22 @@
#:export (make-grapheme
grapheme?
grapheme-glyphs
grapheme-string
grapheme-width
grapheme-sentence-end?
grapheme-word-end?
string->grapheme-stream
input->grapheme-stream))
;; TODO: uniseg also does word and sentence boundaries. These state machines could be implemented if we wanted to.
;; TODO: the golang uniseg also does word and sentence boundaries. These state machines could be implemented if we wanted to.
(define-immutable-record-type <grapheme>
(make-grapheme glyphs width)
grapheme?
(glyphs grapheme-glyphs)
(width grapheme-width))
(define (grapheme-string grapheme)
"Given a grapheme, construct a string representing it."
(list->string (grapheme-glyphs grapheme)))
(define (string->grapheme-stream str)
"Given a string, create a (lazy) stream of graphemes."
(with-input-from-string str