remove button colors from quarks gui (cocoa + swing)
[supercollider.git] / external_libraries / yaml-cpp-0.2.6 / test / spectests.cpp
blobf3f05bc3ee0e9174b70d18f683dbfac8b3558e26
1 #include "spectests.h"
2 #include "yaml-cpp/yaml.h"
3 #include <fstream>
4 #include <sstream>
5 #include <vector>
6 #include <iostream>
8 namespace {
9 struct TEST {
10 TEST(): ok(false) {}
11 TEST(bool ok_): ok(ok_) {}
12 TEST(const char *error_): ok(false), error(error_) {}
14 bool ok;
15 std::string error;
19 #define YAML_ASSERT(cond) do { if(!(cond)) return " Assert failed: " #cond; } while(false)
20 #define PARSE(doc, input) \
21 std::stringstream stream(input);\
22 YAML::Parser parser(stream);\
23 YAML::Node doc;\
24 parser.GetNextDocument(doc)
25 #define PARSE_NEXT(doc) parser.GetNextDocument(doc)
27 namespace Test {
28 namespace {
29 void RunSpecTest(TEST (*test)(), const std::string& index, const std::string& name, int& passed, int& total) {
30 TEST ret;
31 try {
32 ret = test();
33 } catch(const YAML::Exception& e) {
34 ret.ok = false;
35 ret.error = std::string(" Exception caught: ") + e.what();
38 if(!ret.ok) {
39 std::cout << "Spec test " << index << " failed: " << name << "\n";
40 std::cout << ret.error << "\n";
43 if(ret.ok)
44 passed++;
45 total++;
49 namespace Spec {
50 // 2.1
51 TEST SeqScalars() {
52 std::string input =
53 "- Mark McGwire\n"
54 "- Sammy Sosa\n"
55 "- Ken Griffey";
57 PARSE(doc, input);
58 YAML_ASSERT(doc.size() == 3);
59 YAML_ASSERT(doc[0].to<std::string>() == "Mark McGwire");
60 YAML_ASSERT(doc[1].to<std::string>() == "Sammy Sosa");
61 YAML_ASSERT(doc[2].to<std::string>() == "Ken Griffey");
62 return true;
65 // 2.2
66 TEST MappingScalarsToScalars() {
67 std::string input =
68 "hr: 65 # Home runs\n"
69 "avg: 0.278 # Batting average\n"
70 "rbi: 147 # Runs Batted In";
72 PARSE(doc, input);
73 YAML_ASSERT(doc.size() == 3);
74 YAML_ASSERT(doc["hr"].to<std::string>() == "65");
75 YAML_ASSERT(doc["avg"].to<std::string>() == "0.278");
76 YAML_ASSERT(doc["rbi"].to<std::string>() == "147");
77 return true;
80 // 2.3
81 TEST MappingScalarsToSequences() {
82 std::string input =
83 "american:\n"
84 "- Boston Red Sox\n"
85 "- Detroit Tigers\n"
86 "- New York Yankees\n"
87 "national:\n"
88 "- New York Mets\n"
89 "- Chicago Cubs\n"
90 "- Atlanta Braves";
92 PARSE(doc, input);
93 YAML_ASSERT(doc.size() == 2);
94 YAML_ASSERT(doc["american"].size() == 3);
95 YAML_ASSERT(doc["american"][0].to<std::string>() == "Boston Red Sox");
96 YAML_ASSERT(doc["american"][1].to<std::string>() == "Detroit Tigers");
97 YAML_ASSERT(doc["american"][2].to<std::string>() == "New York Yankees");
98 YAML_ASSERT(doc["national"].size() == 3);
99 YAML_ASSERT(doc["national"][0].to<std::string>() == "New York Mets");
100 YAML_ASSERT(doc["national"][1].to<std::string>() == "Chicago Cubs");
101 YAML_ASSERT(doc["national"][2].to<std::string>() == "Atlanta Braves");
102 return true;
105 // 2.4
106 TEST SequenceOfMappings()
108 std::string input =
109 "-\n"
110 " name: Mark McGwire\n"
111 " hr: 65\n"
112 " avg: 0.278\n"
113 "-\n"
114 " name: Sammy Sosa\n"
115 " hr: 63\n"
116 " avg: 0.288";
118 PARSE(doc, input);
119 YAML_ASSERT(doc.size() == 2);
120 YAML_ASSERT(doc[0].size() == 3);
121 YAML_ASSERT(doc[0]["name"].to<std::string>() == "Mark McGwire");
122 YAML_ASSERT(doc[0]["hr"].to<std::string>() == "65");
123 YAML_ASSERT(doc[0]["avg"].to<std::string>() == "0.278");
124 YAML_ASSERT(doc[1].size() == 3);
125 YAML_ASSERT(doc[1]["name"].to<std::string>() == "Sammy Sosa");
126 YAML_ASSERT(doc[1]["hr"].to<std::string>() == "63");
127 YAML_ASSERT(doc[1]["avg"].to<std::string>() == "0.288");
128 return true;
131 // 2.5
132 TEST SequenceOfSequences()
134 std::string input =
135 "- [name , hr, avg ]\n"
136 "- [Mark McGwire, 65, 0.278]\n"
137 "- [Sammy Sosa , 63, 0.288]";
139 PARSE(doc, input);
140 YAML_ASSERT(doc.size() == 3);
141 YAML_ASSERT(doc[0].size() == 3);
142 YAML_ASSERT(doc[0][0].to<std::string>() == "name");
143 YAML_ASSERT(doc[0][1].to<std::string>() == "hr");
144 YAML_ASSERT(doc[0][2].to<std::string>() == "avg");
145 YAML_ASSERT(doc[1].size() == 3);
146 YAML_ASSERT(doc[1][0].to<std::string>() == "Mark McGwire");
147 YAML_ASSERT(doc[1][1].to<std::string>() == "65");
148 YAML_ASSERT(doc[1][2].to<std::string>() == "0.278");
149 YAML_ASSERT(doc[2].size() == 3);
150 YAML_ASSERT(doc[2][0].to<std::string>() == "Sammy Sosa");
151 YAML_ASSERT(doc[2][1].to<std::string>() == "63");
152 YAML_ASSERT(doc[2][2].to<std::string>() == "0.288");
153 return true;
156 // 2.6
157 TEST MappingOfMappings()
159 std::string input =
160 "Mark McGwire: {hr: 65, avg: 0.278}\n"
161 "Sammy Sosa: {\n"
162 " hr: 63,\n"
163 " avg: 0.288\n"
164 " }";
166 PARSE(doc, input);
167 YAML_ASSERT(doc.size() == 2);
168 YAML_ASSERT(doc["Mark McGwire"].size() == 2);
169 YAML_ASSERT(doc["Mark McGwire"]["hr"].to<std::string>() == "65");
170 YAML_ASSERT(doc["Mark McGwire"]["avg"].to<std::string>() == "0.278");
171 YAML_ASSERT(doc["Sammy Sosa"].size() == 2);
172 YAML_ASSERT(doc["Sammy Sosa"]["hr"].to<std::string>() == "63");
173 YAML_ASSERT(doc["Sammy Sosa"]["avg"].to<std::string>() == "0.288");
174 return true;
177 // 2.7
178 TEST TwoDocumentsInAStream()
180 std::string input =
181 "# Ranking of 1998 home runs\n"
182 "---\n"
183 "- Mark McGwire\n"
184 "- Sammy Sosa\n"
185 "- Ken Griffey\n"
186 "\n"
187 "# Team ranking\n"
188 "---\n"
189 "- Chicago Cubs\n"
190 "- St Louis Cardinals";
192 PARSE(doc, input);
193 YAML_ASSERT(doc.size() == 3);
194 YAML_ASSERT(doc[0].to<std::string>() == "Mark McGwire");
195 YAML_ASSERT(doc[1].to<std::string>() == "Sammy Sosa");
196 YAML_ASSERT(doc[2].to<std::string>() == "Ken Griffey");
198 PARSE_NEXT(doc);
199 YAML_ASSERT(doc.size() == 2);
200 YAML_ASSERT(doc[0].to<std::string>() == "Chicago Cubs");
201 YAML_ASSERT(doc[1].to<std::string>() == "St Louis Cardinals");
202 return true;
205 // 2.8
206 TEST PlayByPlayFeed()
208 std::string input =
209 "---\n"
210 "time: 20:03:20\n"
211 "player: Sammy Sosa\n"
212 "action: strike (miss)\n"
213 "...\n"
214 "---\n"
215 "time: 20:03:47\n"
216 "player: Sammy Sosa\n"
217 "action: grand slam\n"
218 "...";
220 PARSE(doc, input);
221 YAML_ASSERT(doc.size() == 3);
222 YAML_ASSERT(doc["time"].to<std::string>() == "20:03:20");
223 YAML_ASSERT(doc["player"].to<std::string>() == "Sammy Sosa");
224 YAML_ASSERT(doc["action"].to<std::string>() == "strike (miss)");
226 PARSE_NEXT(doc);
227 YAML_ASSERT(doc.size() == 3);
228 YAML_ASSERT(doc["time"].to<std::string>() == "20:03:47");
229 YAML_ASSERT(doc["player"].to<std::string>() == "Sammy Sosa");
230 YAML_ASSERT(doc["action"].to<std::string>() == "grand slam");
231 return true;
234 // 2.9
235 TEST SingleDocumentWithTwoComments()
237 std::string input =
238 "---\n"
239 "hr: # 1998 hr ranking\n"
240 " - Mark McGwire\n"
241 " - Sammy Sosa\n"
242 "rbi:\n"
243 " # 1998 rbi ranking\n"
244 " - Sammy Sosa\n"
245 " - Ken Griffey";
247 PARSE(doc, input);
248 YAML_ASSERT(doc.size() == 2);
249 YAML_ASSERT(doc["hr"].size() == 2);
250 YAML_ASSERT(doc["hr"][0].to<std::string>() == "Mark McGwire");
251 YAML_ASSERT(doc["hr"][1].to<std::string>() == "Sammy Sosa");
252 YAML_ASSERT(doc["rbi"].size() == 2);
253 YAML_ASSERT(doc["rbi"][0].to<std::string>() == "Sammy Sosa");
254 YAML_ASSERT(doc["rbi"][1].to<std::string>() == "Ken Griffey");
255 return true;
258 // 2.10
259 TEST SimpleAnchor()
261 std::string input =
262 "---\n"
263 "hr:\n"
264 " - Mark McGwire\n"
265 " # Following node labeled SS\n"
266 " - &SS Sammy Sosa\n"
267 "rbi:\n"
268 " - *SS # Subsequent occurrence\n"
269 " - Ken Griffey";
271 PARSE(doc, input);
272 YAML_ASSERT(doc.size() == 2);
273 YAML_ASSERT(doc["hr"].size() == 2);
274 YAML_ASSERT(doc["hr"][0].to<std::string>() == "Mark McGwire");
275 YAML_ASSERT(doc["hr"][1].to<std::string>() == "Sammy Sosa");
276 YAML_ASSERT(doc["rbi"].size() == 2);
277 YAML_ASSERT(doc["rbi"][0].to<std::string>() == "Sammy Sosa");
278 YAML_ASSERT(doc["rbi"][1].to<std::string>() == "Ken Griffey");
279 return true;
282 struct Pair {
283 Pair() {}
284 Pair(const std::string& f, const std::string& s): first(f), second(s) {}
285 std::string first, second;
288 bool operator == (const Pair& p, const Pair& q) {
289 return p.first == q.first && p.second == q.second;
292 void operator >> (const YAML::Node& node, Pair& p) {
293 node[0] >> p.first;
294 node[1] >> p.second;
297 // 2.11
298 TEST MappingBetweenSequences()
300 std::string input =
301 "? - Detroit Tigers\n"
302 " - Chicago cubs\n"
303 ":\n"
304 " - 2001-07-23\n"
305 "\n"
306 "? [ New York Yankees,\n"
307 " Atlanta Braves ]\n"
308 ": [ 2001-07-02, 2001-08-12,\n"
309 " 2001-08-14 ]";
311 PARSE(doc, input);
312 YAML_ASSERT(doc.size() == 2);
313 YAML_ASSERT(doc[Pair("Detroit Tigers", "Chicago cubs")].size() == 1);
314 YAML_ASSERT(doc[Pair("Detroit Tigers", "Chicago cubs")][0].to<std::string>() == "2001-07-23");
315 YAML_ASSERT(doc[Pair("New York Yankees", "Atlanta Braves")].size() == 3);
316 YAML_ASSERT(doc[Pair("New York Yankees", "Atlanta Braves")][0].to<std::string>() == "2001-07-02");
317 YAML_ASSERT(doc[Pair("New York Yankees", "Atlanta Braves")][1].to<std::string>() == "2001-08-12");
318 YAML_ASSERT(doc[Pair("New York Yankees", "Atlanta Braves")][2].to<std::string>() == "2001-08-14");
319 return true;
322 // 2.12
323 TEST CompactNestedMapping()
325 std::string input =
326 "---\n"
327 "# Products purchased\n"
328 "- item : Super Hoop\n"
329 " quantity: 1\n"
330 "- item : Basketball\n"
331 " quantity: 4\n"
332 "- item : Big Shoes\n"
333 " quantity: 1";
335 PARSE(doc, input);
336 YAML_ASSERT(doc.size() == 3);
337 YAML_ASSERT(doc[0].size() == 2);
338 YAML_ASSERT(doc[0]["item"].to<std::string>() == "Super Hoop");
339 YAML_ASSERT(doc[0]["quantity"].to<int>() == 1);
340 YAML_ASSERT(doc[1].size() == 2);
341 YAML_ASSERT(doc[1]["item"].to<std::string>() == "Basketball");
342 YAML_ASSERT(doc[1]["quantity"].to<int>() == 4);
343 YAML_ASSERT(doc[2].size() == 2);
344 YAML_ASSERT(doc[2]["item"].to<std::string>() == "Big Shoes");
345 YAML_ASSERT(doc[2]["quantity"].to<int>() == 1);
346 return true;
349 // 2.13
350 TEST InLiteralsNewlinesArePreserved()
352 std::string input =
353 "# ASCII Art\n"
354 "--- |\n"
355 " \\//||\\/||\n"
356 " // || ||__";
358 PARSE(doc, input);
359 YAML_ASSERT(doc.to<std::string>() ==
360 "\\//||\\/||\n"
361 "// || ||__");
362 return true;
365 // 2.14
366 TEST InFoldedScalarsNewlinesBecomeSpaces()
368 std::string input =
369 "--- >\n"
370 " Mark McGwire's\n"
371 " year was crippled\n"
372 " by a knee injury.";
374 PARSE(doc, input);
375 YAML_ASSERT(doc.to<std::string>() == "Mark McGwire's year was crippled by a knee injury.");
376 return true;
379 // 2.15
380 TEST FoldedNewlinesArePreservedForMoreIndentedAndBlankLines()
382 std::string input =
383 ">\n"
384 " Sammy Sosa completed another\n"
385 " fine season with great stats.\n"
386 " \n"
387 " 63 Home Runs\n"
388 " 0.288 Batting Average\n"
389 " \n"
390 " What a year!";
392 PARSE(doc, input);
393 YAML_ASSERT(doc.to<std::string>() ==
394 "Sammy Sosa completed another fine season with great stats.\n\n"
395 " 63 Home Runs\n"
396 " 0.288 Batting Average\n\n"
397 "What a year!");
398 return true;
401 // 2.16
402 TEST IndentationDeterminesScope()
404 std::string input =
405 "name: Mark McGwire\n"
406 "accomplishment: >\n"
407 " Mark set a major league\n"
408 " home run record in 1998.\n"
409 "stats: |\n"
410 " 65 Home Runs\n"
411 " 0.278 Batting Average\n";
413 PARSE(doc, input);
414 YAML_ASSERT(doc.size() == 3);
415 YAML_ASSERT(doc["name"].to<std::string>() == "Mark McGwire");
416 YAML_ASSERT(doc["accomplishment"].to<std::string>() == "Mark set a major league home run record in 1998.\n");
417 YAML_ASSERT(doc["stats"].to<std::string>() == "65 Home Runs\n0.278 Batting Average\n");
418 return true;
421 // 2.17
422 TEST QuotedScalars()
424 std::string input =
425 "unicode: \"Sosa did fine.\\u263A\"\n"
426 "control: \"\\b1998\\t1999\\t2000\\n\"\n"
427 "hex esc: \"\\x0d\\x0a is \\r\\n\"\n"
428 "\n"
429 "single: '\"Howdy!\" he cried.'\n"
430 "quoted: ' # Not a ''comment''.'\n"
431 "tie-fighter: '|\\-*-/|'";
433 PARSE(doc, input);
434 YAML_ASSERT(doc.size() == 6);
435 YAML_ASSERT(doc["unicode"].to<std::string>() == "Sosa did fine.\xe2\x98\xba");
436 YAML_ASSERT(doc["control"].to<std::string>() == "\b1998\t1999\t2000\n");
437 YAML_ASSERT(doc["hex esc"].to<std::string>() == "\x0d\x0a is \r\n");
438 YAML_ASSERT(doc["single"].to<std::string>() == "\"Howdy!\" he cried.");
439 YAML_ASSERT(doc["quoted"].to<std::string>() == " # Not a 'comment'.");
440 YAML_ASSERT(doc["tie-fighter"].to<std::string>() == "|\\-*-/|");
441 return true;
444 // 2.18
445 TEST MultiLineFlowScalars()
447 std::string input =
448 "plain:\n"
449 " This unquoted scalar\n"
450 " spans many lines.\n"
451 "\n"
452 "quoted: \"So does this\n"
453 " quoted scalar.\\n\"";
455 PARSE(doc, input);
456 YAML_ASSERT(doc.size() == 2);
457 YAML_ASSERT(doc["plain"].to<std::string>() == "This unquoted scalar spans many lines.");
458 YAML_ASSERT(doc["quoted"].to<std::string>() == "So does this quoted scalar.\n");
459 return true;
462 // TODO: 2.19 - 2.22 schema tags
464 // 2.23
465 TEST VariousExplicitTags()
467 std::string input =
468 "---\n"
469 "not-date: !!str 2002-04-28\n"
470 "\n"
471 "picture: !!binary |\n"
472 " R0lGODlhDAAMAIQAAP//9/X\n"
473 " 17unp5WZmZgAAAOfn515eXv\n"
474 " Pz7Y6OjuDg4J+fn5OTk6enp\n"
475 " 56enmleECcgggoBADs=\n"
476 "\n"
477 "application specific tag: !something |\n"
478 " The semantics of the tag\n"
479 " above may be different for\n"
480 " different documents.";
482 PARSE(doc, input);
483 YAML_ASSERT(doc.size() == 3);
484 YAML_ASSERT(doc["not-date"].Tag() == "tag:yaml.org,2002:str");
485 YAML_ASSERT(doc["not-date"].to<std::string>() == "2002-04-28");
486 YAML_ASSERT(doc["picture"].Tag() == "tag:yaml.org,2002:binary");
487 YAML_ASSERT(doc["picture"].to<std::string>() ==
488 "R0lGODlhDAAMAIQAAP//9/X\n"
489 "17unp5WZmZgAAAOfn515eXv\n"
490 "Pz7Y6OjuDg4J+fn5OTk6enp\n"
491 "56enmleECcgggoBADs=\n"
493 YAML_ASSERT(doc["application specific tag"].Tag() == "!something");
494 YAML_ASSERT(doc["application specific tag"].to<std::string>() ==
495 "The semantics of the tag\n"
496 "above may be different for\n"
497 "different documents."
499 return true;
502 // 2.24
503 TEST GlobalTags()
505 std::string input =
506 "%TAG ! tag:clarkevans.com,2002:\n"
507 "--- !shape\n"
508 " # Use the ! handle for presenting\n"
509 " # tag:clarkevans.com,2002:circle\n"
510 "- !circle\n"
511 " center: &ORIGIN {x: 73, y: 129}\n"
512 " radius: 7\n"
513 "- !line\n"
514 " start: *ORIGIN\n"
515 " finish: { x: 89, y: 102 }\n"
516 "- !label\n"
517 " start: *ORIGIN\n"
518 " color: 0xFFEEBB\n"
519 " text: Pretty vector drawing.";
521 PARSE(doc, input);
522 YAML_ASSERT(doc.Tag() == "tag:clarkevans.com,2002:shape");
523 YAML_ASSERT(doc.size() == 3);
524 YAML_ASSERT(doc[0].Tag() == "tag:clarkevans.com,2002:circle");
525 YAML_ASSERT(doc[0].size() == 2);
526 YAML_ASSERT(doc[0]["center"].size() == 2);
527 YAML_ASSERT(doc[0]["center"]["x"].to<int>() == 73);
528 YAML_ASSERT(doc[0]["center"]["y"].to<int>() == 129);
529 YAML_ASSERT(doc[0]["radius"].to<int>() == 7);
530 YAML_ASSERT(doc[1].Tag() == "tag:clarkevans.com,2002:line");
531 YAML_ASSERT(doc[1].size() == 2);
532 YAML_ASSERT(doc[1]["start"].size() == 2);
533 YAML_ASSERT(doc[1]["start"]["x"].to<int>() == 73);
534 YAML_ASSERT(doc[1]["start"]["y"].to<int>() == 129);
535 YAML_ASSERT(doc[1]["finish"].size() == 2);
536 YAML_ASSERT(doc[1]["finish"]["x"].to<int>() == 89);
537 YAML_ASSERT(doc[1]["finish"]["y"].to<int>() == 102);
538 YAML_ASSERT(doc[2].Tag() == "tag:clarkevans.com,2002:label");
539 YAML_ASSERT(doc[2].size() == 3);
540 YAML_ASSERT(doc[2]["start"].size() == 2);
541 YAML_ASSERT(doc[2]["start"]["x"].to<int>() == 73);
542 YAML_ASSERT(doc[2]["start"]["y"].to<int>() == 129);
543 YAML_ASSERT(doc[2]["color"].to<std::string>() == "0xFFEEBB");
544 YAML_ASSERT(doc[2]["text"].to<std::string>() == "Pretty vector drawing.");
545 return true;
548 // 2.25
549 TEST UnorderedSets()
551 std::string input =
552 "# Sets are represented as a\n"
553 "# Mapping where each key is\n"
554 "# associated with a null value\n"
555 "--- !!set\n"
556 "? Mark McGwire\n"
557 "? Sammy Sosa\n"
558 "? Ken Griffey";
560 PARSE(doc, input);
561 YAML_ASSERT(doc.Tag() == "tag:yaml.org,2002:set");
562 YAML_ASSERT(doc.size() == 3);
563 YAML_ASSERT(IsNull(doc["Mark McGwire"]));
564 YAML_ASSERT(IsNull(doc["Sammy Sosa"]));
565 YAML_ASSERT(IsNull(doc["Ken Griffey"]));
566 return true;
569 // 2.26
570 TEST OrderedMappings()
572 std::string input =
573 "# Ordered maps are represented as\n"
574 "# A sequence of mappings, with\n"
575 "# each mapping having one key\n"
576 "--- !!omap\n"
577 "- Mark McGwire: 65\n"
578 "- Sammy Sosa: 63\n"
579 "- Ken Griffey: 58";
581 PARSE(doc, input);
582 YAML_ASSERT(doc.Tag() == "tag:yaml.org,2002:omap");
583 YAML_ASSERT(doc.size() == 3);
584 YAML_ASSERT(doc[0].size() == 1);
585 YAML_ASSERT(doc[0]["Mark McGwire"].to<int>() == 65);
586 YAML_ASSERT(doc[1].size() == 1);
587 YAML_ASSERT(doc[1]["Sammy Sosa"].to<int>() == 63);
588 YAML_ASSERT(doc[2].size() == 1);
589 YAML_ASSERT(doc[2]["Ken Griffey"].to<int>() == 58);
590 return true;
593 // 2.27
594 TEST Invoice()
596 std::string input =
597 "--- !<tag:clarkevans.com,2002:invoice>\n"
598 "invoice: 34843\n"
599 "date : 2001-01-23\n"
600 "bill-to: &id001\n"
601 " given : Chris\n"
602 " family : Dumars\n"
603 " address:\n"
604 " lines: |\n"
605 " 458 Walkman Dr.\n"
606 " Suite #292\n"
607 " city : Royal Oak\n"
608 " state : MI\n"
609 " postal : 48046\n"
610 "ship-to: *id001\n"
611 "product:\n"
612 " - sku : BL394D\n"
613 " quantity : 4\n"
614 " description : Basketball\n"
615 " price : 450.00\n"
616 " - sku : BL4438H\n"
617 " quantity : 1\n"
618 " description : Super Hoop\n"
619 " price : 2392.00\n"
620 "tax : 251.42\n"
621 "total: 4443.52\n"
622 "comments:\n"
623 " Late afternoon is best.\n"
624 " Backup contact is Nancy\n"
625 " Billsmer @ 338-4338.";
627 PARSE(doc, input);
628 YAML_ASSERT(doc.Tag() == "tag:clarkevans.com,2002:invoice");
629 YAML_ASSERT(doc.size() == 8);
630 YAML_ASSERT(doc["invoice"].to<int>() == 34843);
631 YAML_ASSERT(doc["date"].to<std::string>() == "2001-01-23");
632 YAML_ASSERT(doc["bill-to"].size() == 3);
633 YAML_ASSERT(doc["bill-to"]["given"].to<std::string>() == "Chris");
634 YAML_ASSERT(doc["bill-to"]["family"].to<std::string>() == "Dumars");
635 YAML_ASSERT(doc["bill-to"]["address"].size() == 4);
636 YAML_ASSERT(doc["bill-to"]["address"]["lines"].to<std::string>() == "458 Walkman Dr.\nSuite #292\n");
637 YAML_ASSERT(doc["bill-to"]["address"]["city"].to<std::string>() == "Royal Oak");
638 YAML_ASSERT(doc["bill-to"]["address"]["state"].to<std::string>() == "MI");
639 YAML_ASSERT(doc["bill-to"]["address"]["postal"].to<std::string>() == "48046");
640 YAML_ASSERT(doc["ship-to"].size() == 3);
641 YAML_ASSERT(doc["ship-to"]["given"].to<std::string>() == "Chris");
642 YAML_ASSERT(doc["ship-to"]["family"].to<std::string>() == "Dumars");
643 YAML_ASSERT(doc["ship-to"]["address"].size() == 4);
644 YAML_ASSERT(doc["ship-to"]["address"]["lines"].to<std::string>() == "458 Walkman Dr.\nSuite #292\n");
645 YAML_ASSERT(doc["ship-to"]["address"]["city"].to<std::string>() == "Royal Oak");
646 YAML_ASSERT(doc["ship-to"]["address"]["state"].to<std::string>() == "MI");
647 YAML_ASSERT(doc["ship-to"]["address"]["postal"].to<std::string>() == "48046");
648 YAML_ASSERT(doc["product"].size() == 2);
649 YAML_ASSERT(doc["product"][0].size() == 4);
650 YAML_ASSERT(doc["product"][0]["sku"].to<std::string>() == "BL394D");
651 YAML_ASSERT(doc["product"][0]["quantity"].to<int>() == 4);
652 YAML_ASSERT(doc["product"][0]["description"].to<std::string>() == "Basketball");
653 YAML_ASSERT(doc["product"][0]["price"].to<std::string>() == "450.00");
654 YAML_ASSERT(doc["product"][1].size() == 4);
655 YAML_ASSERT(doc["product"][1]["sku"].to<std::string>() == "BL4438H");
656 YAML_ASSERT(doc["product"][1]["quantity"].to<int>() == 1);
657 YAML_ASSERT(doc["product"][1]["description"].to<std::string>() == "Super Hoop");
658 YAML_ASSERT(doc["product"][1]["price"].to<std::string>() == "2392.00");
659 YAML_ASSERT(doc["tax"].to<std::string>() == "251.42");
660 YAML_ASSERT(doc["total"].to<std::string>() == "4443.52");
661 YAML_ASSERT(doc["comments"].to<std::string>() == "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338.");
662 return true;
665 // 2.28
666 TEST LogFile()
668 std::string input =
669 "---\n"
670 "Time: 2001-11-23 15:01:42 -5\n"
671 "User: ed\n"
672 "Warning:\n"
673 " This is an error message\n"
674 " for the log file\n"
675 "---\n"
676 "Time: 2001-11-23 15:02:31 -5\n"
677 "User: ed\n"
678 "Warning:\n"
679 " A slightly different error\n"
680 " message.\n"
681 "---\n"
682 "Date: 2001-11-23 15:03:17 -5\n"
683 "User: ed\n"
684 "Fatal:\n"
685 " Unknown variable \"bar\"\n"
686 "Stack:\n"
687 " - file: TopClass.py\n"
688 " line: 23\n"
689 " code: |\n"
690 " x = MoreObject(\"345\\n\")\n"
691 " - file: MoreClass.py\n"
692 " line: 58\n"
693 " code: |-\n"
694 " foo = bar";
696 PARSE(doc, input);
697 YAML_ASSERT(doc.size() == 3);
698 YAML_ASSERT(doc["Time"].to<std::string>() == "2001-11-23 15:01:42 -5");
699 YAML_ASSERT(doc["User"].to<std::string>() == "ed");
700 YAML_ASSERT(doc["Warning"].to<std::string>() == "This is an error message for the log file");
702 PARSE_NEXT(doc);
703 YAML_ASSERT(doc.size() == 3);
704 YAML_ASSERT(doc["Time"].to<std::string>() == "2001-11-23 15:02:31 -5");
705 YAML_ASSERT(doc["User"].to<std::string>() == "ed");
706 YAML_ASSERT(doc["Warning"].to<std::string>() == "A slightly different error message.");
708 PARSE_NEXT(doc);
709 YAML_ASSERT(doc.size() == 4);
710 YAML_ASSERT(doc["Date"].to<std::string>() == "2001-11-23 15:03:17 -5");
711 YAML_ASSERT(doc["User"].to<std::string>() == "ed");
712 YAML_ASSERT(doc["Fatal"].to<std::string>() == "Unknown variable \"bar\"");
713 YAML_ASSERT(doc["Stack"].size() == 2);
714 YAML_ASSERT(doc["Stack"][0].size() == 3);
715 YAML_ASSERT(doc["Stack"][0]["file"].to<std::string>() == "TopClass.py");
716 YAML_ASSERT(doc["Stack"][0]["line"].to<std::string>() == "23");
717 YAML_ASSERT(doc["Stack"][0]["code"].to<std::string>() == "x = MoreObject(\"345\\n\")\n");
718 YAML_ASSERT(doc["Stack"][1].size() == 3);
719 YAML_ASSERT(doc["Stack"][1]["file"].to<std::string>() == "MoreClass.py");
720 YAML_ASSERT(doc["Stack"][1]["line"].to<std::string>() == "58");
721 YAML_ASSERT(doc["Stack"][1]["code"].to<std::string>() == "foo = bar");
722 return true;
725 // TODO: 5.1 - 5.2 BOM
727 // 5.3
728 TEST BlockStructureIndicators()
730 std::string input =
731 "sequence:\n"
732 "- one\n"
733 "- two\n"
734 "mapping:\n"
735 " ? sky\n"
736 " : blue\n"
737 " sea : green";
739 PARSE(doc, input);
740 YAML_ASSERT(doc.size() == 2);
741 YAML_ASSERT(doc["sequence"].size() == 2);
742 YAML_ASSERT(doc["sequence"][0].to<std::string>() == "one");
743 YAML_ASSERT(doc["sequence"][1].to<std::string>() == "two");
744 YAML_ASSERT(doc["mapping"].size() == 2);
745 YAML_ASSERT(doc["mapping"]["sky"].to<std::string>() == "blue");
746 YAML_ASSERT(doc["mapping"]["sea"].to<std::string>() == "green");
747 return true;
750 // 5.4
751 TEST FlowStructureIndicators()
753 std::string input =
754 "sequence: [ one, two, ]\n"
755 "mapping: { sky: blue, sea: green }";
757 PARSE(doc, input);
758 YAML_ASSERT(doc.size() == 2);
759 YAML_ASSERT(doc["sequence"].size() == 2);
760 YAML_ASSERT(doc["sequence"][0].to<std::string>() == "one");
761 YAML_ASSERT(doc["sequence"][1].to<std::string>() == "two");
762 YAML_ASSERT(doc["mapping"].size() == 2);
763 YAML_ASSERT(doc["mapping"]["sky"].to<std::string>() == "blue");
764 YAML_ASSERT(doc["mapping"]["sea"].to<std::string>() == "green");
765 return true;
768 // 5.5
769 TEST CommentIndicator()
771 std::string input =
772 "# Comment only.";
774 PARSE(doc, input);
775 YAML_ASSERT(doc.size() == 0);
776 return true;
779 // 5.6
780 TEST NodePropertyIndicators()
782 std::string input =
783 "anchored: !local &anchor value\n"
784 "alias: *anchor";
786 PARSE(doc, input);
787 YAML_ASSERT(doc.size() == 2);
788 YAML_ASSERT(doc["anchored"].to<std::string>() == "value"); // TODO: assert tag
789 YAML_ASSERT(doc["alias"].to<std::string>() == "value");
790 return true;
793 // 5.7
794 TEST BlockScalarIndicators()
796 std::string input =
797 "literal: |\n"
798 " some\n"
799 " text\n"
800 "folded: >\n"
801 " some\n"
802 " text\n";
804 PARSE(doc, input);
805 YAML_ASSERT(doc.size() == 2);
806 YAML_ASSERT(doc["literal"].to<std::string>() == "some\ntext\n");
807 YAML_ASSERT(doc["folded"].to<std::string>() == "some text\n");
808 return true;
811 // 5.8
812 TEST QuotedScalarIndicators()
814 std::string input =
815 "single: 'text'\n"
816 "double: \"text\"";
818 PARSE(doc, input);
819 YAML_ASSERT(doc.size() == 2);
820 YAML_ASSERT(doc["single"].to<std::string>() == "text");
821 YAML_ASSERT(doc["double"].to<std::string>() == "text");
822 return true;
825 // TODO: 5.9 directive
826 // TODO: 5.10 reserved indicator
828 // 5.11
829 TEST LineBreakCharacters()
831 std::string input =
832 "|\n"
833 " Line break (no glyph)\n"
834 " Line break (glyphed)\n";
836 PARSE(doc, input);
837 YAML_ASSERT(doc.to<std::string>() == "Line break (no glyph)\nLine break (glyphed)\n");
838 return true;
841 // 5.12
842 TEST TabsAndSpaces()
844 std::string input =
845 "# Tabs and spaces\n"
846 "quoted: \"Quoted\t\"\n"
847 "block: |\n"
848 " void main() {\n"
849 " \tprintf(\"Hello, world!\\n\");\n"
850 " }";
852 PARSE(doc, input);
853 YAML_ASSERT(doc.size() == 2);
854 YAML_ASSERT(doc["quoted"].to<std::string>() == "Quoted\t");
855 YAML_ASSERT(doc["block"].to<std::string>() ==
856 "void main() {\n"
857 "\tprintf(\"Hello, world!\\n\");\n"
858 "}");
859 return true;
862 // 5.13
863 TEST EscapedCharacters()
865 std::string input =
866 "\"Fun with \\\\\n"
867 "\\\" \\a \\b \\e \\f \\\n"
868 "\\n \\r \\t \\v \\0 \\\n"
869 "\\ \\_ \\N \\L \\P \\\n"
870 "\\x41 \\u0041 \\U00000041\"";
872 PARSE(doc, input);
873 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");
874 return true;
877 // 5.14
878 TEST InvalidEscapedCharacters()
880 std::string input =
881 "Bad escapes:\n"
882 " \"\\c\n"
883 " \\xq-\"";
885 std::stringstream stream(input);
886 try {
887 YAML::Parser parser(stream);
888 YAML::Node doc;
889 parser.GetNextDocument(doc);
890 } catch(const YAML::ParserException& e) {
891 YAML_ASSERT(e.msg == std::string(YAML::ErrorMsg::INVALID_ESCAPE) + "c");
892 return true;
895 return false;
898 // 6.1
899 TEST IndentationSpaces()
901 std::string input =
902 " # Leading comment line spaces are\n"
903 " # neither content nor indentation.\n"
904 " \n"
905 "Not indented:\n"
906 " By one space: |\n"
907 " By four\n"
908 " spaces\n"
909 " Flow style: [ # Leading spaces\n"
910 " By two, # in flow style\n"
911 " Also by two, # are neither\n"
912 " \tStill by two # content nor\n"
913 " ] # indentation.";
915 PARSE(doc, input);
916 YAML_ASSERT(doc.size() == 1);
917 YAML_ASSERT(doc["Not indented"].size() == 2);
918 YAML_ASSERT(doc["Not indented"]["By one space"].to<std::string>() == "By four\n spaces\n");
919 YAML_ASSERT(doc["Not indented"]["Flow style"].size() == 3);
920 YAML_ASSERT(doc["Not indented"]["Flow style"][0].to<std::string>() == "By two");
921 YAML_ASSERT(doc["Not indented"]["Flow style"][1].to<std::string>() == "Also by two");
922 YAML_ASSERT(doc["Not indented"]["Flow style"][2].to<std::string>() == "Still by two");
923 return true;
926 // 6.2
927 TEST IndentationIndicators()
929 std::string input =
930 "? a\n"
931 ": -\tb\n"
932 " - -\tc\n"
933 " - d";
935 PARSE(doc, input);
936 YAML_ASSERT(doc.size() == 1);
937 YAML_ASSERT(doc["a"].size() == 2);
938 YAML_ASSERT(doc["a"][0].to<std::string>() == "b");
939 YAML_ASSERT(doc["a"][1].size() == 2);
940 YAML_ASSERT(doc["a"][1][0].to<std::string>() == "c");
941 YAML_ASSERT(doc["a"][1][1].to<std::string>() == "d");
942 return true;
945 // 6.3
946 TEST SeparationSpaces()
948 std::string input =
949 "- foo:\t bar\n"
950 "- - baz\n"
951 " -\tbaz";
953 PARSE(doc, input);
954 YAML_ASSERT(doc.size() == 2);
955 YAML_ASSERT(doc[0].size() == 1);
956 YAML_ASSERT(doc[0]["foo"].to<std::string>() == "bar");
957 YAML_ASSERT(doc[1].size() == 2);
958 YAML_ASSERT(doc[1][0].to<std::string>() == "baz");
959 YAML_ASSERT(doc[1][1].to<std::string>() == "baz");
960 return true;
963 // 6.4
964 TEST LinePrefixes()
966 std::string input =
967 "plain: text\n"
968 " lines\n"
969 "quoted: \"text\n"
970 " \tlines\"\n"
971 "block: |\n"
972 " text\n"
973 " \tlines\n";
975 PARSE(doc, input);
976 YAML_ASSERT(doc.size() == 3);
977 YAML_ASSERT(doc["plain"].to<std::string>() == "text lines");
978 YAML_ASSERT(doc["quoted"].to<std::string>() == "text lines");
979 YAML_ASSERT(doc["block"].to<std::string>() == "text\n \tlines\n");
980 return true;
983 // 6.5
984 TEST EmptyLines()
986 std::string input =
987 "Folding:\n"
988 " \"Empty line\n"
989 " \t\n"
990 " as a line feed\"\n"
991 "Chomping: |\n"
992 " Clipped empty lines\n"
993 " ";
995 PARSE(doc, input);
996 YAML_ASSERT(doc.size() == 2);
997 YAML_ASSERT(doc["Folding"].to<std::string>() == "Empty line\nas a line feed");
998 YAML_ASSERT(doc["Chomping"].to<std::string>() == "Clipped empty lines\n");
999 return true;
1002 // 6.6
1003 TEST LineFolding()
1005 std::string input =
1006 ">-\n"
1007 " trimmed\n"
1008 " \n"
1009 " \n"
1010 "\n"
1011 " as\n"
1012 " space";
1014 PARSE(doc, input);
1015 YAML_ASSERT(doc.to<std::string>() == "trimmed\n\n\nas space");
1016 return true;
1019 // 6.7
1020 TEST BlockFolding()
1022 std::string input =
1023 ">\n"
1024 " foo \n"
1025 " \n"
1026 " \t bar\n"
1027 "\n"
1028 " baz\n";
1030 PARSE(doc, input);
1031 YAML_ASSERT(doc.to<std::string>() == "foo \n\n\t bar\n\nbaz\n");
1032 return true;
1035 // 6.8
1036 TEST FlowFolding()
1038 std::string input =
1039 "\"\n"
1040 " foo \n"
1041 " \n"
1042 " \t bar\n"
1043 "\n"
1044 " baz\n"
1045 "\"";
1047 PARSE(doc, input);
1048 YAML_ASSERT(doc.to<std::string>() == " foo\nbar\nbaz ");
1049 return true;
1052 // 6.9
1053 TEST SeparatedComment()
1055 std::string input =
1056 "key: # Comment\n"
1057 " value";
1059 PARSE(doc, input);
1060 YAML_ASSERT(doc.size() == 1);
1061 YAML_ASSERT(doc["key"].to<std::string>() == "value");
1062 return true;
1065 // 6.10
1066 TEST CommentLines()
1068 std::string input =
1069 " # Comment\n"
1070 " \n"
1071 "\n";
1073 PARSE(doc, input);
1074 YAML_ASSERT(doc.size() == 0);
1075 return true;
1078 // 6.11
1079 TEST MultiLineComments()
1081 std::string input =
1082 "key: # Comment\n"
1083 " # lines\n"
1084 " value\n"
1085 "\n";
1087 PARSE(doc, input);
1088 YAML_ASSERT(doc.size() == 1);
1089 YAML_ASSERT(doc["key"].to<std::string>() == "value");
1090 return true;
1093 struct StringMap {
1094 typedef std::map<std::string, std::string> Map;
1095 Map _;
1098 bool operator == (const StringMap& m, const StringMap& n) {
1099 return m._ == n._;
1102 void operator >> (const YAML::Node& node, StringMap& m) {
1103 m._.clear();
1104 for(YAML::Iterator it=node.begin();it!=node.end();++it) {
1105 std::string key = it.first().to<std::string>();
1106 std::string value = it.second().to<std::string>();
1107 m._[key] = value;
1112 // 6.12
1113 TEST SeparationSpacesII()
1115 std::string input =
1116 "{ first: Sammy, last: Sosa }:\n"
1117 "# Statistics:\n"
1118 " hr: # Home runs\n"
1119 " 65\n"
1120 " avg: # Average\n"
1121 " 0.278";
1123 PARSE(doc, input);
1124 std::map<std::string, std::string> key;
1125 key["first"] = "Sammy";
1126 key["last"] = "Sosa";
1127 YAML_ASSERT(doc.size() == 1);
1128 YAML_ASSERT(doc[key].size() == 2);
1129 YAML_ASSERT(doc[key]["hr"].to<int>() == 65);
1130 YAML_ASSERT(doc[key]["avg"].to<std::string>() == "0.278");
1131 return true;
1134 // 6.13
1135 TEST ReservedDirectives()
1137 std::string input =
1138 "%FOO bar baz # Should be ignored\n"
1139 " # with a warning.\n"
1140 "--- \"foo\"";
1142 PARSE(doc, input);
1143 return true;
1146 // 6.14
1147 TEST YAMLDirective()
1149 std::string input =
1150 "%YAML 1.3 # Attempt parsing\n"
1151 " # with a warning\n"
1152 "---\n"
1153 "\"foo\"";
1155 PARSE(doc, input);
1156 return true;
1159 // 6.15
1160 TEST InvalidRepeatedYAMLDirective()
1162 std::string input =
1163 "%YAML 1.2\n"
1164 "%YAML 1.1\n"
1165 "foo";
1167 try {
1168 PARSE(doc, input);
1169 } catch(const YAML::ParserException& e) {
1170 if(e.msg == YAML::ErrorMsg::REPEATED_YAML_DIRECTIVE)
1171 return true;
1173 throw;
1176 return " No exception was thrown";
1179 // 6.16
1180 TEST TagDirective()
1182 std::string input =
1183 "%TAG !yaml! tag:yaml.org,2002:\n"
1184 "---\n"
1185 "!yaml!str \"foo\"";
1187 PARSE(doc, input);
1188 YAML_ASSERT(doc.Tag() == "tag:yaml.org,2002:str");
1189 YAML_ASSERT(doc.to<std::string>() == "foo");
1190 return true;
1193 // 6.17
1194 TEST InvalidRepeatedTagDirective()
1196 std::string input =
1197 "%TAG ! !foo\n"
1198 "%TAG ! !foo\n"
1199 "bar";
1201 try {
1202 PARSE(doc, input);
1203 } catch(const YAML::ParserException& e) {
1204 if(e.msg == YAML::ErrorMsg::REPEATED_TAG_DIRECTIVE)
1205 return true;
1207 throw;
1210 return " No exception was thrown";
1213 // 6.18
1214 TEST PrimaryTagHandle()
1216 std::string input =
1217 "# Private\n"
1218 "!foo \"bar\"\n"
1219 "...\n"
1220 "# Global\n"
1221 "%TAG ! tag:example.com,2000:app/\n"
1222 "---\n"
1223 "!foo \"bar\"";
1225 PARSE(doc, input);
1226 YAML_ASSERT(doc.Tag() == "!foo");
1227 YAML_ASSERT(doc.to<std::string>() == "bar");
1229 PARSE_NEXT(doc);
1230 YAML_ASSERT(doc.Tag() == "tag:example.com,2000:app/foo");
1231 YAML_ASSERT(doc.to<std::string>() == "bar");
1232 return true;
1235 // 6.19
1236 TEST SecondaryTagHandle()
1238 std::string input =
1239 "%TAG !! tag:example.com,2000:app/\n"
1240 "---\n"
1241 "!!int 1 - 3 # Interval, not integer";
1243 PARSE(doc, input);
1244 YAML_ASSERT(doc.Tag() == "tag:example.com,2000:app/int");
1245 YAML_ASSERT(doc.to<std::string>() == "1 - 3");
1246 return true;
1249 // 6.20
1250 TEST TagHandles()
1252 std::string input =
1253 "%TAG !e! tag:example.com,2000:app/\n"
1254 "---\n"
1255 "!e!foo \"bar\"";
1257 PARSE(doc, input);
1258 YAML_ASSERT(doc.Tag() == "tag:example.com,2000:app/foo");
1259 YAML_ASSERT(doc.to<std::string>() == "bar");
1260 return true;
1263 // 6.21
1264 TEST LocalTagPrefix()
1266 std::string input =
1267 "%TAG !m! !my-\n"
1268 "--- # Bulb here\n"
1269 "!m!light fluorescent\n"
1270 "...\n"
1271 "%TAG !m! !my-\n"
1272 "--- # Color here\n"
1273 "!m!light green";
1275 PARSE(doc, input);
1276 YAML_ASSERT(doc.Tag() == "!my-light");
1277 YAML_ASSERT(doc.to<std::string>() == "fluorescent");
1279 PARSE_NEXT(doc);
1280 YAML_ASSERT(doc.Tag() == "!my-light");
1281 YAML_ASSERT(doc.to<std::string>() == "green");
1282 return true;
1285 // 6.22
1286 TEST GlobalTagPrefix()
1288 std::string input =
1289 "%TAG !e! tag:example.com,2000:app/\n"
1290 "---\n"
1291 "- !e!foo \"bar\"";
1293 PARSE(doc, input);
1294 YAML_ASSERT(doc.size() == 1);
1295 YAML_ASSERT(doc[0].Tag() == "tag:example.com,2000:app/foo");
1296 YAML_ASSERT(doc[0].to<std::string>() == "bar");
1297 return true;
1300 // 6.23
1301 TEST NodeProperties()
1303 std::string input =
1304 "!!str &a1 \"foo\":\n"
1305 " !!str bar\n"
1306 "&a2 baz : *a1";
1308 PARSE(doc, input);
1309 YAML_ASSERT(doc.size() == 2);
1310 for(YAML::Iterator it=doc.begin();it!=doc.end();++it) {
1311 if(it.first().to<std::string>() == "foo") {
1312 YAML_ASSERT(it.first().Tag() == "tag:yaml.org,2002:str");
1313 YAML_ASSERT(it.second().Tag() == "tag:yaml.org,2002:str");
1314 YAML_ASSERT(it.second().to<std::string>() == "bar");
1315 } else if(it.first().to<std::string>() == "baz") {
1316 YAML_ASSERT(it.second().to<std::string>() == "foo");
1317 } else
1318 return " unknown key";
1321 return true;
1324 // 6.24
1325 TEST VerbatimTags()
1327 std::string input =
1328 "!<tag:yaml.org,2002:str> foo :\n"
1329 " !<!bar> baz";
1331 PARSE(doc, input);
1332 YAML_ASSERT(doc.size() == 1);
1333 for(YAML::Iterator it=doc.begin();it!=doc.end();++it) {
1334 YAML_ASSERT(it.first().Tag() == "tag:yaml.org,2002:str");
1335 YAML_ASSERT(it.first().to<std::string>() == "foo");
1336 YAML_ASSERT(it.second().Tag() == "!bar");
1337 YAML_ASSERT(it.second().to<std::string>() == "baz");
1339 return true;
1342 // 6.25
1343 TEST InvalidVerbatimTags()
1345 std::string input =
1346 "- !<!> foo\n"
1347 "- !<$:?> bar\n";
1349 PARSE(doc, input);
1350 return " not implemented yet"; // TODO: check tags (but we probably will say these are valid, I think)
1353 // 6.26
1354 TEST TagShorthands()
1356 std::string input =
1357 "%TAG !e! tag:example.com,2000:app/\n"
1358 "---\n"
1359 "- !local foo\n"
1360 "- !!str bar\n"
1361 "- !e!tag%21 baz\n";
1363 PARSE(doc, input);
1364 YAML_ASSERT(doc.size() == 3);
1365 YAML_ASSERT(doc[0].Tag() == "!local");
1366 YAML_ASSERT(doc[0].to<std::string>() == "foo");
1367 YAML_ASSERT(doc[1].Tag() == "tag:yaml.org,2002:str");
1368 YAML_ASSERT(doc[1].to<std::string>() == "bar");
1369 YAML_ASSERT(doc[2].Tag() == "tag:example.com,2000:app/tag%21");
1370 YAML_ASSERT(doc[2].to<std::string>() == "baz");
1371 return true;
1374 // 6.27
1375 TEST InvalidTagShorthands()
1377 std::string input1 =
1378 "%TAG !e! tag:example,2000:app/\n"
1379 "---\n"
1380 "- !e! foo";
1382 bool threw = false;
1383 try {
1384 PARSE(doc, input1);
1385 } catch(const YAML::ParserException& e) {
1386 threw = true;
1387 if(e.msg != YAML::ErrorMsg::TAG_WITH_NO_SUFFIX)
1388 throw;
1391 if(!threw)
1392 return " No exception was thrown for a tag with no suffix";
1394 std::string input2 =
1395 "%TAG !e! tag:example,2000:app/\n"
1396 "---\n"
1397 "- !h!bar baz";
1399 PARSE(doc, input2); // TODO: should we reject this one (since !h! is not declared)?
1400 return " not implemented yet";
1403 // 6.28
1404 TEST NonSpecificTags()
1406 std::string input =
1407 "# Assuming conventional resolution:\n"
1408 "- \"12\"\n"
1409 "- 12\n"
1410 "- ! 12";
1412 PARSE(doc, input);
1413 YAML_ASSERT(doc.size() == 3);
1414 YAML_ASSERT(doc[0].to<std::string>() == "12"); // TODO: check tags. How?
1415 YAML_ASSERT(doc[1].to<int>() == 12);
1416 YAML_ASSERT(doc[2].to<std::string>() == "12");
1417 return true;
1420 // 6.29
1421 TEST NodeAnchors()
1423 std::string input =
1424 "First occurrence: &anchor Value\n"
1425 "Second occurrence: *anchor";
1427 PARSE(doc, input);
1428 YAML_ASSERT(doc.size() == 2);
1429 YAML_ASSERT(doc["First occurrence"].to<std::string>() == "Value");
1430 YAML_ASSERT(doc["Second occurrence"].to<std::string>() == "Value");
1431 return true;
1434 // 7.1
1435 TEST AliasNodes()
1437 std::string input =
1438 "First occurrence: &anchor Foo\n"
1439 "Second occurrence: *anchor\n"
1440 "Override anchor: &anchor Bar\n"
1441 "Reuse anchor: *anchor";
1443 PARSE(doc, input);
1444 YAML_ASSERT(doc.size() == 4);
1445 YAML_ASSERT(doc["First occurrence"].to<std::string>() == "Foo");
1446 YAML_ASSERT(doc["Second occurrence"].to<std::string>() == "Foo");
1447 YAML_ASSERT(doc["Override anchor"].to<std::string>() == "Bar");
1448 YAML_ASSERT(doc["Reuse anchor"].to<std::string>() == "Bar");
1449 return true;
1452 // 7.2
1453 TEST EmptyNodes()
1455 std::string input =
1456 "{\n"
1457 " foo : !!str,\n"
1458 " !!str : bar,\n"
1459 "}";
1461 PARSE(doc, input);
1462 YAML_ASSERT(doc.size() == 2);
1463 for(YAML::Iterator it=doc.begin();it!=doc.end();++it) {
1464 if(it.first().to<std::string>() == "foo") {
1465 YAML_ASSERT(it.second().Tag() == "tag:yaml.org,2002:str");
1466 YAML_ASSERT(it.second().to<std::string>() == "");
1467 } else if(it.first().to<std::string>() == "") {
1468 YAML_ASSERT(it.first().Tag() == "tag:yaml.org,2002:str");
1469 YAML_ASSERT(it.second().to<std::string>() == "bar");
1470 } else
1471 return " unexpected key";
1473 return true;
1476 // 7.3
1477 TEST CompletelyEmptyNodes()
1479 std::string input =
1480 "{\n"
1481 " ? foo :,\n"
1482 " : bar,\n"
1483 "}\n";
1485 PARSE(doc, input);
1486 YAML_ASSERT(doc.size() == 2);
1487 YAML_ASSERT(IsNull(doc["foo"]));
1488 YAML_ASSERT(doc[YAML::Null].to<std::string>() == "bar");
1489 return true;
1492 // 7.4
1493 TEST DoubleQuotedImplicitKeys()
1495 std::string input =
1496 "\"implicit block key\" : [\n"
1497 " \"implicit flow key\" : value,\n"
1498 " ]";
1500 PARSE(doc, input);
1501 YAML_ASSERT(doc.size() == 1);
1502 YAML_ASSERT(doc["implicit block key"].size() == 1);
1503 YAML_ASSERT(doc["implicit block key"][0].size() == 1);
1504 YAML_ASSERT(doc["implicit block key"][0]["implicit flow key"].to<std::string>() == "value");
1505 return true;
1508 // 7.5
1509 TEST DoubleQuotedLineBreaks()
1511 std::string input =
1512 "\"folded \n"
1513 "to a space,\t\n"
1514 " \n"
1515 "to a line feed, or \t\\\n"
1516 " \\ \tnon-content\"";
1518 PARSE(doc, input);
1519 YAML_ASSERT(doc.to<std::string>() == "folded to a space,\nto a line feed, or \t \tnon-content");
1520 return true;
1523 // 7.6
1524 TEST DoubleQuotedLines()
1526 std::string input =
1527 "\" 1st non-empty\n"
1528 "\n"
1529 " 2nd non-empty \n"
1530 "\t3rd non-empty \"";
1532 PARSE(doc, input);
1533 YAML_ASSERT(doc.to<std::string>() == " 1st non-empty\n2nd non-empty 3rd non-empty ");
1534 return true;
1537 // 7.7
1538 TEST SingleQuotedCharacters()
1540 std::string input = " 'here''s to \"quotes\"'";
1542 PARSE(doc, input);
1543 YAML_ASSERT(doc.to<std::string>() == "here's to \"quotes\"");
1544 return true;
1547 // 7.8
1548 TEST SingleQuotedImplicitKeys()
1550 std::string input =
1551 "'implicit block key' : [\n"
1552 " 'implicit flow key' : value,\n"
1553 " ]";
1555 PARSE(doc, input);
1556 YAML_ASSERT(doc.size() == 1);
1557 YAML_ASSERT(doc["implicit block key"].size() == 1);
1558 YAML_ASSERT(doc["implicit block key"][0].size() == 1);
1559 YAML_ASSERT(doc["implicit block key"][0]["implicit flow key"].to<std::string>() == "value");
1560 return true;
1563 // 7.9
1564 TEST SingleQuotedLines()
1566 std::string input =
1567 "' 1st non-empty\n"
1568 "\n"
1569 " 2nd non-empty \n"
1570 "\t3rd non-empty '";
1572 PARSE(doc, input);
1573 YAML_ASSERT(doc.to<std::string>() == " 1st non-empty\n2nd non-empty 3rd non-empty ");
1574 return true;
1577 // 7.10
1578 TEST PlainCharacters()
1580 std::string input =
1581 "# Outside flow collection:\n"
1582 "- ::vector\n"
1583 "- \": - ()\"\n"
1584 "- Up, up, and away!\n"
1585 "- -123\n"
1586 "- http://example.com/foo#bar\n"
1587 "# Inside flow collection:\n"
1588 "- [ ::vector,\n"
1589 " \": - ()\",\n"
1590 " \"Up, up, and away!\",\n"
1591 " -123,\n"
1592 " http://example.com/foo#bar ]";
1594 PARSE(doc, input);
1595 YAML_ASSERT(doc.size() == 6);
1596 YAML_ASSERT(doc[0].to<std::string>() == "::vector");
1597 YAML_ASSERT(doc[1].to<std::string>() == ": - ()");
1598 YAML_ASSERT(doc[2].to<std::string>() == "Up, up, and away!");
1599 YAML_ASSERT(doc[3].to<int>() == -123);
1600 YAML_ASSERT(doc[4].to<std::string>() == "http://example.com/foo#bar");
1601 YAML_ASSERT(doc[5].size() == 5);
1602 YAML_ASSERT(doc[5][0].to<std::string>() == "::vector");
1603 YAML_ASSERT(doc[5][1].to<std::string>() == ": - ()");
1604 YAML_ASSERT(doc[5][2].to<std::string>() == "Up, up, and away!");
1605 YAML_ASSERT(doc[5][3].to<int>() == -123);
1606 YAML_ASSERT(doc[5][4].to<std::string>() == "http://example.com/foo#bar");
1607 return true;
1610 // 7.11
1611 TEST PlainImplicitKeys()
1613 std::string input =
1614 "implicit block key : [\n"
1615 " implicit flow key : value,\n"
1616 " ]";
1618 PARSE(doc, input);
1619 YAML_ASSERT(doc.size() == 1);
1620 YAML_ASSERT(doc["implicit block key"].size() == 1);
1621 YAML_ASSERT(doc["implicit block key"][0].size() == 1);
1622 YAML_ASSERT(doc["implicit block key"][0]["implicit flow key"].to<std::string>() == "value");
1623 return true;
1626 // 7.12
1627 TEST PlainLines()
1629 std::string input =
1630 "1st non-empty\n"
1631 "\n"
1632 " 2nd non-empty \n"
1633 "\t3rd non-empty";
1635 PARSE(doc, input);
1636 YAML_ASSERT(doc.to<std::string>() == "1st non-empty\n2nd non-empty 3rd non-empty");
1637 return true;
1640 // 7.13
1641 TEST FlowSequence()
1643 std::string input =
1644 "- [ one, two, ]\n"
1645 "- [three ,four]";
1647 PARSE(doc, input);
1648 YAML_ASSERT(doc.size() == 2);
1649 YAML_ASSERT(doc[0].size() == 2);
1650 YAML_ASSERT(doc[0][0].to<std::string>() == "one");
1651 YAML_ASSERT(doc[0][1].to<std::string>() == "two");
1652 YAML_ASSERT(doc[1].size() == 2);
1653 YAML_ASSERT(doc[1][0].to<std::string>() == "three");
1654 YAML_ASSERT(doc[1][1].to<std::string>() == "four");
1655 return true;
1658 // 7.14
1659 TEST FlowSequenceEntries()
1661 std::string input =
1662 "[\n"
1663 "\"double\n"
1664 " quoted\", 'single\n"
1665 " quoted',\n"
1666 "plain\n"
1667 " text, [ nested ],\n"
1668 "single: pair,\n"
1669 "]";
1671 PARSE(doc, input);
1672 YAML_ASSERT(doc.size() == 5);
1673 YAML_ASSERT(doc[0].to<std::string>() == "double quoted");
1674 YAML_ASSERT(doc[1].to<std::string>() == "single quoted");
1675 YAML_ASSERT(doc[2].to<std::string>() == "plain text");
1676 YAML_ASSERT(doc[3].size() == 1);
1677 YAML_ASSERT(doc[3][0].to<std::string>() == "nested");
1678 YAML_ASSERT(doc[4].size() == 1);
1679 YAML_ASSERT(doc[4]["single"].to<std::string>() == "pair");
1680 return true;
1683 // 7.15
1684 TEST FlowMappings()
1686 std::string input =
1687 "- { one : two , three: four , }\n"
1688 "- {five: six,seven : eight}";
1690 PARSE(doc, input);
1691 YAML_ASSERT(doc.size() == 2);
1692 YAML_ASSERT(doc[0].size() == 2);
1693 YAML_ASSERT(doc[0]["one"].to<std::string>() == "two");
1694 YAML_ASSERT(doc[0]["three"].to<std::string>() == "four");
1695 YAML_ASSERT(doc[1].size() == 2);
1696 YAML_ASSERT(doc[1]["five"].to<std::string>() == "six");
1697 YAML_ASSERT(doc[1]["seven"].to<std::string>() == "eight");
1698 return true;
1701 // 7.16
1702 TEST FlowMappingEntries()
1704 std::string input =
1705 "{\n"
1706 "? explicit: entry,\n"
1707 "implicit: entry,\n"
1708 "?\n"
1709 "}";
1711 PARSE(doc, input);
1712 YAML_ASSERT(doc.size() == 3);
1713 YAML_ASSERT(doc["explicit"].to<std::string>() == "entry");
1714 YAML_ASSERT(doc["implicit"].to<std::string>() == "entry");
1715 YAML_ASSERT(IsNull(doc[YAML::Null]));
1716 return true;
1719 // 7.17
1720 TEST FlowMappingSeparateValues()
1722 std::string input =
1723 "{\n"
1724 "unquoted : \"separate\",\n"
1725 "http://foo.com,\n"
1726 "omitted value:,\n"
1727 ": omitted key,\n"
1728 "}";
1730 PARSE(doc, input);
1731 YAML_ASSERT(doc.size() == 4);
1732 YAML_ASSERT(doc["unquoted"].to<std::string>() == "separate");
1733 YAML_ASSERT(IsNull(doc["http://foo.com"]));
1734 YAML_ASSERT(IsNull(doc["omitted value"]));
1735 YAML_ASSERT(doc[YAML::Null].to<std::string>() == "omitted key");
1736 return true;
1739 // 7.18
1740 TEST FlowMappingAdjacentValues()
1742 std::string input =
1743 "{\n"
1744 "\"adjacent\":value,\n"
1745 "\"readable\":value,\n"
1746 "\"empty\":\n"
1747 "}";
1749 PARSE(doc, input);
1750 YAML_ASSERT(doc.size() == 3);
1751 YAML_ASSERT(doc["adjacent"].to<std::string>() == "value");
1752 YAML_ASSERT(doc["readable"].to<std::string>() == "value");
1753 YAML_ASSERT(IsNull(doc["empty"]));
1754 return true;
1757 // 7.19
1758 TEST SinglePairFlowMappings()
1760 std::string input =
1761 "[\n"
1762 "foo: bar\n"
1763 "]";
1765 PARSE(doc, input);
1766 YAML_ASSERT(doc.size() == 1);
1767 YAML_ASSERT(doc[0].size() == 1);
1768 YAML_ASSERT(doc[0]["foo"].to<std::string>() == "bar");
1769 return true;
1772 // 7.20
1773 TEST SinglePairExplicitEntry()
1775 std::string input =
1776 "[\n"
1777 "? foo\n"
1778 " bar : baz\n"
1779 "]";
1781 PARSE(doc, input);
1782 YAML_ASSERT(doc.size() == 1);
1783 YAML_ASSERT(doc[0].size() == 1);
1784 YAML_ASSERT(doc[0]["foo bar"].to<std::string>() == "baz");
1785 return true;
1788 // 7.21
1789 TEST SinglePairImplicitEntries()
1791 std::string input =
1792 "- [ YAML : separate ]\n"
1793 "- [ : empty key entry ]\n"
1794 "- [ {JSON: like}:adjacent ]";
1796 PARSE(doc, input);
1797 YAML_ASSERT(doc.size() == 3);
1798 YAML_ASSERT(doc[0].size() == 1);
1799 YAML_ASSERT(doc[0][0].size() == 1);
1800 YAML_ASSERT(doc[0][0]["YAML"].to<std::string>() == "separate");
1801 YAML_ASSERT(doc[1].size() == 1);
1802 YAML_ASSERT(doc[1][0].size() == 1);
1803 YAML_ASSERT(doc[1][0][YAML::Null].to<std::string>() == "empty key entry");
1804 YAML_ASSERT(doc[2].size() == 1);
1805 YAML_ASSERT(doc[2][0].size() == 1);
1806 StringMap key;
1807 key._["JSON"] = "like";
1808 YAML_ASSERT(doc[2][0][key].to<std::string>() == "adjacent");
1809 return true;
1812 // 7.22
1813 TEST InvalidImplicitKeys()
1815 std::string input =
1816 "[ foo\n"
1817 " bar: invalid,"; // Note: we don't check (on purpose) the >1K chars for an implicit key
1819 try {
1820 PARSE(doc, input);
1821 } catch(const YAML::Exception& e) {
1822 if(e.msg == YAML::ErrorMsg::END_OF_SEQ_FLOW)
1823 return true;
1825 throw;
1827 return " no exception thrown";
1830 // 7.23
1831 TEST FlowContent()
1833 std::string input =
1834 "- [ a, b ]\n"
1835 "- { a: b }\n"
1836 "- \"a\"\n"
1837 "- 'b'\n"
1838 "- c";
1840 PARSE(doc, input);
1841 YAML_ASSERT(doc.size() == 5);
1842 YAML_ASSERT(doc[0].size() == 2);
1843 YAML_ASSERT(doc[0][0].to<std::string>() == "a");
1844 YAML_ASSERT(doc[0][1].to<std::string>() == "b");
1845 YAML_ASSERT(doc[1].size() == 1);
1846 YAML_ASSERT(doc[1]["a"].to<std::string>() == "b");
1847 YAML_ASSERT(doc[2].to<std::string>() == "a");
1848 YAML_ASSERT(doc[3].to<char>() == 'b');
1849 YAML_ASSERT(doc[4].to<std::string>() == "c");
1850 return true;
1853 // 7.24
1854 TEST FlowNodes()
1856 std::string input =
1857 "- !!str \"a\"\n"
1858 "- 'b'\n"
1859 "- &anchor \"c\"\n"
1860 "- *anchor\n"
1861 "- !!str";
1863 PARSE(doc, input);
1864 YAML_ASSERT(doc.size() == 5);
1865 YAML_ASSERT(doc[0].Tag() == "tag:yaml.org,2002:str");
1866 YAML_ASSERT(doc[0].to<std::string>() == "a");
1867 YAML_ASSERT(doc[1].to<char>() == 'b');
1868 YAML_ASSERT(doc[2].to<std::string>() == "c");
1869 YAML_ASSERT(doc[3].to<std::string>() == "c");
1870 YAML_ASSERT(doc[4].Tag() == "tag:yaml.org,2002:str");
1871 YAML_ASSERT(doc[4].to<std::string>() == "");
1872 return true;
1875 // 8.1
1876 TEST BlockScalarHeader()
1878 std::string input =
1879 "- | # Empty header\n"
1880 " literal\n"
1881 "- >1 # Indentation indicator\n"
1882 " folded\n"
1883 "- |+ # Chomping indicator\n"
1884 " keep\n"
1885 "\n"
1886 "- >1- # Both indicators\n"
1887 " strip\n";
1889 PARSE(doc, input);
1890 YAML_ASSERT(doc.size() == 4);
1891 YAML_ASSERT(doc[0].to<std::string>() == "literal\n");
1892 YAML_ASSERT(doc[1].to<std::string>() == " folded\n");
1893 YAML_ASSERT(doc[2].to<std::string>() == "keep\n\n");
1894 YAML_ASSERT(doc[3].to<std::string>() == " strip");
1895 return true;
1898 // 8.2
1899 TEST BlockIndentationHeader()
1901 std::string input =
1902 "- |\n"
1903 " detected\n"
1904 "- >\n"
1905 " \n"
1906 " \n"
1907 " # detected\n"
1908 "- |1\n"
1909 " explicit\n"
1910 "- >\n"
1911 " \t\n"
1912 " detected\n";
1914 PARSE(doc, input);
1915 YAML_ASSERT(doc.size() == 4);
1916 YAML_ASSERT(doc[0].to<std::string>() == "detected\n");
1917 YAML_ASSERT(doc[1].to<std::string>() == "\n\n# detected\n");
1918 YAML_ASSERT(doc[2].to<std::string>() == " explicit\n");
1919 YAML_ASSERT(doc[3].to<std::string>() == "\t\ndetected\n");
1920 return true;
1923 // 8.3
1924 TEST InvalidBlockScalarIndentationIndicators()
1927 std::string input =
1928 "- |\n"
1929 " \n"
1930 " text";
1932 bool threw = false;
1933 try {
1934 PARSE(doc, input);
1935 } catch(const YAML::Exception& e) {
1936 if(e.msg != YAML::ErrorMsg::END_OF_SEQ)
1937 throw;
1939 threw = true;
1942 if(!threw)
1943 return " no exception thrown for less indented auto-detecting indentation for a literal block scalar";
1947 std::string input =
1948 "- >\n"
1949 " text\n"
1950 " text";
1952 bool threw = false;
1953 try {
1954 PARSE(doc, input);
1955 } catch(const YAML::Exception& e) {
1956 if(e.msg != YAML::ErrorMsg::END_OF_SEQ)
1957 throw;
1959 threw = true;
1962 if(!threw)
1963 return " no exception thrown for less indented auto-detecting indentation for a folded block scalar";
1967 std::string input =
1968 "- |2\n"
1969 " text";
1971 bool threw = false;
1972 try {
1973 PARSE(doc, input);
1974 } catch(const YAML::Exception& e) {
1975 if(e.msg != YAML::ErrorMsg::END_OF_SEQ)
1976 throw;
1978 threw = true;
1981 if(!threw)
1982 return " no exception thrown for less indented explicit indentation for a literal block scalar";
1985 return true;
1988 // 8.4
1989 TEST ChompingFinalLineBreak()
1991 std::string input =
1992 "strip: |-\n"
1993 " text\n"
1994 "clip: |\n"
1995 " text\n"
1996 "keep: |+\n"
1997 " text\n";
1999 PARSE(doc, input);
2000 YAML_ASSERT(doc.size() == 3);
2001 YAML_ASSERT(doc["strip"].to<std::string>() == "text");
2002 YAML_ASSERT(doc["clip"].to<std::string>() == "text\n");
2003 YAML_ASSERT(doc["keep"].to<std::string>() == "text\n");
2004 return true;
2007 // 8.5
2008 TEST ChompingTrailingLines()
2010 std::string input =
2011 " # Strip\n"
2012 " # Comments:\n"
2013 "strip: |-\n"
2014 " # text\n"
2015 " \n"
2016 " # Clip\n"
2017 " # comments:\n"
2018 "\n"
2019 "clip: |\n"
2020 " # text\n"
2021 " \n"
2022 " # Keep\n"
2023 " # comments:\n"
2024 "\n"
2025 "keep: |+\n"
2026 " # text\n"
2027 "\n"
2028 " # Trail\n"
2029 " # Comments\n";
2031 PARSE(doc, input);
2032 YAML_ASSERT(doc.size() == 3);
2033 YAML_ASSERT(doc["strip"].to<std::string>() == "# text");
2034 YAML_ASSERT(doc["clip"].to<std::string>() == "# text\n");
2035 YAML_ASSERT(doc["keep"].to<std::string>() == "# text\n");
2036 return true;
2039 // 8.6
2040 TEST EmptyScalarChomping()
2042 std::string input =
2043 "strip: >-\n"
2044 "\n"
2045 "clip: >\n"
2046 "\n"
2047 "keep: |+\n"
2048 "\n";
2050 PARSE(doc, input);
2051 YAML_ASSERT(doc.size() == 3);
2052 YAML_ASSERT(doc["strip"].to<std::string>() == "");
2053 YAML_ASSERT(doc["clip"].to<std::string>() == "");
2054 YAML_ASSERT(doc["keep"].to<std::string>() == "\n");
2055 return true;
2058 // 8.7
2059 TEST LiteralScalar()
2061 std::string input =
2062 "|\n"
2063 " literal\n"
2064 " \ttext\n"
2065 "\n";
2067 PARSE(doc, input);
2068 YAML_ASSERT(doc.to<std::string>() == "literal\n\ttext\n");
2069 return true;
2072 // 8.8
2073 TEST LiteralContent()
2075 std::string input =
2076 "|\n"
2077 " \n"
2078 " \n"
2079 " literal\n"
2080 " \n"
2081 " \n"
2082 " text\n"
2083 "\n"
2084 " # Comment\n";
2086 PARSE(doc, input);
2087 YAML_ASSERT(doc.to<std::string>() == "\n\nliteral\n \n\ntext\n");
2088 return true;
2091 // 8.9
2092 TEST FoldedScalar()
2094 std::string input =
2095 ">\n"
2096 " folded\n"
2097 " text\n"
2098 "\n";
2100 PARSE(doc, input);
2101 YAML_ASSERT(doc.to<std::string>() == "folded text\n");
2102 return true;
2105 // 8.10
2106 TEST FoldedLines()
2108 std::string input =
2109 ">\n"
2110 "\n"
2111 " folded\n"
2112 " line\n"
2113 "\n"
2114 " next\n"
2115 " line\n"
2116 " * bullet\n"
2117 "\n"
2118 " * list\n"
2119 " * lines\n"
2120 "\n"
2121 " last\n"
2122 " line\n"
2123 "\n"
2124 "# Comment\n";
2126 PARSE(doc, input);
2127 YAML_ASSERT(doc.to<std::string>() == "\nfolded line\nnext line\n * bullet\n\n * list\n * lines\n\nlast line\n");
2128 return true;
2131 // 8.11
2132 TEST MoreIndentedLines()
2134 return true; // same as 8.10
2137 // 8.12
2138 TEST EmptySeparationLines()
2140 return true; // same as 8.10
2143 // 8.13
2144 TEST FinalEmptyLines()
2146 return true; // same as 8.10
2149 // 8.14
2150 TEST BlockSequence()
2152 std::string input =
2153 "block sequence:\n"
2154 " - one\n"
2155 " - two : three\n";
2157 PARSE(doc, input);
2158 YAML_ASSERT(doc.size() == 1);
2159 YAML_ASSERT(doc["block sequence"].size() == 2);
2160 YAML_ASSERT(doc["block sequence"][0].to<std::string>() == "one");
2161 YAML_ASSERT(doc["block sequence"][1].size() == 1);
2162 YAML_ASSERT(doc["block sequence"][1]["two"].to<std::string>() == "three");
2163 return true;
2166 // 8.15
2167 TEST BlockSequenceEntryTypes()
2169 std::string input =
2170 "- # Empty\n"
2171 "- |\n"
2172 " block node\n"
2173 "- - one # Compact\n"
2174 " - two # sequence\n"
2175 "- one: two # Compact mapping\n";
2177 PARSE(doc, input);
2178 YAML_ASSERT(doc.size() == 4);
2179 YAML_ASSERT(YAML::IsNull(doc[0]));
2180 YAML_ASSERT(doc[1].to<std::string>() == "block node\n");
2181 YAML_ASSERT(doc[2].size() == 2);
2182 YAML_ASSERT(doc[2][0].to<std::string>() == "one");
2183 YAML_ASSERT(doc[2][1].to<std::string>() == "two");
2184 YAML_ASSERT(doc[3].size() == 1);
2185 YAML_ASSERT(doc[3]["one"].to<std::string>() == "two");
2186 return true;
2189 // 8.16
2190 TEST BlockMappings()
2192 std::string input =
2193 "block mapping:\n"
2194 " key: value\n";
2196 PARSE(doc, input);
2197 YAML_ASSERT(doc.size() == 1);
2198 YAML_ASSERT(doc["block mapping"].size() == 1);
2199 YAML_ASSERT(doc["block mapping"]["key"].to<std::string>() == "value");
2200 return true;
2203 // 8.17
2204 TEST ExplicitBlockMappingEntries()
2206 std::string input =
2207 "? explicit key # Empty value\n"
2208 "? |\n"
2209 " block key\n"
2210 ": - one # Explicit compact\n"
2211 " - two # block value\n";
2213 PARSE(doc, input);
2214 YAML_ASSERT(doc.size() == 2);
2215 YAML_ASSERT(IsNull(doc["explicit key"]));
2216 YAML_ASSERT(doc["block key\n"].size() == 2);
2217 YAML_ASSERT(doc["block key\n"][0].to<std::string>() == "one");
2218 YAML_ASSERT(doc["block key\n"][1].to<std::string>() == "two");
2219 return true;
2222 // 8.18
2223 TEST ImplicitBlockMappingEntries()
2225 std::string input =
2226 "plain key: in-line value\n"
2227 ": # Both empty\n"
2228 "\"quoted key\":\n"
2229 "- entry\n";
2231 PARSE(doc, input);
2232 YAML_ASSERT(doc.size() == 3);
2233 YAML_ASSERT(doc["plain key"].to<std::string>() == "in-line value");
2234 YAML_ASSERT(IsNull(doc[YAML::Null]));
2235 YAML_ASSERT(doc["quoted key"].size() == 1);
2236 YAML_ASSERT(doc["quoted key"][0].to<std::string>() == "entry");
2237 return true;
2240 // 8.19
2241 TEST CompactBlockMappings()
2243 std::string input =
2244 "- sun: yellow\n"
2245 "- ? earth: blue\n"
2246 " : moon: white\n";
2248 PARSE(doc, input);
2249 YAML_ASSERT(doc.size() == 2);
2250 YAML_ASSERT(doc[0].size() == 1);
2251 YAML_ASSERT(doc[0]["sun"].to<std::string>() == "yellow");
2252 YAML_ASSERT(doc[1].size() == 1);
2253 std::map<std::string, std::string> key;
2254 key["earth"] = "blue";
2255 YAML_ASSERT(doc[1][key].size() == 1);
2256 YAML_ASSERT(doc[1][key]["moon"].to<std::string>() == "white");
2257 return true;
2260 // 8.20
2261 TEST BlockNodeTypes()
2263 std::string input =
2264 "-\n"
2265 " \"flow in block\"\n"
2266 "- >\n"
2267 " Block scalar\n"
2268 "- !!map # Block collection\n"
2269 " foo : bar\n";
2271 PARSE(doc, input);
2272 YAML_ASSERT(doc.size() == 3);
2273 YAML_ASSERT(doc[0].to<std::string>() == "flow in block");
2274 YAML_ASSERT(doc[1].to<std::string>() == "Block scalar\n");
2275 YAML_ASSERT(doc[2].size() == 1);
2276 YAML_ASSERT(doc[2]["foo"].to<std::string>() == "bar");
2277 return true;
2280 // 8.21
2281 TEST BlockScalarNodes()
2283 std::string input =
2284 "literal: |2\n"
2285 " value\n"
2286 "folded:\n"
2287 " !foo\n"
2288 " >1\n"
2289 " value\n";
2291 PARSE(doc, input);
2292 YAML_ASSERT(doc.size() == 2);
2293 YAML_ASSERT(doc["literal"].to<std::string>() == "value");
2294 YAML_ASSERT(doc["folded"].to<std::string>() == "value");
2295 YAML_ASSERT(doc["folded"].Tag() == "!foo");
2296 return true;
2299 // 8.22
2300 TEST BlockCollectionNodes()
2302 std::string input =
2303 "sequence: !!seq\n"
2304 "- entry\n"
2305 "- !!seq\n"
2306 " - nested\n"
2307 "mapping: !!map\n"
2308 " foo: bar\n";
2310 PARSE(doc, input);
2311 YAML_ASSERT(doc.size() == 2);
2312 YAML_ASSERT(doc["sequence"].size() == 2);
2313 YAML_ASSERT(doc["sequence"][0].to<std::string>() == "entry");
2314 YAML_ASSERT(doc["sequence"][1].size() == 1);
2315 YAML_ASSERT(doc["sequence"][1][0].to<std::string>() == "nested");
2316 YAML_ASSERT(doc["mapping"].size() == 1);
2317 YAML_ASSERT(doc["mapping"]["foo"].to<std::string>() == "bar");
2318 return true;
2322 bool RunSpecTests()
2324 int passed = 0;
2325 int total = 0;
2326 RunSpecTest(&Spec::SeqScalars, "2.1", "Sequence of Scalars", passed, total);
2327 RunSpecTest(&Spec::MappingScalarsToScalars, "2.2", "Mapping Scalars to Scalars", passed, total);
2328 RunSpecTest(&Spec::MappingScalarsToSequences, "2.3", "Mapping Scalars to Sequences", passed, total);
2329 RunSpecTest(&Spec::SequenceOfMappings, "2.4", "Sequence of Mappings", passed, total);
2330 RunSpecTest(&Spec::SequenceOfSequences, "2.5", "Sequence of Sequences", passed, total);
2331 RunSpecTest(&Spec::MappingOfMappings, "2.6", "Mapping of Mappings", passed, total);
2332 RunSpecTest(&Spec::TwoDocumentsInAStream, "2.7", "Two Documents in a Stream", passed, total);
2333 RunSpecTest(&Spec::PlayByPlayFeed, "2.8", "Play by Play Feed from a Game", passed, total);
2334 RunSpecTest(&Spec::SingleDocumentWithTwoComments, "2.9", "Single Document with Two Comments", passed, total);
2335 RunSpecTest(&Spec::SimpleAnchor, "2.10", "Node for \"Sammy Sosa\" appears twice in this document", passed, total);
2336 RunSpecTest(&Spec::MappingBetweenSequences, "2.11", "Mapping between Sequences", passed, total);
2337 RunSpecTest(&Spec::CompactNestedMapping, "2.12", "Compact Nested Mapping", passed, total);
2338 RunSpecTest(&Spec::InLiteralsNewlinesArePreserved, "2.13", "In literals, newlines are preserved", passed, total);
2339 RunSpecTest(&Spec::InFoldedScalarsNewlinesBecomeSpaces, "2.14", "In folded scalars, newlines become spaces", passed, total);
2340 RunSpecTest(&Spec::FoldedNewlinesArePreservedForMoreIndentedAndBlankLines, "2.15", "Folded newlines are preserved for \"more indented\" and blank lines", passed, total);
2341 RunSpecTest(&Spec::IndentationDeterminesScope, "2.16", "Indentation determines scope", passed, total);
2342 RunSpecTest(&Spec::QuotedScalars, "2.17", "Quoted scalars", passed, total);
2343 RunSpecTest(&Spec::MultiLineFlowScalars, "2.18", "Multi-line flow scalars", passed, total);
2345 RunSpecTest(&Spec::VariousExplicitTags, "2.23", "Various Explicit Tags", passed, total);
2346 RunSpecTest(&Spec::GlobalTags, "2.24", "Global Tags", passed, total);
2347 RunSpecTest(&Spec::UnorderedSets, "2.25", "Unordered Sets", passed, total);
2348 RunSpecTest(&Spec::OrderedMappings, "2.26", "Ordered Mappings", passed, total);
2349 RunSpecTest(&Spec::Invoice, "2.27", "Invoice", passed, total);
2350 RunSpecTest(&Spec::LogFile, "2.28", "Log File", passed, total);
2352 RunSpecTest(&Spec::BlockStructureIndicators, "5.3", "Block Structure Indicators", passed, total);
2353 RunSpecTest(&Spec::FlowStructureIndicators, "5.4", "Flow Structure Indicators", passed, total);
2354 RunSpecTest(&Spec::NodePropertyIndicators, "5.6", "Node Property Indicators", passed, total);
2355 RunSpecTest(&Spec::BlockScalarIndicators, "5.7", "Block Scalar Indicators", passed, total);
2356 RunSpecTest(&Spec::QuotedScalarIndicators, "5.8", "Quoted Scalar Indicators", passed, total);
2357 RunSpecTest(&Spec::LineBreakCharacters, "5.11", "Line Break Characters", passed, total);
2358 RunSpecTest(&Spec::TabsAndSpaces, "5.12", "Tabs and Spaces", passed, total);
2359 RunSpecTest(&Spec::EscapedCharacters, "5.13", "Escaped Characters", passed, total);
2360 RunSpecTest(&Spec::InvalidEscapedCharacters, "5.14", "Invalid Escaped Characters", passed, total);
2362 RunSpecTest(&Spec::IndentationSpaces, "6.1", "Indentation Spaces", passed, total);
2363 RunSpecTest(&Spec::IndentationIndicators, "6.2", "Indentation Indicators", passed, total);
2364 RunSpecTest(&Spec::SeparationSpaces, "6.3", "Separation Spaces", passed, total);
2365 RunSpecTest(&Spec::LinePrefixes, "6.4", "Line Prefixes", passed, total);
2366 RunSpecTest(&Spec::EmptyLines, "6.5", "Empty Lines", passed, total);
2367 RunSpecTest(&Spec::LineFolding, "6.6", "Line Folding", passed, total);
2368 RunSpecTest(&Spec::BlockFolding, "6.7", "Block Folding", passed, total);
2369 RunSpecTest(&Spec::FlowFolding, "6.8", "Flow Folding", passed, total);
2370 RunSpecTest(&Spec::SeparatedComment, "6.9", "Separated Comment", passed, total);
2371 RunSpecTest(&Spec::CommentLines, "6.10", "Comment Lines", passed, total);
2372 RunSpecTest(&Spec::MultiLineComments, "6.11", "Multi-Line Comments", passed, total);
2373 RunSpecTest(&Spec::SeparationSpacesII, "6.12", "Separation Spaces", passed, total);
2374 RunSpecTest(&Spec::ReservedDirectives, "6.13", "Reserved Directives", passed, total);
2375 RunSpecTest(&Spec::YAMLDirective, "6.14", "YAML Directive", passed, total);
2376 RunSpecTest(&Spec::InvalidRepeatedYAMLDirective, "6.15", "Invalid Repeated YAML Directive", passed, total);
2377 RunSpecTest(&Spec::TagDirective, "6.16", "Tag Directive", passed, total);
2378 RunSpecTest(&Spec::InvalidRepeatedTagDirective, "6.17", "Invalid Repeated Tag Directive", passed, total);
2379 RunSpecTest(&Spec::PrimaryTagHandle, "6.18", "Primary Tag Handle", passed, total);
2380 RunSpecTest(&Spec::SecondaryTagHandle, "6.19", "SecondaryTagHandle", passed, total);
2381 RunSpecTest(&Spec::TagHandles, "6.20", "TagHandles", passed, total);
2382 RunSpecTest(&Spec::LocalTagPrefix, "6.21", "LocalTagPrefix", passed, total);
2383 RunSpecTest(&Spec::GlobalTagPrefix, "6.22", "GlobalTagPrefix", passed, total);
2384 RunSpecTest(&Spec::NodeProperties, "6.23", "NodeProperties", passed, total);
2385 RunSpecTest(&Spec::VerbatimTags, "6.24", "Verbatim Tags", passed, total);
2386 RunSpecTest(&Spec::InvalidVerbatimTags, "6.25", "Invalid Verbatim Tags", passed, total);
2387 RunSpecTest(&Spec::TagShorthands, "6.26", "Tag Shorthands", passed, total);
2388 RunSpecTest(&Spec::InvalidTagShorthands, "6.27", "Invalid Tag Shorthands", passed, total);
2389 RunSpecTest(&Spec::NonSpecificTags, "6.28", "Non Specific Tags", passed, total);
2390 RunSpecTest(&Spec::NodeAnchors, "6.29", "Node Anchors", passed, total);
2392 RunSpecTest(&Spec::AliasNodes, "7.1", "Alias Nodes", passed, total);
2393 RunSpecTest(&Spec::EmptyNodes, "7.2", "Empty Nodes", passed, total);
2394 RunSpecTest(&Spec::CompletelyEmptyNodes, "7.3", "Completely Empty Nodes", passed, total);
2395 RunSpecTest(&Spec::DoubleQuotedImplicitKeys, "7.4", "Double Quoted Implicit Keys", passed, total);
2396 RunSpecTest(&Spec::DoubleQuotedLineBreaks, "7.5", "Double Quoted Line Breaks", passed, total);
2397 RunSpecTest(&Spec::DoubleQuotedLines, "7.6", "Double Quoted Lines", passed, total);
2398 RunSpecTest(&Spec::SingleQuotedCharacters, "7.7", "Single Quoted Characters", passed, total);
2399 RunSpecTest(&Spec::SingleQuotedImplicitKeys, "7.8", "Single Quoted Implicit Keys", passed, total);
2400 RunSpecTest(&Spec::SingleQuotedLines, "7.9", "Single Quoted Lines", passed, total);
2401 RunSpecTest(&Spec::PlainCharacters, "7.10", "Plain Characters", passed, total);
2402 RunSpecTest(&Spec::PlainImplicitKeys, "7.11", "Plain Implicit Keys", passed, total);
2403 RunSpecTest(&Spec::PlainLines, "7.12", "Plain Lines", passed, total);
2404 RunSpecTest(&Spec::FlowSequence, "7.13", "Flow Sequence", passed, total);
2405 RunSpecTest(&Spec::FlowSequenceEntries, "7.14", "Flow Sequence Entries", passed, total);
2406 RunSpecTest(&Spec::FlowMappings, "7.15", "Flow Mappings", passed, total);
2407 RunSpecTest(&Spec::FlowMappingEntries, "7.16", "Flow Mapping Entries", passed, total);
2408 RunSpecTest(&Spec::FlowMappingSeparateValues, "7.17", "Flow Mapping Separate Values", passed, total);
2409 RunSpecTest(&Spec::FlowMappingAdjacentValues, "7.18", "Flow Mapping Adjacent Values", passed, total);
2410 RunSpecTest(&Spec::SinglePairFlowMappings, "7.19", "Single Pair Flow Mappings", passed, total);
2411 RunSpecTest(&Spec::SinglePairExplicitEntry, "7.20", "Single Pair Explicit Entry", passed, total);
2412 RunSpecTest(&Spec::SinglePairImplicitEntries, "7.21", "Single Pair Implicit Entries", passed, total);
2413 RunSpecTest(&Spec::InvalidImplicitKeys, "7.22", "Invalid Implicit Keys", passed, total);
2414 RunSpecTest(&Spec::FlowContent, "7.23", "Flow Content", passed, total);
2415 RunSpecTest(&Spec::FlowNodes, "7.24", "FlowNodes", passed, total);
2417 RunSpecTest(&Spec::BlockScalarHeader, "8.1", "Block Scalar Header", passed, total);
2418 RunSpecTest(&Spec::BlockIndentationHeader, "8.2", "Block Indentation Header", passed, total);
2419 RunSpecTest(&Spec::InvalidBlockScalarIndentationIndicators, "8.3", "Invalid Block Scalar Indentation Indicators", passed, total);
2420 RunSpecTest(&Spec::ChompingFinalLineBreak, "8.4", "Chomping Final Line Break", passed, total);
2421 RunSpecTest(&Spec::ChompingTrailingLines, "8.5", "Chomping Trailing Lines", passed, total);
2422 RunSpecTest(&Spec::EmptyScalarChomping, "8.6", "Empty Scalar Chomping", passed, total);
2423 RunSpecTest(&Spec::LiteralScalar, "8.7", "Literal Scalar", passed, total);
2424 RunSpecTest(&Spec::LiteralContent, "8.8", "Literal Content", passed, total);
2425 RunSpecTest(&Spec::FoldedScalar, "8.9", "Folded Scalar", passed, total);
2426 RunSpecTest(&Spec::FoldedLines, "8.10", "Folded Lines", passed, total);
2427 RunSpecTest(&Spec::MoreIndentedLines, "8.11", "More Indented Lines", passed, total);
2428 RunSpecTest(&Spec::EmptySeparationLines, "8.12", "Empty Separation Lines", passed, total);
2429 RunSpecTest(&Spec::FinalEmptyLines, "8.13", "Final Empty Lines", passed, total);
2430 RunSpecTest(&Spec::BlockSequence, "8.14", "Block Sequence", passed, total);
2431 RunSpecTest(&Spec::BlockSequenceEntryTypes, "8.15", "Block Sequence Entry Types", passed, total);
2432 RunSpecTest(&Spec::BlockMappings, "8.16", "Block Mappings", passed, total);
2433 RunSpecTest(&Spec::ExplicitBlockMappingEntries, "8.17", "Explicit Block Mapping Entries", passed, total);
2434 RunSpecTest(&Spec::ImplicitBlockMappingEntries, "8.18", "Implicit Block Mapping Entries", passed, total);
2435 RunSpecTest(&Spec::CompactBlockMappings, "8.19", "Compact Block Mappings", passed, total);
2436 RunSpecTest(&Spec::BlockNodeTypes, "8.20", "Block Node Types", passed, total);
2437 RunSpecTest(&Spec::BlockScalarNodes, "8.21", "Block Scalar Nodes", passed, total);
2438 RunSpecTest(&Spec::BlockCollectionNodes, "8.22", "Block Collection Nodes", passed, total);
2440 std::cout << "Spec tests: " << passed << "/" << total << " passed\n";
2441 return passed == total;