OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library yaml_test; | |
6 | |
7 import '../../../pkg/unittest/lib/unittest.dart'; | |
8 import '../../pub/yaml/yaml.dart'; | |
9 import '../../pub/yaml/deep_equals.dart'; | |
10 import '../../../tests/utils/test_utils.dart'; | |
11 | |
12 /// Constructs a new yaml.YamlMap, optionally from a normal Map. | |
13 Map yamlMap([Map from]) => | |
14 from == null ? new YamlMap() : new YamlMap.from(from); | |
15 | |
16 /// Asserts that a string containing a single YAML document produces a given | |
17 /// value when loaded. | |
18 expectYamlLoads(expected, String source) { | |
19 var actual = loadYaml(cleanUpLiteral(source)); | |
20 Expect.isTrue(deepEquals(expected, actual), | |
21 'expectYamlLoads(expected: <$expected>, actual: <$actual>)'); | |
22 } | |
23 | |
24 /// Asserts that a string containing a stream of YAML documents produces a given | |
25 /// list of values when loaded. | |
26 expectYamlStreamLoads(List expected, String source) { | |
27 var actual = loadYamlStream(cleanUpLiteral(source)); | |
28 Expect.isTrue(deepEquals(expected, actual), | |
29 'expectYamlStreamLoads(expected: <$expected>, actual: <$actual>)'); | |
30 } | |
31 | |
32 main() { | |
33 var infinity = double.parse("Infinity"); | |
34 var nan = double.parse("NaN"); | |
35 | |
36 group('YamlMap', () { | |
37 group('accepts as a key', () { | |
38 _expectKeyWorks(keyFn()) { | |
39 var map = yamlMap(); | |
40 map[keyFn()] = 5; | |
41 expect(map.containsKey(keyFn()), isTrue); | |
42 expect(map[keyFn()], 5); | |
43 } | |
44 | |
45 test('null', () => _expectKeyWorks(() => null)); | |
46 test('true', () => _expectKeyWorks(() => true)); | |
47 test('false', () => _expectKeyWorks(() => false)); | |
48 test('a list', () => _expectKeyWorks(() => [1, 2, 3])); | |
49 test('a map', () => _expectKeyWorks(() => {'foo': 'bar'})); | |
50 test('a YAML map', () => _expectKeyWorks(() => yamlMap({'foo': 'bar'}))); | |
51 }); | |
52 | |
53 test('works as a hash key', () { | |
54 var normalMap = new Map(); | |
55 normalMap[yamlMap({'foo': 'bar'})] = 'baz'; | |
56 expect(normalMap.containsKey(yamlMap({'foo': 'bar'})), isTrue); | |
57 expect(normalMap[yamlMap({'foo': 'bar'})], 'baz'); | |
58 }); | |
59 | |
60 test('treats YamlMap keys the same as normal maps', () { | |
61 var map = yamlMap(); | |
62 map[{'a': 'b'}] = 5; | |
63 expect(map[yamlMap({'a': 'b'})], 5); | |
64 }); | |
65 }); | |
66 | |
67 group('has a friendly error message for', () { | |
68 var tabError = predicate((e) => | |
69 e.toString().contains('tab characters are not allowed as indentation')); | |
70 | |
71 test('using a tab as indentation', () { | |
72 expect(() => loadYaml('foo:\n\tbar'), | |
73 throwsA(tabError)); | |
74 }); | |
75 | |
76 test('using a tab not as indentation', () { | |
77 expect(() => loadYaml(''' | |
78 "foo | |
79 \tbar" | |
80 error'''), | |
81 throwsA(isNot(tabError))); | |
82 }); | |
83 }); | |
84 | |
85 // The following tests are all taken directly from the YAML spec | |
86 // (http://www.yaml.org/spec/1.2/spec.html). Most of them are code examples | |
87 // that are directly included in the spec, but additional tests are derived | |
88 // from the prose. | |
89 | |
90 // A few examples from the spec are deliberately excluded, because they test | |
91 // features that this implementation doesn't intend to support (character | |
92 // encoding detection and user-defined tags). More tests are commented out, | |
93 // because they're intended to be supported but not yet implemented. | |
94 | |
95 // Chapter 2 is just a preview of various Yaml documents. It's probably not | |
96 // necessary to test its examples, but it would be nice to test everything in | |
97 // the spec. | |
98 group('2.1: Collections', () { | |
99 test('[Example 2.1]', () { | |
100 expectYamlLoads(["Mark McGwire", "Sammy Sosa", "Ken Griffey"], | |
101 """ | |
102 - Mark McGwire | |
103 - Sammy Sosa | |
104 - Ken Griffey"""); | |
105 }); | |
106 | |
107 test('[Example 2.2]', () { | |
108 expectYamlLoads({"hr": 65, "avg": 0.278, "rbi": 147}, | |
109 """ | |
110 hr: 65 # Home runs | |
111 avg: 0.278 # Batting average | |
112 rbi: 147 # Runs Batted In"""); | |
113 }); | |
114 | |
115 test('[Example 2.3]', () { | |
116 expectYamlLoads({ | |
117 "american": ["Boston Red Sox", "Detroit Tigers", "New York Yankees"], | |
118 "national": ["New York Mets", "Chicago Cubs", "Atlanta Braves"], | |
119 }, | |
120 """ | |
121 american: | |
122 - Boston Red Sox | |
123 - Detroit Tigers | |
124 - New York Yankees | |
125 national: | |
126 - New York Mets | |
127 - Chicago Cubs | |
128 - Atlanta Braves"""); | |
129 }); | |
130 | |
131 test('[Example 2.4]', () { | |
132 expectYamlLoads([ | |
133 {"name": "Mark McGwire", "hr": 65, "avg": 0.278}, | |
134 {"name": "Sammy Sosa", "hr": 63, "avg": 0.288}, | |
135 ], | |
136 """ | |
137 - | |
138 name: Mark McGwire | |
139 hr: 65 | |
140 avg: 0.278 | |
141 - | |
142 name: Sammy Sosa | |
143 hr: 63 | |
144 avg: 0.288"""); | |
145 }); | |
146 | |
147 test('[Example 2.5]', () { | |
148 expectYamlLoads([ | |
149 ["name", "hr", "avg"], | |
150 ["Mark McGwire", 65, 0.278], | |
151 ["Sammy Sosa", 63, 0.288] | |
152 ], | |
153 """ | |
154 - [name , hr, avg ] | |
155 - [Mark McGwire, 65, 0.278] | |
156 - [Sammy Sosa , 63, 0.288]"""); | |
157 }); | |
158 | |
159 test('[Example 2.6]', () { | |
160 expectYamlLoads({ | |
161 "Mark McGwire": {"hr": 65, "avg": 0.278}, | |
162 "Sammy Sosa": {"hr": 63, "avg": 0.288} | |
163 }, | |
164 """ | |
165 Mark McGwire: {hr: 65, avg: 0.278} | |
166 Sammy Sosa: { | |
167 hr: 63, | |
168 avg: 0.288 | |
169 }"""); | |
170 }); | |
171 }); | |
172 | |
173 group('2.2: Structures', () { | |
174 test('[Example 2.7]', () { | |
175 expectYamlStreamLoads([ | |
176 ["Mark McGwire", "Sammy Sosa", "Ken Griffey"], | |
177 ["Chicago Cubs", "St Louis Cardinals"] | |
178 ], | |
179 """ | |
180 # Ranking of 1998 home runs | |
181 --- | |
182 - Mark McGwire | |
183 - Sammy Sosa | |
184 - Ken Griffey | |
185 | |
186 # Team ranking | |
187 --- | |
188 - Chicago Cubs | |
189 - St Louis Cardinals"""); | |
190 }); | |
191 | |
192 test('[Example 2.8]', () { | |
193 expectYamlStreamLoads([ | |
194 {"time": "20:03:20", "player": "Sammy Sosa", "action": "strike (miss)"}, | |
195 {"time": "20:03:47", "player": "Sammy Sosa", "action": "grand slam"}, | |
196 ], | |
197 """ | |
198 --- | |
199 time: 20:03:20 | |
200 player: Sammy Sosa | |
201 action: strike (miss) | |
202 ... | |
203 --- | |
204 time: 20:03:47 | |
205 player: Sammy Sosa | |
206 action: grand slam | |
207 ..."""); | |
208 }); | |
209 | |
210 test('[Example 2.9]', () { | |
211 expectYamlLoads({ | |
212 "hr": ["Mark McGwire", "Sammy Sosa"], | |
213 "rbi": ["Sammy Sosa", "Ken Griffey"] | |
214 }, | |
215 """ | |
216 --- | |
217 hr: # 1998 hr ranking | |
218 - Mark McGwire | |
219 - Sammy Sosa | |
220 rbi: | |
221 # 1998 rbi ranking | |
222 - Sammy Sosa | |
223 - Ken Griffey"""); | |
224 }); | |
225 | |
226 // test('[Example 2.10]', () { | |
227 // expectYamlLoads({ | |
228 // "hr": ["Mark McGwire", "Sammy Sosa"], | |
229 // "rbi": ["Sammy Sosa", "Ken Griffey"] | |
230 // }, | |
231 // """ | |
232 // --- | |
233 // hr: | |
234 // - Mark McGwire | |
235 // # Following node labeled SS | |
236 // - &SS Sammy Sosa | |
237 // rbi: | |
238 // - *SS # Subsequent occurrence | |
239 // - Ken Griffey"""); | |
240 // }); | |
241 | |
242 test('[Example 2.11]', () { | |
243 var doc = yamlMap(); | |
244 doc[["Detroit Tigers", "Chicago cubs"]] = ["2001-07-23"]; | |
245 doc[["New York Yankees", "Atlanta Braves"]] = | |
246 ["2001-07-02", "2001-08-12", "2001-08-14"]; | |
247 expectYamlLoads(doc, | |
248 """ | |
249 ? - Detroit Tigers | |
250 - Chicago cubs | |
251 : | |
252 - 2001-07-23 | |
253 | |
254 ? [ New York Yankees, | |
255 Atlanta Braves ] | |
256 : [ 2001-07-02, 2001-08-12, | |
257 2001-08-14 ]"""); | |
258 }); | |
259 | |
260 test('[Example 2.12]', () { | |
261 expectYamlLoads([ | |
262 {"item": "Super Hoop", "quantity": 1}, | |
263 {"item": "Basketball", "quantity": 4}, | |
264 {"item": "Big Shoes", "quantity": 1}, | |
265 ], | |
266 """ | |
267 --- | |
268 # Products purchased | |
269 - item : Super Hoop | |
270 quantity: 1 | |
271 - item : Basketball | |
272 quantity: 4 | |
273 - item : Big Shoes | |
274 quantity: 1"""); | |
275 }); | |
276 }); | |
277 | |
278 group('2.3: Scalars', () { | |
279 test('[Example 2.13]', () { | |
280 expectYamlLoads( | |
281 cleanUpLiteral( | |
282 """ | |
283 \\//||\\/|| | |
284 // || ||__"""), | |
285 """ | |
286 # ASCII Art | |
287 --- | | |
288 \\//||\\/|| | |
289 // || ||__"""); | |
290 }); | |
291 | |
292 test('[Example 2.14]', () { | |
293 expectYamlLoads("Mark McGwire's year was crippled by a knee injury.", | |
294 """ | |
295 --- > | |
296 Mark McGwire's | |
297 year was crippled | |
298 by a knee injury."""); | |
299 }); | |
300 | |
301 test('[Example 2.15]', () { | |
302 expectYamlLoads( | |
303 cleanUpLiteral( | |
304 """ | |
305 Sammy Sosa completed another fine season with great stats. | |
306 | |
307 63 Home Runs | |
308 0.288 Batting Average | |
309 | |
310 What a year!"""), | |
311 """ | |
312 > | |
313 Sammy Sosa completed another | |
314 fine season with great stats. | |
315 | |
316 63 Home Runs | |
317 0.288 Batting Average | |
318 | |
319 What a year!"""); | |
320 }); | |
321 | |
322 test('[Example 2.16]', () { | |
323 expectYamlLoads({ | |
324 "name": "Mark McGwire", | |
325 "accomplishment": "Mark set a major league home run record in 1998.\n", | |
326 "stats": "65 Home Runs\n0.278 Batting Average" | |
327 }, | |
328 """ | |
329 name: Mark McGwire | |
330 accomplishment: > | |
331 Mark set a major league | |
332 home run record in 1998. | |
333 stats: | | |
334 65 Home Runs | |
335 0.278 Batting Average"""); | |
336 }); | |
337 | |
338 test('[Example 2.17]', () { | |
339 expectYamlLoads({ | |
340 "unicode": "Sosa did fine.\u263A", | |
341 "control": "\b1998\t1999\t2000\n", | |
342 "hex esc": "\r\n is \r\n", | |
343 "single": '"Howdy!" he cried.', | |
344 "quoted": " # Not a 'comment'.", | |
345 "tie-fighter": "|\\-*-/|" | |
346 }, | |
347 """ | |
348 unicode: "Sosa did fine.\\u263A" | |
349 control: "\\b1998\\t1999\\t2000\\n" | |
350 hex esc: "\\x0d\\x0a is \\r\\n" | |
351 | |
352 single: '"Howdy!" he cried.' | |
353 quoted: ' # Not a ''comment''.' | |
354 tie-fighter: '|\\-*-/|'"""); | |
355 }); | |
356 | |
357 test('[Example 2.18]', () { | |
358 expectYamlLoads({ | |
359 "plain": "This unquoted scalar spans many lines.", | |
360 "quoted": "So does this quoted scalar.\n" | |
361 }, | |
362 ''' | |
363 plain: | |
364 This unquoted scalar | |
365 spans many lines. | |
366 | |
367 quoted: "So does this | |
368 quoted scalar.\\n"'''); | |
369 }); | |
370 }); | |
371 | |
372 group('2.4: Tags', () { | |
373 test('[Example 2.19]', () { | |
374 expectYamlLoads({ | |
375 "canonical": 12345, | |
376 "decimal": 12345, | |
377 "octal": 12, | |
378 "hexadecimal": 12 | |
379 }, | |
380 """ | |
381 canonical: 12345 | |
382 decimal: +12345 | |
383 octal: 0o14 | |
384 hexadecimal: 0xC"""); | |
385 }); | |
386 | |
387 test('[Example 2.20]', () { | |
388 expectYamlLoads({ | |
389 "canonical": 1230.15, | |
390 "exponential": 1230.15, | |
391 "fixed": 1230.15, | |
392 "negative infinity": -infinity, | |
393 "not a number": nan | |
394 }, | |
395 """ | |
396 canonical: 1.23015e+3 | |
397 exponential: 12.3015e+02 | |
398 fixed: 1230.15 | |
399 negative infinity: -.inf | |
400 not a number: .NaN"""); | |
401 }); | |
402 | |
403 test('[Example 2.21]', () { | |
404 var doc = yamlMap({ | |
405 "booleans": [true, false], | |
406 "string": "012345" | |
407 }); | |
408 doc[null] = null; | |
409 expectYamlLoads(doc, | |
410 """ | |
411 null: | |
412 booleans: [ true, false ] | |
413 string: '012345'"""); | |
414 }); | |
415 | |
416 // Examples 2.22 through 2.26 test custom tag URIs, which this | |
417 // implementation currently doesn't plan to support. | |
418 }); | |
419 | |
420 group('2.5 Full Length Example', () { | |
421 // Example 2.27 tests custom tag URIs, which this implementation currently | |
422 // doesn't plan to support. | |
423 | |
424 test('[Example 2.28]', () { | |
425 expectYamlStreamLoads([ | |
426 { | |
427 "Time": "2001-11-23 15:01:42 -5", | |
428 "User": "ed", | |
429 "Warning": "This is an error message for the log file" | |
430 }, | |
431 { | |
432 "Time": "2001-11-23 15:02:31 -5", | |
433 "User": "ed", | |
434 "Warning": "A slightly different error message." | |
435 }, | |
436 { | |
437 "DateTime": "2001-11-23 15:03:17 -5", | |
438 "User": "ed", | |
439 "Fatal": 'Unknown variable "bar"', | |
440 "Stack": [ | |
441 { | |
442 "file": "TopClass.py", | |
443 "line": 23, | |
444 "code": 'x = MoreObject("345\\n")\n' | |
445 }, | |
446 {"file": "MoreClass.py", "line": 58, "code": "foo = bar"} | |
447 ] | |
448 } | |
449 ], | |
450 """ | |
451 --- | |
452 Time: 2001-11-23 15:01:42 -5 | |
453 User: ed | |
454 Warning: | |
455 This is an error message | |
456 for the log file | |
457 --- | |
458 Time: 2001-11-23 15:02:31 -5 | |
459 User: ed | |
460 Warning: | |
461 A slightly different error | |
462 message. | |
463 --- | |
464 DateTime: 2001-11-23 15:03:17 -5 | |
465 User: ed | |
466 Fatal: | |
467 Unknown variable "bar" | |
468 Stack: | |
469 - file: TopClass.py | |
470 line: 23 | |
471 code: | | |
472 x = MoreObject("345\\n") | |
473 - file: MoreClass.py | |
474 line: 58 | |
475 code: |- | |
476 foo = bar"""); | |
477 }); | |
478 }); | |
479 | |
480 // Chapter 3 just talks about the structure of loading and dumping Yaml. | |
481 // Chapter 4 explains conventions used in the spec. | |
482 | |
483 // Chapter 5: Characters | |
484 group('5.1: Character Set', () { | |
485 expectAllowsCharacter(int charCode) { | |
486 var char = new String.fromCharCodes([charCode]); | |
487 expectYamlLoads('The character "$char" is allowed', | |
488 'The character "$char" is allowed'); | |
489 } | |
490 | |
491 expectAllowsQuotedCharacter(int charCode) { | |
492 var char = new String.fromCharCodes([charCode]); | |
493 expectYamlLoads("The character '$char' is allowed", | |
494 '"The character \'$char\' is allowed"'); | |
495 } | |
496 | |
497 expectDisallowsCharacter(int charCode) { | |
498 var char = new String.fromCharCodes([charCode]); | |
499 Expect.throws(() => loadYaml('The character "$char" is disallowed')); | |
500 } | |
501 | |
502 test("doesn't include C0 control characters", () { | |
503 expectDisallowsCharacter(0x0); | |
504 expectDisallowsCharacter(0x8); | |
505 expectDisallowsCharacter(0x1F); | |
506 }); | |
507 | |
508 test("includes TAB", () => expectAllowsCharacter(0x9)); | |
509 test("doesn't include DEL", () => expectDisallowsCharacter(0x7F)); | |
510 | |
511 test("doesn't include C1 control characters", () { | |
512 expectDisallowsCharacter(0x80); | |
513 expectDisallowsCharacter(0x8A); | |
514 expectDisallowsCharacter(0x9F); | |
515 }); | |
516 | |
517 test("includes NEL", () => expectAllowsCharacter(0x85)); | |
518 | |
519 group("within quoted strings", () { | |
520 test("includes DEL", () => expectAllowsQuotedCharacter(0x7F)); | |
521 test("includes C1 control characters", () { | |
522 expectAllowsQuotedCharacter(0x80); | |
523 expectAllowsQuotedCharacter(0x8A); | |
524 expectAllowsQuotedCharacter(0x9F); | |
525 }); | |
526 }); | |
527 }); | |
528 | |
529 // Skipping section 5.2 (Character Encodings), since at the moment the module | |
530 // assumes that the client code is providing it with a string of the proper | |
531 // encoding. | |
532 | |
533 group('5.3: Indicator Characters', () { | |
534 test('[Example 5.3]', () { | |
535 expectYamlLoads({ | |
536 'sequence': ['one', 'two'], | |
537 'mapping': {'sky': 'blue', 'sea': 'green'} | |
538 }, | |
539 """ | |
540 sequence: | |
541 - one | |
542 - two | |
543 mapping: | |
544 ? sky | |
545 : blue | |
546 sea : green"""); | |
547 }); | |
548 | |
549 test('[Example 5.4]', () { | |
550 expectYamlLoads({ | |
551 'sequence': ['one', 'two'], | |
552 'mapping': {'sky': 'blue', 'sea': 'green'} | |
553 }, | |
554 """ | |
555 sequence: [ one, two, ] | |
556 mapping: { sky: blue, sea: green }"""); | |
557 }); | |
558 | |
559 test('[Example 5.5]', () => expectYamlLoads(null, "# Comment only.")); | |
560 | |
561 // Skipping 5.6 because it uses an undefined tag. | |
562 | |
563 test('[Example 5.7]', () { | |
564 expectYamlLoads({ | |
565 'literal': "some\ntext\n", | |
566 'folded': "some text\n" | |
567 }, | |
568 """ | |
569 literal: | | |
570 some | |
571 text | |
572 folded: > | |
573 some | |
574 text | |
575 """); | |
576 }); | |
577 | |
578 test('[Example 5.8]', () { | |
579 expectYamlLoads({ | |
580 'single': "text", | |
581 'double': "text" | |
582 }, | |
583 """ | |
584 single: 'text' | |
585 double: "text" | |
586 """); | |
587 }); | |
588 | |
589 // test('[Example 5.9]', () { | |
590 // expectYamlLoads("text", | |
591 // """ | |
592 // %YAML 1.2 | |
593 // --- text"""); | |
594 // }); | |
595 | |
596 test('[Example 5.10]', () { | |
597 Expect.throws(() => loadYaml("commercial-at: @text")); | |
598 Expect.throws(() => loadYaml("commercial-at: `text")); | |
599 }); | |
600 }); | |
601 | |
602 group('5.4: Line Break Characters', () { | |
603 group('include', () { | |
604 test('\\n', () => expectYamlLoads([1, 2], indentLiteral("- 1\n- 2"))); | |
605 test('\\r', () => expectYamlLoads([1, 2], "- 1\r- 2")); | |
606 }); | |
607 | |
608 group('do not include', () { | |
609 test('form feed', () => Expect.throws(() => loadYaml("- 1\x0C- 2"))); | |
610 test('NEL', () => expectYamlLoads(["1\x85- 2"], "- 1\x85- 2")); | |
611 test('0x2028', () => expectYamlLoads(["1\u2028- 2"], "- 1\u2028- 2")); | |
612 test('0x2029', () => expectYamlLoads(["1\u2029- 2"], "- 1\u2029- 2")); | |
613 }); | |
614 | |
615 group('in a scalar context must be normalized', () { | |
616 test("from \\r to \\n", () => | |
617 expectYamlLoads(["foo\nbar"], indentLiteral('- |\n foo\r bar'))); | |
618 test("from \\r\\n to \\n", () => | |
619 expectYamlLoads(["foo\nbar"], indentLiteral('- |\n foo\r\n bar')))
; | |
620 }); | |
621 | |
622 test('[Example 5.11]', () { | |
623 expectYamlLoads( | |
624 cleanUpLiteral(""" | |
625 Line break (no glyph) | |
626 Line break (glyphed)"""), | |
627 """ | |
628 | | |
629 Line break (no glyph) | |
630 Line break (glyphed)"""); | |
631 }); | |
632 }); | |
633 | |
634 group('5.5: White Space Characters', () { | |
635 test('[Example 5.12]', () { | |
636 expectYamlLoads({ | |
637 "quoted": "Quoted \t", | |
638 "block": 'void main() {\n\tprintf("Hello, world!\\n");\n}\n' | |
639 }, | |
640 """ | |
641 # Tabs and spaces | |
642 quoted: "Quoted \t" | |
643 block:\t| | |
644 void main() { | |
645 \tprintf("Hello, world!\\n"); | |
646 } | |
647 """); | |
648 }); | |
649 }); | |
650 | |
651 group('5.7: Escaped Characters', () { | |
652 test('[Example 5.13]', () { | |
653 expectYamlLoads( | |
654 "Fun with \x5C " | |
655 "\x22 \x07 \x08 \x1B \x0C " | |
656 "\x0A \x0D \x09 \x0B \x00 " | |
657 "\x20 \xA0 \x85 \u2028 \u2029 " | |
658 "A A A", | |
659 ''' | |
660 "Fun with \\\\ | |
661 \\" \\a \\b \\e \\f \\ | |
662 \\n \\r \\t \\v \\0 \\ | |
663 \\ \\_ \\N \\L \\P \\ | |
664 \\x41 \\u0041 \\U00000041"'''); | |
665 }); | |
666 | |
667 test('[Example 5.14]', () { | |
668 Expect.throws(() => loadYaml('Bad escape: "\\c"')); | |
669 Expect.throws(() => loadYaml('Bad escape: "\\xq-"')); | |
670 }); | |
671 }); | |
672 | |
673 // Chapter 6: Basic Structures | |
674 group('6.1: Indentation Spaces', () { | |
675 test('may not include TAB characters', () { | |
676 Expect.throws(() => loadYaml(cleanUpLiteral( | |
677 """ | |
678 - | |
679 \t- foo | |
680 \t- bar"""))); | |
681 }); | |
682 | |
683 test('must be the same for all sibling nodes', () { | |
684 Expect.throws(() => loadYaml(cleanUpLiteral( | |
685 """ | |
686 - | |
687 - foo | |
688 - bar"""))); | |
689 }); | |
690 | |
691 test('may be different for the children of sibling nodes', () { | |
692 expectYamlLoads([["foo"], ["bar"]], | |
693 """ | |
694 - | |
695 - foo | |
696 - | |
697 - bar"""); | |
698 }); | |
699 | |
700 test('[Example 6.1]', () { | |
701 expectYamlLoads({ | |
702 "Not indented": { | |
703 "By one space": "By four\n spaces\n", | |
704 "Flow style": [ | |
705 "By two", | |
706 "Also by two", | |
707 "Still by two" | |
708 ] | |
709 } | |
710 }, | |
711 """ | |
712 # Leading comment line spaces are | |
713 # neither content nor indentation. | |
714 | |
715 Not indented: | |
716 By one space: | | |
717 By four | |
718 spaces | |
719 Flow style: [ # Leading spaces | |
720 By two, # in flow style | |
721 Also by two, # are neither | |
722 \tStill by two # content nor | |
723 ] # indentation."""); | |
724 }); | |
725 | |
726 test('[Example 6.2]', () { | |
727 expectYamlLoads({'a': ['b', ['c', 'd']]}, | |
728 """ | |
729 ? a | |
730 : -\tb | |
731 - -\tc | |
732 - d"""); | |
733 }); | |
734 }); | |
735 | |
736 group('6.2: Separation Spaces', () { | |
737 test('[Example 6.3]', () { | |
738 expectYamlLoads([{'foo': 'bar'}, ['baz', 'baz']], | |
739 """ | |
740 - foo:\t bar | |
741 - - baz | |
742 -\tbaz"""); | |
743 }); | |
744 }); | |
745 | |
746 group('6.3: Line Prefixes', () { | |
747 test('[Example 6.4]', () { | |
748 expectYamlLoads({ | |
749 "plain": "text lines", | |
750 "quoted": "text lines", | |
751 "block": "text\n \tlines\n" | |
752 }, | |
753 """ | |
754 plain: text | |
755 lines | |
756 quoted: "text | |
757 \tlines" | |
758 block: | | |
759 text | |
760 \tlines | |
761 """); | |
762 }); | |
763 }); | |
764 | |
765 group('6.4: Empty Lines', () { | |
766 test('[Example 6.5]', () { | |
767 expectYamlLoads({ | |
768 "Folding": "Empty line\nas a line feed", | |
769 "Chomping": "Clipped empty lines\n", | |
770 }, | |
771 """ | |
772 Folding: | |
773 "Empty line | |
774 \t | |
775 as a line feed" | |
776 Chomping: | | |
777 Clipped empty lines | |
778 """); | |
779 }); | |
780 }); | |
781 | |
782 group('6.5: Line Folding', () { | |
783 test('[Example 6.6]', () { | |
784 expectYamlLoads("trimmed\n\n\nas space", | |
785 """ | |
786 >- | |
787 trimmed | |
788 | |
789 | |
790 | |
791 as | |
792 space | |
793 """); | |
794 }); | |
795 | |
796 test('[Example 6.7]', () { | |
797 expectYamlLoads("foo \n\n\t bar\n\nbaz\n", | |
798 """ | |
799 > | |
800 foo | |
801 | |
802 \t bar | |
803 | |
804 baz | |
805 """); | |
806 }); | |
807 | |
808 test('[Example 6.8]', () { | |
809 expectYamlLoads(" foo\nbar\nbaz ", | |
810 ''' | |
811 " | |
812 foo | |
813 | |
814 \t bar | |
815 | |
816 baz | |
817 "'''); | |
818 }); | |
819 }); | |
820 | |
821 group('6.6: Comments', () { | |
822 test('must be separated from other tokens by white space characters', () { | |
823 expectYamlLoads("foo#bar", "foo#bar"); | |
824 expectYamlLoads("foo:#bar", "foo:#bar"); | |
825 expectYamlLoads("-#bar", "-#bar"); | |
826 }); | |
827 | |
828 test('[Example 6.9]', () { | |
829 expectYamlLoads({'key': 'value'}, | |
830 """ | |
831 key: # Comment | |
832 value"""); | |
833 }); | |
834 | |
835 group('outside of scalar content', () { | |
836 test('may appear on a line of their own', () { | |
837 expectYamlLoads([1, 2], | |
838 """ | |
839 - 1 | |
840 # Comment | |
841 - 2"""); | |
842 }); | |
843 | |
844 test('are independent of indentation level', () { | |
845 expectYamlLoads([[1, 2]], | |
846 """ | |
847 - | |
848 - 1 | |
849 # Comment | |
850 - 2"""); | |
851 }); | |
852 | |
853 test('include lines containing only white space characters', () { | |
854 expectYamlLoads([1, 2], | |
855 """ | |
856 - 1 | |
857 \t | |
858 - 2"""); | |
859 }); | |
860 }); | |
861 | |
862 group('within scalar content', () { | |
863 test('may not appear on a line of their own', () { | |
864 expectYamlLoads(["foo\n# not comment\nbar\n"], | |
865 """ | |
866 - | | |
867 foo | |
868 # not comment | |
869 bar | |
870 """); | |
871 }); | |
872 | |
873 test("don't include lines containing only white space characters", () { | |
874 expectYamlLoads(["foo\n \t \nbar\n"], | |
875 """ | |
876 - | | |
877 foo | |
878 \t | |
879 bar | |
880 """); | |
881 }); | |
882 }); | |
883 | |
884 test('[Example 6.10]', () { | |
885 expectYamlLoads(null, | |
886 """ | |
887 # Comment | |
888 | |
889 """); | |
890 }); | |
891 | |
892 test('[Example 6.11]', () { | |
893 expectYamlLoads({'key': 'value'}, | |
894 """ | |
895 key: # Comment | |
896 # lines | |
897 value | |
898 """); | |
899 }); | |
900 | |
901 group('ending a block scalar header', () { | |
902 test('may not be followed by additional comment lines', () { | |
903 expectYamlLoads(["# not comment\nfoo\n"], | |
904 """ | |
905 - | # comment | |
906 # not comment | |
907 foo | |
908 """); | |
909 }); | |
910 }); | |
911 }); | |
912 | |
913 group('6.7: Separation Lines', () { | |
914 test('may not be used within implicit keys', () { | |
915 Expect.throws(() => loadYaml(cleanUpLiteral( | |
916 """ | |
917 [1, | |
918 2]: 3"""))); | |
919 }); | |
920 | |
921 test('[Example 6.12]', () { | |
922 var doc = yamlMap(); | |
923 doc[{'first': 'Sammy', 'last': 'Sosa'}] = { | |
924 'hr': 65, | |
925 'avg': 0.278 | |
926 }; | |
927 expectYamlLoads(doc, | |
928 """ | |
929 { first: Sammy, last: Sosa }: | |
930 # Statistics: | |
931 hr: # Home runs | |
932 65 | |
933 avg: # Average | |
934 0.278"""); | |
935 }); | |
936 }); | |
937 | |
938 group('6.8: Directives', () { | |
939 // // TODO(nweiz): assert that this produces a warning | |
940 // test('[Example 6.13]', () { | |
941 // expectYamlLoads("foo", | |
942 // ''' | |
943 // %FOO bar baz # Should be ignored | |
944 // # with a warning. | |
945 // --- "foo"'''); | |
946 // }); | |
947 | |
948 // // TODO(nweiz): assert that this produces a warning | |
949 // test('[Example 6.14]', () { | |
950 // expectYamlLoads("foo", | |
951 // ''' | |
952 // %YAML 1.3 # Attempt parsing | |
953 // # with a warning | |
954 // --- | |
955 // "foo"'''); | |
956 // }); | |
957 | |
958 // test('[Example 6.15]', () { | |
959 // Expect.throws(() => loadYaml(cleanUpLiteral( | |
960 // """ | |
961 // %YAML 1.2 | |
962 // %YAML 1.1 | |
963 // foo"""))); | |
964 // }); | |
965 | |
966 // test('[Example 6.16]', () { | |
967 // expectYamlLoads("foo", | |
968 // ''' | |
969 // %TAG !yaml! tag:yaml.org,2002: | |
970 // --- | |
971 // !yaml!str "foo"'''); | |
972 // }); | |
973 | |
974 // test('[Example 6.17]', () { | |
975 // Expect.throws(() => loadYaml(cleanUpLiteral( | |
976 // """ | |
977 // %TAG ! !foo | |
978 // %TAG ! !foo | |
979 // bar"""))); | |
980 // }); | |
981 | |
982 // Examples 6.18 through 6.22 test custom tag URIs, which this | |
983 // implementation currently doesn't plan to support. | |
984 }); | |
985 | |
986 group('6.9: Node Properties', () { | |
987 // test('may be specified in any order', () { | |
988 // expectYamlLoads(["foo", "bar"], | |
989 // """ | |
990 // - !!str &a1 foo | |
991 // - &a2 !!str bar"""); | |
992 // }); | |
993 | |
994 // test('[Example 6.23]', () { | |
995 // expectYamlLoads({ | |
996 // "foo": "bar", | |
997 // "baz": "foo" | |
998 // }, | |
999 // ''' | |
1000 // !!str &a1 "foo": | |
1001 // !!str bar | |
1002 // &a2 baz : *a1'''); | |
1003 // }); | |
1004 | |
1005 // // Example 6.24 tests custom tag URIs, which this implementation currentl
y | |
1006 // // doesn't plan to support. | |
1007 | |
1008 // test('[Example 6.25]', () { | |
1009 // Expect.throws(() => loadYaml("- !<!> foo")); | |
1010 // Expect.throws(() => loadYaml("- !<\$:?> foo")); | |
1011 // }); | |
1012 | |
1013 // // Examples 6.26 and 6.27 test custom tag URIs, which this implementation | |
1014 // // currently doesn't plan to support. | |
1015 | |
1016 // test('[Example 6.28]', () { | |
1017 // expectYamlLoads(["12", 12, "12"], | |
1018 // ''' | |
1019 // # Assuming conventional resolution: | |
1020 // - "12" | |
1021 // - 12 | |
1022 // - ! 12'''); | |
1023 // }); | |
1024 | |
1025 // test('[Example 6.29]', () { | |
1026 // expectYamlLoads({ | |
1027 // "First occurrence": "Value", | |
1028 // "Second occurrence": "anchor" | |
1029 // }, | |
1030 // """ | |
1031 // First occurrence: &anchor Value | |
1032 // Second occurrence: *anchor"""); | |
1033 // }); | |
1034 }); | |
1035 | |
1036 // Chapter 7: Flow Styles | |
1037 group('7.1: Alias Nodes', () { | |
1038 // test("must not use an anchor that doesn't previously occur", () { | |
1039 // Expect.throws(() => loadYaml(cleanUpLiteral( | |
1040 // """ | |
1041 // - *anchor | |
1042 // - &anchor foo""")); | |
1043 // }); | |
1044 | |
1045 // test("don't have to exist for a given anchor node", () { | |
1046 // expectYamlLoads(["foo"], "- &anchor foo"); | |
1047 // }); | |
1048 | |
1049 // group('must not specify', () { | |
1050 // test('tag properties', () => Expect.throws(() => loadYaml(cleanUpLitera
l( | |
1051 // """ | |
1052 // - &anchor foo | |
1053 // - !str *anchor"""))); | |
1054 | |
1055 // test('anchor properties', () => Expect.throws( | |
1056 // () => loadYaml(cleanUpLiteral( | |
1057 // """ | |
1058 // - &anchor foo | |
1059 // - &anchor2 *anchor"""))); | |
1060 | |
1061 // test('content', () => Expect.throws(() => loadYaml(cleanUpLiteral( | |
1062 // """ | |
1063 // - &anchor foo | |
1064 // - *anchor bar"""))); | |
1065 // }); | |
1066 | |
1067 // test('must preserve structural equality', () { | |
1068 // var doc = loadYaml(cleanUpLiteral( | |
1069 // """ | |
1070 // anchor: &anchor [a, b, c] | |
1071 // alias: *anchor"""); | |
1072 // var anchorList = doc['anchor']; | |
1073 // var aliasList = doc['alias']; | |
1074 // Expect.isTrue(anchorList === aliasList); | |
1075 // anchorList.add('d'); | |
1076 // Expect.listEquals(['a', 'b', 'c', 'd'], aliasList); | |
1077 | |
1078 // doc = loadYaml(cleanUpLiteral( | |
1079 // """ | |
1080 // ? &anchor [a, b, c] | |
1081 // : ? *anchor | |
1082 // : bar"""); | |
1083 // anchorList = doc.keys[0]; | |
1084 // aliasList = doc[['a', 'b', 'c']].keys[0]; | |
1085 // Expect.isTrue(anchorList === aliasList); | |
1086 // anchorList.add('d'); | |
1087 // Expect.listEquals(['a', 'b', 'c', 'd'], aliasList); | |
1088 // }); | |
1089 | |
1090 // test('[Example 7.1]', () { | |
1091 // expectYamlLoads({ | |
1092 // "First occurence": "Foo", | |
1093 // "Second occurence": "Foo", | |
1094 // "Override anchor": "Bar", | |
1095 // "Reuse anchor": "Bar", | |
1096 // }, | |
1097 // """ | |
1098 // First occurrence: &anchor Foo | |
1099 // Second occurrence: *anchor | |
1100 // Override anchor: &anchor Bar | |
1101 // Reuse anchor: *anchor"""); | |
1102 // }); | |
1103 }); | |
1104 | |
1105 group('7.2: Empty Nodes', () { | |
1106 // test('[Example 7.2]', () { | |
1107 // expectYamlLoads({ | |
1108 // "foo": "", | |
1109 // "": "bar" | |
1110 // }, | |
1111 // """ | |
1112 // { | |
1113 // foo : !!str, | |
1114 // !!str : bar, | |
1115 // }"""); | |
1116 // }); | |
1117 | |
1118 test('[Example 7.3]', () { | |
1119 var doc = yamlMap({"foo": null}); | |
1120 doc[null] = "bar"; | |
1121 expectYamlLoads(doc, | |
1122 """ | |
1123 { | |
1124 ? foo :, | |
1125 : bar, | |
1126 }"""); | |
1127 }); | |
1128 }); | |
1129 | |
1130 group('7.3: Flow Scalar Styles', () { | |
1131 test('[Example 7.4]', () { | |
1132 expectYamlLoads({ | |
1133 "implicit block key": [{"implicit flow key": "value"}] | |
1134 }, | |
1135 ''' | |
1136 "implicit block key" : [ | |
1137 "implicit flow key" : value, | |
1138 ]'''); | |
1139 }); | |
1140 | |
1141 test('[Example 7.5]', () { | |
1142 expectYamlLoads( | |
1143 "folded to a space,\nto a line feed, or \t \tnon-content", | |
1144 ''' | |
1145 "folded | |
1146 to a space,\t | |
1147 | |
1148 to a line feed, or \t\\ | |
1149 \\ \tnon-content"'''); | |
1150 }); | |
1151 | |
1152 test('[Example 7.6]', () { | |
1153 expectYamlLoads(" 1st non-empty\n2nd non-empty 3rd non-empty ", | |
1154 ''' | |
1155 " 1st non-empty | |
1156 | |
1157 2nd non-empty | |
1158 \t3rd non-empty "'''); | |
1159 }); | |
1160 | |
1161 test('[Example 7.7]', () { | |
1162 expectYamlLoads("here's to \"quotes\"", "'here''s to \"quotes\"'"); | |
1163 }); | |
1164 | |
1165 test('[Example 7.8]', () { | |
1166 expectYamlLoads({ | |
1167 "implicit block key": [{"implicit flow key": "value"}] | |
1168 }, | |
1169 """ | |
1170 'implicit block key' : [ | |
1171 'implicit flow key' : value, | |
1172 ]"""); | |
1173 }); | |
1174 | |
1175 test('[Example 7.9]', () { | |
1176 expectYamlLoads(" 1st non-empty\n2nd non-empty 3rd non-empty ", | |
1177 """ | |
1178 ' 1st non-empty | |
1179 | |
1180 2nd non-empty | |
1181 \t3rd non-empty '"""); | |
1182 }); | |
1183 | |
1184 test('[Example 7.10]', () { | |
1185 expectYamlLoads([ | |
1186 "::vector", ": - ()", "Up, up, and away!", -123, | |
1187 "http://example.com/foo#bar", | |
1188 [ | |
1189 "::vector", ": - ()", "Up, up, and away!", -123, | |
1190 "http://example.com/foo#bar" | |
1191 ] | |
1192 ], | |
1193 ''' | |
1194 # Outside flow collection: | |
1195 - ::vector | |
1196 - ": - ()" | |
1197 - Up, up, and away! | |
1198 - -123 | |
1199 - http://example.com/foo#bar | |
1200 # Inside flow collection: | |
1201 - [ ::vector, | |
1202 ": - ()", | |
1203 "Up, up, and away!", | |
1204 -123, | |
1205 http://example.com/foo#bar ]'''); | |
1206 }); | |
1207 | |
1208 test('[Example 7.11]', () { | |
1209 expectYamlLoads({ | |
1210 "implicit block key": [{"implicit flow key": "value"}] | |
1211 }, | |
1212 """ | |
1213 implicit block key : [ | |
1214 implicit flow key : value, | |
1215 ]"""); | |
1216 }); | |
1217 | |
1218 test('[Example 7.12]', () { | |
1219 expectYamlLoads("1st non-empty\n2nd non-empty 3rd non-empty", | |
1220 """ | |
1221 1st non-empty | |
1222 | |
1223 2nd non-empty | |
1224 \t3rd non-empty"""); | |
1225 }); | |
1226 }); | |
1227 | |
1228 group('7.4: Flow Collection Styles', () { | |
1229 test('[Example 7.13]', () { | |
1230 expectYamlLoads([ | |
1231 ['one', 'two'], | |
1232 ['three', 'four'] | |
1233 ], | |
1234 """ | |
1235 - [ one, two, ] | |
1236 - [three ,four]"""); | |
1237 }); | |
1238 | |
1239 test('[Example 7.14]', () { | |
1240 expectYamlLoads([ | |
1241 "double quoted", "single quoted", "plain text", ["nested"], | |
1242 {"single": "pair"} | |
1243 ], | |
1244 """ | |
1245 [ | |
1246 "double | |
1247 quoted", 'single | |
1248 quoted', | |
1249 plain | |
1250 text, [ nested ], | |
1251 single: pair, | |
1252 ]"""); | |
1253 }); | |
1254 | |
1255 test('[Example 7.15]', () { | |
1256 expectYamlLoads([ | |
1257 {"one": "two", "three": "four"}, | |
1258 {"five": "six", "seven": "eight"}, | |
1259 ], | |
1260 """ | |
1261 - { one : two , three: four , } | |
1262 - {five: six,seven : eight}"""); | |
1263 }); | |
1264 | |
1265 test('[Example 7.16]', () { | |
1266 var doc = yamlMap({ | |
1267 "explicit": "entry", | |
1268 "implicit": "entry" | |
1269 }); | |
1270 doc[null] = null; | |
1271 expectYamlLoads(doc, | |
1272 """ | |
1273 { | |
1274 ? explicit: entry, | |
1275 implicit: entry, | |
1276 ? | |
1277 }"""); | |
1278 }); | |
1279 | |
1280 test('[Example 7.17]', () { | |
1281 var doc = yamlMap({ | |
1282 "unquoted": "separate", | |
1283 "http://foo.com": null, | |
1284 "omitted value": null | |
1285 }); | |
1286 doc[null] = "omitted key"; | |
1287 expectYamlLoads(doc, | |
1288 ''' | |
1289 { | |
1290 unquoted : "separate", | |
1291 http://foo.com, | |
1292 omitted value:, | |
1293 : omitted key, | |
1294 }'''); | |
1295 }); | |
1296 | |
1297 test('[Example 7.18]', () { | |
1298 expectYamlLoads({ | |
1299 "adjacent": "value", | |
1300 "readable": "value", | |
1301 "empty": null | |
1302 }, | |
1303 ''' | |
1304 { | |
1305 "adjacent":value, | |
1306 "readable": value, | |
1307 "empty": | |
1308 }'''); | |
1309 }); | |
1310 | |
1311 test('[Example 7.19]', () { | |
1312 expectYamlLoads([{"foo": "bar"}], | |
1313 """ | |
1314 [ | |
1315 foo: bar | |
1316 ]"""); | |
1317 }); | |
1318 | |
1319 test('[Example 7.20]', () { | |
1320 expectYamlLoads([{"foo bar": "baz"}], | |
1321 """ | |
1322 [ | |
1323 ? foo | |
1324 bar : baz | |
1325 ]"""); | |
1326 }); | |
1327 | |
1328 test('[Example 7.21]', () { | |
1329 var el1 = yamlMap(); | |
1330 el1[null] = "empty key entry"; | |
1331 | |
1332 var el2 = yamlMap(); | |
1333 el2[{"JSON": "like"}] = "adjacent"; | |
1334 | |
1335 expectYamlLoads([[{"YAML": "separate"}], [el1], [el2]], | |
1336 """ | |
1337 - [ YAML : separate ] | |
1338 - [ : empty key entry ] | |
1339 - [ {JSON: like}:adjacent ]"""); | |
1340 }); | |
1341 | |
1342 test('[Example 7.22]', () { | |
1343 Expect.throws(() => loadYaml(cleanUpLiteral( | |
1344 """ | |
1345 [ foo | |
1346 bar: invalid ]"""))); | |
1347 | |
1348 // TODO(nweiz): enable this when we throw an error for long keys | |
1349 // var dotList = []; | |
1350 // dotList.insertRange(0, 1024, ' '); | |
1351 // var dots = Strings.join(dotList, ''); | |
1352 // Expect.throws(() => loadYaml('[ "foo...$dots...bar": invalid ]')); | |
1353 }); | |
1354 }); | |
1355 | |
1356 group('7.5: Flow Nodes', () { | |
1357 test('[Example 7.23]', () { | |
1358 expectYamlLoads([["a", "b"], {"a": "b"}, "a", "b", "c"], | |
1359 """ | |
1360 - [ a, b ] | |
1361 - { a: b } | |
1362 - "a" | |
1363 - 'b' | |
1364 - c"""); | |
1365 }); | |
1366 | |
1367 // test('[Example 7.24]', () { | |
1368 // expectYamlLoads(["a", "b", "c", "c", ""], | |
1369 // """ | |
1370 // - !!str "a" | |
1371 // - 'b' | |
1372 // - &anchor "c" | |
1373 // - *anchor | |
1374 // - !!str"""); | |
1375 // }); | |
1376 }); | |
1377 | |
1378 // Chapter 8: Block Styles | |
1379 group('8.1: Block Scalar Styles', () { | |
1380 test('[Example 8.1]', () { | |
1381 expectYamlLoads(["literal\n", " folded\n", "keep\n\n", " strip"], | |
1382 """ | |
1383 - | # Empty header | |
1384 literal | |
1385 - >1 # Indentation indicator | |
1386 folded | |
1387 - |+ # Chomping indicator | |
1388 keep | |
1389 | |
1390 - >1- # Both indicators | |
1391 strip"""); | |
1392 }); | |
1393 | |
1394 test('[Example 8.2]', () { | |
1395 // Note: in the spec, the fourth element in this array is listed as | |
1396 // "\t detected\n", not "\t\ndetected\n". However, I'm reasonably | |
1397 // confident that "\t\ndetected\n" is correct when parsed according to the | |
1398 // rest of the spec. | |
1399 expectYamlLoads([ | |
1400 "detected\n", | |
1401 "\n\n# detected\n", | |
1402 " explicit\n", | |
1403 "\t\ndetected\n" | |
1404 ], | |
1405 """ | |
1406 - | | |
1407 detected | |
1408 - > | |
1409 | |
1410 | |
1411 # detected | |
1412 - |1 | |
1413 explicit | |
1414 - > | |
1415 \t | |
1416 detected | |
1417 """); | |
1418 }); | |
1419 | |
1420 test('[Example 8.3]', () { | |
1421 Expect.throws(() => loadYaml(cleanUpLiteral( | |
1422 """ | |
1423 - | | |
1424 | |
1425 text"""))); | |
1426 | |
1427 Expect.throws(() => loadYaml(cleanUpLiteral( | |
1428 """ | |
1429 - > | |
1430 text | |
1431 text"""))); | |
1432 | |
1433 Expect.throws(() => loadYaml(cleanUpLiteral( | |
1434 """ | |
1435 - |2 | |
1436 text"""))); | |
1437 }); | |
1438 | |
1439 test('[Example 8.4]', () { | |
1440 expectYamlLoads({"strip": "text", "clip": "text\n", "keep": "text\n"}, | |
1441 """ | |
1442 strip: |- | |
1443 text | |
1444 clip: | | |
1445 text | |
1446 keep: |+ | |
1447 text | |
1448 """); | |
1449 }); | |
1450 | |
1451 test('[Example 8.5]', () { | |
1452 // This example in the spec only includes a single newline in the "keep" | |
1453 // value, but as far as I can tell that's not how it's supposed to be | |
1454 // parsed according to the rest of the spec. | |
1455 expectYamlLoads({ | |
1456 "strip": "# text", | |
1457 "clip": "# text\n", | |
1458 "keep": "# text\n\n" | |
1459 }, | |
1460 """ | |
1461 # Strip | |
1462 # Comments: | |
1463 strip: |- | |
1464 # text | |
1465 | |
1466 # Clip | |
1467 # comments: | |
1468 | |
1469 clip: | | |
1470 # text | |
1471 | |
1472 # Keep | |
1473 # comments: | |
1474 | |
1475 keep: |+ | |
1476 # text | |
1477 | |
1478 # Trail | |
1479 # comments. | |
1480 """); | |
1481 }); | |
1482 | |
1483 test('[Example 8.6]', () { | |
1484 expectYamlLoads({"strip": "", "clip": "", "keep": "\n"}, | |
1485 """ | |
1486 strip: >- | |
1487 | |
1488 clip: > | |
1489 | |
1490 keep: |+ | |
1491 | |
1492 """); | |
1493 }); | |
1494 | |
1495 test('[Example 8.7]', () { | |
1496 expectYamlLoads("literal\n\ttext\n", | |
1497 """ | |
1498 | | |
1499 literal | |
1500 \ttext | |
1501 """); | |
1502 }); | |
1503 | |
1504 test('[Example 8.8]', () { | |
1505 expectYamlLoads("\n\nliteral\n \n\ntext\n", | |
1506 """ | |
1507 | | |
1508 | |
1509 | |
1510 literal | |
1511 | |
1512 | |
1513 text | |
1514 | |
1515 # Comment"""); | |
1516 }); | |
1517 | |
1518 test('[Example 8.9]', () { | |
1519 expectYamlLoads("folded text\n", | |
1520 """ | |
1521 > | |
1522 folded | |
1523 text | |
1524 """); | |
1525 }); | |
1526 | |
1527 test('[Example 8.10]', () { | |
1528 expectYamlLoads( | |
1529 cleanUpLiteral(""" | |
1530 | |
1531 folded line | |
1532 next line | |
1533 * bullet | |
1534 | |
1535 * list | |
1536 * lines | |
1537 | |
1538 last line | |
1539 """), | |
1540 """ | |
1541 > | |
1542 | |
1543 folded | |
1544 line | |
1545 | |
1546 next | |
1547 line | |
1548 * bullet | |
1549 | |
1550 * list | |
1551 * lines | |
1552 | |
1553 last | |
1554 line | |
1555 | |
1556 # Comment"""); | |
1557 }); | |
1558 | |
1559 // Examples 8.11 through 8.13 are duplicates of 8.10. | |
1560 }); | |
1561 | |
1562 group('8.2: Block Collection Styles', () { | |
1563 test('[Example 8.14]', () { | |
1564 expectYamlLoads({"block sequence": ["one", {"two": "three"}]}, | |
1565 """ | |
1566 block sequence: | |
1567 - one | |
1568 - two : three"""); | |
1569 }); | |
1570 | |
1571 test('[Example 8.15]', () { | |
1572 expectYamlLoads([ | |
1573 null, "block node\n", ["one", "two"], {"one": "two"} | |
1574 ], | |
1575 """ | |
1576 - # Empty | |
1577 - | | |
1578 block node | |
1579 - - one # Compact | |
1580 - two # sequence | |
1581 - one: two # Compact mapping"""); | |
1582 }); | |
1583 | |
1584 test('[Example 8.16]', () { | |
1585 expectYamlLoads({"block mapping": {"key": "value"}}, | |
1586 """ | |
1587 block mapping: | |
1588 key: value"""); | |
1589 }); | |
1590 | |
1591 test('[Example 8.17]', () { | |
1592 expectYamlLoads({ | |
1593 "explicit key": null, | |
1594 "block key\n": ["one", "two"] | |
1595 }, | |
1596 """ | |
1597 ? explicit key # Empty value | |
1598 ? | | |
1599 block key | |
1600 : - one # Explicit compact | |
1601 - two # block value"""); | |
1602 }); | |
1603 | |
1604 test('[Example 8.18]', () { | |
1605 var doc = yamlMap({ | |
1606 'plain key': 'in-line value', | |
1607 "quoted key": ["entry"] | |
1608 }); | |
1609 doc[null] = null; | |
1610 expectYamlLoads(doc, | |
1611 ''' | |
1612 plain key: in-line value | |
1613 : # Both empty | |
1614 "quoted key": | |
1615 - entry'''); | |
1616 }); | |
1617 | |
1618 test('[Example 8.19]', () { | |
1619 var el = yamlMap(); | |
1620 el[{'earth': 'blue'}] = {'moon': 'white'}; | |
1621 expectYamlLoads([{'sun': 'yellow'}, el], | |
1622 """ | |
1623 - sun: yellow | |
1624 - ? earth: blue | |
1625 : moon: white"""); | |
1626 }); | |
1627 | |
1628 // test('[Example 8.20]', () { | |
1629 // expectYamlLoads(["flow in block", "Block scalar\n", {"foo": "bar"}], | |
1630 // ''' | |
1631 // - | |
1632 // "flow in block" | |
1633 // - > | |
1634 // Block scalar | |
1635 // - !!map # Block collection | |
1636 // foo : bar'''); | |
1637 // }); | |
1638 | |
1639 // test('[Example 8.21]', () { | |
1640 // expectYamlLoads({"literal": "value", "folded": "value"}, | |
1641 // """ | |
1642 // literal: |2 | |
1643 // value | |
1644 // folded: | |
1645 // !!str | |
1646 // >1 | |
1647 // value"""); | |
1648 // }); | |
1649 | |
1650 // test('[Example 8.22]', () { | |
1651 // expectYamlLoads({ | |
1652 // "sequence": ["entry", ["nested"]], | |
1653 // "mapping": {"foo": "bar"} | |
1654 // }, | |
1655 // """ | |
1656 // sequence: !!seq | |
1657 // - entry | |
1658 // - !!seq | |
1659 // - nested | |
1660 // mapping: !!map | |
1661 // foo: bar"""); | |
1662 // }); | |
1663 }); | |
1664 | |
1665 // Chapter 9: YAML Character Stream | |
1666 group('9.1: Documents', () { | |
1667 // Example 9.1 tests the use of a BOM, which this implementation currently | |
1668 // doesn't plan to support. | |
1669 | |
1670 // test('[Example 9.2]', () { | |
1671 // expectYamlLoads("Document", | |
1672 // """ | |
1673 // %YAML 1.2 | |
1674 // --- | |
1675 // Document | |
1676 // ... # Suffix"""); | |
1677 // }); | |
1678 | |
1679 test('[Example 9.3]', () { | |
1680 // The spec example indicates that the comment after "%!PS-Adobe-2.0" | |
1681 // should be stripped, which would imply that that line is not part of the | |
1682 // literal defined by the "|". The rest of the spec is ambiguous on this | |
1683 // point; the allowable indentation for non-indented literal content is | |
1684 // not clearly explained. However, if both the "|" and the text were | |
1685 // indented the same amount, the text would be part of the literal, which | |
1686 // implies that the spec's parse of this document is incorrect. | |
1687 expectYamlStreamLoads( | |
1688 ["Bare document", "%!PS-Adobe-2.0 # Not the first line\n"], | |
1689 """ | |
1690 Bare | |
1691 document | |
1692 ... | |
1693 # No document | |
1694 ... | |
1695 | | |
1696 %!PS-Adobe-2.0 # Not the first line | |
1697 """); | |
1698 }); | |
1699 | |
1700 test('[Example 9.4]', () { | |
1701 expectYamlStreamLoads([{"matches %": 20}, null], | |
1702 """ | |
1703 --- | |
1704 { matches | |
1705 % : 20 } | |
1706 ... | |
1707 --- | |
1708 # Empty | |
1709 ..."""); | |
1710 }); | |
1711 | |
1712 // test('[Example 9.5]', () { | |
1713 // expectYamlStreamLoads(["%!PS-Adobe-2.0\n", null], | |
1714 // """ | |
1715 // %YAML 1.2 | |
1716 // --- | | |
1717 // %!PS-Adobe-2.0 | |
1718 // ... | |
1719 // %YAML1.2 | |
1720 // --- | |
1721 // # Empty | |
1722 // ..."""); | |
1723 // }); | |
1724 | |
1725 // test('[Example 9.6]', () { | |
1726 // expectYamlStreamLoads(["Document", null, {"matches %": 20}], | |
1727 // """ | |
1728 // Document | |
1729 // --- | |
1730 // # Empty | |
1731 // ... | |
1732 // %YAML 1.2 | |
1733 // --- | |
1734 // matches %: 20"""); | |
1735 // }); | |
1736 }); | |
1737 | |
1738 // Chapter 10: Recommended Schemas | |
1739 group('10.1: Failsafe Schema', () { | |
1740 // test('[Example 10.1]', () { | |
1741 // expectYamlStreamLoads({ | |
1742 // "Block style": { | |
1743 // "Clark": "Evans", | |
1744 // "Ingy": "döt Net", | |
1745 // "Oren": "Ben-Kiki" | |
1746 // }, | |
1747 // "Flow style": { | |
1748 // "Clark": "Evans", | |
1749 // "Ingy": "döt Net", | |
1750 // "Oren": "Ben-Kiki" | |
1751 // } | |
1752 // }, | |
1753 // """ | |
1754 // Block style: !!map | |
1755 // Clark : Evans | |
1756 // Ingy : döt Net | |
1757 // Oren : Ben-Kiki | |
1758 | |
1759 // Flow style: !!map { Clark: Evans, Ingy: döt Net, Oren: Ben-Kiki }""")
; | |
1760 // }); | |
1761 | |
1762 // test('[Example 10.2]', () { | |
1763 // expectYamlStreamLoads({ | |
1764 // "Block style": ["Clark Evans", "Ingy döt Net", "Oren Ben-Kiki"], | |
1765 // "Flow style": ["Clark Evans", "Ingy döt Net", "Oren Ben-Kiki"] | |
1766 // }, | |
1767 // """ | |
1768 // Block style: !!seq | |
1769 // - Clark Evans | |
1770 // - Ingy döt Net | |
1771 // - Oren Ben-Kiki | |
1772 | |
1773 // Flow style: !!seq [ Clark Evans, Ingy döt Net, Oren Ben-Kiki ]"""); | |
1774 // }); | |
1775 | |
1776 // test('[Example 10.3]', () { | |
1777 // expectYamlStreamLoads({ | |
1778 // "Block style": "String: just a theory.", | |
1779 // "Flow style": "String: just a theory." | |
1780 // }, | |
1781 // ''' | |
1782 // Block style: !!str |- | |
1783 // String: just a theory. | |
1784 | |
1785 // Flow style: !!str "String: just a theory."'''); | |
1786 // }); | |
1787 }); | |
1788 | |
1789 group('10.2: JSON Schema', () { | |
1790 // test('[Example 10.4]', () { | |
1791 // var doc = yamlMap({"key with null value": null}); | |
1792 // doc[null] = "value for null key"; | |
1793 // expectYamlStreamLoads(doc, | |
1794 // """ | |
1795 // !!null null: value for null key | |
1796 // key with null value: !!null null"""); | |
1797 // }); | |
1798 | |
1799 // test('[Example 10.5]', () { | |
1800 // expectYamlStreamLoads({ | |
1801 // "YAML is a superset of JSON": true, | |
1802 // "Pluto is a planet": false | |
1803 // }, | |
1804 // """ | |
1805 // YAML is a superset of JSON: !!bool true | |
1806 // Pluto is a planet: !!bool false"""); | |
1807 // }); | |
1808 | |
1809 // test('[Example 10.6]', () { | |
1810 // expectYamlStreamLoads({ | |
1811 // "negative": -12, | |
1812 // "zero": 0, | |
1813 // "positive": 34 | |
1814 // }, | |
1815 // """ | |
1816 // negative: !!int -12 | |
1817 // zero: !!int 0 | |
1818 // positive: !!int 34"""); | |
1819 // }); | |
1820 | |
1821 // test('[Example 10.7]', () { | |
1822 // expectYamlStreamLoads({ | |
1823 // "negative": -1, | |
1824 // "zero": 0, | |
1825 // "positive": 23000, | |
1826 // "infinity": infinity, | |
1827 // "not a number": nan | |
1828 // }, | |
1829 // """ | |
1830 // negative: !!float -1 | |
1831 // zero: !!float 0 | |
1832 // positive: !!float 2.3e4 | |
1833 // infinity: !!float .inf | |
1834 // not a number: !!float .nan"""); | |
1835 // }); | |
1836 | |
1837 // test('[Example 10.8]', () { | |
1838 // expectYamlStreamLoads({ | |
1839 // "A null": null, | |
1840 // "Booleans": [true, false], | |
1841 // "Integers": [0, -0, 3, -19], | |
1842 // "Floats": [0, 0, 12000, -200000], | |
1843 // // Despite being invalid in the JSON schema, these values are valid i
n | |
1844 // // the core schema which this implementation supports. | |
1845 // "Invalid": [ true, null, 7, 0x3A, 12.3] | |
1846 // }, | |
1847 // """ | |
1848 // A null: null | |
1849 // Booleans: [ true, false ] | |
1850 // Integers: [ 0, -0, 3, -19 ] | |
1851 // Floats: [ 0., -0.0, 12e03, -2E+05 ] | |
1852 // Invalid: [ True, Null, 0o7, 0x3A, +12.3 ]"""); | |
1853 // }); | |
1854 }); | |
1855 | |
1856 group('10.3: Core Schema', () { | |
1857 test('[Example 10.9]', () { | |
1858 expectYamlLoads({ | |
1859 "A null": null, | |
1860 "Also a null": null, | |
1861 "Not a null": "", | |
1862 "Booleans": [true, true, false, false], | |
1863 "Integers": [0, 7, 0x3A, -19], | |
1864 "Floats": [0, 0, 0.5, 12000, -200000], | |
1865 "Also floats": [infinity, -infinity, infinity, nan] | |
1866 }, | |
1867 ''' | |
1868 A null: null | |
1869 Also a null: # Empty | |
1870 Not a null: "" | |
1871 Booleans: [ true, True, false, FALSE ] | |
1872 Integers: [ 0, 0o7, 0x3A, -19 ] | |
1873 Floats: [ 0., -0.0, .5, +12e03, -2E+05 ] | |
1874 Also floats: [ .inf, -.Inf, +.INF, .NAN ]'''); | |
1875 }); | |
1876 }); | |
1877 } | |
OLD | NEW |