]> gitweb.factorcode.org Git - factor.git/blob - basis/unicode/breaks/breaks-tests.factor
5aece1798a85666f5d7bd728eb70a975c0250826
[factor.git] / basis / unicode / breaks / breaks-tests.factor
1 USING: tools.test unicode sequences math http.client kernel
2 splitting unicode.categories io.pathnames io.encodings.utf8
3 io.files io.files.temp strings quotations math.parser locals ;
4 IN: unicode.breaks.tests
5
6 { "\u001112\u001161\u0011abA\u000300a\r\r\n" }
7 [ "\r\n\raA\u000300\u001112\u001161\u0011ab" string-reverse ] unit-test
8 { "dcba" } [ "abcd" string-reverse ] unit-test
9 { 3 } [ "\u001112\u001161\u0011abA\u000300a"
10         dup last-grapheme head last-grapheme ] unit-test
11
12 { 3 } [ 2 "hello" first-grapheme-from ] unit-test
13 { 1 } [ 2 "hello" last-grapheme-from ] unit-test
14
15 { 4 } [ 2 "what am I saying" first-word-from ] unit-test
16 { 0 } [ 2 "what am I saying" last-word-from ] unit-test
17 { 16 } [ 11 "what am I saying" first-word-from ] unit-test
18 { 10 } [ 11 "what am I saying" last-word-from ] unit-test
19
20 { { t f t t f t } } [ 6 <iota> [ "as df" word-break-at? ] map ] unit-test
21
22 : grapheme-break-test ( -- filename )
23     "https://downloads.factorcode.org/misc/UCD/auxiliary/GraphemeBreakTest.txt"
24     "GraphemeBreakTest.txt" cache-file [ ?download-to ] keep ;
25
26 : word-break-test ( -- filename )
27     "https://downloads.factorcode.org/misc/UCD/auxiliary/WordBreakTest.txt"
28     "WordBreakTest.txt" cache-file [ ?download-to ] keep ;
29
30 : parse-test-file ( file-name -- tests )
31     utf8 file-lines
32     [ "#" split1 drop ] map harvest [
33         "÷" split
34         [
35             "×" split
36             [ [ blank? ] trim hex> ] map
37             [ { f 0 } member? ] reject
38             >string
39         ] map
40         harvest
41     ] map ;
42
43 :: test-unicode-breaks ( tests quot -- )
44     tests [
45         [ 1quotation ]
46         [ concat [ quot call [ "" like ] map ] curry ] bi unit-test
47     ] each ;
48
49 ! XXX: not used?
50 : grapheme-test ( tests -- )
51     [
52         [ 1quotation ]
53         [ concat [ >graphemes [ "" like ] map ] curry ] bi unit-test
54     ] each ;
55
56 : run-grapheme-break-tests ( -- )
57     grapheme-break-test parse-test-file [ >graphemes ] test-unicode-breaks ;
58
59 : run-word-break-tests ( -- )
60     word-break-test parse-test-file [ >words ] test-unicode-breaks ;