-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.cpp
217 lines (173 loc) · 7.12 KB
/
tests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <catch2/catch.hpp>
#include "Parser.hpp"
#include <cassert>
#include <iostream>
using namespace cpparsec;
using namespace cpparsec::Parsers;
TEST_CASE("Digit Parser") {
auto digit_check = Digit.parse("12");
REQUIRE(digit_check.has_value());
REQUIRE(digit_check.value() == std::make_pair('1', string_view("2")));
digit_check = Digit.parse(digit_check.value().second);
REQUIRE(digit_check.has_value());
REQUIRE(digit_check.value() == std::make_pair('2', string_view("")));
}
TEST_CASE("OneOrMore") {
auto oneormore_check = AlphaNum.oneOrMore().parse("a12");
REQUIRE(oneormore_check.value() ==
std::make_pair(std::vector{'a', '1', '2'}, string_view("")));
}
TEST_CASE("Number") {
auto pos_num_check = PosNum.parse("123a");
REQUIRE(pos_num_check.has_value());
REQUIRE(pos_num_check.value() ==
std::make_pair<size_t, string_view>(123, string_view("a")));
auto negative = Num.parse("-123b");
REQUIRE(negative.has_value());
REQUIRE(negative.value() ==
std::make_pair<long long, string_view>(-123, string_view("b")));
}
TEST_CASE("Brackets Check") {
auto lcurly_check = LeftCurly.parse("{)");
REQUIRE(lcurly_check ==
std::make_optional(std::make_pair('{', string_view(")"))));
lcurly_check = LeftCurly.parse(lcurly_check.value().second);
REQUIRE(!lcurly_check.has_value());
}
TEST_CASE("andThen") {
auto andThenCheck =
String("Hi").andThen(String("Da")).andThen(String("F")).parse("HiDaD");
REQUIRE(!andThenCheck.has_value());
andThenCheck =
String("Hi").andThen(String("Da")).andThen(String("D")).parse("HiDaD");
REQUIRE(andThenCheck.has_value());
REQUIRE(andThenCheck.value().first ==
std::tuple<string_view, string_view, string_view>("Hi", "Da", "D"));
}
TEST_CASE("zips") {
auto zips = zip3(Alpha, PosNum, Alpha);
auto zipcheck = zips.parse("a12b");
REQUIRE(zipcheck.has_value());
REQUIRE(zipcheck.value() ==
std::make_pair(std::tuple<char, size_t, char>('a', 12, 'b'),
string_view("")));
zipcheck = zips.parse("a12");
REQUIRE(!zipcheck.has_value());
auto zipmany_res = zipMany(Alpha, Digit, Alpha, Digit).parse("a1a1");
REQUIRE(zipmany_res.has_value());
REQUIRE(zipmany_res.value() ==
std::make_pair(std::tuple<char, char, char, char>('a', '1', 'a', '1'),
string_view("")));
}
TEST_CASE("WhiteSpace") {
auto space_surr_skip = skipSurrWhitespace(Alpha).parse(" a ");
REQUIRE(space_surr_skip.has_value());
REQUIRE(space_surr_skip.value() == std::pair<char, string_view>('a', ""));
auto space_pre_skip = skipPreWhitespace(Alpha).parse(" a ");
REQUIRE(space_pre_skip.has_value());
REQUIRE(space_pre_skip.value() == std::pair<char, string_view>('a', " "));
}
TEST_CASE("OR operator Check") {
auto or_check = (Digit || Character('c')).parse("c1");
REQUIRE(or_check.has_value());
REQUIRE(or_check.value() == std::pair<char, string_view>('c', "1"));
or_check = (Digit || Character('c')).parse("d1");
REQUIRE(not or_check.has_value());
}
TEST_CASE("oneOf") {
auto oneOfCheck = oneOf(Digit, Alpha).parse("a1");
REQUIRE(oneOfCheck.has_value());
REQUIRE(oneOfCheck.value() == std::pair<char, string_view>('a', "1"));
auto oneOfCheck2 = oneOf(Digit, Alpha, Character('.')).parse(".a1");
REQUIRE(oneOfCheck2.has_value());
REQUIRE(oneOfCheck2.value() == std::pair<char, string_view>('.', "a1"));
auto oneOfCheck3 = oneOf(Digit, Alpha, Character('.')).parse(" a1");
REQUIRE(!oneOfCheck3.has_value());
}
TEST_CASE("zipAndGet") {
auto zipAndGetCheck = zipAndGet<0>(Alpha, Digit).parse("c1");
REQUIRE(zipAndGetCheck.has_value());
REQUIRE(zipAndGetCheck.value() == std::pair<char, string_view>('c', ""));
}
TEST_CASE("Optional") {
auto OptionalCheck1 = Optional(String("(")).parse("())");
REQUIRE(OptionalCheck1.value().first == true);
REQUIRE(OptionalCheck1.value().second == "))");
auto OptionalCheck2 = Optional(String("(")).parse("}}}");
REQUIRE(OptionalCheck2.value().first == false);
REQUIRE(OptionalCheck2.value().second == "}}}");
}
TEST_CASE("Char Template Specialization") {
auto char_template_specialization = Optional(Parser('.')).parse(".1");
REQUIRE(char_template_specialization.has_value());
REQUIRE(char_template_specialization.value() ==
std::pair<bool, string_view>(true, "1"));
}
TEST_CASE("Character Parsers") {
auto excludes1 = Char_excluding(']').parse("]Hi");
REQUIRE(!excludes1.has_value());
excludes1 = Char_excluding(']').parse("Hi");
REQUIRE(excludes1.has_value());
auto excludes2 = Char_excluding_many(std::array{'[', ']'}).parse("[Hi");
REQUIRE(!excludes2.has_value());
auto includes1 = Characters(std::array{'a', 'b', 'c'}).parse("ab");
REQUIRE(includes1.has_value());
includes1 = Characters(std::array{'a', 'b', 'c'}).parse("db");
REQUIRE(!includes1.has_value());
REQUIRE_THROWS(Character('a').orThrow("Couldnt parse a").parse("bc"));
auto error = Character('a').orThrow("Couldnt parse a").parse("abc");
REQUIRE(error.has_value());
}
TEST_CASE("End") {
auto end = End.parse("");
REQUIRE(end.value().first);
end = End.parse("a");
REQUIRE(!end.value().first);
}
TEST_CASE("Skips") {
auto skipmanycheck = skipMany(WhiteSpace).parse("a");
REQUIRE(skipmanycheck.value().first == 0);
skipmanycheck = skipMany1(WhiteSpace).parse(" a");
REQUIRE(skipmanycheck.value().first == 2);
skipmanycheck = skipMany1(WhiteSpace).parse("a");
REQUIRE(!skipmanycheck.has_value());
skipmanycheck = skipMany(WhiteSpace).parse(" a");
REQUIRE(skipmanycheck.value().first == 1);
}
TEST_CASE("Inside Brackets Parser") {
auto inside_parens = Parens(PosNum).parse("(12)");
REQUIRE(inside_parens.has_value());
REQUIRE(inside_parens.value().first == 12);
REQUIRE(inside_parens.value().second == "");
auto inside_curlies = Curlies(PosNum).parse("{12}");
REQUIRE(inside_curlies.has_value());
REQUIRE(inside_curlies.value().first == 12);
REQUIRE(inside_curlies.value().second == "");
}
TEST_CASE("SepBy Checks") {
auto sepby_check = sepBy(Char, Character(',')).parse("");
REQUIRE(sepby_check.has_value());
REQUIRE(sepby_check.value().first.empty());
sepby_check = sepBy(Char, Character(',')).parse("a");
REQUIRE(sepby_check.has_value());
REQUIRE(sepby_check.value().first == std::vector<char>{'a'});
// this isnt allowed
sepby_check = sepBy(Char, Character(',')).parse("a,");
REQUIRE(!sepby_check.has_value());
sepby_check = sepBy(Char, Character(',')).parse("a,b");
REQUIRE(sepby_check.has_value());
REQUIRE(sepby_check.value().first == std::vector<char>{'a', 'b'});
// sepBy1
auto sepby1_check = sepBy1(Char, Character(',')).parse("");
REQUIRE(!sepby1_check.has_value());
sepby1_check = sepBy1(Char, Character(',')).parse("a");
REQUIRE(sepby1_check.has_value());
REQUIRE(sepby1_check.value().first == std::vector<char>{'a'});
REQUIRE(sepby1_check.value().second == "");
sepby1_check = sepBy1(Char, Character(',')).parse("a,");
REQUIRE(!sepby1_check.has_value());
sepby1_check = sepBy1(Char, Character(',')).parse("a,b");
REQUIRE(sepby1_check.has_value());
REQUIRE(sepby1_check.value().first == std::vector<char>{'a', 'b'});
REQUIRE(sepby1_check.value().second == "");
}