]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - utils/text/regex_test.cpp
Import the kyua testing framework for infrastructure software
[FreeBSD/FreeBSD.git] / utils / text / regex_test.cpp
1 // Copyright 2014 The Kyua Authors.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 //   notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 //   notice, this list of conditions and the following disclaimer in the
12 //   documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 //   may be used to endorse or promote products derived from this software
15 //   without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #include "utils/text/regex.hpp"
30
31 #include <atf-c++.hpp>
32
33 #include "utils/text/exceptions.hpp"
34
35 namespace text = utils::text;
36
37
38 ATF_TEST_CASE_WITHOUT_HEAD(integration__no_matches);
39 ATF_TEST_CASE_BODY(integration__no_matches)
40 {
41     const text::regex_matches matches = text::match_regex(
42         "foo.*bar", "this is a string without the searched text", 0);
43     ATF_REQUIRE(!matches);
44     ATF_REQUIRE_EQ(0, matches.count());
45 }
46
47
48 ATF_TEST_CASE_WITHOUT_HEAD(integration__no_capture_groups);
49 ATF_TEST_CASE_BODY(integration__no_capture_groups)
50 {
51     const text::regex_matches matches = text::match_regex(
52         "foo.*bar", "this is a string with foo and bar embedded in it", 0);
53     ATF_REQUIRE(matches);
54     ATF_REQUIRE_EQ(1, matches.count());
55     ATF_REQUIRE_EQ("foo and bar", matches.get(0));
56 }
57
58
59 ATF_TEST_CASE_WITHOUT_HEAD(integration__one_capture_group);
60 ATF_TEST_CASE_BODY(integration__one_capture_group)
61 {
62     const text::regex_matches matches = text::match_regex(
63         "^([^ ]*) ", "the string", 1);
64     ATF_REQUIRE(matches);
65     ATF_REQUIRE_EQ(2, matches.count());
66     ATF_REQUIRE_EQ("the ", matches.get(0));
67     ATF_REQUIRE_EQ("the", matches.get(1));
68 }
69
70
71 ATF_TEST_CASE_WITHOUT_HEAD(integration__many_capture_groups);
72 ATF_TEST_CASE_BODY(integration__many_capture_groups)
73 {
74     const text::regex_matches matches = text::match_regex(
75         "is ([^ ]*) ([a-z]*) to", "this is another string to parse", 2);
76     ATF_REQUIRE(matches);
77     ATF_REQUIRE_EQ(3, matches.count());
78     ATF_REQUIRE_EQ("is another string to", matches.get(0));
79     ATF_REQUIRE_EQ("another", matches.get(1));
80     ATF_REQUIRE_EQ("string", matches.get(2));
81 }
82
83
84 ATF_TEST_CASE_WITHOUT_HEAD(integration__capture_groups_underspecified);
85 ATF_TEST_CASE_BODY(integration__capture_groups_underspecified)
86 {
87     const text::regex_matches matches = text::match_regex(
88         "is ([^ ]*) ([a-z]*) to", "this is another string to parse", 1);
89     ATF_REQUIRE(matches);
90     ATF_REQUIRE_EQ(2, matches.count());
91     ATF_REQUIRE_EQ("is another string to", matches.get(0));
92     ATF_REQUIRE_EQ("another", matches.get(1));
93 }
94
95
96 ATF_TEST_CASE_WITHOUT_HEAD(integration__capture_groups_overspecified);
97 ATF_TEST_CASE_BODY(integration__capture_groups_overspecified)
98 {
99     const text::regex_matches matches = text::match_regex(
100         "is ([^ ]*) ([a-z]*) to", "this is another string to parse", 10);
101     ATF_REQUIRE(matches);
102     ATF_REQUIRE_EQ(3, matches.count());
103     ATF_REQUIRE_EQ("is another string to", matches.get(0));
104     ATF_REQUIRE_EQ("another", matches.get(1));
105     ATF_REQUIRE_EQ("string", matches.get(2));
106 }
107
108
109 ATF_TEST_CASE_WITHOUT_HEAD(integration__reuse_regex_in_multiple_matches);
110 ATF_TEST_CASE_BODY(integration__reuse_regex_in_multiple_matches)
111 {
112     const text::regex regex = text::regex::compile("number is ([0-9]+)", 1);
113
114     {
115         const text::regex_matches matches = regex.match("my number is 581.");
116         ATF_REQUIRE(matches);
117         ATF_REQUIRE_EQ(2, matches.count());
118         ATF_REQUIRE_EQ("number is 581", matches.get(0));
119         ATF_REQUIRE_EQ("581", matches.get(1));
120     }
121
122     {
123         const text::regex_matches matches = regex.match("your number is 6");
124         ATF_REQUIRE(matches);
125         ATF_REQUIRE_EQ(2, matches.count());
126         ATF_REQUIRE_EQ("number is 6", matches.get(0));
127         ATF_REQUIRE_EQ("6", matches.get(1));
128     }
129 }
130
131
132 ATF_TEST_CASE_WITHOUT_HEAD(integration__ignore_case);
133 ATF_TEST_CASE_BODY(integration__ignore_case)
134 {
135     const text::regex regex1 = text::regex::compile("foo", 0, false);
136     ATF_REQUIRE(!regex1.match("bar Foo bar"));
137     ATF_REQUIRE(!regex1.match("bar foO bar"));
138     ATF_REQUIRE(!regex1.match("bar FOO bar"));
139
140     ATF_REQUIRE(!text::match_regex("foo", "bar Foo bar", 0, false));
141     ATF_REQUIRE(!text::match_regex("foo", "bar foO bar", 0, false));
142     ATF_REQUIRE(!text::match_regex("foo", "bar FOO bar", 0, false));
143
144     const text::regex regex2 = text::regex::compile("foo", 0, true);
145     ATF_REQUIRE( regex2.match("bar foo bar"));
146     ATF_REQUIRE( regex2.match("bar Foo bar"));
147     ATF_REQUIRE( regex2.match("bar foO bar"));
148     ATF_REQUIRE( regex2.match("bar FOO bar"));
149
150     ATF_REQUIRE( text::match_regex("foo", "bar foo bar", 0, true));
151     ATF_REQUIRE( text::match_regex("foo", "bar Foo bar", 0, true));
152     ATF_REQUIRE( text::match_regex("foo", "bar foO bar", 0, true));
153     ATF_REQUIRE( text::match_regex("foo", "bar FOO bar", 0, true));
154 }
155
156 ATF_TEST_CASE_WITHOUT_HEAD(integration__invalid_regex);
157 ATF_TEST_CASE_BODY(integration__invalid_regex)
158 {
159     ATF_REQUIRE_THROW(text::regex_error,
160                       text::regex::compile("this is (unbalanced", 0));
161 }
162
163
164 ATF_INIT_TEST_CASES(tcs)
165 {
166     // regex and regex_matches are so coupled that it makes no sense to test
167     // them independently.  Just validate their integration.
168     ATF_ADD_TEST_CASE(tcs, integration__no_matches);
169     ATF_ADD_TEST_CASE(tcs, integration__no_capture_groups);
170     ATF_ADD_TEST_CASE(tcs, integration__one_capture_group);
171     ATF_ADD_TEST_CASE(tcs, integration__many_capture_groups);
172     ATF_ADD_TEST_CASE(tcs, integration__capture_groups_underspecified);
173     ATF_ADD_TEST_CASE(tcs, integration__capture_groups_overspecified);
174     ATF_ADD_TEST_CASE(tcs, integration__reuse_regex_in_multiple_matches);
175     ATF_ADD_TEST_CASE(tcs, integration__ignore_case);
176     ATF_ADD_TEST_CASE(tcs, integration__invalid_regex);
177 }