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