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