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