2 #include "yaml-cpp/yaml.h"
11 TEST(bool ok_
): ok(ok_
) {}
12 TEST(const char *error_
): ok(false), error(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);\
24 parser.GetNextDocument(doc)
25 #define PARSE_NEXT(doc) parser.GetNextDocument(doc)
29 void RunSpecTest(TEST (*test
)(), const std::string
& index
, const std::string
& name
, int& passed
, int& total
) {
33 } catch(const YAML::Exception
& e
) {
35 ret
.error
= std::string(" Exception caught: ") + e
.what();
39 std::cout
<< "Spec test " << index
<< " failed: " << name
<< "\n";
40 std::cout
<< ret
.error
<< "\n";
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");
66 TEST
MappingScalarsToScalars() {
68 "hr: 65 # Home runs\n"
69 "avg: 0.278 # Batting average\n"
70 "rbi: 147 # Runs Batted In";
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");
81 TEST
MappingScalarsToSequences() {
86 "- New York Yankees\n"
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");
106 TEST
SequenceOfMappings()
110 " name: Mark McGwire\n"
114 " name: Sammy Sosa\n"
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");
132 TEST
SequenceOfSequences()
135 "- [name , hr, avg ]\n"
136 "- [Mark McGwire, 65, 0.278]\n"
137 "- [Sammy Sosa , 63, 0.288]";
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");
157 TEST
MappingOfMappings()
160 "Mark McGwire: {hr: 65, avg: 0.278}\n"
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");
178 TEST
TwoDocumentsInAStream()
181 "# Ranking of 1998 home runs\n"
190 "- St Louis Cardinals";
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");
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");
206 TEST
PlayByPlayFeed()
211 "player: Sammy Sosa\n"
212 "action: strike (miss)\n"
216 "player: Sammy Sosa\n"
217 "action: grand slam\n"
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)");
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");
235 TEST
SingleDocumentWithTwoComments()
239 "hr: # 1998 hr ranking\n"
243 " # 1998 rbi ranking\n"
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");
265 " # Following node labeled SS\n"
266 " - &SS Sammy Sosa\n"
268 " - *SS # Subsequent occurrence\n"
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");
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
) {
298 TEST
MappingBetweenSequences()
301 "? - Detroit Tigers\n"
306 "? [ New York Yankees,\n"
307 " Atlanta Braves ]\n"
308 ": [ 2001-07-02, 2001-08-12,\n"
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");
323 TEST
CompactNestedMapping()
327 "# Products purchased\n"
328 "- item : Super Hoop\n"
330 "- item : Basketball\n"
332 "- item : Big Shoes\n"
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);
350 TEST
InLiteralsNewlinesArePreserved()
359 YAML_ASSERT(doc
.to
<std::string
>() ==
366 TEST
InFoldedScalarsNewlinesBecomeSpaces()
371 " year was crippled\n"
372 " by a knee injury.";
375 YAML_ASSERT(doc
.to
<std::string
>() == "Mark McGwire's year was crippled by a knee injury.");
380 TEST
FoldedNewlinesArePreservedForMoreIndentedAndBlankLines()
384 " Sammy Sosa completed another\n"
385 " fine season with great stats.\n"
388 " 0.288 Batting Average\n"
393 YAML_ASSERT(doc
.to
<std::string
>() ==
394 "Sammy Sosa completed another fine season with great stats.\n\n"
396 " 0.288 Batting Average\n\n"
402 TEST
IndentationDeterminesScope()
405 "name: Mark McGwire\n"
406 "accomplishment: >\n"
407 " Mark set a major league\n"
408 " home run record in 1998.\n"
411 " 0.278 Batting Average\n";
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");
425 "unicode: \"Sosa did fine.\\u263A\"\n"
426 "control: \"\\b1998\\t1999\\t2000\\n\"\n"
427 "hex esc: \"\\x0d\\x0a is \\r\\n\"\n"
429 "single: '\"Howdy!\" he cried.'\n"
430 "quoted: ' # Not a ''comment''.'\n"
431 "tie-fighter: '|\\-*-/|'";
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
>() == "|\\-*-/|");
445 TEST
MultiLineFlowScalars()
449 " This unquoted scalar\n"
450 " spans many lines.\n"
452 "quoted: \"So does this\n"
453 " quoted scalar.\\n\"";
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");
462 // TODO: 2.19 - 2.22 schema tags
465 TEST
VariousExplicitTags()
469 "not-date: !!str 2002-04-28\n"
471 "picture: !!binary |\n"
472 " R0lGODlhDAAMAIQAAP//9/X\n"
473 " 17unp5WZmZgAAAOfn515eXv\n"
474 " Pz7Y6OjuDg4J+fn5OTk6enp\n"
475 " 56enmleECcgggoBADs=\n"
477 "application specific tag: !something |\n"
478 " The semantics of the tag\n"
479 " above may be different for\n"
480 " different documents.";
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."
506 "%TAG ! tag:clarkevans.com,2002:\n"
508 " # Use the ! handle for presenting\n"
509 " # tag:clarkevans.com,2002:circle\n"
511 " center: &ORIGIN {x: 73, y: 129}\n"
515 " finish: { x: 89, y: 102 }\n"
519 " text: Pretty vector drawing.";
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.");
552 "# Sets are represented as a\n"
553 "# Mapping where each key is\n"
554 "# associated with a null value\n"
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"]));
570 TEST
OrderedMappings()
573 "# Ordered maps are represented as\n"
574 "# A sequence of mappings, with\n"
575 "# each mapping having one key\n"
577 "- Mark McGwire: 65\n"
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);
597 "--- !<tag:clarkevans.com,2002:invoice>\n"
599 "date : 2001-01-23\n"
607 " city : Royal Oak\n"
614 " description : Basketball\n"
618 " description : Super Hoop\n"
623 " Late afternoon is best.\n"
624 " Backup contact is Nancy\n"
625 " Billsmer @ 338-4338.";
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.");
670 "Time: 2001-11-23 15:01:42 -5\n"
673 " This is an error message\n"
674 " for the log file\n"
676 "Time: 2001-11-23 15:02:31 -5\n"
679 " A slightly different error\n"
682 "Date: 2001-11-23 15:03:17 -5\n"
685 " Unknown variable \"bar\"\n"
687 " - file: TopClass.py\n"
690 " x = MoreObject(\"345\\n\")\n"
691 " - file: MoreClass.py\n"
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");
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.");
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");
725 // TODO: 5.1 - 5.2 BOM
728 TEST
BlockStructureIndicators()
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");
751 TEST
FlowStructureIndicators()
754 "sequence: [ one, two, ]\n"
755 "mapping: { sky: blue, sea: green }";
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");
769 TEST
CommentIndicator()
775 YAML_ASSERT(doc
.size() == 0);
780 TEST
NodePropertyIndicators()
783 "anchored: !local &anchor value\n"
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");
794 TEST
BlockScalarIndicators()
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");
812 TEST
QuotedScalarIndicators()
819 YAML_ASSERT(doc
.size() == 2);
820 YAML_ASSERT(doc
["single"].to
<std::string
>() == "text");
821 YAML_ASSERT(doc
["double"].to
<std::string
>() == "text");
825 // TODO: 5.9 directive
826 // TODO: 5.10 reserved indicator
829 TEST
LineBreakCharacters()
833 " Line break (no glyph)\n"
834 " Line break (glyphed)\n";
837 YAML_ASSERT(doc
.to
<std::string
>() == "Line break (no glyph)\nLine break (glyphed)\n");
845 "# Tabs and spaces\n"
846 "quoted: \"Quoted\t\"\n"
849 " \tprintf(\"Hello, world!\\n\");\n"
853 YAML_ASSERT(doc
.size() == 2);
854 YAML_ASSERT(doc
["quoted"].to
<std::string
>() == "Quoted\t");
855 YAML_ASSERT(doc
["block"].to
<std::string
>() ==
857 "\tprintf(\"Hello, world!\\n\");\n"
863 TEST
EscapedCharacters()
867 "\\\" \\a \\b \\e \\f \\\n"
868 "\\n \\r \\t \\v \\0 \\\n"
869 "\\ \\_ \\N \\L \\P \\\n"
870 "\\x41 \\u0041 \\U00000041\"";
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");
878 TEST
InvalidEscapedCharacters()
885 std::stringstream
stream(input
);
887 YAML::Parser
parser(stream
);
889 parser
.GetNextDocument(doc
);
890 } catch(const YAML::ParserException
& e
) {
891 YAML_ASSERT(e
.msg
== std::string(YAML::ErrorMsg::INVALID_ESCAPE
) + "c");
899 TEST
IndentationSpaces()
902 " # Leading comment line spaces are\n"
903 " # neither content nor indentation.\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"
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");
927 TEST
IndentationIndicators()
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");
946 TEST
SeparationSpaces()
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");
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");
990 " as a line feed\"\n"
992 " Clipped empty lines\n"
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");
1015 YAML_ASSERT(doc
.to
<std::string
>() == "trimmed\n\n\nas space");
1031 YAML_ASSERT(doc
.to
<std::string
>() == "foo \n\n\t bar\n\nbaz\n");
1048 YAML_ASSERT(doc
.to
<std::string
>() == " foo\nbar\nbaz ");
1053 TEST
SeparatedComment()
1060 YAML_ASSERT(doc
.size() == 1);
1061 YAML_ASSERT(doc
["key"].to
<std::string
>() == "value");
1074 YAML_ASSERT(doc
.size() == 0);
1079 TEST
MultiLineComments()
1088 YAML_ASSERT(doc
.size() == 1);
1089 YAML_ASSERT(doc
["key"].to
<std::string
>() == "value");
1094 typedef std::map
<std::string
, std::string
> Map
;
1098 bool operator == (const StringMap
& m
, const StringMap
& n
) {
1102 void operator >> (const YAML::Node
& node
, StringMap
& m
) {
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
>();
1113 TEST
SeparationSpacesII()
1116 "{ first: Sammy, last: Sosa }:\n"
1118 " hr: # Home runs\n"
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");
1135 TEST
ReservedDirectives()
1138 "%FOO bar baz # Should be ignored\n"
1139 " # with a warning.\n"
1147 TEST
YAMLDirective()
1150 "%YAML 1.3 # Attempt parsing\n"
1151 " # with a warning\n"
1160 TEST
InvalidRepeatedYAMLDirective()
1169 } catch(const YAML::ParserException
& e
) {
1170 if(e
.msg
== YAML::ErrorMsg::REPEATED_YAML_DIRECTIVE
)
1176 return " No exception was thrown";
1183 "%TAG !yaml! tag:yaml.org,2002:\n"
1185 "!yaml!str \"foo\"";
1188 YAML_ASSERT(doc
.Tag() == "tag:yaml.org,2002:str");
1189 YAML_ASSERT(doc
.to
<std::string
>() == "foo");
1194 TEST
InvalidRepeatedTagDirective()
1203 } catch(const YAML::ParserException
& e
) {
1204 if(e
.msg
== YAML::ErrorMsg::REPEATED_TAG_DIRECTIVE
)
1210 return " No exception was thrown";
1214 TEST
PrimaryTagHandle()
1221 "%TAG ! tag:example.com,2000:app/\n"
1226 YAML_ASSERT(doc
.Tag() == "!foo");
1227 YAML_ASSERT(doc
.to
<std::string
>() == "bar");
1230 YAML_ASSERT(doc
.Tag() == "tag:example.com,2000:app/foo");
1231 YAML_ASSERT(doc
.to
<std::string
>() == "bar");
1236 TEST
SecondaryTagHandle()
1239 "%TAG !! tag:example.com,2000:app/\n"
1241 "!!int 1 - 3 # Interval, not integer";
1244 YAML_ASSERT(doc
.Tag() == "tag:example.com,2000:app/int");
1245 YAML_ASSERT(doc
.to
<std::string
>() == "1 - 3");
1253 "%TAG !e! tag:example.com,2000:app/\n"
1258 YAML_ASSERT(doc
.Tag() == "tag:example.com,2000:app/foo");
1259 YAML_ASSERT(doc
.to
<std::string
>() == "bar");
1264 TEST
LocalTagPrefix()
1269 "!m!light fluorescent\n"
1272 "--- # Color here\n"
1276 YAML_ASSERT(doc
.Tag() == "!my-light");
1277 YAML_ASSERT(doc
.to
<std::string
>() == "fluorescent");
1280 YAML_ASSERT(doc
.Tag() == "!my-light");
1281 YAML_ASSERT(doc
.to
<std::string
>() == "green");
1286 TEST
GlobalTagPrefix()
1289 "%TAG !e! tag:example.com,2000:app/\n"
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");
1301 TEST
NodeProperties()
1304 "!!str &a1 \"foo\":\n"
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");
1318 return " unknown key";
1328 "!<tag:yaml.org,2002:str> foo :\n"
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");
1343 TEST
InvalidVerbatimTags()
1350 return " not implemented yet"; // TODO: check tags (but we probably will say these are valid, I think)
1354 TEST
TagShorthands()
1357 "%TAG !e! tag:example.com,2000:app/\n"
1361 "- !e!tag%21 baz\n";
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");
1375 TEST
InvalidTagShorthands()
1377 std::string input1
=
1378 "%TAG !e! tag:example,2000:app/\n"
1385 } catch(const YAML::ParserException
& e
) {
1387 if(e
.msg
!= YAML::ErrorMsg::TAG_WITH_NO_SUFFIX
)
1392 return " No exception was thrown for a tag with no suffix";
1394 std::string input2
=
1395 "%TAG !e! tag:example,2000:app/\n"
1399 PARSE(doc
, input2
); // TODO: should we reject this one (since !h! is not declared)?
1400 return " not implemented yet";
1404 TEST
NonSpecificTags()
1407 "# Assuming conventional resolution:\n"
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");
1424 "First occurrence: &anchor Value\n"
1425 "Second occurrence: *anchor";
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");
1438 "First occurrence: &anchor Foo\n"
1439 "Second occurrence: *anchor\n"
1440 "Override anchor: &anchor Bar\n"
1441 "Reuse anchor: *anchor";
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");
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");
1471 return " unexpected key";
1477 TEST
CompletelyEmptyNodes()
1486 YAML_ASSERT(doc
.size() == 2);
1487 YAML_ASSERT(IsNull(doc
["foo"]));
1488 YAML_ASSERT(doc
[YAML::Null
].to
<std::string
>() == "bar");
1493 TEST
DoubleQuotedImplicitKeys()
1496 "\"implicit block key\" : [\n"
1497 " \"implicit flow key\" : value,\n"
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");
1509 TEST
DoubleQuotedLineBreaks()
1515 "to a line feed, or \t\\\n"
1516 " \\ \tnon-content\"";
1519 YAML_ASSERT(doc
.to
<std::string
>() == "folded to a space,\nto a line feed, or \t \tnon-content");
1524 TEST
DoubleQuotedLines()
1527 "\" 1st non-empty\n"
1530 "\t3rd non-empty \"";
1533 YAML_ASSERT(doc
.to
<std::string
>() == " 1st non-empty\n2nd non-empty 3rd non-empty ");
1538 TEST
SingleQuotedCharacters()
1540 std::string input
= " 'here''s to \"quotes\"'";
1543 YAML_ASSERT(doc
.to
<std::string
>() == "here's to \"quotes\"");
1548 TEST
SingleQuotedImplicitKeys()
1551 "'implicit block key' : [\n"
1552 " 'implicit flow key' : value,\n"
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");
1564 TEST
SingleQuotedLines()
1570 "\t3rd non-empty '";
1573 YAML_ASSERT(doc
.to
<std::string
>() == " 1st non-empty\n2nd non-empty 3rd non-empty ");
1578 TEST
PlainCharacters()
1581 "# Outside flow collection:\n"
1584 "- Up, up, and away!\n"
1586 "- http://example.com/foo#bar\n"
1587 "# Inside flow collection:\n"
1590 " \"Up, up, and away!\",\n"
1592 " http://example.com/foo#bar ]";
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");
1611 TEST
PlainImplicitKeys()
1614 "implicit block key : [\n"
1615 " implicit flow key : value,\n"
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");
1636 YAML_ASSERT(doc
.to
<std::string
>() == "1st non-empty\n2nd non-empty 3rd non-empty");
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");
1659 TEST
FlowSequenceEntries()
1664 " quoted\", 'single\n"
1667 " text, [ nested ],\n"
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");
1687 "- { one : two , three: four , }\n"
1688 "- {five: six,seven : eight}";
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");
1702 TEST
FlowMappingEntries()
1706 "? explicit: entry,\n"
1707 "implicit: entry,\n"
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
]));
1720 TEST
FlowMappingSeparateValues()
1724 "unquoted : \"separate\",\n"
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");
1740 TEST
FlowMappingAdjacentValues()
1744 "\"adjacent\":value,\n"
1745 "\"readable\":value,\n"
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"]));
1758 TEST
SinglePairFlowMappings()
1766 YAML_ASSERT(doc
.size() == 1);
1767 YAML_ASSERT(doc
[0].size() == 1);
1768 YAML_ASSERT(doc
[0]["foo"].to
<std::string
>() == "bar");
1773 TEST
SinglePairExplicitEntry()
1782 YAML_ASSERT(doc
.size() == 1);
1783 YAML_ASSERT(doc
[0].size() == 1);
1784 YAML_ASSERT(doc
[0]["foo bar"].to
<std::string
>() == "baz");
1789 TEST
SinglePairImplicitEntries()
1792 "- [ YAML : separate ]\n"
1793 "- [ : empty key entry ]\n"
1794 "- [ {JSON: like}:adjacent ]";
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);
1807 key
._
["JSON"] = "like";
1808 YAML_ASSERT(doc
[2][0][key
].to
<std::string
>() == "adjacent");
1813 TEST
InvalidImplicitKeys()
1817 " bar: invalid,"; // Note: we don't check (on purpose) the >1K chars for an implicit key
1821 } catch(const YAML::Exception
& e
) {
1822 if(e
.msg
== YAML::ErrorMsg::END_OF_SEQ_FLOW
)
1827 return " no exception thrown";
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");
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
>() == "");
1876 TEST
BlockScalarHeader()
1879 "- | # Empty header\n"
1881 "- >1 # Indentation indicator\n"
1883 "- |+ # Chomping indicator\n"
1886 "- >1- # Both indicators\n"
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");
1899 TEST
BlockIndentationHeader()
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");
1924 TEST
InvalidBlockScalarIndentationIndicators()
1935 } catch(const YAML::Exception
& e
) {
1936 if(e
.msg
!= YAML::ErrorMsg::END_OF_SEQ
)
1943 return " no exception thrown for less indented auto-detecting indentation for a literal block scalar";
1955 } catch(const YAML::Exception
& e
) {
1956 if(e
.msg
!= YAML::ErrorMsg::END_OF_SEQ
)
1963 return " no exception thrown for less indented auto-detecting indentation for a folded block scalar";
1974 } catch(const YAML::Exception
& e
) {
1975 if(e
.msg
!= YAML::ErrorMsg::END_OF_SEQ
)
1982 return " no exception thrown for less indented explicit indentation for a literal block scalar";
1989 TEST
ChompingFinalLineBreak()
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");
2008 TEST
ChompingTrailingLines()
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");
2040 TEST
EmptyScalarChomping()
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");
2059 TEST
LiteralScalar()
2068 YAML_ASSERT(doc
.to
<std::string
>() == "literal\n\ttext\n");
2073 TEST
LiteralContent()
2087 YAML_ASSERT(doc
.to
<std::string
>() == "\n\nliteral\n \n\ntext\n");
2101 YAML_ASSERT(doc
.to
<std::string
>() == "folded text\n");
2127 YAML_ASSERT(doc
.to
<std::string
>() == "\nfolded line\nnext line\n * bullet\n\n * list\n * lines\n\nlast line\n");
2132 TEST
MoreIndentedLines()
2134 return true; // same as 8.10
2138 TEST
EmptySeparationLines()
2140 return true; // same as 8.10
2144 TEST
FinalEmptyLines()
2146 return true; // same as 8.10
2150 TEST
BlockSequence()
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");
2167 TEST
BlockSequenceEntryTypes()
2173 "- - one # Compact\n"
2174 " - two # sequence\n"
2175 "- one: two # Compact mapping\n";
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");
2190 TEST
BlockMappings()
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");
2204 TEST
ExplicitBlockMappingEntries()
2207 "? explicit key # Empty value\n"
2210 ": - one # Explicit compact\n"
2211 " - two # block value\n";
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");
2223 TEST
ImplicitBlockMappingEntries()
2226 "plain key: in-line value\n"
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");
2241 TEST
CompactBlockMappings()
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");
2261 TEST
BlockNodeTypes()
2265 " \"flow in block\"\n"
2268 "- !!map # Block collection\n"
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");
2281 TEST
BlockScalarNodes()
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");
2300 TEST
BlockCollectionNodes()
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");
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
;