| 1 | package parser_test |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "path/filepath" |
| 6 | "reflect" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/goccy/go-yaml" |
| 12 | "github.com/goccy/go-yaml/ast" |
| 13 | "github.com/goccy/go-yaml/lexer" |
| 14 | "github.com/goccy/go-yaml/parser" |
| 15 | "github.com/goccy/go-yaml/token" |
| 16 | ) |
| 17 | |
| 18 | func TestParser(t *testing.T) { |
| 19 | sources := []string{ |
| 20 | "", |
| 21 | "null\n", |
| 22 | "0_", |
| 23 | "{}\n", |
| 24 | "v: hi\n", |
| 25 | "v: \"true\"\n", |
| 26 | "v: \"false\"\n", |
| 27 | "v: true\n", |
| 28 | "v: false\n", |
| 29 | "v: 10\n", |
| 30 | "v: -10\n", |
| 31 | "v: 42\n", |
| 32 | "v: 4294967296\n", |
| 33 | "v: \"10\"\n", |
| 34 | "v: 0.1\n", |
| 35 | "v: 0.99\n", |
| 36 | "v: -0.1\n", |
| 37 | "v: .inf\n", |
| 38 | "v: -.inf\n", |
| 39 | "v: .nan\n", |
| 40 | "v: null\n", |
| 41 | "v: \"\"\n", |
| 42 | "v:\n- A\n- B\n", |
| 43 | "a: '-'\n", |
| 44 | "123\n", |
| 45 | "hello: world\n", |
| 46 | "a: null\n", |
| 47 | "v:\n- A\n- 1\n- B:\n - 2\n - 3\n", |
| 48 | "a:\n b: c\n", |
| 49 | "a: {x: 1}\n", |
| 50 | "t2: 2018-01-09T10:40:47Z\nt4: 2098-01-09T10:40:47Z\n", |
| 51 | "a: [1, 2]\n", |
| 52 | "a: {b: c, d: e}\n", |
| 53 | "a: 3s\n", |
| 54 | "a: <foo>\n", |
| 55 | "a: \"1:1\"\n", |
| 56 | "a: 1.2.3.4\n", |
| 57 | "a: \"2015-02-24T18:19:39Z\"\n", |
| 58 | "a: 'b: c'\n", |
| 59 | "a: 'Hello #comment'\n", |
| 60 | "a: abc <<def>> ghi", |
| 61 | "a: <<abcd", |
| 62 | "a: <<:abcd", |
| 63 | "a: << :abcd", |
| 64 | "a: 100.5\n", |
| 65 | "a: bogus\n", |
| 66 | "a: \"\\0\"\n", |
| 67 | "b: 2\na: 1\nd: 4\nc: 3\nsub:\n e: 5\n", |
| 68 | " a : b \n", |
| 69 | "a: b # comment\nb: c\n", |
| 70 | "---\na: b\n", |
| 71 | "a: b\n...\n", |
| 72 | "%YAML 1.2\n---\n", |
| 73 | "a: !!binary gIGC\n", |
| 74 | "a: !!binary |\n " + strings.Repeat("kJCQ", 17) + "kJ\n CQ\n", |
| 75 | "v: !!foo 1", |
| 76 | "v:\n- A\n- |-\n B\n C\n", |
| 77 | "v:\n- A\n- >-\n B\n C\n", |
| 78 | "v: |-\n 0\n", |
| 79 | "v: |-\n 0\nx: 0", |
| 80 | `"a\n1\nb"`, |
| 81 | `{"a":"b"}`, |
| 82 | `!!map { |
| 83 | ? !!str "explicit":!!str "entry", |
| 84 | ? !!str "implicit" : !!str "entry", |
| 85 | ? !!null "" : !!null "", |
| 86 | }`, |
| 87 | "\"a\": a\n\"b\": b", |
| 88 | "'a': a\n'b': b", |
| 89 | "a: \r\n b: 1\r\n", |
| 90 | "a_ok: \r bc: 2\r", |
| 91 | "a_mk: \n bd: 3\n", |
| 92 | "a: :a", |
| 93 | "{a: , b: c}", |
| 94 | "value: >\n", |
| 95 | "value: >\n\n", |
| 96 | "value: >\nother:", |
| 97 | "value: >\n\nother:", |
| 98 | "a:\n-", |
| 99 | "a: {foo}", |
| 100 | "a: {foo,bar}", |
| 101 | ` |
| 102 | { |
| 103 | a: { |
| 104 | b: c |
| 105 | }, |
| 106 | d: e |
| 107 | } |
| 108 | `, |
| 109 | ` |
| 110 | [ |
| 111 | a: { |
| 112 | b: c |
| 113 | }] |
| 114 | `, |
| 115 | ` |
| 116 | { |
| 117 | a: { |
| 118 | b: c |
| 119 | }} |
| 120 | `, |
| 121 | ` |
| 122 | - !tag |
| 123 | a: b |
| 124 | c: d |
| 125 | `, |
| 126 | ` |
| 127 | a: !tag |
| 128 | b: c |
| 129 | `, |
| 130 | ` |
| 131 | a: !tag |
| 132 | b: c |
| 133 | d: e |
| 134 | `, |
| 135 | ` |
| 136 | a: |
| 137 | b: c |
| 138 | |
| 139 | `, |
| 140 | ` |
| 141 | foo: xxx |
| 142 | --- |
| 143 | foo: yyy |
| 144 | --- |
| 145 | foo: zzz |
| 146 | `, |
| 147 | ` |
| 148 | v: |
| 149 | a : 'a' |
| 150 | bb : 'a' |
| 151 | `, |
| 152 | ` |
| 153 | v: |
| 154 | a : 'x' |
| 155 | b : 'y' |
| 156 | `, |
| 157 | ` |
| 158 | v: |
| 159 | a : 'x' |
| 160 | b : 'y' |
| 161 | c : 'z' |
| 162 | `, |
| 163 | `{a: &a c, *a : b}`, |
| 164 | } |
| 165 | for idx, src := range sources { |
| 166 | t.Run(strconv.Itoa(idx), func(t *testing.T) { |
| 167 | f, err := parser.Parse(lexer.Tokenize(src), 0) |
| 168 | if err != nil { |
| 169 | t.Fatalf("parse error: source [%s]: %+v", src, err) |
| 170 | } |
| 171 | _ = f.String() // ensure no panic |
| 172 | }) |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | func TestParseEmptyDocument(t *testing.T) { |
| 177 | t.Run("empty document", func(t *testing.T) { |
| 178 | f, err := parser.ParseBytes([]byte(""), parser.ParseComments) |
| 179 | if err != nil { |
| 180 | t.Fatal(err) |
| 181 | } |
| 182 | got := f.String() |
| 183 | expected := "\n" |
| 184 | if got != expected { |
| 185 | t.Fatalf("failed to parse comment:\nexpected:\n%q\ngot:\n%q", expected, got) |
| 186 | } |
| 187 | }) |
| 188 | |
| 189 | t.Run("empty document with comment (parse comment = off)", func(t *testing.T) { |
| 190 | f, err := parser.ParseBytes([]byte("# comment"), 0) |
| 191 | if err != nil { |
| 192 | t.Fatal(err) |
| 193 | } |
| 194 | got := f.String() |
| 195 | expected := "\n" |
| 196 | if got != expected { |
| 197 | t.Fatalf("failed to parse comment:\nexpected:\n%q\ngot:\n%q", expected, got) |
| 198 | } |
| 199 | }) |
| 200 | |
| 201 | t.Run("empty document with comment (parse comment = on)", func(t *testing.T) { |
| 202 | f, err := parser.ParseBytes([]byte("# comment"), parser.ParseComments) |
| 203 | if err != nil { |
| 204 | t.Fatal(err) |
| 205 | } |
| 206 | got := f.String() |
| 207 | expected := "# comment\n" |
| 208 | if got != expected { |
| 209 | t.Fatalf("failed to parse comment:\nexpected:\n%q\ngot:\n%q", expected, got) |
| 210 | } |
| 211 | }) |
| 212 | } |
| 213 | |
| 214 | func TestParseComplicatedDocument(t *testing.T) { |
| 215 | tests := []struct { |
| 216 | source string |
| 217 | expect string |
| 218 | }{ |
| 219 | { |
| 220 | ` |
| 221 | american: |
| 222 | - Boston Red Sox |
| 223 | - Detroit Tigers |
| 224 | - New York Yankees |
| 225 | national: |
| 226 | - New York Mets |
| 227 | - Chicago Cubs |
| 228 | - Atlanta Braves |
| 229 | `, ` |
| 230 | american: |
| 231 | - Boston Red Sox |
| 232 | - Detroit Tigers |
| 233 | - New York Yankees |
| 234 | national: |
| 235 | - New York Mets |
| 236 | - Chicago Cubs |
| 237 | - Atlanta Braves |
| 238 | `, |
| 239 | }, |
| 240 | { |
| 241 | ` |
| 242 | a: |
| 243 | b: c |
| 244 | d: e |
| 245 | f: g |
| 246 | h: |
| 247 | i: j |
| 248 | k: |
| 249 | l: m |
| 250 | n: o |
| 251 | p: q |
| 252 | r: s |
| 253 | `, ` |
| 254 | a: |
| 255 | b: c |
| 256 | d: e |
| 257 | f: g |
| 258 | h: |
| 259 | i: j |
| 260 | k: |
| 261 | l: m |
| 262 | n: o |
| 263 | p: q |
| 264 | r: s |
| 265 | `, |
| 266 | }, |
| 267 | { |
| 268 | ` |
| 269 | - a: |
| 270 | - b |
| 271 | - c |
| 272 | - d |
| 273 | `, ` |
| 274 | - a: |
| 275 | - b |
| 276 | - c |
| 277 | - d |
| 278 | `, |
| 279 | }, |
| 280 | { |
| 281 | ` |
| 282 | - a |
| 283 | - b |
| 284 | - c |
| 285 | - d |
| 286 | - e |
| 287 | - f |
| 288 | `, ` |
| 289 | - a |
| 290 | - b |
| 291 | - c - d - e |
| 292 | - f |
| 293 | `, |
| 294 | }, |
| 295 | { |
| 296 | ` |
| 297 | a: 0 - 1 |
| 298 | `, |
| 299 | ` |
| 300 | a: 0 - 1 |
| 301 | `, |
| 302 | }, |
| 303 | {` |
| 304 | - a: |
| 305 | b: c |
| 306 | d: e |
| 307 | - f: |
| 308 | g: h |
| 309 | `, |
| 310 | ` |
| 311 | - a: |
| 312 | b: c |
| 313 | d: e |
| 314 | - f: |
| 315 | g: h |
| 316 | `, |
| 317 | }, |
| 318 | { |
| 319 | ` |
| 320 | a: |
| 321 | b |
| 322 | c |
| 323 | d: e |
| 324 | `, ` |
| 325 | a: b c |
| 326 | d: e |
| 327 | `, |
| 328 | }, |
| 329 | { |
| 330 | ` |
| 331 | a |
| 332 | b |
| 333 | c |
| 334 | `, ` |
| 335 | a b c |
| 336 | `, |
| 337 | }, |
| 338 | { |
| 339 | ` |
| 340 | a: |
| 341 | - b |
| 342 | - c |
| 343 | `, ` |
| 344 | a: |
| 345 | - b |
| 346 | - c |
| 347 | `, |
| 348 | }, |
| 349 | { |
| 350 | ` |
| 351 | - a : |
| 352 | b: c |
| 353 | `, ` |
| 354 | - a: |
| 355 | b: c |
| 356 | `, |
| 357 | }, |
| 358 | { |
| 359 | ` |
| 360 | - a: |
| 361 | b |
| 362 | c |
| 363 | d |
| 364 | hoge: fuga |
| 365 | `, ` |
| 366 | - a: b c d |
| 367 | hoge: fuga |
| 368 | `, |
| 369 | }, |
| 370 | { |
| 371 | ` |
| 372 | - a # ' " # - : % |
| 373 | - b # " # - : % ' |
| 374 | - c # # - : % ' " |
| 375 | - d # - : % ' " # |
| 376 | - e # : % ' " # - |
| 377 | - f # % ' : # - : |
| 378 | `, |
| 379 | ` |
| 380 | - a |
| 381 | - b |
| 382 | - c |
| 383 | - d |
| 384 | - e |
| 385 | - f |
| 386 | `, |
| 387 | }, |
| 388 | { |
| 389 | ` |
| 390 | # comment |
| 391 | a: # comment |
| 392 | # comment |
| 393 | b: c # comment |
| 394 | # comment |
| 395 | d: e # comment |
| 396 | # comment |
| 397 | `, |
| 398 | ` |
| 399 | a: |
| 400 | b: c |
| 401 | d: e |
| 402 | `, |
| 403 | }, |
| 404 | { |
| 405 | ` |
| 406 | a: b#notcomment |
| 407 | `, |
| 408 | ` |
| 409 | a: b#notcomment |
| 410 | `, |
| 411 | }, |
| 412 | { |
| 413 | ` |
| 414 | anchored: &anchor foo |
| 415 | aliased: *anchor |
| 416 | `, |
| 417 | ` |
| 418 | anchored: &anchor foo |
| 419 | aliased: *anchor |
| 420 | `, |
| 421 | }, |
| 422 | { |
| 423 | ` |
| 424 | --- |
| 425 | - &CENTER { x: 1, y: 2 } |
| 426 | - &LEFT { x: 0, y: 2 } |
| 427 | - &BIG { r: 10 } |
| 428 | - &SMALL { r: 1 } |
| 429 | |
| 430 | # All the following maps are equal: |
| 431 | |
| 432 | - # Explicit keys |
| 433 | x: 1 |
| 434 | y: 2 |
| 435 | r: 10 |
| 436 | label: center/big |
| 437 | |
| 438 | - # Merge one map |
| 439 | << : *CENTER |
| 440 | r: 10 |
| 441 | label: center/big |
| 442 | |
| 443 | - # Merge multiple maps |
| 444 | << : [ *CENTER, *BIG ] |
| 445 | label: center/big |
| 446 | |
| 447 | - # Override |
| 448 | << : [ *BIG, *LEFT, *SMALL ] |
| 449 | x: 1 |
| 450 | label: center/big |
| 451 | `, |
| 452 | ` |
| 453 | --- |
| 454 | - &CENTER {x: 1, y: 2} |
| 455 | - &LEFT {x: 0, y: 2} |
| 456 | - &BIG {r: 10} |
| 457 | - &SMALL {r: 1} |
| 458 | - x: 1 |
| 459 | y: 2 |
| 460 | r: 10 |
| 461 | label: center/big |
| 462 | - <<: *CENTER |
| 463 | r: 10 |
| 464 | label: center/big |
| 465 | - <<: [*CENTER, *BIG] |
| 466 | label: center/big |
| 467 | - <<: [*BIG, *LEFT, *SMALL] |
| 468 | x: 1 |
| 469 | label: center/big |
| 470 | `, |
| 471 | }, |
| 472 | { |
| 473 | ` |
| 474 | a: |
| 475 | - - b |
| 476 | - - c |
| 477 | - d |
| 478 | `, |
| 479 | ` |
| 480 | a: |
| 481 | - - b |
| 482 | - - c |
| 483 | - d |
| 484 | `, |
| 485 | }, |
| 486 | { |
| 487 | ` |
| 488 | a: |
| 489 | b: |
| 490 | c: d |
| 491 | e: |
| 492 | f: g |
| 493 | h: i |
| 494 | j: k |
| 495 | `, |
| 496 | ` |
| 497 | a: |
| 498 | b: |
| 499 | c: d |
| 500 | e: |
| 501 | f: g |
| 502 | h: i |
| 503 | j: k |
| 504 | `, |
| 505 | }, |
| 506 | { |
| 507 | ` |
| 508 | --- |
| 509 | a: 1 |
| 510 | b: 2 |
| 511 | ... |
| 512 | --- |
| 513 | c: 3 |
| 514 | d: 4 |
| 515 | ... |
| 516 | `, |
| 517 | ` |
| 518 | --- |
| 519 | a: 1 |
| 520 | b: 2 |
| 521 | ... |
| 522 | --- |
| 523 | c: 3 |
| 524 | d: 4 |
| 525 | ... |
| 526 | `, |
| 527 | }, |
| 528 | { |
| 529 | ` |
| 530 | a: |
| 531 | b: | |
| 532 | { |
| 533 | [ 1, 2 ] |
| 534 | } |
| 535 | c: d |
| 536 | `, |
| 537 | ` |
| 538 | a: |
| 539 | b: | |
| 540 | { |
| 541 | [ 1, 2 ] |
| 542 | } |
| 543 | c: d |
| 544 | `, |
| 545 | }, |
| 546 | { |
| 547 | ` |
| 548 | | |
| 549 | hoge |
| 550 | fuga |
| 551 | piyo`, |
| 552 | ` |
| 553 | | |
| 554 | hoge |
| 555 | fuga |
| 556 | piyo |
| 557 | `, |
| 558 | }, |
| 559 | { |
| 560 | ` |
| 561 | v: | |
| 562 | a |
| 563 | b |
| 564 | c`, |
| 565 | ` |
| 566 | v: | |
| 567 | a |
| 568 | b |
| 569 | c |
| 570 | `, |
| 571 | }, |
| 572 | |
| 573 | { |
| 574 | ` |
| 575 | a: | |
| 576 | bbbbbbb |
| 577 | |
| 578 | |
| 579 | ccccccc |
| 580 | d: eeeeeeeeeeeeeeeee |
| 581 | `, |
| 582 | ` |
| 583 | a: | |
| 584 | bbbbbbb |
| 585 | |
| 586 | |
| 587 | ccccccc |
| 588 | d: eeeeeeeeeeeeeeeee |
| 589 | `, |
| 590 | }, |
| 591 | { |
| 592 | ` |
| 593 | a: b |
| 594 | c |
| 595 | `, |
| 596 | ` |
| 597 | a: b c |
| 598 | `, |
| 599 | }, |
| 600 | { |
| 601 | ` |
| 602 | a: |
| 603 | b: c |
| 604 | `, |
| 605 | ` |
| 606 | a: |
| 607 | b: c |
| 608 | `, |
| 609 | }, |
| 610 | { |
| 611 | ` |
| 612 | a: b |
| 613 | c: d |
| 614 | `, |
| 615 | ` |
| 616 | a: b |
| 617 | c: d |
| 618 | `, |
| 619 | }, |
| 620 | { |
| 621 | ` |
| 622 | - ab - cd |
| 623 | - ef - gh |
| 624 | `, |
| 625 | ` |
| 626 | - ab - cd |
| 627 | - ef - gh |
| 628 | `, |
| 629 | }, |
| 630 | { |
| 631 | ` |
| 632 | - 0 - 1 |
| 633 | - 2 - 3 |
| 634 | `, |
| 635 | ` |
| 636 | - 0 - 1 - 2 - 3 |
| 637 | `, |
| 638 | }, |
| 639 | { |
| 640 | ` |
| 641 | a - b - c: value |
| 642 | `, |
| 643 | ` |
| 644 | a - b - c: value |
| 645 | `, |
| 646 | }, |
| 647 | { |
| 648 | ` |
| 649 | a: |
| 650 | - |
| 651 | b: c |
| 652 | d: e |
| 653 | - |
| 654 | f: g |
| 655 | h: i |
| 656 | `, |
| 657 | ` |
| 658 | a: |
| 659 | - b: c |
| 660 | d: e |
| 661 | - f: g |
| 662 | h: i |
| 663 | `, |
| 664 | }, |
| 665 | { |
| 666 | ` |
| 667 | a: |- |
| 668 | value |
| 669 | b: c |
| 670 | `, |
| 671 | ` |
| 672 | a: |- |
| 673 | value |
| 674 | b: c |
| 675 | `, |
| 676 | }, |
| 677 | { |
| 678 | ` |
| 679 | a: |+ |
| 680 | value |
| 681 | b: c |
| 682 | `, |
| 683 | ` |
| 684 | a: |+ |
| 685 | value |
| 686 | b: c |
| 687 | `, |
| 688 | }, |
| 689 | { |
| 690 | ` |
| 691 | - key1: val |
| 692 | key2: |
| 693 | ( |
| 694 | foo |
| 695 | + |
| 696 | bar |
| 697 | ) |
| 698 | `, |
| 699 | ` |
| 700 | - key1: val |
| 701 | key2: ( foo + bar ) |
| 702 | `, |
| 703 | }, |
| 704 | { |
| 705 | ` |
| 706 | "a": b |
| 707 | 'c': d |
| 708 | "e": "f" |
| 709 | g: "h" |
| 710 | i: 'j' |
| 711 | `, |
| 712 | ` |
| 713 | "a": b |
| 714 | 'c': d |
| 715 | "e": "f" |
| 716 | g: "h" |
| 717 | i: 'j' |
| 718 | `, |
| 719 | }, |
| 720 | { |
| 721 | ` |
| 722 | a: |
| 723 | - |2 |
| 724 | b |
| 725 | c: d |
| 726 | `, |
| 727 | ` |
| 728 | a: |
| 729 | - |2 |
| 730 | b |
| 731 | c: d |
| 732 | `, |
| 733 | }, |
| 734 | { |
| 735 | ` |
| 736 | a: |
| 737 | b: &anchor |
| 738 | c: &anchor2 |
| 739 | d: e |
| 740 | `, |
| 741 | ` |
| 742 | a: |
| 743 | b: &anchor |
| 744 | c: &anchor2 |
| 745 | d: e |
| 746 | `, |
| 747 | }, |
| 748 | } |
| 749 | |
| 750 | for _, test := range tests { |
| 751 | t.Run(test.source, func(t *testing.T) { |
| 752 | tokens := lexer.Tokenize(test.source) |
| 753 | f, err := parser.Parse(tokens, 0) |
| 754 | if err != nil { |
| 755 | t.Fatalf("%+v", err) |
| 756 | } |
| 757 | got := f.String() |
| 758 | if got != strings.TrimPrefix(test.expect, "\n") { |
| 759 | t.Fatalf("failed to parse comment:\nexpected:\n%s\ngot:\n%s", strings.TrimPrefix(test.expect, "\n"), got) |
| 760 | } |
| 761 | var v Visitor |
| 762 | for _, doc := range f.Docs { |
| 763 | ast.Walk(&v, doc.Body) |
| 764 | } |
| 765 | expect := fmt.Sprintf("\n%+v", f) |
| 766 | if test.expect != expect { |
| 767 | tokens.Dump() |
| 768 | t.Fatalf("unexpected output: [%s] != [%s]", test.expect, expect) |
| 769 | } |
| 770 | }) |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | func TestParseWhitespace(t *testing.T) { |
| 775 | tests := []struct { |
| 776 | source string |
| 777 | expect string |
| 778 | }{ |
| 779 | { |
| 780 | ` |
| 781 | a: b |
| 782 | |
| 783 | c: d |
| 784 | |
| 785 | |
| 786 | e: f |
| 787 | g: h |
| 788 | `, |
| 789 | ` |
| 790 | a: b |
| 791 | |
| 792 | c: d |
| 793 | |
| 794 | e: f |
| 795 | g: h |
| 796 | `, |
| 797 | }, |
| 798 | { |
| 799 | ` |
| 800 | a: |
| 801 | - b: c |
| 802 | d: e |
| 803 | |
| 804 | - f: g |
| 805 | h: i |
| 806 | `, |
| 807 | ` |
| 808 | a: |
| 809 | - b: c |
| 810 | d: e |
| 811 | |
| 812 | - f: g |
| 813 | h: i |
| 814 | `, |
| 815 | }, |
| 816 | { |
| 817 | ` |
| 818 | a: |
| 819 | - b: c |
| 820 | d: e |
| 821 | |
| 822 | - f: g |
| 823 | h: i |
| 824 | `, |
| 825 | ` |
| 826 | a: |
| 827 | - b: c |
| 828 | d: e |
| 829 | |
| 830 | - f: g |
| 831 | h: i |
| 832 | `, |
| 833 | }, |
| 834 | { |
| 835 | ` |
| 836 | a: |
| 837 | - b: c |
| 838 | d: e |
| 839 | |
| 840 | - f: g |
| 841 | h: i |
| 842 | `, |
| 843 | ` |
| 844 | a: |
| 845 | - b: c |
| 846 | d: e |
| 847 | |
| 848 | - f: g |
| 849 | h: i |
| 850 | `, |
| 851 | }, |
| 852 | { |
| 853 | ` |
| 854 | a: |
| 855 | # comment 1 |
| 856 | - b: c |
| 857 | d: e |
| 858 | |
| 859 | # comment 2 |
| 860 | - f: g |
| 861 | h: i |
| 862 | `, |
| 863 | ` |
| 864 | a: |
| 865 | # comment 1 |
| 866 | - b: c |
| 867 | d: e |
| 868 | |
| 869 | # comment 2 |
| 870 | - f: g |
| 871 | h: i |
| 872 | `, |
| 873 | }, |
| 874 | { |
| 875 | ` |
| 876 | a: |
| 877 | # comment 1 |
| 878 | - b: c |
| 879 | # comment 2 |
| 880 | d: e |
| 881 | |
| 882 | # comment 3 |
| 883 | # comment 4 |
| 884 | - f: g |
| 885 | h: i # comment 5 |
| 886 | `, |
| 887 | ` |
| 888 | a: |
| 889 | # comment 1 |
| 890 | - b: c |
| 891 | # comment 2 |
| 892 | d: e |
| 893 | |
| 894 | # comment 3 |
| 895 | # comment 4 |
| 896 | - f: g |
| 897 | h: i # comment 5 |
| 898 | `, |
| 899 | }, |
| 900 | { |
| 901 | ` |
| 902 | a: |
| 903 | # comment 1 |
| 904 | - b: c |
| 905 | # comment 2 |
| 906 | d: e |
| 907 | |
| 908 | # comment 3 |
| 909 | # comment 4 |
| 910 | - f: | |
| 911 | g |
| 912 | g |
| 913 | h: i # comment 5 |
| 914 | `, |
| 915 | ` |
| 916 | a: |
| 917 | # comment 1 |
| 918 | - b: c |
| 919 | # comment 2 |
| 920 | d: e |
| 921 | |
| 922 | # comment 3 |
| 923 | # comment 4 |
| 924 | - f: | |
| 925 | g |
| 926 | g |
| 927 | h: i # comment 5 |
| 928 | `, |
| 929 | }, |
| 930 | { |
| 931 | ` |
| 932 | a: |
| 933 | # comment 1 |
| 934 | - b: c |
| 935 | # comment 2 |
| 936 | d: e |
| 937 | |
| 938 | # comment 3 |
| 939 | # comment 4 |
| 940 | - f: | |
| 941 | asd |
| 942 | def |
| 943 | |
| 944 | h: i # comment 5 |
| 945 | `, |
| 946 | ` |
| 947 | a: |
| 948 | # comment 1 |
| 949 | - b: c |
| 950 | # comment 2 |
| 951 | d: e |
| 952 | |
| 953 | # comment 3 |
| 954 | # comment 4 |
| 955 | - f: | |
| 956 | asd |
| 957 | def |
| 958 | |
| 959 | h: i # comment 5 |
| 960 | `, |
| 961 | }, |
| 962 | { |
| 963 | ` |
| 964 | - b: c |
| 965 | d: e |
| 966 | |
| 967 | - f: g |
| 968 | h: i # comment 4 |
| 969 | `, |
| 970 | ` |
| 971 | - b: c |
| 972 | d: e |
| 973 | |
| 974 | - f: g |
| 975 | h: i # comment 4 |
| 976 | `, |
| 977 | }, |
| 978 | { |
| 979 | ` |
| 980 | a: null |
| 981 | b: null |
| 982 | |
| 983 | d: e |
| 984 | `, |
| 985 | ` |
| 986 | a: null |
| 987 | b: null |
| 988 | |
| 989 | d: e |
| 990 | `, |
| 991 | }, |
| 992 | { |
| 993 | ` |
| 994 | foo: |
| 995 | bar: null # comment |
| 996 | |
| 997 | baz: 1 |
| 998 | `, |
| 999 | ` |
| 1000 | foo: |
| 1001 | bar: null # comment |
| 1002 | |
| 1003 | baz: 1 |
| 1004 | `, |
| 1005 | }, |
| 1006 | { |
| 1007 | ` |
| 1008 | foo: |
| 1009 | bar: null # comment |
| 1010 | |
| 1011 | baz: 1 |
| 1012 | `, |
| 1013 | ` |
| 1014 | foo: |
| 1015 | bar: null # comment |
| 1016 | |
| 1017 | baz: 1 |
| 1018 | `, |
| 1019 | }, |
| 1020 | { |
| 1021 | ` |
| 1022 | { |
| 1023 | "apiVersion": "apps/v1", |
| 1024 | "kind": "Deployment", |
| 1025 | "metadata": { |
| 1026 | "name": "foo", |
| 1027 | "labels": { |
| 1028 | "app": "bar" |
| 1029 | } |
| 1030 | }, |
| 1031 | "spec": { |
| 1032 | "replicas": 3, |
| 1033 | "selector": { |
| 1034 | "matchLabels": { |
| 1035 | "app": "bar" |
| 1036 | } |
| 1037 | }, |
| 1038 | "template": { |
| 1039 | "metadata": { |
| 1040 | "labels": { |
| 1041 | "app": "bar" |
| 1042 | } |
| 1043 | } |
| 1044 | } |
| 1045 | } |
| 1046 | } |
| 1047 | `, |
| 1048 | ` |
| 1049 | {"apiVersion": "apps/v1", "kind": "Deployment", "metadata": {"name": "foo", "labels": {"app": "bar"}}, "spec": {"replicas": 3, "selector": {"matchLabels": {"app": "bar"}}, "template": {"metadata": {"labels": {"app": "bar"}}}}} |
| 1050 | `, |
| 1051 | }, |
| 1052 | } |
| 1053 | |
| 1054 | for _, test := range tests { |
| 1055 | t.Run(test.source, func(t *testing.T) { |
| 1056 | f, err := parser.ParseBytes([]byte(test.source), parser.ParseComments) |
| 1057 | if err != nil { |
| 1058 | t.Fatal(err) |
| 1059 | } |
| 1060 | got := f.String() |
| 1061 | if got != strings.TrimPrefix(test.expect, "\n") { |
| 1062 | t.Fatalf("failed to parse comment:\nexpected:\n%s\ngot:\n%s", strings.TrimPrefix(test.expect, "\n"), got) |
| 1063 | } |
| 1064 | }) |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | func TestNewLineChar(t *testing.T) { |
| 1069 | for _, f := range []string{ |
| 1070 | "lf.yml", |
| 1071 | "cr.yml", |
| 1072 | "crlf.yml", |
| 1073 | } { |
| 1074 | ast, err := parser.ParseFile(filepath.Join("testdata", f), 0) |
| 1075 | if err != nil { |
| 1076 | t.Fatalf("%+v", err) |
| 1077 | } |
| 1078 | actual := fmt.Sprintf("%v", ast) |
| 1079 | expect := `a: "a" |
| 1080 | |
| 1081 | b: 1 |
| 1082 | ` |
| 1083 | if expect != actual { |
| 1084 | t.Fatalf("unexpected result\nexpected:\n%s\ngot:\n%s", expect, actual) |
| 1085 | } |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | func TestSyntaxError(t *testing.T) { |
| 1090 | tests := []struct { |
| 1091 | source string |
| 1092 | expect string |
| 1093 | }{ |
| 1094 | { |
| 1095 | ` |
| 1096 | a: |
| 1097 | - b |
| 1098 | c: d |
| 1099 | e: f |
| 1100 | g: h`, |
| 1101 | ` |
| 1102 | [3:3] unexpected key name |
| 1103 | 2 | a: |
| 1104 | > 3 | - b |
| 1105 | 4 | c: d |
| 1106 | ^ |
| 1107 | 5 | e: f |
| 1108 | 6 | g: h`, |
| 1109 | }, |
| 1110 | { |
| 1111 | ` |
| 1112 | a |
| 1113 | - b: c`, |
| 1114 | ` |
| 1115 | [2:1] unexpected key name |
| 1116 | > 2 | a |
| 1117 | 3 | - b: c |
| 1118 | ^ |
| 1119 | `, |
| 1120 | }, |
| 1121 | { |
| 1122 | `%YAML 1.1 {}`, |
| 1123 | ` |
| 1124 | [1:1] unexpected directive value. document not started |
| 1125 | > 1 | %YAML 1.1 {} |
| 1126 | ^ |
| 1127 | `, |
| 1128 | }, |
| 1129 | { |
| 1130 | `{invalid`, |
| 1131 | ` |
| 1132 | [1:2] could not find flow map content |
| 1133 | > 1 | {invalid |
| 1134 | ^ |
| 1135 | `, |
| 1136 | }, |
| 1137 | { |
| 1138 | `{ "key": "value" `, |
| 1139 | ` |
| 1140 | [1:1] could not find flow mapping end token '}' |
| 1141 | > 1 | { "key": "value" |
| 1142 | ^ |
| 1143 | `, |
| 1144 | }, |
| 1145 | { |
| 1146 | ` |
| 1147 | a: |invalidopt |
| 1148 | foo |
| 1149 | `, |
| 1150 | ` |
| 1151 | [2:4] invalid header option: "invalidopt" |
| 1152 | > 2 | a: |invalidopt |
| 1153 | ^ |
| 1154 | 3 | foo`, |
| 1155 | }, |
| 1156 | { |
| 1157 | ` |
| 1158 | a: 1 |
| 1159 | b |
| 1160 | `, |
| 1161 | ` |
| 1162 | [3:1] non-map value is specified |
| 1163 | 2 | a: 1 |
| 1164 | > 3 | b |
| 1165 | ^ |
| 1166 | `, |
| 1167 | }, |
| 1168 | { |
| 1169 | ` |
| 1170 | a: 'b' |
| 1171 | c: d |
| 1172 | `, |
| 1173 | ` |
| 1174 | [3:3] value is not allowed in this context. map key-value is pre-defined |
| 1175 | 2 | a: 'b' |
| 1176 | > 3 | c: d |
| 1177 | ^ |
| 1178 | `, |
| 1179 | }, |
| 1180 | { |
| 1181 | ` |
| 1182 | a: 'b' |
| 1183 | - c |
| 1184 | `, |
| 1185 | ` |
| 1186 | [3:3] value is not allowed in this context. map key-value is pre-defined |
| 1187 | 2 | a: 'b' |
| 1188 | > 3 | - c |
| 1189 | ^ |
| 1190 | `, |
| 1191 | }, |
| 1192 | { |
| 1193 | ` |
| 1194 | a: 'b' |
| 1195 | # comment |
| 1196 | - c |
| 1197 | `, |
| 1198 | ` |
| 1199 | [4:3] value is not allowed in this context. map key-value is pre-defined |
| 1200 | 2 | a: 'b' |
| 1201 | 3 | # comment |
| 1202 | > 4 | - c |
| 1203 | ^ |
| 1204 | `, |
| 1205 | }, |
| 1206 | { |
| 1207 | ` |
| 1208 | a: 1 |
| 1209 | b |
| 1210 | - c |
| 1211 | `, |
| 1212 | ` |
| 1213 | [3:1] non-map value is specified |
| 1214 | 2 | a: 1 |
| 1215 | > 3 | b |
| 1216 | ^ |
| 1217 | 4 | - c`, |
| 1218 | }, |
| 1219 | { |
| 1220 | `a: [`, |
| 1221 | ` |
| 1222 | [1:4] sequence end token ']' not found |
| 1223 | > 1 | a: [ |
| 1224 | ^ |
| 1225 | `, |
| 1226 | }, |
| 1227 | { |
| 1228 | `a: ]`, |
| 1229 | ` |
| 1230 | [1:4] could not find '[' character corresponding to ']' |
| 1231 | > 1 | a: ] |
| 1232 | ^ |
| 1233 | `, |
| 1234 | }, |
| 1235 | { |
| 1236 | `a: [ [1] [2] [3] ]`, |
| 1237 | ` |
| 1238 | [1:10] ',' or ']' must be specified |
| 1239 | > 1 | a: [ [1] [2] [3] ] |
| 1240 | ^ |
| 1241 | `, |
| 1242 | }, |
| 1243 | { |
| 1244 | ` |
| 1245 | a: - |
| 1246 | b: - |
| 1247 | `, |
| 1248 | ` |
| 1249 | [2:4] block sequence entries are not allowed in this context |
| 1250 | > 2 | a: - |
| 1251 | ^ |
| 1252 | 3 | b: -`, |
| 1253 | }, |
| 1254 | { |
| 1255 | ` |
| 1256 | a: - 1 |
| 1257 | b: - 2 |
| 1258 | `, |
| 1259 | ` |
| 1260 | [2:4] block sequence entries are not allowed in this context |
| 1261 | > 2 | a: - 1 |
| 1262 | ^ |
| 1263 | 3 | b: - 2`, |
| 1264 | }, |
| 1265 | { |
| 1266 | `a: 'foobarbaz`, |
| 1267 | ` |
| 1268 | [1:4] could not find end character of single-quoted text |
| 1269 | > 1 | a: 'foobarbaz |
| 1270 | ^ |
| 1271 | `, |
| 1272 | }, |
| 1273 | { |
| 1274 | `a: "\"key\": \"value:\"`, |
| 1275 | ` |
| 1276 | [1:4] could not find end character of double-quoted text |
| 1277 | > 1 | a: "\"key\": \"value:\" |
| 1278 | ^ |
| 1279 | `, |
| 1280 | }, |
| 1281 | { |
| 1282 | `foo: [${should not be allowed}]`, |
| 1283 | ` |
| 1284 | [1:8] ',' or ']' must be specified |
| 1285 | > 1 | foo: [${should not be allowed}] |
| 1286 | ^ |
| 1287 | `, |
| 1288 | }, |
| 1289 | { |
| 1290 | `foo: [$[should not be allowed]]`, |
| 1291 | ` |
| 1292 | [1:8] ',' or ']' must be specified |
| 1293 | > 1 | foo: [$[should not be allowed]] |
| 1294 | ^ |
| 1295 | `, |
| 1296 | }, |
| 1297 | { |
| 1298 | ">\n>", |
| 1299 | ` |
| 1300 | [2:1] could not find multi-line content |
| 1301 | 1 | > |
| 1302 | > 2 | > |
| 1303 | ^ |
| 1304 | `, |
| 1305 | }, |
| 1306 | { |
| 1307 | ">\n1", |
| 1308 | ` |
| 1309 | [2:1] could not find multi-line content |
| 1310 | 1 | > |
| 1311 | > 2 | 1 |
| 1312 | ^ |
| 1313 | `, |
| 1314 | }, |
| 1315 | { |
| 1316 | "|\n1", |
| 1317 | ` |
| 1318 | [2:1] could not find multi-line content |
| 1319 | 1 | | |
| 1320 | > 2 | 1 |
| 1321 | ^ |
| 1322 | `, |
| 1323 | }, |
| 1324 | { |
| 1325 | "a: >3\n 1", |
| 1326 | ` |
| 1327 | [2:3] invalid number of indent is specified in the multi-line header |
| 1328 | 1 | a: >3 |
| 1329 | > 2 | 1 |
| 1330 | ^ |
| 1331 | `, |
| 1332 | }, |
| 1333 | { |
| 1334 | ` |
| 1335 | a: |
| 1336 | - | |
| 1337 | b |
| 1338 | c: d |
| 1339 | `, |
| 1340 | ` |
| 1341 | [5:5] value is not allowed in this context |
| 1342 | 2 | a: |
| 1343 | 3 | - | |
| 1344 | 4 | b |
| 1345 | > 5 | c: d |
| 1346 | ^ |
| 1347 | `, |
| 1348 | }, |
| 1349 | { |
| 1350 | ` |
| 1351 | a: |
| 1352 | - | |
| 1353 | b |
| 1354 | c: |
| 1355 | d: e |
| 1356 | `, |
| 1357 | ` |
| 1358 | [5:5] value is not allowed in this context |
| 1359 | 2 | a: |
| 1360 | 3 | - | |
| 1361 | 4 | b |
| 1362 | > 5 | c: |
| 1363 | ^ |
| 1364 | 6 | d: e`, |
| 1365 | }, |
| 1366 | { |
| 1367 | "key: [@val]", |
| 1368 | ` |
| 1369 | [1:7] '@' is a reserved character |
| 1370 | > 1 | key: [@val] |
| 1371 | ^ |
| 1372 | `, |
| 1373 | }, |
| 1374 | { |
| 1375 | "key: [`val]", |
| 1376 | "\n[1:7] '`' is a reserved character\n> 1 | key: [`val]\n ^\n", |
| 1377 | }, |
| 1378 | { |
| 1379 | `{a: b}: v`, |
| 1380 | ` |
| 1381 | [1:7] found an invalid key for this map |
| 1382 | > 1 | {a: b}: v |
| 1383 | ^ |
| 1384 | `, |
| 1385 | }, |
| 1386 | { |
| 1387 | `[a]: v`, |
| 1388 | ` |
| 1389 | [1:4] found an invalid key for this map |
| 1390 | > 1 | [a]: v |
| 1391 | ^ |
| 1392 | `, |
| 1393 | }, |
| 1394 | { |
| 1395 | ` |
| 1396 | foo: |
| 1397 | bar: |
| 1398 | foo: 2 |
| 1399 | baz: |
| 1400 | foo: 3 |
| 1401 | foo: 2 |
| 1402 | `, |
| 1403 | ` |
| 1404 | [7:1] mapping key "foo" already defined at [2:1] |
| 1405 | 4 | foo: 2 |
| 1406 | 5 | baz: |
| 1407 | 6 | foo: 3 |
| 1408 | > 7 | foo: 2 |
| 1409 | ^ |
| 1410 | `, |
| 1411 | }, |
| 1412 | { |
| 1413 | ` |
| 1414 | foo: |
| 1415 | bar: |
| 1416 | foo: 2 |
| 1417 | baz: |
| 1418 | foo: 3 |
| 1419 | foo: 4 |
| 1420 | `, |
| 1421 | ` |
| 1422 | [7:5] mapping key "foo" already defined at [6:5] |
| 1423 | 4 | foo: 2 |
| 1424 | 5 | baz: |
| 1425 | 6 | foo: 3 |
| 1426 | > 7 | foo: 4 |
| 1427 | ^ |
| 1428 | `, |
| 1429 | }, |
| 1430 | { |
| 1431 | `{"000":0000A,`, |
| 1432 | ` |
| 1433 | [1:13] could not find flow map content |
| 1434 | > 1 | {"000":0000A, |
| 1435 | ^ |
| 1436 | `, |
| 1437 | }, |
| 1438 | } |
| 1439 | for _, test := range tests { |
| 1440 | t.Run(test.source, func(t *testing.T) { |
| 1441 | _, err := parser.ParseBytes([]byte(test.source), 0) |
| 1442 | if err == nil { |
| 1443 | t.Fatal("cannot catch syntax error") |
| 1444 | } |
| 1445 | actual := "\n" + err.Error() |
| 1446 | if test.expect != actual { |
| 1447 | t.Fatalf("expected: [%s] but got [%s]", test.expect, actual) |
| 1448 | } |
| 1449 | }) |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | func TestComment(t *testing.T) { |
| 1454 | tests := []struct { |
| 1455 | name string |
| 1456 | yaml string |
| 1457 | expected string |
| 1458 | }{ |
| 1459 | { |
| 1460 | name: "map with comment", |
| 1461 | yaml: ` |
| 1462 | # commentA |
| 1463 | a: #commentB |
| 1464 | # commentC |
| 1465 | b: c # commentD |
| 1466 | # commentE |
| 1467 | d: e # commentF |
| 1468 | # commentG |
| 1469 | f: g # commentH |
| 1470 | # commentI |
| 1471 | f: g # commentJ |
| 1472 | # commentK |
| 1473 | `, |
| 1474 | }, |
| 1475 | { |
| 1476 | name: "sequence with comment", |
| 1477 | yaml: ` |
| 1478 | # commentA |
| 1479 | - a # commentB |
| 1480 | # commentC |
| 1481 | - b: # commentD |
| 1482 | # commentE |
| 1483 | - d # commentF |
| 1484 | - e # commentG |
| 1485 | # commentH |
| 1486 | `, |
| 1487 | }, |
| 1488 | { |
| 1489 | name: "anchor and alias", |
| 1490 | yaml: ` |
| 1491 | a: &x b # commentA |
| 1492 | c: *x # commentB |
| 1493 | `, |
| 1494 | }, |
| 1495 | { |
| 1496 | name: "multiline", |
| 1497 | yaml: ` |
| 1498 | # foo comment |
| 1499 | # foo comment2 |
| 1500 | foo: # map key comment |
| 1501 | # bar above comment |
| 1502 | # bar above comment2 |
| 1503 | bar: 10 # comment for bar |
| 1504 | # baz above comment |
| 1505 | # baz above comment2 |
| 1506 | baz: bbbb # comment for baz |
| 1507 | piyo: # sequence key comment |
| 1508 | # sequence1 above comment 1 |
| 1509 | # sequence1 above comment 2 |
| 1510 | - sequence1 # sequence1 |
| 1511 | # sequence2 above comment 1 |
| 1512 | # sequence2 above comment 2 |
| 1513 | - sequence2 # sequence2 |
| 1514 | # sequence3 above comment 1 |
| 1515 | # sequence3 above comment 2 |
| 1516 | - false # sequence3 |
| 1517 | # foo2 comment |
| 1518 | # foo2 comment2 |
| 1519 | foo2: &anchor text # anchor comment |
| 1520 | # foo3 comment |
| 1521 | # foo3 comment2 |
| 1522 | foo3: *anchor # alias comment |
| 1523 | `, |
| 1524 | }, |
| 1525 | { |
| 1526 | name: "literal", |
| 1527 | yaml: ` |
| 1528 | foo: | # comment |
| 1529 | x: 42 |
| 1530 | `, |
| 1531 | }, |
| 1532 | { |
| 1533 | name: "folded", |
| 1534 | yaml: ` |
| 1535 | foo: > # comment |
| 1536 | x: 42 |
| 1537 | `, |
| 1538 | }, |
| 1539 | { |
| 1540 | name: "unattached comment", |
| 1541 | yaml: ` |
| 1542 | # This comment is in its own document |
| 1543 | --- |
| 1544 | a: b |
| 1545 | `, |
| 1546 | }, |
| 1547 | { |
| 1548 | name: "map with misaligned indentation in comments", |
| 1549 | yaml: ` |
| 1550 | # commentA |
| 1551 | a: #commentB |
| 1552 | # commentC |
| 1553 | b: c # commentD |
| 1554 | # commentE |
| 1555 | d: e # commentF |
| 1556 | # commentG |
| 1557 | `, |
| 1558 | expected: ` |
| 1559 | # commentA |
| 1560 | a: #commentB |
| 1561 | # commentC |
| 1562 | b: c # commentD |
| 1563 | # commentE |
| 1564 | d: e # commentF |
| 1565 | # commentG |
| 1566 | `, |
| 1567 | }, |
| 1568 | { |
| 1569 | name: "sequence with misaligned indentation in comments", |
| 1570 | yaml: ` |
| 1571 | # commentA |
| 1572 | - a # commentB |
| 1573 | # commentC |
| 1574 | - b: # commentD |
| 1575 | # commentE |
| 1576 | - d # commentF |
| 1577 | # commentG |
| 1578 | - e # commentG |
| 1579 | # commentH |
| 1580 | `, |
| 1581 | expected: ` |
| 1582 | # commentA |
| 1583 | - a # commentB |
| 1584 | # commentC |
| 1585 | - b: # commentD |
| 1586 | # commentE |
| 1587 | - d # commentF |
| 1588 | # commentG |
| 1589 | - e # commentG |
| 1590 | # commentH |
| 1591 | `, |
| 1592 | }, |
| 1593 | } |
| 1594 | for _, test := range tests { |
| 1595 | t.Run(test.name, func(t *testing.T) { |
| 1596 | f, err := parser.ParseBytes([]byte(test.yaml), parser.ParseComments) |
| 1597 | if err != nil { |
| 1598 | t.Fatalf("%+v", err) |
| 1599 | } |
| 1600 | got := "\n" + f.String() |
| 1601 | expected := test.yaml |
| 1602 | if test.expected != "" { |
| 1603 | expected = test.expected |
| 1604 | } |
| 1605 | if expected != got { |
| 1606 | t.Fatalf("expected:%s\ngot:%s", expected, got) |
| 1607 | } |
| 1608 | }) |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | func TestCommentWithNull(t *testing.T) { |
| 1613 | t.Run("same line", func(t *testing.T) { |
| 1614 | content := ` |
| 1615 | foo: |
| 1616 | bar: # comment |
| 1617 | baz: 1 |
| 1618 | ` |
| 1619 | expected := ` |
| 1620 | foo: |
| 1621 | bar: # comment |
| 1622 | baz: 1` |
| 1623 | f, err := parser.ParseBytes([]byte(content), parser.ParseComments) |
| 1624 | if err != nil { |
| 1625 | t.Fatal(err) |
| 1626 | } |
| 1627 | if len(f.Docs) != 1 { |
| 1628 | t.Fatal("failed to parse content with same line comment") |
| 1629 | } |
| 1630 | got := f.Docs[0].String() |
| 1631 | if got != strings.TrimPrefix(expected, "\n") { |
| 1632 | t.Fatalf("failed to parse comment:\nexpected:\n%s\ngot:\n%s", strings.TrimPrefix(expected, "\n"), got) |
| 1633 | } |
| 1634 | }) |
| 1635 | t.Run("next line", func(t *testing.T) { |
| 1636 | content := ` |
| 1637 | foo: |
| 1638 | bar: |
| 1639 | # comment |
| 1640 | baz: 1 |
| 1641 | ` |
| 1642 | expected := ` |
| 1643 | foo: |
| 1644 | bar: |
| 1645 | # comment |
| 1646 | baz: 1` |
| 1647 | f, err := parser.ParseBytes([]byte(content), parser.ParseComments) |
| 1648 | if err != nil { |
| 1649 | t.Fatal(err) |
| 1650 | } |
| 1651 | if len(f.Docs) != 1 { |
| 1652 | t.Fatal("failed to parse content with next line comment") |
| 1653 | } |
| 1654 | got := f.Docs[0].String() |
| 1655 | if got != strings.TrimPrefix(expected, "\n") { |
| 1656 | t.Fatalf("failed to parse comment:\nexpected:\n%s\ngot:\n%s", strings.TrimPrefix(expected, "\n"), got) |
| 1657 | } |
| 1658 | }) |
| 1659 | t.Run("next line and different indent", func(t *testing.T) { |
| 1660 | content := ` |
| 1661 | foo: |
| 1662 | bar: |
| 1663 | # comment |
| 1664 | baz: 1` |
| 1665 | f, err := parser.ParseBytes([]byte(content), parser.ParseComments) |
| 1666 | if err != nil { |
| 1667 | t.Fatal(err) |
| 1668 | } |
| 1669 | if len(f.Docs) != 1 { |
| 1670 | t.Fatal("failed to parse content with next line comment") |
| 1671 | } |
| 1672 | expected := ` |
| 1673 | foo: |
| 1674 | bar: |
| 1675 | # comment |
| 1676 | baz: 1` |
| 1677 | got := f.Docs[0].String() |
| 1678 | if got != strings.TrimPrefix(expected, "\n") { |
| 1679 | t.Fatalf("failed to parse comment:\nexpected:\n%s\ngot:\n%s", strings.TrimPrefix(expected, "\n"), got) |
| 1680 | } |
| 1681 | }) |
| 1682 | } |
| 1683 | |
| 1684 | func TestSequenceComment(t *testing.T) { |
| 1685 | content := ` |
| 1686 | foo: |
| 1687 | - # comment |
| 1688 | bar: 1 |
| 1689 | baz: |
| 1690 | - xxx |
| 1691 | ` |
| 1692 | f, err := parser.ParseBytes([]byte(content), parser.ParseComments) |
| 1693 | if err != nil { |
| 1694 | t.Fatal(err) |
| 1695 | } |
| 1696 | if len(f.Docs) != 1 { |
| 1697 | t.Fatal("failed to parse content with next line with sequence") |
| 1698 | } |
| 1699 | expected := ` |
| 1700 | foo: |
| 1701 | # comment |
| 1702 | - bar: 1 |
| 1703 | baz: |
| 1704 | - xxx` |
| 1705 | got := f.Docs[0].String() |
| 1706 | if got != strings.TrimPrefix(expected, "\n") { |
| 1707 | t.Fatalf("failed to parse comment:\nexpected:\n%s\ngot:\n%s", strings.TrimPrefix(expected, "\n"), got) |
| 1708 | } |
| 1709 | t.Run("foo[0].bar", func(t *testing.T) { |
| 1710 | path, err := yaml.PathString("$.foo[0].bar") |
| 1711 | if err != nil { |
| 1712 | t.Fatal(err) |
| 1713 | } |
| 1714 | v, err := path.FilterFile(f) |
| 1715 | if err != nil { |
| 1716 | t.Fatal(err) |
| 1717 | } |
| 1718 | if v.String() != "1" { |
| 1719 | t.Fatal("failed to get foo[0].bar value") |
| 1720 | } |
| 1721 | }) |
| 1722 | t.Run("baz[0]", func(t *testing.T) { |
| 1723 | path, err := yaml.PathString("$.baz[0]") |
| 1724 | if err != nil { |
| 1725 | t.Fatal(err) |
| 1726 | } |
| 1727 | v, err := path.FilterFile(f) |
| 1728 | if err != nil { |
| 1729 | t.Fatal(err) |
| 1730 | } |
| 1731 | if v.String() != "xxx" { |
| 1732 | t.Fatal("failed to get baz[0] value") |
| 1733 | } |
| 1734 | }) |
| 1735 | } |
| 1736 | |
| 1737 | func TestCommentWithMap(t *testing.T) { |
| 1738 | yml := ` |
| 1739 | single: |
| 1740 | # foo comment |
| 1741 | foo: bar |
| 1742 | |
| 1743 | multiple: |
| 1744 | # a comment |
| 1745 | a: b |
| 1746 | # c comment |
| 1747 | c: d |
| 1748 | ` |
| 1749 | |
| 1750 | file, err := parser.ParseBytes([]byte(yml), parser.ParseComments) |
| 1751 | if err != nil { |
| 1752 | t.Fatal(err) |
| 1753 | } |
| 1754 | if len(file.Docs) == 0 { |
| 1755 | t.Fatal("cannot get file docs") |
| 1756 | } |
| 1757 | if file.Docs[0].Body == nil { |
| 1758 | t.Fatal("cannot get docs body") |
| 1759 | } |
| 1760 | mapNode, ok := file.Docs[0].Body.(*ast.MappingNode) |
| 1761 | if !ok { |
| 1762 | t.Fatalf("failed to get map node. got: %T\n", file.Docs[0].Body) |
| 1763 | } |
| 1764 | if len(mapNode.Values) != 2 { |
| 1765 | t.Fatalf("failed to get map values. got %d", len(mapNode.Values)) |
| 1766 | } |
| 1767 | |
| 1768 | singleNode, ok := mapNode.Values[0].Value.(*ast.MappingNode) |
| 1769 | if !ok { |
| 1770 | t.Fatalf("failed to get single node. got %T", mapNode.Values[0].Value) |
| 1771 | } |
| 1772 | if len(singleNode.Values) != 1 { |
| 1773 | t.Fatalf("failed to get single node values. got %d", len(singleNode.Values)) |
| 1774 | } |
| 1775 | if singleNode.Values[0].GetComment().GetToken().Value != " foo comment" { |
| 1776 | t.Fatalf("failed to get comment from single. got %q", singleNode.GetComment().GetToken().Value) |
| 1777 | } |
| 1778 | |
| 1779 | multiNode, ok := mapNode.Values[1].Value.(*ast.MappingNode) |
| 1780 | if !ok { |
| 1781 | t.Fatalf("failed to get multiple node. got: %T", mapNode.Values[1]) |
| 1782 | } |
| 1783 | if multiNode.GetComment() != nil { |
| 1784 | t.Fatalf("found unexpected comment") |
| 1785 | } |
| 1786 | if len(multiNode.Values) != 2 { |
| 1787 | t.Fatalf("failed to get multiple node values. got %d", len(multiNode.Values)) |
| 1788 | } |
| 1789 | if multiNode.Values[0].GetComment().GetToken().Value != " a comment" { |
| 1790 | t.Fatalf("failed to get comment from multiple[0]. got %q", multiNode.Values[0].GetComment().GetToken().Value) |
| 1791 | } |
| 1792 | if multiNode.Values[1].GetComment().GetToken().Value != " c comment" { |
| 1793 | t.Fatalf("failed to get comment from multiple[1]. got %q", multiNode.Values[1].GetComment().GetToken().Value) |
| 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | func TestInFlowStyle(t *testing.T) { |
| 1798 | type inFlowStyle interface { |
| 1799 | SetIsFlowStyle(bool) |
| 1800 | } |
| 1801 | |
| 1802 | tests := []struct { |
| 1803 | source string |
| 1804 | expect string |
| 1805 | }{ |
| 1806 | { |
| 1807 | ` |
| 1808 | - foo |
| 1809 | - bar |
| 1810 | - baz |
| 1811 | `, |
| 1812 | `[foo, bar, baz] |
| 1813 | `, |
| 1814 | }, |
| 1815 | { |
| 1816 | ` |
| 1817 | foo: bar |
| 1818 | baz: fizz |
| 1819 | `, |
| 1820 | `{foo: bar, baz: fizz} |
| 1821 | `, |
| 1822 | }, |
| 1823 | { |
| 1824 | ` |
| 1825 | foo: |
| 1826 | - bar |
| 1827 | - baz |
| 1828 | - fizz: buzz |
| 1829 | `, |
| 1830 | `{foo: [bar, baz, {fizz: buzz}]} |
| 1831 | `, |
| 1832 | }, |
| 1833 | } |
| 1834 | |
| 1835 | for _, test := range tests { |
| 1836 | t.Run(test.source, func(t *testing.T) { |
| 1837 | f, err := parser.ParseBytes([]byte(test.source), parser.ParseComments) |
| 1838 | if err != nil { |
| 1839 | t.Fatal(err) |
| 1840 | } |
| 1841 | |
| 1842 | if len(f.Docs) != 1 { |
| 1843 | t.Fatal("failed to parse content") |
| 1844 | } |
| 1845 | |
| 1846 | ifs, ok := f.Docs[0].Body.(inFlowStyle) |
| 1847 | if !ok { |
| 1848 | t.Fatalf("failed to get inFlowStyle. got: %T", f.Docs[0].Body) |
| 1849 | } |
| 1850 | ifs.SetIsFlowStyle(true) |
| 1851 | |
| 1852 | got := f.String() |
| 1853 | |
| 1854 | if got != test.expect { |
| 1855 | t.Fatalf("failed to parse comment:\nexpected:\n%s\ngot:\n%s", test.expect, got) |
| 1856 | } |
| 1857 | }) |
| 1858 | } |
| 1859 | } |
| 1860 | |
| 1861 | func TestNodePath(t *testing.T) { |
| 1862 | yml := ` |
| 1863 | a: # commentA |
| 1864 | b: # commentB |
| 1865 | c: foo # commentC |
| 1866 | d: bar # commentD |
| 1867 | e: baz # commentE |
| 1868 | f: # commentF |
| 1869 | g: hoge # commentG |
| 1870 | h: # commentH |
| 1871 | - list1 # comment list1 |
| 1872 | - list2 # comment list2 |
| 1873 | - list3 # comment list3 |
| 1874 | i: fuga # commentI |
| 1875 | j: piyo # commentJ |
| 1876 | k.l.m.n: moge # commentKLMN |
| 1877 | o#p: hogera # commentOP |
| 1878 | q#.r: hogehoge # commentQR |
| 1879 | ` |
| 1880 | f, err := parser.ParseBytes([]byte(yml), parser.ParseComments) |
| 1881 | if err != nil { |
| 1882 | t.Fatalf("%+v", err) |
| 1883 | } |
| 1884 | var capturer pathCapturer |
| 1885 | for _, doc := range f.Docs { |
| 1886 | ast.Walk(&capturer, doc.Body) |
| 1887 | } |
| 1888 | commentPaths := []string{} |
| 1889 | for i := 0; i < capturer.capturedNum; i++ { |
| 1890 | if capturer.orderedTypes[i] == ast.CommentType { |
| 1891 | commentPaths = append(commentPaths, capturer.orderedPaths[i]) |
| 1892 | } |
| 1893 | } |
| 1894 | expectedPaths := []string{ |
| 1895 | "$.a", |
| 1896 | "$.a.b", |
| 1897 | "$.a.b.c", |
| 1898 | "$.a.b.d", |
| 1899 | "$.a.b.e", |
| 1900 | "$.a.f", |
| 1901 | "$.a.f.g", |
| 1902 | "$.a.h", |
| 1903 | "$.a.h[0]", |
| 1904 | "$.a.h[1]", |
| 1905 | "$.a.h[2]", |
| 1906 | "$.a.i", |
| 1907 | "$.j", |
| 1908 | "$.'k.l.m.n'", |
| 1909 | "$.o#p", |
| 1910 | "$.'q#.r'", |
| 1911 | } |
| 1912 | if !reflect.DeepEqual(expectedPaths, commentPaths) { |
| 1913 | t.Fatalf("failed to get YAMLPath to the comment node:\nexpected[%s]\ngot [%s]", expectedPaths, commentPaths) |
| 1914 | } |
| 1915 | } |
| 1916 | |
| 1917 | type pathCapturer struct { |
| 1918 | capturedNum int |
| 1919 | orderedPaths []string |
| 1920 | orderedTypes []ast.NodeType |
| 1921 | orderedTokens []*token.Token |
| 1922 | } |
| 1923 | |
| 1924 | func (c *pathCapturer) Visit(node ast.Node) ast.Visitor { |
| 1925 | c.capturedNum++ |
| 1926 | c.orderedPaths = append(c.orderedPaths, node.GetPath()) |
| 1927 | c.orderedTypes = append(c.orderedTypes, node.Type()) |
| 1928 | c.orderedTokens = append(c.orderedTokens, node.GetToken()) |
| 1929 | return c |
| 1930 | } |
| 1931 | |
| 1932 | type Visitor struct{} |
| 1933 | |
| 1934 | func (v *Visitor) Visit(node ast.Node) ast.Visitor { |
| 1935 | tk := node.GetToken() |
| 1936 | tk.Prev = nil |
| 1937 | tk.Next = nil |
| 1938 | return v |
| 1939 | } |