spectests.cpp 41.3 KB
Newer Older
Jesse Beder's avatar
Jesse Beder committed
1
#include "spectests.h"
2
#include "specexamples.h"
3
#include "yaml-cpp/yaml.h"
Jesse Beder's avatar
Jesse Beder committed
4
5
6
7
8
#include <fstream>
#include <sstream>
#include <vector>
#include <iostream>

9
#define YAML_ASSERT(cond) do { if(!(cond)) return "  Assert failed: " #cond; } while(false)
10
11
12
13
14
15
#define PARSE(doc, input) \
	std::stringstream stream(input);\
	YAML::Parser parser(stream);\
	YAML::Node doc;\
	parser.GetNextDocument(doc)
#define PARSE_NEXT(doc) parser.GetNextDocument(doc)
16

Jesse Beder's avatar
Jesse Beder committed
17
18
namespace Test {
	namespace Spec {
19
		// 2.1
Jesse Beder's avatar
Jesse Beder committed
20
		TEST SeqScalars() {
21
			PARSE(doc, ex2_1);
22
			YAML_ASSERT(doc.size() == 3);
23
24
25
			YAML_ASSERT(doc[0].to<std::string>() == "Mark McGwire");
			YAML_ASSERT(doc[1].to<std::string>() == "Sammy Sosa");
			YAML_ASSERT(doc[2].to<std::string>() == "Ken Griffey");
Jesse Beder's avatar
Jesse Beder committed
26
27
28
			return true;
		}
		
29
		// 2.2
Jesse Beder's avatar
Jesse Beder committed
30
		TEST MappingScalarsToScalars() {
31
			PARSE(doc, ex2_2);
32
			YAML_ASSERT(doc.size() == 3);
33
34
35
			YAML_ASSERT(doc["hr"].to<std::string>() == "65");
			YAML_ASSERT(doc["avg"].to<std::string>() == "0.278");
			YAML_ASSERT(doc["rbi"].to<std::string>() == "147");
Jesse Beder's avatar
Jesse Beder committed
36
37
			return true;
		}
Jesse Beder's avatar
Jesse Beder committed
38
		
39
		// 2.3
Jesse Beder's avatar
Jesse Beder committed
40
		TEST MappingScalarsToSequences() {
41
			PARSE(doc, ex2_3);
Jesse Beder's avatar
Jesse Beder committed
42
43
			YAML_ASSERT(doc.size() == 2);
			YAML_ASSERT(doc["american"].size() == 3);
44
45
46
			YAML_ASSERT(doc["american"][0].to<std::string>() == "Boston Red Sox");
			YAML_ASSERT(doc["american"][1].to<std::string>() == "Detroit Tigers");
			YAML_ASSERT(doc["american"][2].to<std::string>() == "New York Yankees");
Jesse Beder's avatar
Jesse Beder committed
47
			YAML_ASSERT(doc["national"].size() == 3);
48
49
50
			YAML_ASSERT(doc["national"][0].to<std::string>() == "New York Mets");
			YAML_ASSERT(doc["national"][1].to<std::string>() == "Chicago Cubs");
			YAML_ASSERT(doc["national"][2].to<std::string>() == "Atlanta Braves");
Jesse Beder's avatar
Jesse Beder committed
51
52
53
			return true;
		}
		
54
		// 2.4
Jesse Beder's avatar
Jesse Beder committed
55
56
		TEST SequenceOfMappings()
		{
57
			PARSE(doc, ex2_4);
Jesse Beder's avatar
Jesse Beder committed
58
59
			YAML_ASSERT(doc.size() == 2);
			YAML_ASSERT(doc[0].size() == 3);
60
61
62
			YAML_ASSERT(doc[0]["name"].to<std::string>() == "Mark McGwire");
			YAML_ASSERT(doc[0]["hr"].to<std::string>() == "65");
			YAML_ASSERT(doc[0]["avg"].to<std::string>() == "0.278");
Jesse Beder's avatar
Jesse Beder committed
63
			YAML_ASSERT(doc[1].size() == 3);
64
65
66
			YAML_ASSERT(doc[1]["name"].to<std::string>() == "Sammy Sosa");
			YAML_ASSERT(doc[1]["hr"].to<std::string>() == "63");
			YAML_ASSERT(doc[1]["avg"].to<std::string>() == "0.288");
Jesse Beder's avatar
Jesse Beder committed
67
68
69
			return true;
		}
		
70
		// 2.5
Jesse Beder's avatar
Jesse Beder committed
71
72
		TEST SequenceOfSequences()
		{
73
			PARSE(doc, ex2_5);
Jesse Beder's avatar
Jesse Beder committed
74
75
			YAML_ASSERT(doc.size() == 3);
			YAML_ASSERT(doc[0].size() == 3);
76
77
78
			YAML_ASSERT(doc[0][0].to<std::string>() == "name");
			YAML_ASSERT(doc[0][1].to<std::string>() == "hr");
			YAML_ASSERT(doc[0][2].to<std::string>() == "avg");
Jesse Beder's avatar
Jesse Beder committed
79
			YAML_ASSERT(doc[1].size() == 3);
80
81
82
			YAML_ASSERT(doc[1][0].to<std::string>() == "Mark McGwire");
			YAML_ASSERT(doc[1][1].to<std::string>() == "65");
			YAML_ASSERT(doc[1][2].to<std::string>() == "0.278");
Jesse Beder's avatar
Jesse Beder committed
83
			YAML_ASSERT(doc[2].size() == 3);
84
85
86
			YAML_ASSERT(doc[2][0].to<std::string>() == "Sammy Sosa");
			YAML_ASSERT(doc[2][1].to<std::string>() == "63");
			YAML_ASSERT(doc[2][2].to<std::string>() == "0.288");
Jesse Beder's avatar
Jesse Beder committed
87
88
89
			return true;
		}
		
90
		// 2.6
Jesse Beder's avatar
Jesse Beder committed
91
92
		TEST MappingOfMappings()
		{
93
			PARSE(doc, ex2_6);
Jesse Beder's avatar
Jesse Beder committed
94
95
			YAML_ASSERT(doc.size() == 2);
			YAML_ASSERT(doc["Mark McGwire"].size() == 2);
96
97
			YAML_ASSERT(doc["Mark McGwire"]["hr"].to<std::string>() == "65");
			YAML_ASSERT(doc["Mark McGwire"]["avg"].to<std::string>() == "0.278");
Jesse Beder's avatar
Jesse Beder committed
98
			YAML_ASSERT(doc["Sammy Sosa"].size() == 2);
99
100
			YAML_ASSERT(doc["Sammy Sosa"]["hr"].to<std::string>() == "63");
			YAML_ASSERT(doc["Sammy Sosa"]["avg"].to<std::string>() == "0.288");
Jesse Beder's avatar
Jesse Beder committed
101
102
103
			return true;
		}
		
104
		// 2.7
Jesse Beder's avatar
Jesse Beder committed
105
106
		TEST TwoDocumentsInAStream()
		{
107
			PARSE(doc, ex2_7);
Jesse Beder's avatar
Jesse Beder committed
108
			YAML_ASSERT(doc.size() == 3);
109
110
111
			YAML_ASSERT(doc[0].to<std::string>() == "Mark McGwire");
			YAML_ASSERT(doc[1].to<std::string>() == "Sammy Sosa");
			YAML_ASSERT(doc[2].to<std::string>() == "Ken Griffey");
112
113

			PARSE_NEXT(doc);
Jesse Beder's avatar
Jesse Beder committed
114
			YAML_ASSERT(doc.size() == 2);
115
116
			YAML_ASSERT(doc[0].to<std::string>() == "Chicago Cubs");
			YAML_ASSERT(doc[1].to<std::string>() == "St Louis Cardinals");
Jesse Beder's avatar
Jesse Beder committed
117
118
119
			return true;
		}
		
120
		// 2.8
Jesse Beder's avatar
Jesse Beder committed
121
122
		TEST PlayByPlayFeed()
		{
123
			PARSE(doc, ex2_8);
Jesse Beder's avatar
Jesse Beder committed
124
			YAML_ASSERT(doc.size() == 3);
125
126
127
			YAML_ASSERT(doc["time"].to<std::string>() == "20:03:20");
			YAML_ASSERT(doc["player"].to<std::string>() == "Sammy Sosa");
			YAML_ASSERT(doc["action"].to<std::string>() == "strike (miss)");
128
129

			PARSE_NEXT(doc);
Jesse Beder's avatar
Jesse Beder committed
130
			YAML_ASSERT(doc.size() == 3);
131
132
133
			YAML_ASSERT(doc["time"].to<std::string>() == "20:03:47");
			YAML_ASSERT(doc["player"].to<std::string>() == "Sammy Sosa");
			YAML_ASSERT(doc["action"].to<std::string>() == "grand slam");
Jesse Beder's avatar
Jesse Beder committed
134
135
136
			return true;
		}
		
137
		// 2.9
Jesse Beder's avatar
Jesse Beder committed
138
139
		TEST SingleDocumentWithTwoComments()
		{
140
			PARSE(doc, ex2_9);
Jesse Beder's avatar
Jesse Beder committed
141
142
			YAML_ASSERT(doc.size() == 2);
			YAML_ASSERT(doc["hr"].size() == 2);
143
144
			YAML_ASSERT(doc["hr"][0].to<std::string>() == "Mark McGwire");
			YAML_ASSERT(doc["hr"][1].to<std::string>() == "Sammy Sosa");
Jesse Beder's avatar
Jesse Beder committed
145
			YAML_ASSERT(doc["rbi"].size() == 2);
146
147
			YAML_ASSERT(doc["rbi"][0].to<std::string>() == "Sammy Sosa");
			YAML_ASSERT(doc["rbi"][1].to<std::string>() == "Ken Griffey");
Jesse Beder's avatar
Jesse Beder committed
148
149
150
			return true;
		}
		
151
		// 2.10
Jesse Beder's avatar
Jesse Beder committed
152
153
		TEST SimpleAnchor()
		{
154
			PARSE(doc, ex2_10);
Jesse Beder's avatar
Jesse Beder committed
155
156
			YAML_ASSERT(doc.size() == 2);
			YAML_ASSERT(doc["hr"].size() == 2);
157
158
			YAML_ASSERT(doc["hr"][0].to<std::string>() == "Mark McGwire");
			YAML_ASSERT(doc["hr"][1].to<std::string>() == "Sammy Sosa");
Jesse Beder's avatar
Jesse Beder committed
159
			YAML_ASSERT(doc["rbi"].size() == 2);
160
161
			YAML_ASSERT(doc["rbi"][0].to<std::string>() == "Sammy Sosa");
			YAML_ASSERT(doc["rbi"][1].to<std::string>() == "Ken Griffey");
Jesse Beder's avatar
Jesse Beder committed
162
			return true;
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
		}
		
		struct Pair {
			Pair() {}
			Pair(const std::string& f, const std::string& s): first(f), second(s) {}
			std::string first, second;
		};
		
		bool operator == (const Pair& p, const Pair& q) {
			return p.first == q.first && p.second == q.second;
		}
		
		void operator >> (const YAML::Node& node, Pair& p) {
			node[0] >> p.first;
			node[1] >> p.second;
		}
		
180
		// 2.11
181
182
		TEST MappingBetweenSequences()
		{
183
			PARSE(doc, ex2_11);
184
			YAML_ASSERT(doc.size() == 2);
185
			YAML_ASSERT(doc[Pair("Detroit Tigers", "Chicago cubs")].size() == 1);
186
			YAML_ASSERT(doc[Pair("Detroit Tigers", "Chicago cubs")][0].to<std::string>() == "2001-07-23");
187
			YAML_ASSERT(doc[Pair("New York Yankees", "Atlanta Braves")].size() == 3);
188
189
190
			YAML_ASSERT(doc[Pair("New York Yankees", "Atlanta Braves")][0].to<std::string>() == "2001-07-02");
			YAML_ASSERT(doc[Pair("New York Yankees", "Atlanta Braves")][1].to<std::string>() == "2001-08-12");
			YAML_ASSERT(doc[Pair("New York Yankees", "Atlanta Braves")][2].to<std::string>() == "2001-08-14");
191
192
			return true;
		}
193
		
194
		// 2.12
195
196
		TEST CompactNestedMapping()
		{
197
			PARSE(doc, ex2_12);
198
199
			YAML_ASSERT(doc.size() == 3);
			YAML_ASSERT(doc[0].size() == 2);
200
201
			YAML_ASSERT(doc[0]["item"].to<std::string>() == "Super Hoop");
			YAML_ASSERT(doc[0]["quantity"].to<int>() == 1);
202
			YAML_ASSERT(doc[1].size() == 2);
203
204
			YAML_ASSERT(doc[1]["item"].to<std::string>() == "Basketball");
			YAML_ASSERT(doc[1]["quantity"].to<int>() == 4);
205
			YAML_ASSERT(doc[2].size() == 2);
206
207
			YAML_ASSERT(doc[2]["item"].to<std::string>() == "Big Shoes");
			YAML_ASSERT(doc[2]["quantity"].to<int>() == 1);
208
209
210
			return true;
		}
		
211
		// 2.13
212
213
		TEST InLiteralsNewlinesArePreserved()
		{
214
			PARSE(doc, ex2_13);
215
			YAML_ASSERT(doc.to<std::string>() ==
216
217
218
219
						"\\//||\\/||\n"
						"// ||  ||__");
			return true;
		}
220
221
222
223
		
		// 2.14
		TEST InFoldedScalarsNewlinesBecomeSpaces()
		{
224
			PARSE(doc, ex2_14);
225
			YAML_ASSERT(doc.to<std::string>() == "Mark McGwire's year was crippled by a knee injury.");
226
227
228
229
230
231
			return true;
		}
		
		// 2.15
		TEST FoldedNewlinesArePreservedForMoreIndentedAndBlankLines()
		{
232
			PARSE(doc, ex2_15);
233
			YAML_ASSERT(doc.to<std::string>() ==
Jesse Beder's avatar
Jesse Beder committed
234
						"Sammy Sosa completed another fine season with great stats.\n\n"
235
						"  63 Home Runs\n"
Jesse Beder's avatar
Jesse Beder committed
236
						"  0.288 Batting Average\n\n"
237
238
239
240
241
242
243
						"What a year!");
			return true;
		}
		
		// 2.16
		TEST IndentationDeterminesScope()
		{
244
			PARSE(doc, ex2_16);
245
			YAML_ASSERT(doc.size() == 3);
246
247
248
			YAML_ASSERT(doc["name"].to<std::string>() == "Mark McGwire");
			YAML_ASSERT(doc["accomplishment"].to<std::string>() == "Mark set a major league home run record in 1998.\n");
			YAML_ASSERT(doc["stats"].to<std::string>() == "65 Home Runs\n0.278 Batting Average\n");
249
250
251
252
253
254
			return true;
		}
		
		// 2.17
		TEST QuotedScalars()
		{
255
			PARSE(doc, ex2_17);
256
			YAML_ASSERT(doc.size() == 6);
257
258
259
260
261
262
			YAML_ASSERT(doc["unicode"].to<std::string>() == "Sosa did fine.\xe2\x98\xba");
			YAML_ASSERT(doc["control"].to<std::string>() == "\b1998\t1999\t2000\n");
			YAML_ASSERT(doc["hex esc"].to<std::string>() == "\x0d\x0a is \r\n");
			YAML_ASSERT(doc["single"].to<std::string>() == "\"Howdy!\" he cried.");
			YAML_ASSERT(doc["quoted"].to<std::string>() == " # Not a 'comment'.");
			YAML_ASSERT(doc["tie-fighter"].to<std::string>() == "|\\-*-/|");
263
264
265
266
267
268
			return true;
		}
		
		// 2.18
		TEST MultiLineFlowScalars()
		{
269
			PARSE(doc, ex2_18);
270
			YAML_ASSERT(doc.size() == 2);
271
272
			YAML_ASSERT(doc["plain"].to<std::string>() == "This unquoted scalar spans many lines.");
			YAML_ASSERT(doc["quoted"].to<std::string>() == "So does this quoted scalar.\n");
273
274
275
			return true;
		}
		
Jesse Beder's avatar
Jesse Beder committed
276
		// TODO: 2.19 - 2.22 schema tags
277
278
279
280
		
		// 2.23
		TEST VariousExplicitTags()
		{
281
			PARSE(doc, ex2_23);
282
			YAML_ASSERT(doc.size() == 3);
283
			YAML_ASSERT(doc["not-date"].Tag() == "tag:yaml.org,2002:str");
284
			YAML_ASSERT(doc["not-date"].to<std::string>() == "2002-04-28");
285
			YAML_ASSERT(doc["picture"].Tag() == "tag:yaml.org,2002:binary");
286
			YAML_ASSERT(doc["picture"].to<std::string>() ==
287
288
289
290
291
				"R0lGODlhDAAMAIQAAP//9/X\n"
				"17unp5WZmZgAAAOfn515eXv\n"
				"Pz7Y6OjuDg4J+fn5OTk6enp\n"
				"56enmleECcgggoBADs=\n"
			);
292
			YAML_ASSERT(doc["application specific tag"].Tag() == "!something");
293
			YAML_ASSERT(doc["application specific tag"].to<std::string>() ==
294
295
296
297
298
299
300
301
302
303
				"The semantics of the tag\n"
				"above may be different for\n"
				"different documents."
			);
			return true;
		}
		
		// 2.24
		TEST GlobalTags()
		{
304
			PARSE(doc, ex2_24);
305
			YAML_ASSERT(doc.Tag() == "tag:clarkevans.com,2002:shape");
306
			YAML_ASSERT(doc.size() == 3);
307
			YAML_ASSERT(doc[0].Tag() == "tag:clarkevans.com,2002:circle");
308
309
			YAML_ASSERT(doc[0].size() == 2);
			YAML_ASSERT(doc[0]["center"].size() == 2);
310
311
312
			YAML_ASSERT(doc[0]["center"]["x"].to<int>() == 73);
			YAML_ASSERT(doc[0]["center"]["y"].to<int>() == 129);
			YAML_ASSERT(doc[0]["radius"].to<int>() == 7);
313
			YAML_ASSERT(doc[1].Tag() == "tag:clarkevans.com,2002:line");
314
315
			YAML_ASSERT(doc[1].size() == 2);
			YAML_ASSERT(doc[1]["start"].size() == 2);
316
317
			YAML_ASSERT(doc[1]["start"]["x"].to<int>() == 73);
			YAML_ASSERT(doc[1]["start"]["y"].to<int>() == 129);
318
			YAML_ASSERT(doc[1]["finish"].size() == 2);
319
320
			YAML_ASSERT(doc[1]["finish"]["x"].to<int>() == 89);
			YAML_ASSERT(doc[1]["finish"]["y"].to<int>() == 102);
321
			YAML_ASSERT(doc[2].Tag() == "tag:clarkevans.com,2002:label");
322
323
			YAML_ASSERT(doc[2].size() == 3);
			YAML_ASSERT(doc[2]["start"].size() == 2);
324
325
326
327
			YAML_ASSERT(doc[2]["start"]["x"].to<int>() == 73);
			YAML_ASSERT(doc[2]["start"]["y"].to<int>() == 129);
			YAML_ASSERT(doc[2]["color"].to<std::string>() == "0xFFEEBB");
			YAML_ASSERT(doc[2]["text"].to<std::string>() == "Pretty vector drawing.");
328
329
330
331
332
333
			return true;
		}
		
		// 2.25
		TEST UnorderedSets()
		{
334
			PARSE(doc, ex2_25);
335
			YAML_ASSERT(doc.Tag() == "tag:yaml.org,2002:set");
336
337
338
339
340
341
342
343
344
345
			YAML_ASSERT(doc.size() == 3);
			YAML_ASSERT(IsNull(doc["Mark McGwire"]));
			YAML_ASSERT(IsNull(doc["Sammy Sosa"]));
			YAML_ASSERT(IsNull(doc["Ken Griffey"]));
			return true;
		}
		
		// 2.26
		TEST OrderedMappings()
		{
346
			PARSE(doc, ex2_26);
347
			YAML_ASSERT(doc.Tag() == "tag:yaml.org,2002:omap");
348
349
			YAML_ASSERT(doc.size() == 3);
			YAML_ASSERT(doc[0].size() == 1);
350
			YAML_ASSERT(doc[0]["Mark McGwire"].to<int>() == 65);
351
			YAML_ASSERT(doc[1].size() == 1);
352
			YAML_ASSERT(doc[1]["Sammy Sosa"].to<int>() == 63);
353
			YAML_ASSERT(doc[2].size() == 1);
354
			YAML_ASSERT(doc[2]["Ken Griffey"].to<int>() == 58);
355
356
			return true;
		}
357
358
359
360
		
		// 2.27
		TEST Invoice()
		{
361
			PARSE(doc, ex2_27);
362
			YAML_ASSERT(doc.Tag() == "tag:clarkevans.com,2002:invoice");
363
			YAML_ASSERT(doc.size() == 8);
364
365
			YAML_ASSERT(doc["invoice"].to<int>() == 34843);
			YAML_ASSERT(doc["date"].to<std::string>() == "2001-01-23");
366
			YAML_ASSERT(doc["bill-to"].size() == 3);
367
368
			YAML_ASSERT(doc["bill-to"]["given"].to<std::string>() == "Chris");
			YAML_ASSERT(doc["bill-to"]["family"].to<std::string>() == "Dumars");
369
			YAML_ASSERT(doc["bill-to"]["address"].size() == 4);
370
371
372
373
			YAML_ASSERT(doc["bill-to"]["address"]["lines"].to<std::string>() == "458 Walkman Dr.\nSuite #292\n");
			YAML_ASSERT(doc["bill-to"]["address"]["city"].to<std::string>() == "Royal Oak");
			YAML_ASSERT(doc["bill-to"]["address"]["state"].to<std::string>() == "MI");
			YAML_ASSERT(doc["bill-to"]["address"]["postal"].to<std::string>() == "48046");
374
			YAML_ASSERT(doc["ship-to"].size() == 3);
375
376
			YAML_ASSERT(doc["ship-to"]["given"].to<std::string>() == "Chris");
			YAML_ASSERT(doc["ship-to"]["family"].to<std::string>() == "Dumars");
377
			YAML_ASSERT(doc["ship-to"]["address"].size() == 4);
378
379
380
381
			YAML_ASSERT(doc["ship-to"]["address"]["lines"].to<std::string>() == "458 Walkman Dr.\nSuite #292\n");
			YAML_ASSERT(doc["ship-to"]["address"]["city"].to<std::string>() == "Royal Oak");
			YAML_ASSERT(doc["ship-to"]["address"]["state"].to<std::string>() == "MI");
			YAML_ASSERT(doc["ship-to"]["address"]["postal"].to<std::string>() == "48046");
382
383
			YAML_ASSERT(doc["product"].size() == 2);
			YAML_ASSERT(doc["product"][0].size() == 4);
384
385
386
387
			YAML_ASSERT(doc["product"][0]["sku"].to<std::string>() == "BL394D");
			YAML_ASSERT(doc["product"][0]["quantity"].to<int>() == 4);
			YAML_ASSERT(doc["product"][0]["description"].to<std::string>() == "Basketball");
			YAML_ASSERT(doc["product"][0]["price"].to<std::string>() == "450.00");
388
			YAML_ASSERT(doc["product"][1].size() == 4);
389
390
391
392
393
394
395
			YAML_ASSERT(doc["product"][1]["sku"].to<std::string>() == "BL4438H");
			YAML_ASSERT(doc["product"][1]["quantity"].to<int>() == 1);
			YAML_ASSERT(doc["product"][1]["description"].to<std::string>() == "Super Hoop");
			YAML_ASSERT(doc["product"][1]["price"].to<std::string>() == "2392.00");
			YAML_ASSERT(doc["tax"].to<std::string>() == "251.42");
			YAML_ASSERT(doc["total"].to<std::string>() == "4443.52");
			YAML_ASSERT(doc["comments"].to<std::string>() == "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338.");
396
397
398
399
400
401
			return true;
		}
		
		// 2.28
		TEST LogFile()
		{
402
			PARSE(doc, ex2_28);
403
			YAML_ASSERT(doc.size() == 3);
404
405
406
			YAML_ASSERT(doc["Time"].to<std::string>() == "2001-11-23 15:01:42 -5");
			YAML_ASSERT(doc["User"].to<std::string>() == "ed");
			YAML_ASSERT(doc["Warning"].to<std::string>() == "This is an error message for the log file");
407
408

			PARSE_NEXT(doc);
409
			YAML_ASSERT(doc.size() == 3);
410
411
412
			YAML_ASSERT(doc["Time"].to<std::string>() == "2001-11-23 15:02:31 -5");
			YAML_ASSERT(doc["User"].to<std::string>() == "ed");
			YAML_ASSERT(doc["Warning"].to<std::string>() == "A slightly different error message.");
413
414

			PARSE_NEXT(doc);
415
			YAML_ASSERT(doc.size() == 4);
416
417
418
			YAML_ASSERT(doc["Date"].to<std::string>() == "2001-11-23 15:03:17 -5");
			YAML_ASSERT(doc["User"].to<std::string>() == "ed");
			YAML_ASSERT(doc["Fatal"].to<std::string>() == "Unknown variable \"bar\"");
419
420
			YAML_ASSERT(doc["Stack"].size() == 2);
			YAML_ASSERT(doc["Stack"][0].size() == 3);
421
422
423
			YAML_ASSERT(doc["Stack"][0]["file"].to<std::string>() == "TopClass.py");
			YAML_ASSERT(doc["Stack"][0]["line"].to<std::string>() == "23");
			YAML_ASSERT(doc["Stack"][0]["code"].to<std::string>() == "x = MoreObject(\"345\\n\")\n");
424
			YAML_ASSERT(doc["Stack"][1].size() == 3);
425
426
427
			YAML_ASSERT(doc["Stack"][1]["file"].to<std::string>() == "MoreClass.py");
			YAML_ASSERT(doc["Stack"][1]["line"].to<std::string>() == "58");
			YAML_ASSERT(doc["Stack"][1]["code"].to<std::string>() == "foo = bar");
428
429
430
431
432
433
434
435
			return true;
		}
		
		// TODO: 5.1 - 5.2 BOM
		
		// 5.3
		TEST BlockStructureIndicators()
		{
436
			PARSE(doc, ex5_3);
437
438
			YAML_ASSERT(doc.size() == 2);
			YAML_ASSERT(doc["sequence"].size() == 2);
439
440
			YAML_ASSERT(doc["sequence"][0].to<std::string>() == "one");
			YAML_ASSERT(doc["sequence"][1].to<std::string>() == "two");
441
			YAML_ASSERT(doc["mapping"].size() == 2);
442
443
			YAML_ASSERT(doc["mapping"]["sky"].to<std::string>() == "blue");
			YAML_ASSERT(doc["mapping"]["sea"].to<std::string>() == "green");
444
445
446
447
448
449
			return true;
		}
		
		// 5.4
		TEST FlowStructureIndicators()
		{
450
			PARSE(doc, ex5_4);
451
452
			YAML_ASSERT(doc.size() == 2);
			YAML_ASSERT(doc["sequence"].size() == 2);
453
454
			YAML_ASSERT(doc["sequence"][0].to<std::string>() == "one");
			YAML_ASSERT(doc["sequence"][1].to<std::string>() == "two");
455
			YAML_ASSERT(doc["mapping"].size() == 2);
456
457
			YAML_ASSERT(doc["mapping"]["sky"].to<std::string>() == "blue");
			YAML_ASSERT(doc["mapping"]["sea"].to<std::string>() == "green");
458
459
460
			return true;
		}
		
Jesse Beder's avatar
Jesse Beder committed
461
462
463
		// 5.5
		TEST CommentIndicator()
		{
464
			PARSE(doc, ex5_5);
Jesse Beder's avatar
Jesse Beder committed
465
466
467
			YAML_ASSERT(doc.size() == 0);
			return true;
		}
468
469
470
471
		
		// 5.6
		TEST NodePropertyIndicators()
		{
472
			PARSE(doc, ex5_6);
473
			YAML_ASSERT(doc.size() == 2);
474
475
			YAML_ASSERT(doc["anchored"].to<std::string>() == "value"); // TODO: assert tag
			YAML_ASSERT(doc["alias"].to<std::string>() == "value");
476
477
478
479
480
481
			return true;
		}
		
		// 5.7
		TEST BlockScalarIndicators()
		{
482
			PARSE(doc, ex5_7);
483
			YAML_ASSERT(doc.size() == 2);
484
485
			YAML_ASSERT(doc["literal"].to<std::string>() == "some\ntext\n");
			YAML_ASSERT(doc["folded"].to<std::string>() == "some text\n");
486
487
488
489
490
491
			return true;
		}
		
		// 5.8
		TEST QuotedScalarIndicators()
		{
492
			PARSE(doc, ex5_8);
493
			YAML_ASSERT(doc.size() == 2);
494
495
			YAML_ASSERT(doc["single"].to<std::string>() == "text");
			YAML_ASSERT(doc["double"].to<std::string>() == "text");
496
497
498
499
500
501
502
503
504
			return true;
		}
		
		// TODO: 5.9 directive
		// TODO: 5.10 reserved indicator
		
		// 5.11
		TEST LineBreakCharacters()
		{
505
			PARSE(doc, ex5_11);
506
			YAML_ASSERT(doc.to<std::string>() == "Line break (no glyph)\nLine break (glyphed)\n");
507
508
509
510
511
512
			return true;
		}
		
		// 5.12
		TEST TabsAndSpaces()
		{
513
			PARSE(doc, ex5_12);
514
			YAML_ASSERT(doc.size() == 2);
515
516
			YAML_ASSERT(doc["quoted"].to<std::string>() == "Quoted\t");
			YAML_ASSERT(doc["block"].to<std::string>() ==
517
518
519
520
521
						"void main() {\n"
						"\tprintf(\"Hello, world!\\n\");\n"
						"}");
			return true;
		}
522
523
524
525
		
		// 5.13
		TEST EscapedCharacters()
		{
526
			PARSE(doc, ex5_13);
527
			YAML_ASSERT(doc.to<std::string>() == "Fun with \x5C \x22 \x07 \x08 \x1B \x0C \x0A \x0D \x09 \x0B " + std::string("\x00", 1) + " \x20 \xA0 \x85 \xe2\x80\xa8 \xe2\x80\xa9 A A A");
528
529
530
531
532
533
			return true;
		}
		
		// 5.14
		TEST InvalidEscapedCharacters()
		{
534
			std::stringstream stream(ex5_14);
535
536
537
538
539
			try {
				YAML::Parser parser(stream);
				YAML::Node doc;
				parser.GetNextDocument(doc);
			} catch(const YAML::ParserException& e) {
540
				YAML_ASSERT(e.msg == std::string(YAML::ErrorMsg::INVALID_ESCAPE) + "c");
541
542
543
544
545
				return true;
			}
			
			return false;
		}
546
547
548
549
		
		// 6.1
		TEST IndentationSpaces()
		{
550
			PARSE(doc, ex6_1);
551
552
			YAML_ASSERT(doc.size() == 1);
			YAML_ASSERT(doc["Not indented"].size() == 2);
553
			YAML_ASSERT(doc["Not indented"]["By one space"].to<std::string>() == "By four\n  spaces\n");
554
			YAML_ASSERT(doc["Not indented"]["Flow style"].size() == 3);
555
556
557
			YAML_ASSERT(doc["Not indented"]["Flow style"][0].to<std::string>() == "By two");
			YAML_ASSERT(doc["Not indented"]["Flow style"][1].to<std::string>() == "Also by two");
			YAML_ASSERT(doc["Not indented"]["Flow style"][2].to<std::string>() == "Still by two");
558
559
560
561
562
563
			return true;
		}
		
		// 6.2
		TEST IndentationIndicators()
		{
564
			PARSE(doc, ex6_2);
565
566
			YAML_ASSERT(doc.size() == 1);
			YAML_ASSERT(doc["a"].size() == 2);
567
			YAML_ASSERT(doc["a"][0].to<std::string>() == "b");
568
			YAML_ASSERT(doc["a"][1].size() == 2);
569
570
			YAML_ASSERT(doc["a"][1][0].to<std::string>() == "c");
			YAML_ASSERT(doc["a"][1][1].to<std::string>() == "d");
571
572
573
574
575
576
			return true;
		}
		
		// 6.3
		TEST SeparationSpaces()
		{
577
			PARSE(doc, ex6_3);
578
579
			YAML_ASSERT(doc.size() == 2);
			YAML_ASSERT(doc[0].size() == 1);
580
			YAML_ASSERT(doc[0]["foo"].to<std::string>() == "bar");
581
			YAML_ASSERT(doc[1].size() == 2);
582
583
			YAML_ASSERT(doc[1][0].to<std::string>() == "baz");
			YAML_ASSERT(doc[1][1].to<std::string>() == "baz");
584
585
586
587
588
589
			return true;
		}
		
		// 6.4
		TEST LinePrefixes()
		{
590
			PARSE(doc, ex6_4);
591
			YAML_ASSERT(doc.size() == 3);
592
593
594
			YAML_ASSERT(doc["plain"].to<std::string>() == "text lines");
			YAML_ASSERT(doc["quoted"].to<std::string>() == "text lines");
			YAML_ASSERT(doc["block"].to<std::string>() == "text\n \tlines\n");
595
596
597
598
599
600
			return true;
		}
		
		// 6.5
		TEST EmptyLines()
		{
601
			PARSE(doc, ex6_5);
602
			YAML_ASSERT(doc.size() == 2);
603
604
			YAML_ASSERT(doc["Folding"].to<std::string>() == "Empty line\nas a line feed");
			YAML_ASSERT(doc["Chomping"].to<std::string>() == "Clipped empty lines\n");
605
606
			return true;
		}
607
608
609
610
		
		// 6.6
		TEST LineFolding()
		{
611
			PARSE(doc, ex6_6);
612
			YAML_ASSERT(doc.to<std::string>() == "trimmed\n\n\nas space");
613
614
615
616
617
618
			return true;
		}
		
		// 6.7
		TEST BlockFolding()
		{
619
			PARSE(doc, ex6_7);
620
			YAML_ASSERT(doc.to<std::string>() == "foo \n\n\t bar\n\nbaz\n");
621
622
623
624
625
626
			return true;
		}
		
		// 6.8
		TEST FlowFolding()
		{
627
			PARSE(doc, ex6_8);			
628
			YAML_ASSERT(doc.to<std::string>() == " foo\nbar\nbaz ");
629
630
			return true;
		}
631
632
633
634
		
		// 6.9
		TEST SeparatedComment()
		{
635
			PARSE(doc, ex6_9);
636
			YAML_ASSERT(doc.size() == 1);
637
			YAML_ASSERT(doc["key"].to<std::string>() == "value");
638
639
640
641
642
643
			return true;
		}
		
		// 6.10
		TEST CommentLines()
		{
644
			PARSE(doc, ex6_10);
Jesse Beder's avatar
Jesse Beder committed
645
			YAML_ASSERT(doc.size() == 0);
646
647
648
649
650
651
			return true;
		}
		
		// 6.11
		TEST MultiLineComments()
		{
652
			PARSE(doc, ex6_11);
653
			YAML_ASSERT(doc.size() == 1);
654
			YAML_ASSERT(doc["key"].to<std::string>() == "value");
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
			return true;
		}

		struct StringMap {
			typedef std::map<std::string, std::string> Map;
			Map _;
		};
		
		bool operator == (const StringMap& m, const StringMap& n) {
			return m._ == n._;
		}
		
		void operator >> (const YAML::Node& node, StringMap& m) {
			m._.clear();
			for(YAML::Iterator it=node.begin();it!=node.end();++it) {
670
671
				std::string key = it.first().to<std::string>();
				std::string value = it.second().to<std::string>();
672
673
674
675
676
677
678
679
				m._[key] = value;
			}
		}

		
		// 6.12
		TEST SeparationSpacesII()
		{
680
			PARSE(doc, ex6_12);
681
682
683
			std::map<std::string, std::string> key;
			key["first"] = "Sammy";
			key["last"] = "Sosa";
684
685
			YAML_ASSERT(doc.size() == 1);
			YAML_ASSERT(doc[key].size() == 2);
686
687
			YAML_ASSERT(doc[key]["hr"].to<int>() == 65);
			YAML_ASSERT(doc[key]["avg"].to<std::string>() == "0.278");
688
689
690
			return true;
		}
		
691
692
693
		// 6.13
		TEST ReservedDirectives()
		{
694
			PARSE(doc, ex6_13);
695
696
697
698
699
700
			return true;
		}
		
		// 6.14
		TEST YAMLDirective()
		{
701
			PARSE(doc, ex6_14);
702
703
704
705
706
707
708
			return true;
		}
		
		// 6.15
		TEST InvalidRepeatedYAMLDirective()
		{
			try {
709
				PARSE(doc, ex6_15);
710
711
712
713
714
715
716
717
718
719
720
721
722
			} catch(const YAML::ParserException& e) {
				if(e.msg == YAML::ErrorMsg::REPEATED_YAML_DIRECTIVE)
					return true;

				throw;
			}
			
			return "  No exception was thrown";
		}
		
		// 6.16
		TEST TagDirective()
		{
723
			PARSE(doc, ex6_16);
724
			YAML_ASSERT(doc.Tag() == "tag:yaml.org,2002:str");
725
			YAML_ASSERT(doc.to<std::string>() == "foo");
726
727
728
729
730
731
732
			return true;
		}
		
		// 6.17
		TEST InvalidRepeatedTagDirective()
		{
			try {
733
				PARSE(doc, ex6_17);
734
735
736
737
738
739
740
741
742
743
744
745
746
			} catch(const YAML::ParserException& e) {
				if(e.msg == YAML::ErrorMsg::REPEATED_TAG_DIRECTIVE)
					return true;
				
				throw;
			}
	
			return "  No exception was thrown";
		}

		// 6.18
		TEST PrimaryTagHandle()
		{
747
			PARSE(doc, ex6_18);
748
			YAML_ASSERT(doc.Tag() == "!foo");
749
			YAML_ASSERT(doc.to<std::string>() == "bar");
750
751

			PARSE_NEXT(doc);
752
			YAML_ASSERT(doc.Tag() == "tag:example.com,2000:app/foo");
753
			YAML_ASSERT(doc.to<std::string>() == "bar");
754
755
756
757
758
759
			return true;
		}
		
		// 6.19
		TEST SecondaryTagHandle()
		{
760
			PARSE(doc, ex6_19);
761
			YAML_ASSERT(doc.Tag() == "tag:example.com,2000:app/int");
762
			YAML_ASSERT(doc.to<std::string>() == "1 - 3");
763
764
765
766
767
768
			return true;
		}
		
		// 6.20
		TEST TagHandles()
		{
769
			PARSE(doc, ex6_20);
770
			YAML_ASSERT(doc.Tag() == "tag:example.com,2000:app/foo");
771
			YAML_ASSERT(doc.to<std::string>() == "bar");
772
773
774
775
776
777
			return true;
		}
		
		// 6.21
		TEST LocalTagPrefix()
		{
778
			PARSE(doc, ex6_21);
779
			YAML_ASSERT(doc.Tag() == "!my-light");
780
			YAML_ASSERT(doc.to<std::string>() == "fluorescent");
781
782
			
			PARSE_NEXT(doc);
783
			YAML_ASSERT(doc.Tag() == "!my-light");
784
			YAML_ASSERT(doc.to<std::string>() == "green");
785
786
787
788
789
790
			return true;
		}
		
		// 6.22
		TEST GlobalTagPrefix()
		{
791
			PARSE(doc, ex6_22);
792
			YAML_ASSERT(doc.size() == 1);
793
			YAML_ASSERT(doc[0].Tag() == "tag:example.com,2000:app/foo");
794
			YAML_ASSERT(doc[0].to<std::string>() == "bar");
795
796
797
798
799
800
			return true;
		}
		
		// 6.23
		TEST NodeProperties()
		{
801
			PARSE(doc, ex6_23);
802
803
			YAML_ASSERT(doc.size() == 2);
			for(YAML::Iterator it=doc.begin();it!=doc.end();++it) {
804
				if(it.first().to<std::string>() == "foo") {
805
806
					YAML_ASSERT(it.first().Tag() == "tag:yaml.org,2002:str");
					YAML_ASSERT(it.second().Tag() == "tag:yaml.org,2002:str");
807
808
809
					YAML_ASSERT(it.second().to<std::string>() == "bar");
				} else if(it.first().to<std::string>() == "baz") {
					YAML_ASSERT(it.second().to<std::string>() == "foo");
810
811
812
813
814
815
816
817
818
819
				} else
					return "  unknown key";
			}
			
			return true;
		}
		
		// 6.24
		TEST VerbatimTags()
		{
820
			PARSE(doc, ex6_24);
821
822
			YAML_ASSERT(doc.size() == 1);
			for(YAML::Iterator it=doc.begin();it!=doc.end();++it) {
823
				YAML_ASSERT(it.first().Tag() == "tag:yaml.org,2002:str");
824
				YAML_ASSERT(it.first().to<std::string>() == "foo");
825
				YAML_ASSERT(it.second().Tag() == "!bar");
826
				YAML_ASSERT(it.second().to<std::string>() == "baz");
827
828
829
830
831
832
833
			}
			return true;
		}
		
		// 6.25
		TEST InvalidVerbatimTags()
		{
834
			PARSE(doc, ex6_25);
835
836
837
838
839
840
			return "  not implemented yet"; // TODO: check tags (but we probably will say these are valid, I think)
		}
		
		// 6.26
		TEST TagShorthands()
		{
841
			PARSE(doc, ex6_26);
842
			YAML_ASSERT(doc.size() == 3);
843
			YAML_ASSERT(doc[0].Tag() == "!local");
844
			YAML_ASSERT(doc[0].to<std::string>() == "foo");
845
			YAML_ASSERT(doc[1].Tag() == "tag:yaml.org,2002:str");
846
			YAML_ASSERT(doc[1].to<std::string>() == "bar");
847
			YAML_ASSERT(doc[2].Tag() == "tag:example.com,2000:app/tag%21");
848
			YAML_ASSERT(doc[2].to<std::string>() == "baz");
849
850
851
852
853
854
855
856
			return true;
		}
		
		// 6.27
		TEST InvalidTagShorthands()
		{
			bool threw = false;
			try {
857
				PARSE(doc, ex6_27a);
858
859
860
861
862
863
864
865
866
			} catch(const YAML::ParserException& e) {
				threw = true;
				if(e.msg != YAML::ErrorMsg::TAG_WITH_NO_SUFFIX)
					throw;
			}
			
			if(!threw)
				return "  No exception was thrown for a tag with no suffix";

867
			PARSE(doc, ex6_27b); // TODO: should we reject this one (since !h! is not declared)?
868
869
870
871
872
873
			return "  not implemented yet";
		}
		
		// 6.28
		TEST NonSpecificTags()
		{
874
			PARSE(doc, ex6_28);
875
			YAML_ASSERT(doc.size() == 3);
876
877
878
			YAML_ASSERT(doc[0].to<std::string>() == "12"); // TODO: check tags. How?
			YAML_ASSERT(doc[1].to<int>() == 12);
			YAML_ASSERT(doc[2].to<std::string>() == "12");
879
880
			return true;
		}
881
882
883
884

		// 6.29
		TEST NodeAnchors()
		{
885
			PARSE(doc, ex6_29);
886
			YAML_ASSERT(doc.size() == 2);
887
888
			YAML_ASSERT(doc["First occurrence"].to<std::string>() == "Value");
			YAML_ASSERT(doc["Second occurrence"].to<std::string>() == "Value");
889
890
			return true;
		}
891
892
893
894
		
		// 7.1
		TEST AliasNodes()
		{
895
			PARSE(doc, ex7_1);
896
			YAML_ASSERT(doc.size() == 4);
897
898
899
900
			YAML_ASSERT(doc["First occurrence"].to<std::string>() == "Foo");
			YAML_ASSERT(doc["Second occurrence"].to<std::string>() == "Foo");
			YAML_ASSERT(doc["Override anchor"].to<std::string>() == "Bar");
			YAML_ASSERT(doc["Reuse anchor"].to<std::string>() == "Bar");
901
902
903
904
905
906
			return true;
		}
		
		// 7.2
		TEST EmptyNodes()
		{
907
			PARSE(doc, ex7_2);
908
			YAML_ASSERT(doc.size() == 2);
909
			for(YAML::Iterator it=doc.begin();it!=doc.end();++it) {
910
				if(it.first().to<std::string>() == "foo") {
911
					YAML_ASSERT(it.second().Tag() == "tag:yaml.org,2002:str");
912
913
					YAML_ASSERT(it.second().to<std::string>() == "");
				} else if(it.first().to<std::string>() == "") {
914
					YAML_ASSERT(it.first().Tag() == "tag:yaml.org,2002:str");
915
					YAML_ASSERT(it.second().to<std::string>() == "bar");
916
917
918
				} else
					return "  unexpected key";
			}
919
920
921
922
923
924
			return true;
		}
		
		// 7.3
		TEST CompletelyEmptyNodes()
		{
925
			PARSE(doc, ex7_3);
926
927
			YAML_ASSERT(doc.size() == 2);
			YAML_ASSERT(IsNull(doc["foo"]));
928
			YAML_ASSERT(doc[YAML::Null].to<std::string>() == "bar");
929
930
931
932
933
934
			return true;
		}
		
		// 7.4
		TEST DoubleQuotedImplicitKeys()
		{
935
			PARSE(doc, ex7_4);
936
937
938
			YAML_ASSERT(doc.size() == 1);
			YAML_ASSERT(doc["implicit block key"].size() == 1);
			YAML_ASSERT(doc["implicit block key"][0].size() == 1);
939
			YAML_ASSERT(doc["implicit block key"][0]["implicit flow key"].to<std::string>() == "value");
940
941
942
943
944
945
			return true;
		}
		
		// 7.5
		TEST DoubleQuotedLineBreaks()
		{
946
			PARSE(doc, ex7_5);
947
			YAML_ASSERT(doc.to<std::string>() == "folded to a space,\nto a line feed, or \t \tnon-content");
948
949
950
951
952
953
			return true;
		}
		
		// 7.6
		TEST DoubleQuotedLines()
		{
954
			PARSE(doc, ex7_6);
955
			YAML_ASSERT(doc.to<std::string>() == " 1st non-empty\n2nd non-empty 3rd non-empty ");
956
957
958
959
960
961
			return true;
		}
		
		// 7.7
		TEST SingleQuotedCharacters()
		{
962
			PARSE(doc, ex7_7);
963
			YAML_ASSERT(doc.to<std::string>() == "here's to \"quotes\"");
964
965
966
967
968
969
			return true;
		}
		
		// 7.8
		TEST SingleQuotedImplicitKeys()
		{
970
			PARSE(doc, ex7_8);
971
972
973
			YAML_ASSERT(doc.size() == 1);
			YAML_ASSERT(doc["implicit block key"].size() == 1);
			YAML_ASSERT(doc["implicit block key"][0].size() == 1);
974
			YAML_ASSERT(doc["implicit block key"][0]["implicit flow key"].to<std::string>() == "value");
975
976
977
978
979
980
			return true;
		}
		
		// 7.9
		TEST SingleQuotedLines()
		{
981
			PARSE(doc, ex7_9);
982
			YAML_ASSERT(doc.to<std::string>() == " 1st non-empty\n2nd non-empty 3rd non-empty ");
983
984
985
986
987
988
			return true;
		}
		
		// 7.10
		TEST PlainCharacters()
		{
989
			PARSE(doc, ex7_10);
990
			YAML_ASSERT(doc.size() == 6);
991
992
993
994
995
			YAML_ASSERT(doc[0].to<std::string>() == "::vector");
			YAML_ASSERT(doc[1].to<std::string>() == ": - ()");
			YAML_ASSERT(doc[2].to<std::string>() == "Up, up, and away!");
			YAML_ASSERT(doc[3].to<int>() == -123);
			YAML_ASSERT(doc[4].to<std::string>() == "http://example.com/foo#bar");
996
			YAML_ASSERT(doc[5].size() == 5);
997
998
999
1000
1001
			YAML_ASSERT(doc[5][0].to<std::string>() == "::vector");
			YAML_ASSERT(doc[5][1].to<std::string>() == ": - ()");
			YAML_ASSERT(doc[5][2].to<std::string>() == "Up, up, and away!");
			YAML_ASSERT(doc[5][3].to<int>() == -123);
			YAML_ASSERT(doc[5][4].to<std::string>() == "http://example.com/foo#bar");
1002
1003
1004
1005
1006
1007
			return true;
		}
		
		// 7.11
		TEST PlainImplicitKeys()
		{
1008
			PARSE(doc, ex7_11);
1009
1010
1011
			YAML_ASSERT(doc.size() == 1);
			YAML_ASSERT(doc["implicit block key"].size() == 1);
			YAML_ASSERT(doc["implicit block key"][0].size() == 1);
1012
			YAML_ASSERT(doc["implicit block key"][0]["implicit flow key"].to<std::string>() == "value");
1013
1014
			return true;
		}
1015
1016
1017
1018
		
		// 7.12
		TEST PlainLines()
		{
1019
			PARSE(doc, ex7_12);
1020
			YAML_ASSERT(doc.to<std::string>() == "1st non-empty\n2nd non-empty 3rd non-empty");
1021
1022
			return true;
		}
Jesse Beder's avatar
Jesse Beder committed
1023
1024
1025
1026
		
		// 7.13
		TEST FlowSequence()
		{
1027
			PARSE(doc, ex7_13);
Jesse Beder's avatar
Jesse Beder committed
1028
1029
			YAML_ASSERT(doc.size() == 2);
			YAML_ASSERT(doc[0].size() == 2);
1030
1031
			YAML_ASSERT(doc[0][0].to<std::string>() == "one");
			YAML_ASSERT(doc[0][1].to<std::string>() == "two");
Jesse Beder's avatar
Jesse Beder committed
1032
			YAML_ASSERT(doc[1].size() == 2);
1033
1034
			YAML_ASSERT(doc[1][0].to<std::string>() == "three");
			YAML_ASSERT(doc[1][1].to<std::string>() == "four");
Jesse Beder's avatar
Jesse Beder committed
1035
1036
1037
1038
1039
1040
			return true;
		}
		
		// 7.14
		TEST FlowSequenceEntries()
		{
1041
			PARSE(doc, ex7_14);
Jesse Beder's avatar
Jesse Beder committed
1042
			YAML_ASSERT(doc.size() == 5);
1043
1044
1045
			YAML_ASSERT(doc[0].to<std::string>() == "double quoted");
			YAML_ASSERT(doc[1].to<std::string>() == "single quoted");
			YAML_ASSERT(doc[2].to<std::string>() == "plain text");
Jesse Beder's avatar
Jesse Beder committed
1046
			YAML_ASSERT(doc[3].size() == 1);
1047
			YAML_ASSERT(doc[3][0].to<std::string>() == "nested");
Jesse Beder's avatar
Jesse Beder committed
1048
			YAML_ASSERT(doc[4].size() == 1);
1049
			YAML_ASSERT(doc[4]["single"].to<std::string>() == "pair");
Jesse Beder's avatar
Jesse Beder committed
1050
1051
1052
1053
1054
1055
			return true;
		}
		
		// 7.15
		TEST FlowMappings()
		{
1056
			PARSE(doc, ex7_15);
Jesse Beder's avatar
Jesse Beder committed
1057
1058
			YAML_ASSERT(doc.size() == 2);
			YAML_ASSERT(doc[0].size() == 2);
1059
1060
			YAML_ASSERT(doc[0]["one"].to<std::string>() == "two");
			YAML_ASSERT(doc[0]["three"].to<std::string>() == "four");
Jesse Beder's avatar
Jesse Beder committed
1061
			YAML_ASSERT(doc[1].size() == 2);
1062
1063
			YAML_ASSERT(doc[1]["five"].to<std::string>() == "six");
			YAML_ASSERT(doc[1]["seven"].to<std::string>() == "eight");
Jesse Beder's avatar
Jesse Beder committed
1064
1065
1066
1067
1068
1069
			return true;
		}
		
		// 7.16
		TEST FlowMappingEntries()
		{
1070
			PARSE(doc, ex7_16);
Jesse Beder's avatar
Jesse Beder committed
1071
			YAML_ASSERT(doc.size() == 3);
1072
1073
			YAML_ASSERT(doc["explicit"].to<std::string>() == "entry");
			YAML_ASSERT(doc["implicit"].to<std::string>() == "entry");
Jesse Beder's avatar
Jesse Beder committed
1074
1075
1076
1077
1078
1079
1080
			YAML_ASSERT(IsNull(doc[YAML::Null]));
			return true;
		}
		
		// 7.17
		TEST FlowMappingSeparateValues()
		{
1081
			PARSE(doc, ex7_17);
Jesse Beder's avatar
Jesse Beder committed
1082
			YAML_ASSERT(doc.size() == 4);
1083
			YAML_ASSERT(doc["unquoted"].to<std::string>() == "separate");
Jesse Beder's avatar
Jesse Beder committed
1084
1085
			YAML_ASSERT(IsNull(doc["http://foo.com"]));
			YAML_ASSERT(IsNull(doc["omitted value"]));
1086
			YAML_ASSERT(doc[YAML::Null].to<std::string>() == "omitted key");
Jesse Beder's avatar
Jesse Beder committed
1087
1088
1089
1090
1091
1092
			return true;
		}
		
		// 7.18
		TEST FlowMappingAdjacentValues()
		{
1093
			PARSE(doc, ex7_18);
Jesse Beder's avatar
Jesse Beder committed
1094
			YAML_ASSERT(doc.size() == 3);
1095
1096
			YAML_ASSERT(doc["adjacent"].to<std::string>() == "value");
			YAML_ASSERT(doc["readable"].to<std::string>() == "value");
Jesse Beder's avatar
Jesse Beder committed
1097
1098
1099
1100
1101
1102
1103
			YAML_ASSERT(IsNull(doc["empty"]));
			return true;
		}
		
		// 7.19
		TEST SinglePairFlowMappings()
		{
1104
			PARSE(doc, ex7_19);
Jesse Beder's avatar
Jesse Beder committed
1105
1106
			YAML_ASSERT(doc.size() == 1);
			YAML_ASSERT(doc[0].size() == 1);
1107
			YAML_ASSERT(doc[0]["foo"].to<std::string>() == "bar");
Jesse Beder's avatar
Jesse Beder committed
1108
1109
1110
1111
1112
1113
			return true;
		}
		
		// 7.20
		TEST SinglePairExplicitEntry()
		{
1114
			PARSE(doc, ex7_20);
Jesse Beder's avatar
Jesse Beder committed
1115
1116
			YAML_ASSERT(doc.size() == 1);
			YAML_ASSERT(doc[0].size() == 1);
1117
			YAML_ASSERT(doc[0]["foo bar"].to<std::string>() == "baz");
Jesse Beder's avatar
Jesse Beder committed
1118
1119
1120
1121
1122
1123
			return true;
		}
		
		// 7.21
		TEST SinglePairImplicitEntries()
		{
1124
			PARSE(doc, ex7_21);
Jesse Beder's avatar
Jesse Beder committed
1125
1126
1127
			YAML_ASSERT(doc.size() == 3);
			YAML_ASSERT(doc[0].size() == 1);
			YAML_ASSERT(doc[0][0].size() == 1);
1128
			YAML_ASSERT(doc[0][0]["YAML"].to<std::string>() == "separate");
Jesse Beder's avatar
Jesse Beder committed
1129
1130
			YAML_ASSERT(doc[1].size() == 1);
			YAML_ASSERT(doc[1][0].size() == 1);
1131
			YAML_ASSERT(doc[1][0][YAML::Null].to<std::string>() == "empty key entry");
Jesse Beder's avatar
Jesse Beder committed
1132
1133
1134
1135
			YAML_ASSERT(doc[2].size() == 1);
			YAML_ASSERT(doc[2][0].size() == 1);
			StringMap key;
			key._["JSON"] = "like";
1136
			YAML_ASSERT(doc[2][0][key].to<std::string>() == "adjacent");
Jesse Beder's avatar
Jesse Beder committed
1137
1138
1139
1140
1141
1142
1143
			return true;
		}
		
		// 7.22
		TEST InvalidImplicitKeys()
		{
			try {
1144
				PARSE(doc, ex7_22);
Jesse Beder's avatar
Jesse Beder committed
1145
			} catch(const YAML::Exception& e) {
Jesse Beder's avatar
Jesse Beder committed
1146
				if(e.msg == YAML::ErrorMsg::END_OF_SEQ_FLOW)
Jesse Beder's avatar
Jesse Beder committed
1147
1148
1149
1150
1151
1152
					return true;
				
				throw;
			}
			return "  no exception thrown";
		}
1153
1154
1155
1156
		
		// 7.23
		TEST FlowContent()
		{
1157
			PARSE(doc, ex7_23);
1158
1159
			YAML_ASSERT(doc.size() == 5);
			YAML_ASSERT(doc[0].size() == 2);
1160
1161
			YAML_ASSERT(doc[0][0].to<std::string>() == "a");
			YAML_ASSERT(doc[0][1].to<std::string>() == "b");
1162
			YAML_ASSERT(doc[1].size() == 1);
1163
1164
1165
1166
			YAML_ASSERT(doc[1]["a"].to<std::string>() == "b");
			YAML_ASSERT(doc[2].to<std::string>() == "a");
			YAML_ASSERT(doc[3].to<char>() == 'b');
			YAML_ASSERT(doc[4].to<std::string>() == "c");
1167
1168
1169
1170
1171
1172
			return true;
		}
		
		// 7.24
		TEST FlowNodes()
		{
1173
			PARSE(doc, ex7_24);
1174
			YAML_ASSERT(doc.size() == 5);
1175
			YAML_ASSERT(doc[0].Tag() == "tag:yaml.org,2002:str");
1176
1177
1178
1179
			YAML_ASSERT(doc[0].to<std::string>() == "a");
			YAML_ASSERT(doc[1].to<char>() == 'b');
			YAML_ASSERT(doc[2].to<std::string>() == "c");
			YAML_ASSERT(doc[3].to<std::string>() == "c");
1180
			YAML_ASSERT(doc[4].Tag() == "tag:yaml.org,2002:str");
1181
			YAML_ASSERT(doc[4].to<std::string>() == "");
1182
1183
1184
1185
1186
1187
			return true;
		}
		
		// 8.1
		TEST BlockScalarHeader()
		{
1188
			PARSE(doc, ex8_1);
1189
			YAML_ASSERT(doc.size() == 4);
1190
1191
1192
1193
			YAML_ASSERT(doc[0].to<std::string>() == "literal\n");
			YAML_ASSERT(doc[1].to<std::string>() == " folded\n");
			YAML_ASSERT(doc[2].to<std::string>() == "keep\n\n");
			YAML_ASSERT(doc[3].to<std::string>() == " strip");
1194
1195
1196
1197
1198
1199
			return true;
		}
		
		// 8.2
		TEST BlockIndentationHeader()
		{
1200
			PARSE(doc, ex8_2);
1201
			YAML_ASSERT(doc.size() == 4);
1202
1203
1204
1205
			YAML_ASSERT(doc[0].to<std::string>() == "detected\n");
			YAML_ASSERT(doc[1].to<std::string>() == "\n\n# detected\n");
			YAML_ASSERT(doc[2].to<std::string>() == " explicit\n");
			YAML_ASSERT(doc[3].to<std::string>() == "\t\ndetected\n");
1206
1207
1208
1209
1210
1211
1212
1213
1214
			return true;
		}
		
		// 8.3
		TEST InvalidBlockScalarIndentationIndicators()
		{
			{
				bool threw = false;
				try {
1215
					PARSE(doc, ex8_3a);
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
				} catch(const YAML::Exception& e) {
					if(e.msg != YAML::ErrorMsg::END_OF_SEQ)
						throw;
					
					threw = true;
				}
				
				if(!threw)
					return "  no exception thrown for less indented auto-detecting indentation for a literal block scalar";
			}
			
			{
				bool threw = false;
				try {
1230
					PARSE(doc, ex8_3b);
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
				} catch(const YAML::Exception& e) {
					if(e.msg != YAML::ErrorMsg::END_OF_SEQ)
						throw;
					
					threw = true;
				}
				
				if(!threw)
					return "  no exception thrown for less indented auto-detecting indentation for a folded block scalar";
			}
			
			{
				bool threw = false;
				try {
1245
					PARSE(doc, ex8_3c);
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
				} catch(const YAML::Exception& e) {
					if(e.msg != YAML::ErrorMsg::END_OF_SEQ)
						throw;
					
					threw = true;
				}
				
				if(!threw)
					return "  no exception thrown for less indented explicit indentation for a literal block scalar";
			}
			
			return true;
		}
		
		// 8.4
		TEST ChompingFinalLineBreak()
		{
1263
			PARSE(doc, ex8_4);
1264
			YAML_ASSERT(doc.size() == 3);
1265
1266
1267
			YAML_ASSERT(doc["strip"].to<std::string>() == "text");
			YAML_ASSERT(doc["clip"].to<std::string>() == "text\n");
			YAML_ASSERT(doc["keep"].to<std::string>() == "text\n");
1268
1269
1270
1271
1272
1273
			return true;
		}
		
		// 8.5
		TEST ChompingTrailingLines()
		{
1274
			PARSE(doc, ex8_5);
1275
			YAML_ASSERT(doc.size() == 3);
1276
1277
			YAML_ASSERT(doc["strip"].to<std::string>() == "# text");
			YAML_ASSERT(doc["clip"].to<std::string>() == "# text\n");
1278
			YAML_ASSERT(doc["keep"].to<std::string>() == "# text\n"); // Note: I believe this is a bug in the YAML spec - it should be "# text\n\n"
1279
1280
1281
1282
1283
1284
			return true;
		}
		
		// 8.6
		TEST EmptyScalarChomping()
		{
1285
			PARSE(doc, ex8_6);
1286
			YAML_ASSERT(doc.size() == 3);
1287
1288
1289
			YAML_ASSERT(doc["strip"].to<std::string>() == "");
			YAML_ASSERT(doc["clip"].to<std::string>() == "");
			YAML_ASSERT(doc["keep"].to<std::string>() == "\n");
1290
1291
			return true;
		}
1292
1293
1294
1295
		
		// 8.7
		TEST LiteralScalar()
		{
1296
			PARSE(doc, ex8_7);
1297
			YAML_ASSERT(doc.to<std::string>() == "literal\n\ttext\n");
1298
1299
1300
1301
1302
1303
			return true;
		}
		
		// 8.8
		TEST LiteralContent()
		{
1304
			PARSE(doc, ex8_8);
1305
			YAML_ASSERT(doc.to<std::string>() == "\n\nliteral\n \n\ntext\n");
1306
1307
1308
1309
1310
1311
			return true;
		}
		
		// 8.9
		TEST FoldedScalar()
		{
1312
			PARSE(doc, ex8_9);
1313
			YAML_ASSERT(doc.to<std::string>() == "folded text\n");
1314
1315
1316
1317
1318
1319
			return true;
		}
		
		// 8.10
		TEST FoldedLines()
		{
1320
			PARSE(doc, ex8_10);
1321
			YAML_ASSERT(doc.to<std::string>() == "\nfolded line\nnext line\n  * bullet\n\n  * list\n  * lines\n\nlast line\n");
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
			return true;
		}
		
		// 8.11
		TEST MoreIndentedLines()
		{
			return true; // same as 8.10
		}
		
		// 8.12
		TEST EmptySeparationLines()
		{
			return true; // same as 8.10
		}
		
		// 8.13
		TEST FinalEmptyLines()
		{
			return true; // same as 8.10
		}
		
		// 8.14
		TEST BlockSequence()
		{
1346
			PARSE(doc, ex8_14);
1347
1348
			YAML_ASSERT(doc.size() == 1);
			YAML_ASSERT(doc["block sequence"].size() == 2);
1349
			YAML_ASSERT(doc["block sequence"][0].to<std::string>() == "one");
1350
			YAML_ASSERT(doc["block sequence"][1].size() == 1);
1351
			YAML_ASSERT(doc["block sequence"][1]["two"].to<std::string>() == "three");
1352
1353
1354
1355
1356
1357
			return true;
		}
		
		// 8.15
		TEST BlockSequenceEntryTypes()
		{
1358
			PARSE(doc, ex8_15);
1359
1360
			YAML_ASSERT(doc.size() == 4);
			YAML_ASSERT(YAML::IsNull(doc[0]));
1361
			YAML_ASSERT(doc[1].to<std::string>() == "block node\n");
1362
			YAML_ASSERT(doc[2].size() == 2);
1363
1364
			YAML_ASSERT(doc[2][0].to<std::string>() == "one");
			YAML_ASSERT(doc[2][1].to<std::string>() == "two");
1365
			YAML_ASSERT(doc[3].size() == 1);
1366
			YAML_ASSERT(doc[3]["one"].to<std::string>() == "two");
1367
1368
1369
1370
1371
1372
			return true;
		}
		
		// 8.16
		TEST BlockMappings()
		{
1373
			PARSE(doc, ex8_16);
1374
1375
			YAML_ASSERT(doc.size() == 1);
			YAML_ASSERT(doc["block mapping"].size() == 1);
1376
			YAML_ASSERT(doc["block mapping"]["key"].to<std::string>() == "value");
1377
1378
1379
1380
1381
1382
			return true;
		}
		
		// 8.17
		TEST ExplicitBlockMappingEntries()
		{
1383
			PARSE(doc, ex8_17);
1384
1385
1386
			YAML_ASSERT(doc.size() == 2);
			YAML_ASSERT(IsNull(doc["explicit key"]));
			YAML_ASSERT(doc["block key\n"].size() == 2);
1387
1388
			YAML_ASSERT(doc["block key\n"][0].to<std::string>() == "one");
			YAML_ASSERT(doc["block key\n"][1].to<std::string>() == "two");
1389
1390
1391
1392
1393
1394
			return true;
		}
		
		// 8.18
		TEST ImplicitBlockMappingEntries()
		{
1395
			PARSE(doc, ex8_18);
1396
			YAML_ASSERT(doc.size() == 3);
1397
			YAML_ASSERT(doc["plain key"].to<std::string>() == "in-line value");
1398
1399
			YAML_ASSERT(IsNull(doc[YAML::Null]));
			YAML_ASSERT(doc["quoted key"].size() == 1);
1400
			YAML_ASSERT(doc["quoted key"][0].to<std::string>() == "entry");
1401
1402
1403
1404
1405
1406
			return true;
		}
		
		// 8.19
		TEST CompactBlockMappings()
		{
1407
			PARSE(doc, ex8_19);
1408
1409
			YAML_ASSERT(doc.size() == 2);
			YAML_ASSERT(doc[0].size() == 1);
1410
			YAML_ASSERT(doc[0]["sun"].to<std::string>() == "yellow");
1411
1412
1413
1414
			YAML_ASSERT(doc[1].size() == 1);
			std::map<std::string, std::string> key;
			key["earth"] = "blue";
			YAML_ASSERT(doc[1][key].size() == 1);
1415
			YAML_ASSERT(doc[1][key]["moon"].to<std::string>() == "white");
1416
1417
1418
1419
1420
1421
			return true;
		}
		
		// 8.20
		TEST BlockNodeTypes()
		{
1422
			PARSE(doc, ex8_20);
1423
			YAML_ASSERT(doc.size() == 3);
1424
1425
			YAML_ASSERT(doc[0].to<std::string>() == "flow in block");
			YAML_ASSERT(doc[1].to<std::string>() == "Block scalar\n");
1426
			YAML_ASSERT(doc[2].size() == 1);
1427
			YAML_ASSERT(doc[2]["foo"].to<std::string>() == "bar");
1428
1429
1430
1431
1432
1433
			return true;
		}
		
		// 8.21
		TEST BlockScalarNodes()
		{
1434
			PARSE(doc, ex8_21);
1435
			YAML_ASSERT(doc.size() == 2);
1436
			YAML_ASSERT(doc["literal"].to<std::string>() == "value"); // Note: I believe this is a bug in the YAML spec - it should be "value\n"
1437
			YAML_ASSERT(doc["folded"].to<std::string>() == "value");
1438
1439
1440
1441
1442
1443
1444
			YAML_ASSERT(doc["folded"].Tag() == "!foo");
			return true;
		}
		
		// 8.22
		TEST BlockCollectionNodes()
		{
1445
			PARSE(doc, ex8_22);
1446
1447
			YAML_ASSERT(doc.size() == 2);
			YAML_ASSERT(doc["sequence"].size() == 2);
1448
			YAML_ASSERT(doc["sequence"][0].to<std::string>() == "entry");
1449
			YAML_ASSERT(doc["sequence"][1].size() == 1);
1450
			YAML_ASSERT(doc["sequence"][1][0].to<std::string>() == "nested");
1451
			YAML_ASSERT(doc["mapping"].size() == 1);
1452
			YAML_ASSERT(doc["mapping"]["foo"].to<std::string>() == "bar");
1453
1454
			return true;
		}
Jesse Beder's avatar
Jesse Beder committed
1455
1456
	}
}