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