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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
// file : tests/manifest-parser/driver.cxx -*- C++ -*-
// license : MIT; see accompanying LICENSE file
#include <cassert>
#ifndef __cpp_lib_modules_ts
#include <vector>
#include <string>
#include <utility> // pair, move()
#include <sstream>
#include <iostream>
#endif
// Other includes.
#ifdef __cpp_modules_ts
#ifdef __cpp_lib_modules_ts
import std.core;
import std.io;
#endif
import butl.optional;
import butl.manifest_parser;
#else
#include <libbutl/optional.mxx>
#include <libbutl/manifest-parser.mxx>
#endif
using namespace std;
namespace butl
{
using butl::optional;
using butl::nullopt;
using pairs = vector<pair<string, string>>;
static optional<pairs>
to_pairs (optional<vector<manifest_name_value>>&&);
static bool
equal (const optional<pairs>& actual, const optional<pairs>& expected);
// Test manifest as it is represented in the stream, including format
// version and end-of-manifest values.
//
static bool
test (const char* manifest,
const pairs& expected,
manifest_parser::filter_function = {});
static bool
fail (const char* manifest);
// Test manifest as it is returned by parse_manifest(), without the special
// format version and end-of-manifest values.
//
static bool
test_parse (const char* manifest, const optional<pairs>& expected);
static bool
fail_parse (const char* manifest);
int
main ()
{
// Whitespaces and comments.
//
assert (test (" \t", {{"",""}}));
assert (test (" \t\n \n\n", {{"",""}}));
assert (test ("# one\n #two", {{"",""}}));
// Test encountering eos at various points.
//
assert (test ("", {{"",""}}));
assert (test (" ", {{"",""}}));
assert (test ("\n", {{"",""}}));
assert (fail ("a"));
assert (test (":1\na:", {{"","1"},{"a", ""},{"",""},{"",""}}));
// Invalid manifests.
//
assert (fail ("a:")); // format version pair expected
assert (fail (":")); // format version value expected
assert (fail (":9")); // unsupported format version
assert (fail ("a")); // ':' expected after name
assert (fail ("a b")); // ':' expected after name
assert (fail ("a\tb")); // ':' expected after name
assert (fail ("a\nb")); // ':' expected after name
assert (fail (":1\na:b\n:9")); // unsupported format version
// Empty manifest.
//
assert (test (":1", {{"","1"},{"",""},{"",""}}));
assert (test (" \t :1", {{"","1"},{"",""},{"",""}}));
assert (test (" \t : 1", {{"","1"},{"",""},{"",""}}));
assert (test (" \t : 1 ", {{"","1"},{"",""},{"",""}}));
assert (test (":1\n", {{"","1"},{"",""},{"",""}}));
assert (test (":1 \n", {{"","1"},{"",""},{"",""}}));
// Single manifest.
//
assert (test (":1\na:x", {{"","1"},{"a", "x"},{"",""},{"",""}}));
assert (test (":1\na:x\n", {{"","1"},{"a","x"},{"",""},{"",""}}));
assert (test (":1\na:x\nb:y",
{{"","1"},{"a","x"},{"b","y"},{"",""},{"",""}}));
assert (test (":1\na:x\n\tb : y\n #comment",
{{"","1"},{"a","x"},{"b","y"},{"",""},{"",""}}));
// Multiple manifests.
//
assert (test (":1\na:x\n:\nb:y",
{{"","1"},{"a", "x"},{"",""},
{"","1"},{"b", "y"},{"",""},{"",""}}));
assert (test (":1\na:x\n:1\nb:y",
{{"","1"},{"a", "x"},{"",""},
{"","1"},{"b", "y"},{"",""},{"",""}}));
assert (test (":1\na:x\n:\nb:y\n:\nc:z\n",
{{"","1"},{"a", "x"},{"",""},
{"","1"},{"b", "y"},{"",""},
{"","1"},{"c", "z"},{"",""},{"",""}}));
// Name parsing.
//
assert (test (":1\nabc:", {{"","1"},{"abc",""},{"",""},{"",""}}));
assert (test (":1\nabc :", {{"","1"},{"abc",""},{"",""},{"",""}}));
assert (test (":1\nabc\t:", {{"","1"},{"abc",""},{"",""},{"",""}}));
// Simple value parsing.
//
assert (test (":1\na: \t xyz \t ",
{{"","1"},{"a","xyz"},{"",""},{"",""}}));
// Simple value escaping.
//
assert (test (":1\na:x\\", {{"","1"},{"a","x"},{"",""},{"",""}}));
assert (test (":1\na:x\\\ny", {{"","1"},{"a","xy"},{"",""},{"",""}}));
assert (test (":1\na:x\\\\\nb:",
{{"","1"},{"a","x\\"},{"b",""},{"",""},{"",""}}));
assert (test (":1\na:x\\\\\\\nb:",
{{"","1"},{"a","x\\\\"},{"b",""},{"",""},{"",""}}));
// Simple value literal newline.
//
assert (test (":1\na:x\\\n\\", {{"","1"},{"a","x\n"},{"",""},{"",""}}));
assert (test (":1\na:x\\\n\\\ny",
{{"","1"},{"a","x\ny"},{"",""},{"",""}}));
assert (test (":1\na:x\\\n\\\ny\\\n\\\nz",
{{"","1"},{"a","x\ny\nz"},{"",""},{"",""}}));
// Multi-line value parsing.
//
assert (test (":1\na:\\", {{"","1"},{"a", ""},{"",""},{"",""}}));
assert (test (":1\na:\\\n", {{"","1"},{"a", ""},{"",""},{"",""}}));
assert (test (":1\na:\\x", {{"","1"},{"a", "\\x"},{"",""},{"",""}}));
assert (test (":1\na:\\\n\\", {{"","1"},{"a", ""},{"",""},{"",""}}));
assert (test (":1\na:\\\n\\\n", {{"","1"},{"a", ""},{"",""},{"",""}}));
assert (test (":1\na:\\\n\\x\n\\",
{{"","1"},{"a", "\\x"},{"",""},{"",""}}));
assert (test (":1\na:\\\nx\ny", {{"","1"},{"a", "x\ny"},{"",""},{"",""}}));
assert (test (":1\na:\\\n \n#\t\n\\",
{{"","1"},{"a", " \n#\t"},{"",""},{"",""}}));
assert (test (":1\na:\\\n\n\n\\", {{"","1"},{"a", "\n"},{"",""},{"",""}}));
// Multi-line value escaping.
//
assert (test (":1\na:\\\nx\\", {{"","1"},{"a","x"},{"",""},{"",""}}));
assert (test (":1\na:\\\nx\\\ny\n\\",
{{"","1"},{"a","xy"},{"",""},{"",""}}));
assert (test (":1\na:\\\nx\\\\\n\\\nb:",
{{"","1"},{"a","x\\"},{"b",""},{"",""},{"",""}}));
assert (test (":1\na:\\\nx\\\\\\\n\\\nb:",
{{"","1"},{"a","x\\\\"},{"b",""},{"",""},{"",""}}));
// Manifest value splitting (into the value/comment pair).
//
{
auto p (manifest_parser::split_comment ("value\\; text ; comment text"));
assert (p.first == "value; text" && p.second == "comment text");
}
{
auto p (manifest_parser::split_comment ("value"));
assert (p.first == "value" && p.second == "");
}
{
auto p (manifest_parser::split_comment ("; comment"));
assert (p.first == "" && p.second == "comment");
}
// Filtering.
//
assert (test (":1\na: abc\nb: bca\nc: cab",
{{"","1"},{"a","abc"},{"c","cab"},{"",""},{"",""}},
[] (manifest_name_value& nv) {return nv.name != "b";}));
assert (test (":1\na: abc\nb: bca",
{{"","1"},{"ax","abc."},{"bx","bca."},{"",""},{"",""}},
[] (manifest_name_value& nv)
{
if (!nv.name.empty ())
{
nv.name += 'x';
nv.value += '.';
}
return true;
}));
// Test parse_manifest().
//
test_parse ("", nullopt);
test_parse (":1", pairs ());
test_parse (":1\na: abc\nb: cde", pairs ({{"a", "abc"}, {"b", "cde"}}));
fail_parse ("# abc");
fail_parse ("a: abc");
// Parse the manifest list.
//
{
istringstream is (":1\na: abc\nb: bcd\n:\nx: xyz");
is.exceptions (istream::failbit | istream::badbit);
manifest_parser p (is, "");
equal (to_pairs (try_parse_manifest (p)),
pairs ({{"a", "abc"}, {"b", "bcd"}}));
equal (to_pairs (try_parse_manifest (p)), pairs ({{"x", "xyz"}}));
equal (to_pairs (try_parse_manifest (p)), nullopt);
}
return 0;
}
static ostream&
operator<< (ostream& os, const pairs& ps)
{
os << '{';
bool f (true);
for (const auto& p: ps)
os << (f ? (f = false, "") : ",")
<< '{' << p.first << ',' << p.second << '}';
os << '}';
return os;
}
static ostream&
operator<< (ostream& os, const optional<pairs>& ps)
{
return ps ? (os << *ps) : (os << "[null]");
}
static bool
equal (const optional<pairs>& a, const optional<pairs>& e)
{
if (a != e)
{
cerr << "actual: " << a << endl
<< "expect: " << e << endl;
return false;
}
return true;
}
static optional<pairs>
to_pairs (optional<vector<manifest_name_value>>&& nvs)
{
if (!nvs)
return nullopt;
pairs r;
for (manifest_name_value& nv: *nvs)
r.emplace_back (move (nv.name), move (nv.value));
return r;
}
static pairs
parse (const char* m, manifest_parser::filter_function f = {})
{
istringstream is (m);
is.exceptions (istream::failbit | istream::badbit);
manifest_parser p (is, "", move (f));
pairs r;
for (bool eom (true), eos (false); !eos; )
{
manifest_name_value nv (p.next ());
if (nv.empty ()) // End pair.
{
eos = eom;
eom = true;
}
else
eom = false;
r.emplace_back (move (nv.name), move (nv.value));
}
return r;
}
static bool
test (const char* m, const pairs& e, manifest_parser::filter_function f)
{
return equal (parse (m, move (f)), e);
}
static bool
fail (const char* m)
{
try
{
pairs r (parse (m));
cerr << "nofail: " << r << endl;
return false;
}
catch (const manifest_parsing&)
{
//cerr << e << endl;
}
return true;
}
static bool
test_parse (const char* m, const optional<pairs>& e)
{
istringstream is (m);
is.exceptions (istream::failbit | istream::badbit);
manifest_parser p (is, "");
return equal (to_pairs (try_parse_manifest (p)), e);
}
static bool
fail_parse (const char* m)
{
try
{
istringstream is (m);
is.exceptions (istream::failbit | istream::badbit);
manifest_parser p (is, "");
optional<pairs> r (to_pairs (parse_manifest (p)));
cerr << "nofail: " << r << endl;
return false;
}
catch (const manifest_parsing&)
{
//cerr << e << endl;
}
return true;
}
}
int
main ()
{
return butl::main ();
}
|