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
|
// file : tests/manifest-serializer/driver.cxx -*- C++ -*-
// license : MIT; see accompanying LICENSE file
#include <vector>
#include <string>
#include <utility> // pair
#include <sstream>
#include <iostream>
#include <libbutl/manifest-serializer.hxx>
#undef NDEBUG
#include <cassert>
using namespace std;
using namespace butl;
using pairs = vector<pair<string, string>>;
static bool
test (const pairs& manifest,
const string& expected,
bool long_lines = false,
manifest_serializer::filter_function f = {});
static bool
fail (const pairs& manifest);
int
main ()
{
// Comments.
//
assert (test ({{"#", ""}}, "#\n"));
assert (test ({{"#", "x"}}, "# x\n"));
assert (test ({{"#", "x"},{"#", "y"},{"#", ""}}, "# x\n# y\n#\n"));
assert (fail ({{"",""},{"#", "x"}})); // serialization after eos
assert (fail ({{"#", "\xB0"}})); // invalid UTF-8 sequence
// Empty manifest stream.
//
assert (test ({}, ""));
assert (test ({{"",""}}, ""));
// Empty manifest.
//
assert (test ({{"","1"},{"",""},{"",""}}, ": 1\n"));
assert (test ({{"","1"},{"",""},{"","1"},{"",""},{"",""}}, ": 1\n:\n"));
// Invalid manifests.
//
assert (fail ({{"a",""}})); // format version pair expected
assert (fail ({{"","1"},{"",""},{"a",""}})); // format version pair expected
assert (fail ({{"","9"}})); // unsupported format version 9
assert (fail ({{"","1"},{"","x"}})); // non-empty value in end pair
assert (fail ({{"",""},{"","1"}})); // serialization after eos
// Single manifest.
//
assert (test ({{"","1"},{"a","x"},{"",""},{"",""}}, ": 1\na: x\n"));
assert (test ({{"","1"},{"a","x"},{"b","y"},{"",""},{"",""}},
": 1\na: x\nb: y\n"));
assert (test ({{"","1"},{"#","c"},{"a","x"},{"",""},{"",""}},
": 1\n# c\na: x\n"));
// Multiple manifests.
//
assert (test ({{"","1"},{"a","x"},{"",""},
{"","1"},{"b","y"},{"",""},{"",""}}, ": 1\na: x\n:\nb: y\n"));
assert (test ({{"","1"},{"a","x"},{"",""},
{"","1"},{"b","y"},{"",""},
{"","1"},{"c","z"},{"",""},{"",""}},
": 1\na: x\n:\nb: y\n:\nc: z\n"));
// Invalid name.
//
assert (fail ({{"","1"},{"#a",""}}));
assert (fail ({{"","1"},{"a:b",""}}));
assert (fail ({{"","1"},{"a b",""}}));
assert (fail ({{"","1"},{"a\tb",""}}));
assert (fail ({{"","1"},{"a\n",""}}));
assert (fail ({{"","1"},{"a\xB0",""}})); // invalid UTF-8 sequence
// Invalid value.
//
assert (fail ({{"","1"},{"a","\xB0"}})); // invalid UTF-8 sequence
assert (fail ({{"","1"},{"a","\xD0"}})); // incomplete UTF-8 sequence
// Simple value.
//
assert (test ({{"","1"},{"a",""},{"",""},{"",""}}, ": 1\na:\n"));
assert (test ({{"","1"},{"a","x y z"},{"",""},{"",""}}, ": 1\na: x y z\n"));
// Long simple value (newline escaping).
//
//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zzzzzzzzzzzzzzzzzzzzzzzzz");
// Space/soft break.
//
string l3 ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
"xxxxxxxxxxxxxxxxxxx"
" Yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
"yyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
" Zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz");
string e3 ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
"xxxxxxxxxxxxxxxxxxx\\\n"
" Yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
"yyyyyyyyyyyyyyyyyyyyyyyyyyyyy\\\n"
" Zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz");
// Space with a better one/soft break.
//
string l4 ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
"xxxxxxxxx xxxxxxxxx"
" Yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
"yyyyyyyyyyyyyyyyyy yyyyyyyyyy"
" Zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz");
string e4 ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
"xxxxxxxxx xxxxxxxxx\\\n"
" Yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
"yyyyyyyyyyyyyyyyyy yyyyyyyyyy\\\n"
" Zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz");
// Hard break after the backslash/delayed hard break.
//
string l5 ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\\"
"Yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
string e5 ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\\Y\\\n"
"yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
// Hard break after the UTF-8/delayed hard break.
//
string l6 ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\xF0\x90\x8C\x82"
"\xF0\x90\x8C\x82yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
string e6 ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\xF0\x90\x8C\x82\\\n"
"\xF0\x90\x8C\x82yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
assert (test ({{"","1"},{"a",l1},{"",""},{"",""}}, ": 1\na: " + e1 + "\n"));
assert (test ({{"","1"},{"a",l2},{"",""},{"",""}}, ": 1\na: " + e2 + "\n"));
assert (test ({{"","1"},{"a",l3},{"",""},{"",""}}, ": 1\na: " + e3 + "\n"));
assert (test ({{"","1"},{"a",l4},{"",""},{"",""}}, ": 1\na: " + e4 + "\n"));
assert (test ({{"","1"},{"a",l5},{"",""},{"",""}}, ": 1\na: " + e5 + "\n"));
assert (test ({{"","1"},{"a",l6},{"",""},{"",""}}, ": 1\na: " + e6 + "\n"));
// Multi-line value.
//
string n ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
assert (test ({{"","1"},{n,"x"},{"",""},{"",""}},
": 1\n" + n + ": \\\nx\n\\\n"));
assert (test ({{"","1"},{"a","\n"},{"",""},{"",""}},
": 1\na: \\\n\n\n\\\n"));
assert (test ({{"","1"},{"a","\n\n"},{"",""},{"",""}},
": 1\na: \\\n\n\n\n\\\n"));
assert (test ({{"","1"},{"a","\nx\n"},{"",""},{"",""}},
": 1\na: \\\n\nx\n\n\\\n"));
assert (test ({{"","1"},{"a","x\ny\nz"},{"",""},{"",""}},
": 1\na: \\\nx\ny\nz\n\\\n"));
assert (test ({{"","1"},{"a"," x"},{"",""},{"",""}},
": 1\na: \\\n x\n\\\n"));
assert (test ({{"","1"},{"a","x "},{"",""},{"",""}},
": 1\na: \\\nx \n\\\n"));
assert (test ({{"","1"},{"a"," x "},{"",""},{"",""}},
": 1\na: \\\n x \n\\\n"));
// The long lines mode.
//
assert (test ({{"","1"},{"a",l1},{"",""},{"",""}},
": 1\na: " + l1 + "\n",
true /* long_lines */));
assert (test ({{"","1"},{"a", " abc\n" + l1 + "\ndef"},{"",""},{"",""}},
": 1\na: \\\n abc\n" + l1 + "\ndef\n\\\n",
true /* long_lines */));
assert (test ({{"","1"},{n,l1},{"",""},{"",""}},
": 1\n" + n + ": \\\n" + l1 + "\n\\\n",
true /* long_lines */));
// Carriage return character.
//
assert (test ({{"","1"},{"a","x\ry"},{"",""},{"",""}},
": 1\na: \\\nx\ny\n\\\n"));
assert (test ({{"","1"},{"a","x\r"},{"",""},{"",""}},
": 1\na: \\\nx\n\n\\\n"));
assert (test ({{"","1"},{"a","x\r\ny"},{"",""},{"",""}},
": 1\na: \\\nx\ny\n\\\n"));
assert (test ({{"","1"},{"a","x\r\n"},{"",""},{"",""}},
": 1\na: \\\nx\n\n\\\n"));
// Extra three x's are for the leading name part ("a: ") that we
// don't have.
//
assert (test ({{"","1"},{"a","\nxxx" + l1},{"",""},{"",""}},
": 1\na: \\\n\nxxx" + e1 + "\n\\\n"));
assert (test ({{"","1"},{"a","\nxxx" + l2},{"",""},{"",""}},
": 1\na: \\\n\nxxx" + e2 + "\n\\\n"));
assert (test ({{"","1"},{"a","\nxxx" + l3},{"",""},{"",""}},
": 1\na: \\\n\nxxx" + e3 + "\n\\\n"));
assert (test ({{"","1"},{"a","\nxxx" + l4},{"",""},{"",""}},
": 1\na: \\\n\nxxx" + e4 + "\n\\\n"));
// Backslash escaping (simple and multi-line).
//
assert (test ({{"","1"},{"a","c:\\"},{"",""},{"",""}},
": 1\na: c:\\\\\n"));
assert (test ({{"","1"},{"a","c:\\\nd:\\"},{"",""},{"",""}},
": 1\na: \\\nc:\\\\\nd:\\\\\n\\\n"));
// Manifest value/comment merging.
//
assert (manifest_serializer::merge_comment ("value; text", "comment") ==
"value\\; text; comment");
assert (manifest_serializer::merge_comment ("value text", "") ==
"value text");
// Filtering.
//
assert (test ({{"","1"},{"a","abc"},{"b","bca"},{"c","cab"},{"",""},{"",""}},
": 1\na: abc\nc: cab\n",
false /* long_lines */,
[] (const string& n, const string&) {return n != "b";}));
}
static string
serialize (const pairs& m,
bool long_lines = false,
manifest_serializer::filter_function f = {})
{
ostringstream os;
os.exceptions (istream::failbit | istream::badbit);
manifest_serializer s (os, "", long_lines, f);
for (const auto& p: m)
{
if (p.first != "#")
s.next (p.first, p.second);
else
s.comment (p.second);
}
return os.str ();
}
static bool
test (const pairs& m,
const string& e,
bool long_lines,
manifest_serializer::filter_function f)
{
string r (serialize (m, long_lines, f));
if (r != e)
{
cerr << "actual:" << endl << "'" << r << "'"<< endl
<< "expect:" << endl << "'" << e << "'"<< endl;
return false;
}
return true;
}
static bool
fail (const pairs& m)
{
try
{
string r (serialize (m));
cerr << "nofail: " << r << endl;
return false;
}
catch (const manifest_serialization&)
{
//cerr << e << endl;
}
return true;
}
|