2 #include "yaml-cpp/yaml.h"
7 ////////////////////////////////////////////////////////////////////////////////////////////////////////
10 void SimpleScalar(YAML::Emitter
& out
, std::string
& desiredOutput
) {
11 out
<< "Hello, World!";
12 desiredOutput
= "Hello, World!";
15 void SimpleSeq(YAML::Emitter
& out
, std::string
& desiredOutput
) {
16 out
<< YAML::BeginSeq
;
22 desiredOutput
= "- eggs\n- bread\n- milk";
25 void SimpleFlowSeq(YAML::Emitter
& out
, std::string
& desiredOutput
) {
27 out
<< YAML::BeginSeq
;
33 desiredOutput
= "[Larry, Curly, Moe]";
36 void EmptyFlowSeq(YAML::Emitter
& out
, std::string
& desiredOutput
) {
38 out
<< YAML::BeginSeq
;
44 void NestedBlockSeq(YAML::Emitter
& out
, std::string
& desiredOutput
) {
45 out
<< YAML::BeginSeq
;
47 out
<< YAML::BeginSeq
<< "subitem 1" << "subitem 2" << YAML::EndSeq
;
50 desiredOutput
= "- item 1\n-\n - subitem 1\n - subitem 2";
53 void NestedFlowSeq(YAML::Emitter
& out
, std::string
& desiredOutput
) {
54 out
<< YAML::BeginSeq
;
56 out
<< YAML::Flow
<< YAML::BeginSeq
<< "two" << "three" << YAML::EndSeq
;
59 desiredOutput
= "- one\n- [two, three]";
62 void SimpleMap(YAML::Emitter
& out
, std::string
& desiredOutput
) {
63 out
<< YAML::BeginMap
;
64 out
<< YAML::Key
<< "name";
65 out
<< YAML::Value
<< "Ryan Braun";
66 out
<< YAML::Key
<< "position";
67 out
<< YAML::Value
<< "3B";
70 desiredOutput
= "name: Ryan Braun\nposition: 3B";
73 void SimpleFlowMap(YAML::Emitter
& out
, std::string
& desiredOutput
) {
75 out
<< YAML::BeginMap
;
76 out
<< YAML::Key
<< "shape";
77 out
<< YAML::Value
<< "square";
78 out
<< YAML::Key
<< "color";
79 out
<< YAML::Value
<< "blue";
82 desiredOutput
= "{shape: square, color: blue}";
85 void MapAndList(YAML::Emitter
& out
, std::string
& desiredOutput
) {
86 out
<< YAML::BeginMap
;
87 out
<< YAML::Key
<< "name";
88 out
<< YAML::Value
<< "Barack Obama";
89 out
<< YAML::Key
<< "children";
90 out
<< YAML::Value
<< YAML::BeginSeq
<< "Sasha" << "Malia" << YAML::EndSeq
;
93 desiredOutput
= "name: Barack Obama\nchildren:\n - Sasha\n - Malia";
96 void ListAndMap(YAML::Emitter
& out
, std::string
& desiredOutput
) {
97 out
<< YAML::BeginSeq
;
99 out
<< YAML::BeginMap
;
100 out
<< YAML::Key
<< "pens" << YAML::Value
<< 8;
101 out
<< YAML::Key
<< "pencils" << YAML::Value
<< 14;
106 desiredOutput
= "- item 1\n- pens: 8\n pencils: 14\n- item 2";
109 void NestedBlockMap(YAML::Emitter
& out
, std::string
& desiredOutput
) {
110 out
<< YAML::BeginMap
;
111 out
<< YAML::Key
<< "name";
112 out
<< YAML::Value
<< "Fred";
113 out
<< YAML::Key
<< "grades";
115 out
<< YAML::BeginMap
;
116 out
<< YAML::Key
<< "algebra" << YAML::Value
<< "A";
117 out
<< YAML::Key
<< "physics" << YAML::Value
<< "C+";
118 out
<< YAML::Key
<< "literature" << YAML::Value
<< "B";
122 desiredOutput
= "name: Fred\ngrades:\n algebra: A\n physics: C+\n literature: B";
125 void NestedFlowMap(YAML::Emitter
& out
, std::string
& desiredOutput
) {
127 out
<< YAML::BeginMap
;
128 out
<< YAML::Key
<< "name";
129 out
<< YAML::Value
<< "Fred";
130 out
<< YAML::Key
<< "grades";
132 out
<< YAML::BeginMap
;
133 out
<< YAML::Key
<< "algebra" << YAML::Value
<< "A";
134 out
<< YAML::Key
<< "physics" << YAML::Value
<< "C+";
135 out
<< YAML::Key
<< "literature" << YAML::Value
<< "B";
139 desiredOutput
= "{name: Fred, grades: {algebra: A, physics: C+, literature: B}}";
142 void MapListMix(YAML::Emitter
& out
, std::string
& desiredOutput
) {
143 out
<< YAML::BeginMap
;
144 out
<< YAML::Key
<< "name";
145 out
<< YAML::Value
<< "Bob";
146 out
<< YAML::Key
<< "position";
148 out
<< YAML::Flow
<< YAML::BeginSeq
<< 2 << 4 << YAML::EndSeq
;
149 out
<< YAML::Key
<< "invincible" << YAML::Value
<< YAML::OnOffBool
<< false;
152 desiredOutput
= "name: Bob\nposition: [2, 4]\ninvincible: off";
155 void SimpleLongKey(YAML::Emitter
& out
, std::string
& desiredOutput
)
157 out
<< YAML::LongKey
;
158 out
<< YAML::BeginMap
;
159 out
<< YAML::Key
<< "height";
160 out
<< YAML::Value
<< "5'9\"";
161 out
<< YAML::Key
<< "weight";
162 out
<< YAML::Value
<< 145;
165 desiredOutput
= "? height\n: 5'9\"\n? weight\n: 145";
168 void SingleLongKey(YAML::Emitter
& out
, std::string
& desiredOutput
)
170 out
<< YAML::BeginMap
;
171 out
<< YAML::Key
<< "age";
172 out
<< YAML::Value
<< "24";
173 out
<< YAML::LongKey
<< YAML::Key
<< "height";
174 out
<< YAML::Value
<< "5'9\"";
175 out
<< YAML::Key
<< "weight";
176 out
<< YAML::Value
<< 145;
179 desiredOutput
= "age: 24\n? height\n: 5'9\"\nweight: 145";
182 void ComplexLongKey(YAML::Emitter
& out
, std::string
& desiredOutput
)
184 out
<< YAML::LongKey
;
185 out
<< YAML::BeginMap
;
186 out
<< YAML::Key
<< YAML::BeginSeq
<< 1 << 3 << YAML::EndSeq
;
187 out
<< YAML::Value
<< "monster";
188 out
<< YAML::Key
<< YAML::Flow
<< YAML::BeginSeq
<< 2 << 0 << YAML::EndSeq
;
189 out
<< YAML::Value
<< "demon";
192 desiredOutput
= "?\n - 1\n - 3\n: monster\n? [2, 0]\n: demon";
195 void AutoLongKey(YAML::Emitter
& out
, std::string
& desiredOutput
)
197 out
<< YAML::BeginMap
;
198 out
<< YAML::Key
<< YAML::BeginSeq
<< 1 << 3 << YAML::EndSeq
;
199 out
<< YAML::Value
<< "monster";
200 out
<< YAML::Key
<< YAML::Flow
<< YAML::BeginSeq
<< 2 << 0 << YAML::EndSeq
;
201 out
<< YAML::Value
<< "demon";
202 out
<< YAML::Key
<< "the origin";
203 out
<< YAML::Value
<< "angel";
206 desiredOutput
= "?\n - 1\n - 3\n: monster\n? [2, 0]\n: demon\nthe origin: angel";
209 void ScalarFormat(YAML::Emitter
& out
, std::string
& desiredOutput
)
211 out
<< YAML::BeginSeq
;
212 out
<< "simple scalar";
213 out
<< YAML::SingleQuoted
<< "explicit single-quoted scalar";
214 out
<< YAML::DoubleQuoted
<< "explicit double-quoted scalar";
215 out
<< "auto-detected\ndouble-quoted scalar";
216 out
<< "a non-\"auto-detected\" double-quoted scalar";
217 out
<< YAML::Literal
<< "literal scalar\nthat may span\nmany, many\nlines and have \"whatever\" crazy\tsymbols that we like";
220 desiredOutput
= "- simple scalar\n- 'explicit single-quoted scalar'\n- \"explicit double-quoted scalar\"\n- \"auto-detected\\x0adouble-quoted scalar\"\n- a non-\"auto-detected\" double-quoted scalar\n- |\n literal scalar\n that may span\n many, many\n lines and have \"whatever\" crazy\tsymbols that we like";
223 void AutoLongKeyScalar(YAML::Emitter
& out
, std::string
& desiredOutput
)
225 out
<< YAML::BeginMap
;
226 out
<< YAML::Key
<< YAML::Literal
<< "multi-line\nscalar";
227 out
<< YAML::Value
<< "and its value";
230 desiredOutput
= "? |\n multi-line\n scalar\n: and its value";
233 void LongKeyFlowMap(YAML::Emitter
& out
, std::string
& desiredOutput
)
236 out
<< YAML::BeginMap
;
237 out
<< YAML::Key
<< "simple key";
238 out
<< YAML::Value
<< "and value";
239 out
<< YAML::LongKey
<< YAML::Key
<< "long key";
240 out
<< YAML::Value
<< "and its value";
243 desiredOutput
= "{simple key: and value, ? long key: and its value}";
246 void BlockMapAsKey(YAML::Emitter
& out
, std::string
& desiredOutput
)
248 out
<< YAML::BeginMap
;
250 out
<< YAML::BeginMap
;
251 out
<< YAML::Key
<< "key" << YAML::Value
<< "value";
252 out
<< YAML::Key
<< "next key" << YAML::Value
<< "next value";
255 out
<< "total value";
258 desiredOutput
= "?\n key: value\n next key: next value\n: total value";
261 void AliasAndAnchor(YAML::Emitter
& out
, std::string
& desiredOutput
)
263 out
<< YAML::BeginSeq
;
264 out
<< YAML::Anchor("fred");
265 out
<< YAML::BeginMap
;
266 out
<< YAML::Key
<< "name" << YAML::Value
<< "Fred";
267 out
<< YAML::Key
<< "age" << YAML::Value
<< 42;
269 out
<< YAML::Alias("fred");
272 desiredOutput
= "- &fred\n name: Fred\n age: 42\n- *fred";
275 void AliasAndAnchorWithNull(YAML::Emitter
& out
, std::string
& desiredOutput
)
277 out
<< YAML::BeginSeq
;
278 out
<< YAML::Anchor("fred") << YAML::Null
;
279 out
<< YAML::Alias("fred");
282 desiredOutput
= "- &fred ~\n- *fred";
285 void SimpleVerbatimTag(YAML::Emitter
& out
, std::string
& desiredOutput
)
287 out
<< YAML::VerbatimTag("!foo") << "bar";
289 desiredOutput
= "!<!foo> bar";
292 void VerbatimTagInBlockSeq(YAML::Emitter
& out
, std::string
& desiredOutput
)
294 out
<< YAML::BeginSeq
;
295 out
<< YAML::VerbatimTag("!foo") << "bar";
299 desiredOutput
= "- !<!foo> bar\n- baz";
302 void VerbatimTagInFlowSeq(YAML::Emitter
& out
, std::string
& desiredOutput
)
304 out
<< YAML::Flow
<< YAML::BeginSeq
;
305 out
<< YAML::VerbatimTag("!foo") << "bar";
309 desiredOutput
= "[!<!foo> bar, baz]";
312 void VerbatimTagInFlowSeqWithNull(YAML::Emitter
& out
, std::string
& desiredOutput
)
314 out
<< YAML::Flow
<< YAML::BeginSeq
;
315 out
<< YAML::VerbatimTag("!foo") << YAML::Null
;
319 desiredOutput
= "[!<!foo> ~, baz]";
322 void VerbatimTagInBlockMap(YAML::Emitter
& out
, std::string
& desiredOutput
)
324 out
<< YAML::BeginMap
;
325 out
<< YAML::Key
<< YAML::VerbatimTag("!foo") << "bar";
326 out
<< YAML::Value
<< YAML::VerbatimTag("!waz") << "baz";
329 desiredOutput
= "!<!foo> bar: !<!waz> baz";
332 void VerbatimTagInFlowMap(YAML::Emitter
& out
, std::string
& desiredOutput
)
334 out
<< YAML::Flow
<< YAML::BeginMap
;
335 out
<< YAML::Key
<< YAML::VerbatimTag("!foo") << "bar";
336 out
<< YAML::Value
<< "baz";
339 desiredOutput
= "{!<!foo> bar: baz}";
342 void VerbatimTagInFlowMapWithNull(YAML::Emitter
& out
, std::string
& desiredOutput
)
344 out
<< YAML::Flow
<< YAML::BeginMap
;
345 out
<< YAML::Key
<< YAML::VerbatimTag("!foo") << YAML::Null
;
346 out
<< YAML::Value
<< "baz";
349 desiredOutput
= "{!<!foo> ~: baz}";
352 void VerbatimTagWithEmptySeq(YAML::Emitter
& out
, std::string
& desiredOutput
)
354 out
<< YAML::VerbatimTag("!foo") << YAML::BeginSeq
<< YAML::EndSeq
;
356 desiredOutput
= "!<!foo>\n[]";
359 void VerbatimTagWithEmptyMap(YAML::Emitter
& out
, std::string
& desiredOutput
)
361 out
<< YAML::VerbatimTag("!bar") << YAML::BeginMap
<< YAML::EndMap
;
363 desiredOutput
= "!<!bar>\n{}";
366 void VerbatimTagWithEmptySeqAndMap(YAML::Emitter
& out
, std::string
& desiredOutput
)
368 out
<< YAML::BeginSeq
;
369 out
<< YAML::VerbatimTag("!foo") << YAML::BeginSeq
<< YAML::EndSeq
;
370 out
<< YAML::VerbatimTag("!bar") << YAML::BeginMap
<< YAML::EndMap
;
373 desiredOutput
= "- !<!foo>\n []\n- !<!bar>\n {}";
376 void ByKindTagWithScalar(YAML::Emitter
& out
, std::string
& desiredOutput
)
378 out
<< YAML::BeginSeq
;
379 out
<< YAML::DoubleQuoted
<< "12";
381 out
<< YAML::TagByKind
<< "12";
384 desiredOutput
= "- \"12\"\n- 12\n- ! 12";
387 void LocalTagWithScalar(YAML::Emitter
& out
, std::string
& desiredOutput
)
389 out
<< YAML::LocalTag("foo") << "bar";
391 desiredOutput
= "!foo bar";
394 void BadLocalTag(YAML::Emitter
& out
, std::string
& desiredError
)
396 out
<< YAML::LocalTag("e!far") << "bar";
398 desiredError
= "invalid tag";
401 void ComplexDoc(YAML::Emitter
& out
, std::string
& desiredOutput
)
403 out
<< YAML::BeginMap
;
404 out
<< YAML::Key
<< "receipt";
405 out
<< YAML::Value
<< "Oz-Ware Purchase Invoice";
406 out
<< YAML::Key
<< "date";
407 out
<< YAML::Value
<< "2007-08-06";
408 out
<< YAML::Key
<< "customer";
410 out
<< YAML::BeginMap
;
411 out
<< YAML::Key
<< "given";
412 out
<< YAML::Value
<< "Dorothy";
413 out
<< YAML::Key
<< "family";
414 out
<< YAML::Value
<< "Gale";
416 out
<< YAML::Key
<< "items";
418 out
<< YAML::BeginSeq
;
419 out
<< YAML::BeginMap
;
420 out
<< YAML::Key
<< "part_no";
421 out
<< YAML::Value
<< "A4786";
422 out
<< YAML::Key
<< "descrip";
423 out
<< YAML::Value
<< "Water Bucket (Filled)";
424 out
<< YAML::Key
<< "price";
425 out
<< YAML::Value
<< 1.47;
426 out
<< YAML::Key
<< "quantity";
427 out
<< YAML::Value
<< 4;
429 out
<< YAML::BeginMap
;
430 out
<< YAML::Key
<< "part_no";
431 out
<< YAML::Value
<< "E1628";
432 out
<< YAML::Key
<< "descrip";
433 out
<< YAML::Value
<< "High Heeled \"Ruby\" Slippers";
434 out
<< YAML::Key
<< "price";
435 out
<< YAML::Value
<< 100.27;
436 out
<< YAML::Key
<< "quantity";
437 out
<< YAML::Value
<< 1;
440 out
<< YAML::Key
<< "bill-to";
441 out
<< YAML::Value
<< YAML::Anchor("id001");
442 out
<< YAML::BeginMap
;
443 out
<< YAML::Key
<< "street";
444 out
<< YAML::Value
<< YAML::Literal
<< "123 Tornado Alley\nSuite 16";
445 out
<< YAML::Key
<< "city";
446 out
<< YAML::Value
<< "East Westville";
447 out
<< YAML::Key
<< "state";
448 out
<< YAML::Value
<< "KS";
450 out
<< YAML::Key
<< "ship-to";
451 out
<< YAML::Value
<< YAML::Alias("id001");
454 desiredOutput
= "receipt: Oz-Ware Purchase Invoice\ndate: 2007-08-06\ncustomer:\n given: Dorothy\n family: Gale\nitems:\n - part_no: A4786\n descrip: Water Bucket (Filled)\n price: 1.47\n quantity: 4\n - part_no: E1628\n descrip: High Heeled \"Ruby\" Slippers\n price: 100.27\n quantity: 1\nbill-to: &id001\n street: |\n 123 Tornado Alley\n Suite 16\n city: East Westville\n state: KS\nship-to: *id001";
457 void STLContainers(YAML::Emitter
& out
, std::string
& desiredOutput
)
459 out
<< YAML::BeginSeq
;
460 std::vector
<int> primes
;
465 primes
.push_back(11);
466 primes
.push_back(13);
467 out
<< YAML::Flow
<< primes
;
468 std::map
<std::string
, int> ages
;
474 desiredOutput
= "- [2, 3, 5, 7, 11, 13]\n- Daniel: 26\n Jesse: 24";
477 void SimpleComment(YAML::Emitter
& out
, std::string
& desiredOutput
)
479 out
<< YAML::BeginMap
;
480 out
<< YAML::Key
<< "method";
481 out
<< YAML::Value
<< "least squares" << YAML::Comment("should we change this method?");
484 desiredOutput
= "method: least squares # should we change this method?";
487 void MultiLineComment(YAML::Emitter
& out
, std::string
& desiredOutput
)
489 out
<< YAML::BeginSeq
;
490 out
<< "item 1" << YAML::Comment("really really long\ncomment that couldn't possibly\nfit on one line");
494 desiredOutput
= "- item 1 # really really long\n # comment that couldn't possibly\n # fit on one line\n- item 2";
497 void ComplexComments(YAML::Emitter
& out
, std::string
& desiredOutput
)
499 out
<< YAML::BeginMap
;
500 out
<< YAML::LongKey
<< YAML::Key
<< "long key" << YAML::Comment("long key");
501 out
<< YAML::Value
<< "value";
504 desiredOutput
= "? long key # long key\n: value";
507 void Indentation(YAML::Emitter
& out
, std::string
& desiredOutput
)
509 out
<< YAML::Indent(4);
510 out
<< YAML::BeginSeq
;
511 out
<< YAML::BeginMap
;
512 out
<< YAML::Key
<< "key 1" << YAML::Value
<< "value 1";
513 out
<< YAML::Key
<< "key 2" << YAML::Value
<< YAML::BeginSeq
<< "a" << "b" << "c" << YAML::EndSeq
;
517 desiredOutput
= "- key 1: value 1\n key 2:\n - a\n - b\n - c";
520 void SimpleGlobalSettings(YAML::Emitter
& out
, std::string
& desiredOutput
)
523 out
.SetMapFormat(YAML::LongKey
);
525 out
<< YAML::BeginSeq
;
526 out
<< YAML::BeginMap
;
527 out
<< YAML::Key
<< "key 1" << YAML::Value
<< "value 1";
528 out
<< YAML::Key
<< "key 2" << YAML::Value
<< YAML::Flow
<< YAML::BeginSeq
<< "a" << "b" << "c" << YAML::EndSeq
;
532 desiredOutput
= "- ? key 1\n : value 1\n ? key 2\n : [a, b, c]";
535 void ComplexGlobalSettings(YAML::Emitter
& out
, std::string
& desiredOutput
)
537 out
<< YAML::BeginSeq
;
539 out
<< YAML::BeginMap
;
540 out
<< YAML::Key
<< "key 1" << YAML::Value
<< "value 1";
541 out
<< YAML::Key
<< "key 2" << YAML::Value
;
542 out
.SetSeqFormat(YAML::Flow
);
543 out
<< YAML::BeginSeq
<< "a" << "b" << "c" << YAML::EndSeq
;
545 out
<< YAML::BeginMap
;
546 out
<< YAML::Key
<< YAML::BeginSeq
<< 1 << 2 << YAML::EndSeq
;
547 out
<< YAML::Value
<< YAML::BeginMap
<< YAML::Key
<< "a" << YAML::Value
<< "b" << YAML::EndMap
;
551 desiredOutput
= "- key 1: value 1\n key 2: [a, b, c]\n- ? [1, 2]\n :\n a: b";
554 void Null(YAML::Emitter
& out
, std::string
& desiredOutput
)
556 out
<< YAML::BeginSeq
;
558 out
<< YAML::BeginMap
;
559 out
<< YAML::Key
<< "null value" << YAML::Value
<< YAML::Null
;
560 out
<< YAML::Key
<< YAML::Null
<< YAML::Value
<< "null key";
564 desiredOutput
= "- ~\n- null value: ~\n ~: null key";
567 void EscapedUnicode(YAML::Emitter
& out
, std::string
& desiredOutput
)
569 out
<< YAML::EscapeNonAscii
<< "\x24 \xC2\xA2 \xE2\x82\xAC \xF0\xA4\xAD\xA2";
571 desiredOutput
= "\"$ \\xa2 \\u20ac \\U00024b62\"";
574 void Unicode(YAML::Emitter
& out
, std::string
& desiredOutput
)
576 out
<< "\x24 \xC2\xA2 \xE2\x82\xAC \xF0\xA4\xAD\xA2";
577 desiredOutput
= "\x24 \xC2\xA2 \xE2\x82\xAC \xF0\xA4\xAD\xA2";
580 void DoubleQuotedUnicode(YAML::Emitter
& out
, std::string
& desiredOutput
)
582 out
<< YAML::DoubleQuoted
<< "\x24 \xC2\xA2 \xE2\x82\xAC \xF0\xA4\xAD\xA2";
583 desiredOutput
= "\"\x24 \xC2\xA2 \xE2\x82\xAC \xF0\xA4\xAD\xA2\"";
588 Foo(int x_
, const std::string
& bar_
): x(x_
), bar(bar_
) {}
594 YAML::Emitter
& operator << (YAML::Emitter
& out
, const Foo
& foo
) {
595 out
<< YAML::BeginMap
;
596 out
<< YAML::Key
<< "x" << YAML::Value
<< foo
.x
;
597 out
<< YAML::Key
<< "bar" << YAML::Value
<< foo
.bar
;
602 void UserType(YAML::Emitter
& out
, std::string
& desiredOutput
)
604 out
<< YAML::BeginSeq
;
605 out
<< Foo(5, "hello");
606 out
<< Foo(3, "goodbye");
609 desiredOutput
= "- x: 5\n bar: hello\n- x: 3\n bar: goodbye";
612 void UserTypeInContainer(YAML::Emitter
& out
, std::string
& desiredOutput
)
615 fv
.push_back(Foo(5, "hello"));
616 fv
.push_back(Foo(3, "goodbye"));
619 desiredOutput
= "- x: 5\n bar: hello\n- x: 3\n bar: goodbye";
622 template <typename T
>
623 YAML::Emitter
& operator << (YAML::Emitter
& out
, const T
*v
) {
631 void PointerToInt(YAML::Emitter
& out
, std::string
& desiredOutput
)
636 out
<< YAML::BeginSeq
;
640 desiredOutput
= "- 5\n- ~";
643 void PointerToUserType(YAML::Emitter
& out
, std::string
& desiredOutput
)
648 out
<< YAML::BeginSeq
;
652 desiredOutput
= "- x: 5\n bar: hello\n- ~";
655 void NewlineAtEnd(YAML::Emitter
& out
, std::string
& desiredOutput
)
657 out
<< "Hello" << YAML::Newline
<< YAML::Newline
;
658 desiredOutput
= "Hello\n\n";
661 void NewlineInBlockSequence(YAML::Emitter
& out
, std::string
& desiredOutput
)
663 out
<< YAML::BeginSeq
;
664 out
<< "a" << YAML::Newline
<< "b" << "c" << YAML::Newline
<< "d";
666 desiredOutput
= "- a\n\n- b\n- c\n\n- d";
669 void NewlineInFlowSequence(YAML::Emitter
& out
, std::string
& desiredOutput
)
671 out
<< YAML::Flow
<< YAML::BeginSeq
;
672 out
<< "a" << YAML::Newline
<< "b" << "c" << YAML::Newline
<< "d";
674 desiredOutput
= "[a\n, b, c\n, d]";
677 void NewlineInBlockMap(YAML::Emitter
& out
, std::string
& desiredOutput
)
679 out
<< YAML::BeginMap
;
680 out
<< YAML::Key
<< "a" << YAML::Value
<< "foo" << YAML::Newline
;
681 out
<< YAML::Key
<< "b" << YAML::Newline
<< YAML::Value
<< "bar";
682 out
<< YAML::LongKey
<< YAML::Key
<< "c" << YAML::Newline
<< YAML::Value
<< "car";
684 desiredOutput
= "a: foo\n\nb: bar\n? c\n\n: car";
687 void NewlineInFlowMap(YAML::Emitter
& out
, std::string
& desiredOutput
)
689 out
<< YAML::Flow
<< YAML::BeginMap
;
690 out
<< YAML::Key
<< "a" << YAML::Value
<< "foo" << YAML::Newline
;
691 out
<< YAML::Key
<< "b" << YAML::Newline
<< YAML::Value
<< "bar";
693 desiredOutput
= "{a: foo\n, b\n: bar}";
696 void LotsOfNewlines(YAML::Emitter
& out
, std::string
& desiredOutput
)
698 out
<< YAML::BeginSeq
;
699 out
<< "a" << YAML::Newline
;
700 out
<< YAML::BeginSeq
;
701 out
<< "b" << "c" << YAML::Newline
;
703 out
<< YAML::Newline
;
704 out
<< YAML::BeginMap
;
705 out
<< YAML::Newline
<< YAML::Key
<< "d" << YAML::Value
<< YAML::Newline
<< "e";
706 out
<< YAML::LongKey
<< YAML::Key
<< "f" << YAML::Newline
<< YAML::Value
<< "foo";
709 desiredOutput
= "- a\n\n-\n - b\n - c\n\n\n-\n d: e\n ? f\n\n : foo";
712 void Binary(YAML::Emitter
& out
, std::string
& desiredOutput
)
714 out
<< YAML::Binary("Hello, World!", 13);
715 desiredOutput
= "!!binary \"SGVsbG8sIFdvcmxkIQ==\"";
718 void LongBinary(YAML::Emitter
& out
, std::string
& desiredOutput
)
720 out
<< YAML::Binary("Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.\n", 270);
721 desiredOutput
= "!!binary \"TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4K\"";
724 void EmptyBinary(YAML::Emitter
& out
, std::string
& desiredOutput
)
726 out
<< YAML::Binary("", 0);
727 desiredOutput
= "!!binary \"\"";
730 void ColonAtEndOfScalar(YAML::Emitter
& out
, std::string
& desiredOutput
)
733 desiredOutput
= "\"a:\"";
736 void ColonAsScalar(YAML::Emitter
& out
, std::string
& desiredOutput
)
738 out
<< YAML::BeginMap
;
739 out
<< YAML::Key
<< "apple" << YAML::Value
<< ":";
740 out
<< YAML::Key
<< "banana" << YAML::Value
<< ":";
742 desiredOutput
= "apple: \":\"\nbanana: \":\"";
745 void BoolFormatting(YAML::Emitter
& out
, std::string
& desiredOutput
)
747 out
<< YAML::BeginSeq
;
748 out
<< YAML::TrueFalseBool
<< YAML::UpperCase
<< true;
749 out
<< YAML::TrueFalseBool
<< YAML::CamelCase
<< true;
750 out
<< YAML::TrueFalseBool
<< YAML::LowerCase
<< true;
751 out
<< YAML::TrueFalseBool
<< YAML::UpperCase
<< false;
752 out
<< YAML::TrueFalseBool
<< YAML::CamelCase
<< false;
753 out
<< YAML::TrueFalseBool
<< YAML::LowerCase
<< false;
754 out
<< YAML::YesNoBool
<< YAML::UpperCase
<< true;
755 out
<< YAML::YesNoBool
<< YAML::CamelCase
<< true;
756 out
<< YAML::YesNoBool
<< YAML::LowerCase
<< true;
757 out
<< YAML::YesNoBool
<< YAML::UpperCase
<< false;
758 out
<< YAML::YesNoBool
<< YAML::CamelCase
<< false;
759 out
<< YAML::YesNoBool
<< YAML::LowerCase
<< false;
760 out
<< YAML::OnOffBool
<< YAML::UpperCase
<< true;
761 out
<< YAML::OnOffBool
<< YAML::CamelCase
<< true;
762 out
<< YAML::OnOffBool
<< YAML::LowerCase
<< true;
763 out
<< YAML::OnOffBool
<< YAML::UpperCase
<< false;
764 out
<< YAML::OnOffBool
<< YAML::CamelCase
<< false;
765 out
<< YAML::OnOffBool
<< YAML::LowerCase
<< false;
766 out
<< YAML::ShortBool
<< YAML::UpperCase
<< true;
767 out
<< YAML::ShortBool
<< YAML::CamelCase
<< true;
768 out
<< YAML::ShortBool
<< YAML::LowerCase
<< true;
769 out
<< YAML::ShortBool
<< YAML::UpperCase
<< false;
770 out
<< YAML::ShortBool
<< YAML::CamelCase
<< false;
771 out
<< YAML::ShortBool
<< YAML::LowerCase
<< false;
774 "- TRUE\n- True\n- true\n- FALSE\n- False\n- false\n"
775 "- YES\n- Yes\n- yes\n- NO\n- No\n- no\n"
776 "- ON\n- On\n- on\n- OFF\n- Off\n- off\n"
777 "- Y\n- Y\n- y\n- N\n- N\n- n";
780 void DocStartAndEnd(YAML::Emitter
& out
, std::string
& desiredOutput
)
782 out
<< YAML::BeginDoc
;
783 out
<< YAML::BeginSeq
<< 1 << 2 << 3 << YAML::EndSeq
;
784 out
<< YAML::BeginDoc
;
789 out
<< YAML::BeginDoc
;
790 out
<< YAML::VerbatimTag("foo") << "bar";
791 desiredOutput
= "---\n- 1\n- 2\n- 3\n---\nHi there!\n...\n...\n...\n---\n!<foo> bar";
794 void ImplicitDocStart(YAML::Emitter
& out
, std::string
& desiredOutput
)
799 desiredOutput
= "Hi\n---\nBye\n---\nOops";
802 ////////////////////////////////////////////////////////////////////////////////////////////////////////
803 // incorrect emitting
805 void ExtraEndSeq(YAML::Emitter
& out
, std::string
& desiredError
)
807 desiredError
= YAML::ErrorMsg::UNEXPECTED_END_SEQ
;
809 out
<< YAML::BeginSeq
;
816 void ExtraEndMap(YAML::Emitter
& out
, std::string
& desiredError
)
818 desiredError
= YAML::ErrorMsg::UNEXPECTED_END_MAP
;
820 out
<< YAML::BeginMap
;
821 out
<< YAML::Key
<< "Hello" << YAML::Value
<< "World";
826 void BadSingleQuoted(YAML::Emitter
& out
, std::string
& desiredError
)
828 desiredError
= YAML::ErrorMsg::SINGLE_QUOTED_CHAR
;
830 out
<< YAML::SingleQuoted
<< "Hello\nWorld";
833 void InvalidAnchor(YAML::Emitter
& out
, std::string
& desiredError
)
835 desiredError
= YAML::ErrorMsg::INVALID_ANCHOR
;
837 out
<< YAML::BeginSeq
;
838 out
<< YAML::Anchor("new\nline") << "Test";
842 void InvalidAlias(YAML::Emitter
& out
, std::string
& desiredError
)
844 desiredError
= YAML::ErrorMsg::INVALID_ALIAS
;
846 out
<< YAML::BeginSeq
;
847 out
<< YAML::Alias("new\nline");
851 void MissingKey(YAML::Emitter
& out
, std::string
& desiredError
)
853 desiredError
= YAML::ErrorMsg::EXPECTED_KEY_TOKEN
;
855 out
<< YAML::BeginMap
;
856 out
<< YAML::Key
<< "key" << YAML::Value
<< "value";
857 out
<< "missing key" << YAML::Value
<< "value";
861 void MissingValue(YAML::Emitter
& out
, std::string
& desiredError
)
863 desiredError
= YAML::ErrorMsg::EXPECTED_VALUE_TOKEN
;
865 out
<< YAML::BeginMap
;
866 out
<< YAML::Key
<< "key" << "value";
870 void UnexpectedKey(YAML::Emitter
& out
, std::string
& desiredError
)
872 desiredError
= YAML::ErrorMsg::UNEXPECTED_KEY_TOKEN
;
874 out
<< YAML::BeginSeq
;
875 out
<< YAML::Key
<< "hi";
879 void UnexpectedValue(YAML::Emitter
& out
, std::string
& desiredError
)
881 desiredError
= YAML::ErrorMsg::UNEXPECTED_VALUE_TOKEN
;
883 out
<< YAML::BeginSeq
;
884 out
<< YAML::Value
<< "hi";
890 void RunEmitterTest(void (*test
)(YAML::Emitter
&, std::string
&), const std::string
& name
, int& passed
, int& total
) {
892 std::string desiredOutput
;
893 test(out
, desiredOutput
);
894 std::string output
= out
.c_str();
895 std::string lastError
= out
.GetLastError();
897 if(output
== desiredOutput
) {
899 std::stringstream
stream(output
);
902 parser
.GetNextDocument(node
);
904 } catch(const YAML::Exception
& e
) {
905 std::cout
<< "Emitter test failed: " << name
<< "\n";
906 std::cout
<< "Parsing output error: " << e
.what() << "\n";
909 std::cout
<< "Emitter test failed: " << name
<< "\n";
910 std::cout
<< "Output:\n";
911 std::cout
<< output
<< "<<<\n";
912 std::cout
<< "Desired output:\n";
913 std::cout
<< desiredOutput
<< "<<<\n";
915 std::cout
<< "Emitter error: " << lastError
<< "\n";
920 void RunEmitterErrorTest(void (*test
)(YAML::Emitter
&, std::string
&), const std::string
& name
, int& passed
, int& total
) {
922 std::string desiredError
;
923 test(out
, desiredError
);
924 std::string lastError
= out
.GetLastError();
925 if(!out
.good() && lastError
== desiredError
) {
928 std::cout
<< "Emitter test failed: " << name
<< "\n";
930 std::cout
<< "No error detected\n";
932 std::cout
<< "Detected error: " << lastError
<< "\n";
933 std::cout
<< "Expected error: " << desiredError
<< "\n";
939 bool RunEmitterTests()
943 RunEmitterTest(&Emitter::SimpleScalar
, "simple scalar", passed
, total
);
944 RunEmitterTest(&Emitter::SimpleSeq
, "simple seq", passed
, total
);
945 RunEmitterTest(&Emitter::SimpleFlowSeq
, "simple flow seq", passed
, total
);
946 RunEmitterTest(&Emitter::EmptyFlowSeq
, "empty flow seq", passed
, total
);
947 RunEmitterTest(&Emitter::NestedBlockSeq
, "nested block seq", passed
, total
);
948 RunEmitterTest(&Emitter::NestedFlowSeq
, "nested flow seq", passed
, total
);
949 RunEmitterTest(&Emitter::SimpleMap
, "simple map", passed
, total
);
950 RunEmitterTest(&Emitter::SimpleFlowMap
, "simple flow map", passed
, total
);
951 RunEmitterTest(&Emitter::MapAndList
, "map and list", passed
, total
);
952 RunEmitterTest(&Emitter::ListAndMap
, "list and map", passed
, total
);
953 RunEmitterTest(&Emitter::NestedBlockMap
, "nested block map", passed
, total
);
954 RunEmitterTest(&Emitter::NestedFlowMap
, "nested flow map", passed
, total
);
955 RunEmitterTest(&Emitter::MapListMix
, "map list mix", passed
, total
);
956 RunEmitterTest(&Emitter::SimpleLongKey
, "simple long key", passed
, total
);
957 RunEmitterTest(&Emitter::SingleLongKey
, "single long key", passed
, total
);
958 RunEmitterTest(&Emitter::ComplexLongKey
, "complex long key", passed
, total
);
959 RunEmitterTest(&Emitter::AutoLongKey
, "auto long key", passed
, total
);
960 RunEmitterTest(&Emitter::ScalarFormat
, "scalar format", passed
, total
);
961 RunEmitterTest(&Emitter::AutoLongKeyScalar
, "auto long key scalar", passed
, total
);
962 RunEmitterTest(&Emitter::LongKeyFlowMap
, "long key flow map", passed
, total
);
963 RunEmitterTest(&Emitter::BlockMapAsKey
, "block map as key", passed
, total
);
964 RunEmitterTest(&Emitter::AliasAndAnchor
, "alias and anchor", passed
, total
);
965 RunEmitterTest(&Emitter::AliasAndAnchorWithNull
, "alias and anchor with null", passed
, total
);
966 RunEmitterTest(&Emitter::SimpleVerbatimTag
, "simple verbatim tag", passed
, total
);
967 RunEmitterTest(&Emitter::VerbatimTagInBlockSeq
, "verbatim tag in block seq", passed
, total
);
968 RunEmitterTest(&Emitter::VerbatimTagInFlowSeq
, "verbatim tag in flow seq", passed
, total
);
969 RunEmitterTest(&Emitter::VerbatimTagInFlowSeqWithNull
, "verbatim tag in flow seq with null", passed
, total
);
970 RunEmitterTest(&Emitter::VerbatimTagInBlockMap
, "verbatim tag in block map", passed
, total
);
971 RunEmitterTest(&Emitter::VerbatimTagInFlowMap
, "verbatim tag in flow map", passed
, total
);
972 RunEmitterTest(&Emitter::VerbatimTagInFlowMapWithNull
, "verbatim tag in flow map with null", passed
, total
);
973 RunEmitterTest(&Emitter::VerbatimTagWithEmptySeq
, "verbatim tag with empty seq", passed
, total
);
974 RunEmitterTest(&Emitter::VerbatimTagWithEmptyMap
, "verbatim tag with empty map", passed
, total
);
975 RunEmitterTest(&Emitter::VerbatimTagWithEmptySeqAndMap
, "verbatim tag with empty seq and map", passed
, total
);
976 RunEmitterTest(&Emitter::ByKindTagWithScalar
, "by-kind tag with scalar", passed
, total
);
977 RunEmitterTest(&Emitter::LocalTagWithScalar
, "local tag with scalar", passed
, total
);
978 RunEmitterTest(&Emitter::ComplexDoc
, "complex doc", passed
, total
);
979 RunEmitterTest(&Emitter::STLContainers
, "STL containers", passed
, total
);
980 RunEmitterTest(&Emitter::SimpleComment
, "simple comment", passed
, total
);
981 RunEmitterTest(&Emitter::MultiLineComment
, "multi-line comment", passed
, total
);
982 RunEmitterTest(&Emitter::ComplexComments
, "complex comments", passed
, total
);
983 RunEmitterTest(&Emitter::Indentation
, "indentation", passed
, total
);
984 RunEmitterTest(&Emitter::SimpleGlobalSettings
, "simple global settings", passed
, total
);
985 RunEmitterTest(&Emitter::ComplexGlobalSettings
, "complex global settings", passed
, total
);
986 RunEmitterTest(&Emitter::Null
, "null", passed
, total
);
987 RunEmitterTest(&Emitter::EscapedUnicode
, "escaped unicode", passed
, total
);
988 RunEmitterTest(&Emitter::Unicode
, "unicode", passed
, total
);
989 RunEmitterTest(&Emitter::DoubleQuotedUnicode
, "double quoted unicode", passed
, total
);
990 RunEmitterTest(&Emitter::UserType
, "user type", passed
, total
);
991 RunEmitterTest(&Emitter::UserTypeInContainer
, "user type in container", passed
, total
);
992 RunEmitterTest(&Emitter::PointerToInt
, "pointer to int", passed
, total
);
993 RunEmitterTest(&Emitter::PointerToUserType
, "pointer to user type", passed
, total
);
994 RunEmitterTest(&Emitter::NewlineAtEnd
, "newline at end", passed
, total
);
995 RunEmitterTest(&Emitter::NewlineInBlockSequence
, "newline in block sequence", passed
, total
);
996 RunEmitterTest(&Emitter::NewlineInFlowSequence
, "newline in flow sequence", passed
, total
);
997 RunEmitterTest(&Emitter::NewlineInBlockMap
, "newline in block map", passed
, total
);
998 RunEmitterTest(&Emitter::NewlineInFlowMap
, "newline in flow map", passed
, total
);
999 RunEmitterTest(&Emitter::LotsOfNewlines
, "lots of newlines", passed
, total
);
1000 RunEmitterTest(&Emitter::Binary
, "binary", passed
, total
);
1001 RunEmitterTest(&Emitter::LongBinary
, "long binary", passed
, total
);
1002 RunEmitterTest(&Emitter::EmptyBinary
, "empty binary", passed
, total
);
1003 RunEmitterTest(&Emitter::ColonAtEndOfScalar
, "colon at end of scalar", passed
, total
);
1004 RunEmitterTest(&Emitter::ColonAsScalar
, "colon as scalar", passed
, total
);
1005 RunEmitterTest(&Emitter::BoolFormatting
, "bool formatting", passed
, total
);
1006 RunEmitterTest(&Emitter::DocStartAndEnd
, "doc start and end", passed
, total
);
1007 RunEmitterTest(&Emitter::ImplicitDocStart
, "implicit doc start", passed
, total
);
1009 RunEmitterErrorTest(&Emitter::ExtraEndSeq
, "extra EndSeq", passed
, total
);
1010 RunEmitterErrorTest(&Emitter::ExtraEndMap
, "extra EndMap", passed
, total
);
1011 RunEmitterErrorTest(&Emitter::BadSingleQuoted
, "bad single quoted string", passed
, total
);
1012 RunEmitterErrorTest(&Emitter::InvalidAnchor
, "invalid anchor", passed
, total
);
1013 RunEmitterErrorTest(&Emitter::InvalidAlias
, "invalid alias", passed
, total
);
1014 RunEmitterErrorTest(&Emitter::MissingKey
, "missing key", passed
, total
);
1015 RunEmitterErrorTest(&Emitter::MissingValue
, "missing value", passed
, total
);
1016 RunEmitterErrorTest(&Emitter::UnexpectedKey
, "unexpected key", passed
, total
);
1017 RunEmitterErrorTest(&Emitter::UnexpectedValue
, "unexpected value", passed
, total
);
1018 RunEmitterErrorTest(&Emitter::BadLocalTag
, "bad local tag", passed
, total
);
1020 std::cout
<< "Emitter tests: " << passed
<< "/" << total
<< " passed\n";
1021 return passed
== total
;