]> gitweb.factorcode.org Git - factor.git/blob - extra/pcre/pcre-tests.factor
pcre: stop using mirror and cloning matchers.
[factor.git] / extra / pcre / pcre-tests.factor
1 USING: accessors arrays assocs http.client kernel math.ranges
2 pcre pcre.ffi pcre.private random sequences system tools.test ;
3 QUALIFIED: regexp
4 IN: pcre.tests
5
6 [ { "Bords" "words" "word" } ] [
7     "Bords, words, word." { ", " ", " "." } split-subseqs
8 ] unit-test
9
10 [ { { 3 "day" } { 2 "month" } { 1 "year" } } ] [
11     "(?P<year>\\d{4})-(?P<month>\\d{2})-(?P<day>\\d{2})"
12     <compiled-pcre> nametable>>
13 ] unit-test
14
15 CONSTANT: iso-date "(?P<year>\\d{4})-(?P<month>\\d{2})-(?P<day>\\d{2})"
16
17 ! On windows the erroffset appears to be set to 0 despite there being
18 ! nothing wrong with the regexp.
19 [ t ] [
20     "foo" (pcre) 3array 1 tail { { f -1 } { f 0 } } member?
21 ] unit-test
22
23 [ { 1 2 3 } ] [
24     iso-date <pcre>
25     { "year" "month" "day" } [ pcre_get_stringnumber ] with map
26 ] unit-test
27
28 [ t ] [ "foo" <compiled-pcre> PCRE_UTF8 has-option? ] unit-test
29
30 os unix? [ [ 10 ] [ PCRE_CONFIG_NEWLINE pcre-config ] unit-test ] when
31
32 ! In this day and age, not supporting utf-8 is broken.
33 [ 1 ] [ PCRE_CONFIG_UTF8 pcre-config ] unit-test
34
35 [ 1 ] [ PCRE_CONFIG_UNICODE_PROPERTIES pcre-config ] unit-test
36
37 ! libpcre must not support 16 or 32 bit code points.
38 [ 0 ] [ PCRE_CONFIG_UTF16 pcre-config ] unit-test
39 [ 0 ] [ PCRE_CONFIG_UTF32 pcre-config ] unit-test
40
41 ! Tests for findall
42 [
43     { { f "1999-01-12" } { "year" "1999" } { "month" "01" } { "day" "12" } }
44 ] [
45     "1999-01-12" iso-date <compiled-pcre> findall first
46 ] unit-test
47
48 [ 3 ] [
49     "2003-10-09 1999-09-01 1514-10-20" iso-date <compiled-pcre> findall length
50 ] unit-test
51
52 [ 5 ] [ "abcdef" "[a-e]" findall length ] unit-test
53
54 [ 3 ] [ "foo bar baz" "foo|bar|baz" findall length ] unit-test
55
56 [ 3 ] [ "örjan är åtta" "[åäö]" findall length ] unit-test
57
58 [ 3 ] [ "ÅÄÖ" "\\p{Lu}" findall length ] unit-test
59
60 [ 3 ] [ "foobar" "foo(?=bar)" findall first first second length ] unit-test
61
62 [ { { { f ", " } } { { f ", " } } { { f "." } } } ] [
63     "Words, words, word." "\\W+" findall
64 ] unit-test
65
66 [ { ", " ", " "." } ] [
67     "Words, words, word." "\\W+" findall [ first second ] map
68 ] unit-test
69
70 : long-string ( -- x )
71     10000 [ CHAR: a CHAR: z [a,b] random ] "" replicate-as ;
72
73 ! Performance
74 [ 0 ] [ long-string ".{0,15}foobar.{0,10}" findall length ] unit-test
75
76 ! Empty matches, corner case behaviour is copied from pcredemo.c
77 [ { { { f "foo" } } { { f "" } } } ]
78 [ "foo" ".*" findall ] unit-test
79
80 [ { { { f "" } } { { f "" } } { { f "" } } } ]
81 [ "foo" "B*" findall ] unit-test
82
83 ! Empty matches in strings with multi-byte characters are tricky.
84 [ { { { f "" } } { { f "" } } { { f "" } } { { f "" } } } ]
85 [ "öööö" "x*" findall ] unit-test
86
87 ! Tests for matches?
88 [ t ] [ "örjan" "örjan" matches? ] unit-test
89
90 [ t ] [ "abcö" "\\p{Ll}{4}" matches? ] unit-test
91
92 ! Dotall mode, off by default
93 [ f ] [ "." <compiled-pcre> PCRE_DOTALL has-option? ] unit-test
94 [ t ] [ "(?s)." <compiled-pcre> PCRE_DOTALL has-option? ] unit-test
95
96 [ f ] [ "\n" "." matches? ] unit-test
97 [ t ] [ "\n" "(?s)." matches? ] unit-test
98
99 ! Caseless mode, off by default
100 [ { f t } ] [
101     { "x" "(?i)x" } [ <compiled-pcre> PCRE_CASELESS has-option? ] map
102 ] unit-test
103
104 ! Backreferences
105 [ { t f } ] [
106     { "response and responsibility" "sense and responsibility" }
107     [ "(sens|respons)e and \\1ibility" matches? ] map
108 ] unit-test
109
110 [ { t t f } ] [
111     { "rah rah" "RAH RAH" "RAH rah" } [ "((?i)rah)\\s+\\1" matches? ] map
112 ] unit-test
113
114 ! Splitting
115 [ { { "Words" "words" "word" } { "Words" "words" "word" } } ] [
116     "Words, words, word." { "\\W+" "[,. ]" } [ split ] with map
117 ] unit-test
118
119 ! Bigger tests
120 [ t ] [
121     "http://factorcode.org/" http-get nip
122     "href=\"(?P<link>[^\"]+)\"" findall [ "link" of ] map sequence?
123 ] unit-test
124
125 ! Test that the regexp syntax works.
126 [ t ] [ "1234abcd" regexp:R[ ^\d+\w+$] matches? ] unit-test