| 1 |
package parser_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"path/filepath" |
| 6 |
"reflect" |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/goccy/go-yaml" |
| 11 |
"github.com/goccy/go-yaml/ast" |
| 12 |
"github.com/goccy/go-yaml/lexer" |
| 13 |
"github.com/goccy/go-yaml/parser" |
| 14 |
"github.com/goccy/go-yaml/token" |
| 15 |
) |
| 16 |
|
| 17 |
func TestParser(t *testing.T) { |
| 18 |
sources := []string{ |
| 19 |
"null\n", |
| 20 |
"{}\n", |
| 21 |
"v: hi\n", |
| 22 |
"v: \"true\"\n", |
| 23 |
"v: \"false\"\n", |
| 24 |
"v: true\n", |
| 25 |
"v: false\n", |
| 26 |
"v: 10\n", |
| 27 |
"v: -10\n", |
| 28 |
"v: 42\n", |
| 29 |
"v: 4294967296\n", |
| 30 |
"v: \"10\"\n", |
| 31 |
"v: 0.1\n", |
| 32 |
"v: 0.99\n", |
| 33 |
"v: -0.1\n", |
| 34 |
"v: .inf\n", |
| 35 |
"v: -.inf\n", |
| 36 |
"v: .nan\n", |
| 37 |
"v: null\n", |
| 38 |
"v: \"\"\n", |
| 39 |
"v:\n- A\n- B\n", |
| 40 |
"a: '-'\n", |
| 41 |
"123\n", |
| 42 |
"hello: world\n", |
| 43 |
"a: null\n", |
| 44 |
"v:\n- A\n- 1\n- B:\n - 2\n - 3\n", |
| 45 |
"a:\n b: c\n", |
| 46 |
"a: {x: 1}\n", |
| 47 |
"t2: 2018-01-09T10:40:47Z\nt4: 2098-01-09T10:40:47Z\n", |
| 48 |
"a: [1, 2]\n", |
| 49 |
"a: {b: c, d: e}\n", |
| 50 |
"a: 3s\n", |
| 51 |
"a: <foo>\n", |
| 52 |
"a: \"1:1\"\n", |
| 53 |
"a: 1.2.3.4\n", |
| 54 |
"a: \"2015-02-24T18:19:39Z\"\n", |
| 55 |
"a: 'b: c'\n", |
| 56 |
"a: 'Hello #comment'\n", |
| 57 |
"a: abc <<def>> ghi", |
| 58 |
"a: <<abcd", |
| 59 |
"a: <<:abcd", |
| 60 |
"a: << :abcd", |
| 61 |
"a: 100.5\n", |
| 62 |
"a: bogus\n", |
| 63 |
"a: \"\\0\"\n", |
| 64 |
"b: 2\na: 1\nd: 4\nc: 3\nsub:\n e: 5\n", |
| 65 |
" a : b \n", |
| 66 |
"a: b # comment\nb: c\n", |
| 67 |
"---\na: b\n", |
| 68 |
"a: b\n...\n", |
| 69 |
"%YAML 1.2\n---\n", |
| 70 |
"a: !!binary gIGC\n", |
| 71 |
"a: !!binary |\n " + strings.Repeat("kJCQ", 17) + "kJ\n CQ\n", |
| 72 |
"- !tag\n a: b\n c: d\n", |
| 73 |
"v:\n- A\n- |-\n B\n C\n", |
| 74 |
"v:\n- A\n- >-\n B\n C\n", |
| 75 |
"v: |-\n 0\n", |
| 76 |
"v: |-\n 0\nx: 0", |
| 77 |
`"a\n1\nb"`, |
| 78 |
`{"a":"b"}`, |
| 79 |
`!!map { |
| 80 |
? !!str "explicit":!!str "entry", |
| 81 |
? !!str "implicit" : !!str "entry", |
| 82 |
? !!null "" : !!null "", |
| 83 |
}`, |
| 84 |
"\"a\": a\n\"b\": b", |
| 85 |
"'a': a\n'b': b", |
| 86 |
"a: \r\n b: 1\r\n", |
| 87 |
"a_ok: \r bc: 2\r", |
| 88 |
"a_mk: \n bd: 3\n", |
| 89 |
} |
| 90 |
for _, src := range sources { |
| 91 |
if _, err := parser.Parse(lexer.Tokenize(src), 0); err != nil { |
| 92 |
t.Fatalf("parse error: source [%s]: %+v", src, err) |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
func TestParseComplicatedDocument(t *testing.T) { |
| 98 |
tests := []struct { |
| 99 |
source string |
| 100 |
expect string |
| 101 |
}{ |
| 102 |
{ |
| 103 |
` |
| 104 |
american: |
| 105 |
- Boston Red Sox |
| 106 |
- Detroit Tigers |
| 107 |
- New York Yankees |
| 108 |
national: |
| 109 |
- New York Mets |
| 110 |
- Chicago Cubs |
| 111 |
- Atlanta Braves |
| 112 |
`, ` |
| 113 |
american: |
| 114 |
- Boston Red Sox |
| 115 |
- Detroit Tigers |
| 116 |
- New York Yankees |
| 117 |
national: |
| 118 |
- New York Mets |
| 119 |
- Chicago Cubs |
| 120 |
- Atlanta Braves |
| 121 |
`, |
| 122 |
}, |
| 123 |
{ |
| 124 |
` |
| 125 |
a: |
| 126 |
b: c |
| 127 |
d: e |
| 128 |
f: g |
| 129 |
h: |
| 130 |
i: j |
| 131 |
k: |
| 132 |
l: m |
| 133 |
n: o |
| 134 |
p: q |
| 135 |
r: s |
| 136 |
`, ` |
| 137 |
a: |
| 138 |
b: c |
| 139 |
d: e |
| 140 |
f: g |
| 141 |
h: |
| 142 |
i: j |
| 143 |
k: |
| 144 |
l: m |
| 145 |
n: o |
| 146 |
p: q |
| 147 |
r: s |
| 148 |
`, |
| 149 |
}, |
| 150 |
{ |
| 151 |
` |
| 152 |
- a: |
| 153 |
- b |
| 154 |
- c |
| 155 |
- d |
| 156 |
`, ` |
| 157 |
- a: |
| 158 |
- b |
| 159 |
- c |
| 160 |
- d |
| 161 |
`, |
| 162 |
}, |
| 163 |
{ |
| 164 |
` |
| 165 |
- a |
| 166 |
- b |
| 167 |
- c |
| 168 |
- d |
| 169 |
- e |
| 170 |
- f |
| 171 |
`, ` |
| 172 |
- a |
| 173 |
- b |
| 174 |
- c - d - e |
| 175 |
- f |
| 176 |
`, |
| 177 |
}, |
| 178 |
{ |
| 179 |
` |
| 180 |
a: 0 - 1 |
| 181 |
`, |
| 182 |
` |
| 183 |
a: 0 - 1 |
| 184 |
`, |
| 185 |
}, |
| 186 |
{` |
| 187 |
- a: |
| 188 |
b: c |
| 189 |
d: e |
| 190 |
- f: |
| 191 |
g: h |
| 192 |
`, |
| 193 |
` |
| 194 |
- a: |
| 195 |
b: c |
| 196 |
d: e |
| 197 |
- f: null |
| 198 |
g: h |
| 199 |
`, |
| 200 |
}, |
| 201 |
{ |
| 202 |
` |
| 203 |
a: |
| 204 |
b |
| 205 |
c |
| 206 |
d: e |
| 207 |
`, ` |
| 208 |
a: b c |
| 209 |
d: e |
| 210 |
`, |
| 211 |
}, |
| 212 |
{ |
| 213 |
` |
| 214 |
a |
| 215 |
b |
| 216 |
c |
| 217 |
`, ` |
| 218 |
a b c |
| 219 |
`, |
| 220 |
}, |
| 221 |
{ |
| 222 |
` |
| 223 |
a: |
| 224 |
- b |
| 225 |
- c |
| 226 |
`, ` |
| 227 |
a: |
| 228 |
- b |
| 229 |
- c |
| 230 |
`, |
| 231 |
}, |
| 232 |
{ |
| 233 |
` |
| 234 |
- a : |
| 235 |
b: c |
| 236 |
`, ` |
| 237 |
- a: null |
| 238 |
b: c |
| 239 |
`, |
| 240 |
}, |
| 241 |
{ |
| 242 |
` |
| 243 |
- a: |
| 244 |
b |
| 245 |
c |
| 246 |
d |
| 247 |
hoge: fuga |
| 248 |
`, ` |
| 249 |
- a: b c d |
| 250 |
hoge: fuga |
| 251 |
`, |
| 252 |
}, |
| 253 |
{ |
| 254 |
` |
| 255 |
- a # ' " # - : % |
| 256 |
- b # " # - : % ' |
| 257 |
- c # # - : % ' " |
| 258 |
- d # - : % ' " # |
| 259 |
- e # : % ' " # - |
| 260 |
- f # % ' : # - : |
| 261 |
`, |
| 262 |
` |
| 263 |
- a |
| 264 |
- b |
| 265 |
- c |
| 266 |
- d |
| 267 |
- e |
| 268 |
- f |
| 269 |
`, |
| 270 |
}, |
| 271 |
{ |
| 272 |
` |
| 273 |
# comment |
| 274 |
a: # comment |
| 275 |
# comment |
| 276 |
b: c # comment |
| 277 |
# comment |
| 278 |
d: e # comment |
| 279 |
# comment |
| 280 |
`, |
| 281 |
` |
| 282 |
a: |
| 283 |
b: c |
| 284 |
d: e |
| 285 |
`, |
| 286 |
}, |
| 287 |
{ |
| 288 |
` |
| 289 |
a: b#notcomment |
| 290 |
`, |
| 291 |
` |
| 292 |
a: b#notcomment |
| 293 |
`, |
| 294 |
}, |
| 295 |
{ |
| 296 |
` |
| 297 |
anchored: &anchor foo |
| 298 |
aliased: *anchor |
| 299 |
`, |
| 300 |
` |
| 301 |
anchored: &anchor foo |
| 302 |
aliased: *anchor |
| 303 |
`, |
| 304 |
}, |
| 305 |
{ |
| 306 |
` |
| 307 |
--- |
| 308 |
- &CENTER { x: 1, y: 2 } |
| 309 |
- &LEFT { x: 0, y: 2 } |
| 310 |
- &BIG { r: 10 } |
| 311 |
- &SMALL { r: 1 } |
| 312 |
|
| 313 |
# All the following maps are equal: |
| 314 |
|
| 315 |
- # Explicit keys |
| 316 |
x: 1 |
| 317 |
y: 2 |
| 318 |
r: 10 |
| 319 |
label: center/big |
| 320 |
|
| 321 |
- # Merge one map |
| 322 |
<< : *CENTER |
| 323 |
r: 10 |
| 324 |
label: center/big |
| 325 |
|
| 326 |
- # Merge multiple maps |
| 327 |
<< : [ *CENTER, *BIG ] |
| 328 |
label: center/big |
| 329 |
|
| 330 |
- # Override |
| 331 |
<< : [ *BIG, *LEFT, *SMALL ] |
| 332 |
x: 1 |
| 333 |
label: center/big |
| 334 |
`, |
| 335 |
` |
| 336 |
--- |
| 337 |
- &CENTER {x: 1, y: 2} |
| 338 |
- &LEFT {x: 0, y: 2} |
| 339 |
- &BIG {r: 10} |
| 340 |
- &SMALL {r: 1} |
| 341 |
- x: 1 |
| 342 |
y: 2 |
| 343 |
r: 10 |
| 344 |
label: center/big |
| 345 |
- <<: *CENTER |
| 346 |
r: 10 |
| 347 |
label: center/big |
| 348 |
- <<: [*CENTER, *BIG] |
| 349 |
label: center/big |
| 350 |
- <<: [*BIG, *LEFT, *SMALL] |
| 351 |
x: 1 |
| 352 |
label: center/big |
| 353 |
`, |
| 354 |
}, |
| 355 |
{ |
| 356 |
` |
| 357 |
a: |
| 358 |
- - b |
| 359 |
- - c |
| 360 |
- d |
| 361 |
`, |
| 362 |
` |
| 363 |
a: |
| 364 |
- - b |
| 365 |
- - c |
| 366 |
- d |
| 367 |
`, |
| 368 |
}, |
| 369 |
{ |
| 370 |
` |
| 371 |
a: |
| 372 |
b: |
| 373 |
c: d |
| 374 |
e: |
| 375 |
f: g |
| 376 |
h: i |
| 377 |
j: k |
| 378 |
`, |
| 379 |
` |
| 380 |
a: |
| 381 |
b: |
| 382 |
c: d |
| 383 |
e: |
| 384 |
f: g |
| 385 |
h: i |
| 386 |
j: k |
| 387 |
`, |
| 388 |
}, |
| 389 |
{ |
| 390 |
` |
| 391 |
--- |
| 392 |
a: 1 |
| 393 |
b: 2 |
| 394 |
... |
| 395 |
--- |
| 396 |
c: 3 |
| 397 |
d: 4 |
| 398 |
... |
| 399 |
`, |
| 400 |
` |
| 401 |
--- |
| 402 |
a: 1 |
| 403 |
b: 2 |
| 404 |
... |
| 405 |
--- |
| 406 |
c: 3 |
| 407 |
d: 4 |
| 408 |
... |
| 409 |
`, |
| 410 |
}, |
| 411 |
{ |
| 412 |
` |
| 413 |
a: |
| 414 |
b: | |
| 415 |
{ |
| 416 |
[ 1, 2 ] |
| 417 |
} |
| 418 |
c: d |
| 419 |
`, |
| 420 |
` |
| 421 |
a: |
| 422 |
b: | |
| 423 |
{ |
| 424 |
[ 1, 2 ] |
| 425 |
} |
| 426 |
c: d |
| 427 |
`, |
| 428 |
}, |
| 429 |
{ |
| 430 |
` |
| 431 |
| |
| 432 |
hoge |
| 433 |
fuga |
| 434 |
piyo`, |
| 435 |
` |
| 436 |
| |
| 437 |
hoge |
| 438 |
fuga |
| 439 |
piyo |
| 440 |
`, |
| 441 |
}, |
| 442 |
{ |
| 443 |
` |
| 444 |
v: | |
| 445 |
a |
| 446 |
b |
| 447 |
c`, |
| 448 |
` |
| 449 |
v: | |
| 450 |
a |
| 451 |
b |
| 452 |
c |
| 453 |
`, |
| 454 |
}, |
| 455 |
|
| 456 |
{ |
| 457 |
` |
| 458 |
a: | |
| 459 |
bbbbbbb |
| 460 |
|
| 461 |
|
| 462 |
ccccccc |
| 463 |
d: eeeeeeeeeeeeeeeee |
| 464 |
`, |
| 465 |
` |
| 466 |
a: | |
| 467 |
bbbbbbb |
| 468 |
|
| 469 |
|
| 470 |
ccccccc |
| 471 |
d: eeeeeeeeeeeeeeeee |
| 472 |
`, |
| 473 |
}, |
| 474 |
{ |
| 475 |
` |
| 476 |
a: b |
| 477 |
c |
| 478 |
`, |
| 479 |
` |
| 480 |
a: b c |
| 481 |
`, |
| 482 |
}, |
| 483 |
{ |
| 484 |
` |
| 485 |
a: |
| 486 |
b: c |
| 487 |
`, |
| 488 |
` |
| 489 |
a: |
| 490 |
b: c |
| 491 |
`, |
| 492 |
}, |
| 493 |
{ |
| 494 |
` |
| 495 |
a: b |
| 496 |
c: d |
| 497 |
`, |
| 498 |
` |
| 499 |
a: b |
| 500 |
c: d |
| 501 |
`, |
| 502 |
}, |
| 503 |
{ |
| 504 |
` |
| 505 |
- ab - cd |
| 506 |
- ef - gh |
| 507 |
`, |
| 508 |
` |
| 509 |
- ab - cd |
| 510 |
- ef - gh |
| 511 |
`, |
| 512 |
}, |
| 513 |
{ |
| 514 |
` |
| 515 |
- 0 - 1 |
| 516 |
- 2 - 3 |
| 517 |
`, |
| 518 |
` |
| 519 |
- 0 - 1 - 2 - 3 |
| 520 |
`, |
| 521 |
}, |
| 522 |
{ |
| 523 |
` |
| 524 |
a - b - c: value |
| 525 |
`, |
| 526 |
` |
| 527 |
a - b - c: value |
| 528 |
`, |
| 529 |
}, |
| 530 |
{ |
| 531 |
` |
| 532 |
a: |
| 533 |
- |
| 534 |
b: c |
| 535 |
d: e |
| 536 |
- |
| 537 |
f: g |
| 538 |
h: i |
| 539 |
`, |
| 540 |
` |
| 541 |
a: |
| 542 |
- b: c |
| 543 |
d: e |
| 544 |
- f: g |
| 545 |
h: i |
| 546 |
`, |
| 547 |
}, |
| 548 |
{ |
| 549 |
` |
| 550 |
a: |- |
| 551 |
value |
| 552 |
b: c |
| 553 |
`, |
| 554 |
` |
| 555 |
a: |- |
| 556 |
value |
| 557 |
b: c |
| 558 |
`, |
| 559 |
}, |
| 560 |
{ |
| 561 |
` |
| 562 |
a: |+ |
| 563 |
value |
| 564 |
b: c |
| 565 |
`, |
| 566 |
` |
| 567 |
a: |+ |
| 568 |
value |
| 569 |
b: c |
| 570 |
`, |
| 571 |
}, |
| 572 |
{ |
| 573 |
` |
| 574 |
- key1: val |
| 575 |
key2: |
| 576 |
( |
| 577 |
foo |
| 578 |
+ |
| 579 |
bar |
| 580 |
) |
| 581 |
`, |
| 582 |
` |
| 583 |
- key1: val |
| 584 |
key2: ( foo + bar ) |
| 585 |
`, |
| 586 |
}, |
| 587 |
{ |
| 588 |
` |
| 589 |
"a": b |
| 590 |
'c': d |
| 591 |
"e": "f" |
| 592 |
g: "h" |
| 593 |
i: 'j' |
| 594 |
`, |
| 595 |
` |
| 596 |
"a": b |
| 597 |
'c': d |
| 598 |
"e": "f" |
| 599 |
g: "h" |
| 600 |
i: 'j' |
| 601 |
`, |
| 602 |
}, |
| 603 |
} |
| 604 |
|
| 605 |
for _, test := range tests { |
| 606 |
t.Run(test.source, func(t *testing.T) { |
| 607 |
tokens := lexer.Tokenize(test.source) |
| 608 |
f, err := parser.Parse(tokens, 0) |
| 609 |
if err != nil { |
| 610 |
t.Fatalf("%+v", err) |
| 611 |
} |
| 612 |
var v Visitor |
| 613 |
for _, doc := range f.Docs { |
| 614 |
ast.Walk(&v, doc.Body) |
| 615 |
} |
| 616 |
expect := fmt.Sprintf("\n%+v", f) |
| 617 |
if test.expect != expect { |
| 618 |
tokens.Dump() |
| 619 |
t.Fatalf("unexpected output: [%s] != [%s]", test.expect, expect) |
| 620 |
} |
| 621 |
}) |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
func TestNewLineChar(t *testing.T) { |
| 626 |
for _, f := range []string{ |
| 627 |
"lf.yml", |
| 628 |
"cr.yml", |
| 629 |
"crlf.yml", |
| 630 |
} { |
| 631 |
ast, err := parser.ParseFile(filepath.Join("testdata", f), 0) |
| 632 |
if err != nil { |
| 633 |
t.Fatalf("%+v", err) |
| 634 |
} |
| 635 |
actual := fmt.Sprintf("%v", ast) |
| 636 |
expect := `a: "a" |
| 637 |
b: 1 |
| 638 |
` |
| 639 |
if expect != actual { |
| 640 |
t.Fatal("unexpected result") |
| 641 |
} |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
func TestSyntaxError(t *testing.T) { |
| 646 |
tests := []struct { |
| 647 |
source string |
| 648 |
expect string |
| 649 |
}{ |
| 650 |
{ |
| 651 |
` |
| 652 |
a: |
| 653 |
- b |
| 654 |
c: d |
| 655 |
e: f |
| 656 |
g: h`, |
| 657 |
` |
| 658 |
[3:3] unexpected key name |
| 659 |
2 | a: |
| 660 |
> 3 | - b |
| 661 |
4 | c: d |
| 662 |
^ |
| 663 |
5 | e: f |
| 664 |
6 | g: h`, |
| 665 |
}, |
| 666 |
{ |
| 667 |
` |
| 668 |
a |
| 669 |
- b: c`, |
| 670 |
` |
| 671 |
[2:1] unexpected key name |
| 672 |
> 2 | a |
| 673 |
3 | - b: c |
| 674 |
^ |
| 675 |
`, |
| 676 |
}, |
| 677 |
{ |
| 678 |
`%YAML 1.1 {}`, |
| 679 |
` |
| 680 |
[1:2] unexpected directive value. document not started |
| 681 |
> 1 | %YAML 1.1 {} |
| 682 |
^ |
| 683 |
`, |
| 684 |
}, |
| 685 |
{ |
| 686 |
`{invalid`, |
| 687 |
` |
| 688 |
[1:2] unexpected map |
| 689 |
> 1 | {invalid |
| 690 |
^ |
| 691 |
`, |
| 692 |
}, |
| 693 |
{ |
| 694 |
`{ "key": "value" `, |
| 695 |
` |
| 696 |
[1:1] unterminated flow mapping |
| 697 |
> 1 | { "key": "value" |
| 698 |
^ |
| 699 |
`, |
| 700 |
}, |
| 701 |
{ |
| 702 |
` |
| 703 |
a: |
| 704 |
- b: c |
| 705 |
- `, |
| 706 |
` |
| 707 |
[4:1] empty sequence entry |
| 708 |
2 | a: |
| 709 |
3 | - b: c |
| 710 |
> 4 | - |
| 711 |
^ |
| 712 |
`, |
| 713 |
}, |
| 714 |
} |
| 715 |
for _, test := range tests { |
| 716 |
t.Run(test.source, func(t *testing.T) { |
| 717 |
_, err := parser.ParseBytes([]byte(test.source), 0) |
| 718 |
if err == nil { |
| 719 |
t.Fatal("cannot catch syntax error") |
| 720 |
} |
| 721 |
actual := "\n" + err.Error() |
| 722 |
if test.expect != actual { |
| 723 |
t.Fatalf("expected: [%s] but got [%s]", test.expect, actual) |
| 724 |
} |
| 725 |
}) |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
func TestComment(t *testing.T) { |
| 730 |
tests := []struct { |
| 731 |
name string |
| 732 |
yaml string |
| 733 |
}{ |
| 734 |
{ |
| 735 |
name: "map with comment", |
| 736 |
yaml: ` |
| 737 |
# commentA |
| 738 |
a: #commentB |
| 739 |
# commentC |
| 740 |
b: c # commentD |
| 741 |
# commentE |
| 742 |
d: e # commentF |
| 743 |
# commentG |
| 744 |
f: g # commentH |
| 745 |
# commentI |
| 746 |
f: g # commentJ |
| 747 |
# commentK |
| 748 |
`, |
| 749 |
}, |
| 750 |
{ |
| 751 |
name: "sequence with comment", |
| 752 |
yaml: ` |
| 753 |
# commentA |
| 754 |
- a # commentB |
| 755 |
# commentC |
| 756 |
- b: # commentD |
| 757 |
# commentE |
| 758 |
- d # commentF |
| 759 |
- e # commentG |
| 760 |
# commentH |
| 761 |
`, |
| 762 |
}, |
| 763 |
{ |
| 764 |
name: "anchor and alias", |
| 765 |
yaml: ` |
| 766 |
a: &x b # commentA |
| 767 |
c: *x # commentB |
| 768 |
`, |
| 769 |
}, |
| 770 |
{ |
| 771 |
name: "multiline", |
| 772 |
yaml: ` |
| 773 |
# foo comment |
| 774 |
# foo comment2 |
| 775 |
foo: # map key comment |
| 776 |
# bar above comment |
| 777 |
# bar above comment2 |
| 778 |
bar: 10 # comment for bar |
| 779 |
# baz above comment |
| 780 |
# baz above comment2 |
| 781 |
baz: bbbb # comment for baz |
| 782 |
piyo: # sequence key comment |
| 783 |
# sequence1 above comment 1 |
| 784 |
# sequence1 above comment 2 |
| 785 |
- sequence1 # sequence1 |
| 786 |
# sequence2 above comment 1 |
| 787 |
# sequence2 above comment 2 |
| 788 |
- sequence2 # sequence2 |
| 789 |
# sequence3 above comment 1 |
| 790 |
# sequence3 above comment 2 |
| 791 |
- false # sequence3 |
| 792 |
# foo2 comment |
| 793 |
# foo2 comment2 |
| 794 |
foo2: &anchor text # anchor comment |
| 795 |
# foo3 comment |
| 796 |
# foo3 comment2 |
| 797 |
foo3: *anchor # alias comment |
| 798 |
`, |
| 799 |
}, |
| 800 |
{ |
| 801 |
name: "literal", |
| 802 |
yaml: ` |
| 803 |
foo: | # comment |
| 804 |
x: 42 |
| 805 |
`, |
| 806 |
}, |
| 807 |
{ |
| 808 |
name: "folded", |
| 809 |
yaml: ` |
| 810 |
foo: > # comment |
| 811 |
x: 42 |
| 812 |
`, |
| 813 |
}, |
| 814 |
} |
| 815 |
for _, test := range tests { |
| 816 |
t.Run(test.name, func(t *testing.T) { |
| 817 |
f, err := parser.ParseBytes([]byte(test.yaml), parser.ParseComments) |
| 818 |
if err != nil { |
| 819 |
t.Fatalf("%+v", err) |
| 820 |
} |
| 821 |
got := "\n" + f.String() |
| 822 |
if test.yaml != got { |
| 823 |
t.Fatalf("expected:%s\ngot:%s", test.yaml, got) |
| 824 |
} |
| 825 |
}) |
| 826 |
} |
| 827 |
} |
| 828 |
|
| 829 |
func TestCommentWithNull(t *testing.T) { |
| 830 |
t.Run("same line", func(t *testing.T) { |
| 831 |
content := ` |
| 832 |
foo: |
| 833 |
bar: # comment |
| 834 |
baz: 1 |
| 835 |
` |
| 836 |
expected := ` |
| 837 |
foo: |
| 838 |
bar: null # comment |
| 839 |
baz: 1` |
| 840 |
f, err := parser.ParseBytes([]byte(content), parser.ParseComments) |
| 841 |
if err != nil { |
| 842 |
t.Fatal(err) |
| 843 |
} |
| 844 |
if len(f.Docs) != 1 { |
| 845 |
t.Fatal("failed to parse content with same line comment") |
| 846 |
} |
| 847 |
if f.Docs[0].String() != strings.TrimPrefix(expected, "\n") { |
| 848 |
t.Fatal("failed to parse comment") |
| 849 |
} |
| 850 |
}) |
| 851 |
t.Run("next line", func(t *testing.T) { |
| 852 |
content := ` |
| 853 |
foo: |
| 854 |
bar: |
| 855 |
# comment |
| 856 |
baz: 1 |
| 857 |
` |
| 858 |
expected := ` |
| 859 |
foo: |
| 860 |
bar: null # comment |
| 861 |
baz: 1` |
| 862 |
f, err := parser.ParseBytes([]byte(content), parser.ParseComments) |
| 863 |
if err != nil { |
| 864 |
t.Fatal(err) |
| 865 |
} |
| 866 |
if len(f.Docs) != 1 { |
| 867 |
t.Fatal("failed to parse content with next line comment") |
| 868 |
} |
| 869 |
if f.Docs[0].String() != strings.TrimPrefix(expected, "\n") { |
| 870 |
t.Fatal("failed to parse comment") |
| 871 |
} |
| 872 |
}) |
| 873 |
t.Run("next line and different indent", func(t *testing.T) { |
| 874 |
content := ` |
| 875 |
foo: |
| 876 |
bar: |
| 877 |
# comment |
| 878 |
baz: 1` |
| 879 |
f, err := parser.ParseBytes([]byte(content), parser.ParseComments) |
| 880 |
if err != nil { |
| 881 |
t.Fatal(err) |
| 882 |
} |
| 883 |
if len(f.Docs) != 1 { |
| 884 |
t.Fatal("failed to parse content with next line comment") |
| 885 |
} |
| 886 |
expected := ` |
| 887 |
foo: |
| 888 |
bar: null # comment |
| 889 |
baz: 1` |
| 890 |
if f.Docs[0].String() != strings.TrimPrefix(expected, "\n") { |
| 891 |
t.Fatal("failed to parse comment") |
| 892 |
} |
| 893 |
}) |
| 894 |
} |
| 895 |
|
| 896 |
func TestSequenceComment(t *testing.T) { |
| 897 |
content := ` |
| 898 |
foo: |
| 899 |
- # comment |
| 900 |
bar: 1 |
| 901 |
baz: |
| 902 |
- xxx |
| 903 |
` |
| 904 |
f, err := parser.ParseBytes([]byte(content), parser.ParseComments) |
| 905 |
if err != nil { |
| 906 |
t.Fatal(err) |
| 907 |
} |
| 908 |
if len(f.Docs) != 1 { |
| 909 |
t.Fatal("failed to parse content with next line with sequence") |
| 910 |
} |
| 911 |
expected := ` |
| 912 |
foo: |
| 913 |
# comment |
| 914 |
- bar: 1 |
| 915 |
baz: |
| 916 |
- xxx` |
| 917 |
if f.Docs[0].String() != strings.TrimPrefix(expected, "\n") { |
| 918 |
t.Fatal("failed to parse comment") |
| 919 |
} |
| 920 |
t.Run("foo[0].bar", func(t *testing.T) { |
| 921 |
path, err := yaml.PathString("$.foo[0].bar") |
| 922 |
if err != nil { |
| 923 |
t.Fatal(err) |
| 924 |
} |
| 925 |
v, err := path.FilterFile(f) |
| 926 |
if err != nil { |
| 927 |
t.Fatal(err) |
| 928 |
} |
| 929 |
if v.String() != "1" { |
| 930 |
t.Fatal("failed to get foo[0].bar value") |
| 931 |
} |
| 932 |
}) |
| 933 |
t.Run("baz[0]", func(t *testing.T) { |
| 934 |
path, err := yaml.PathString("$.baz[0]") |
| 935 |
if err != nil { |
| 936 |
t.Fatal(err) |
| 937 |
} |
| 938 |
v, err := path.FilterFile(f) |
| 939 |
if err != nil { |
| 940 |
t.Fatal(err) |
| 941 |
} |
| 942 |
if v.String() != "xxx" { |
| 943 |
t.Fatal("failed to get baz[0] value") |
| 944 |
} |
| 945 |
}) |
| 946 |
} |
| 947 |
|
| 948 |
func TestNodePath(t *testing.T) { |
| 949 |
yml := ` |
| 950 |
a: # commentA |
| 951 |
b: # commentB |
| 952 |
c: foo # commentC |
| 953 |
d: bar # commentD |
| 954 |
e: baz # commentE |
| 955 |
f: # commentF |
| 956 |
g: hoge # commentG |
| 957 |
h: # commentH |
| 958 |
- list1 # comment list1 |
| 959 |
- list2 # comment list2 |
| 960 |
- list3 # comment list3 |
| 961 |
i: fuga # commentI |
| 962 |
j: piyo # commentJ |
| 963 |
k.l.m.n: moge # commentKLMN |
| 964 |
o#p: hogera # commentOP |
| 965 |
q#.r: hogehoge # commentQR |
| 966 |
` |
| 967 |
f, err := parser.ParseBytes([]byte(yml), parser.ParseComments) |
| 968 |
if err != nil { |
| 969 |
t.Fatalf("%+v", err) |
| 970 |
} |
| 971 |
var capturer pathCapturer |
| 972 |
for _, doc := range f.Docs { |
| 973 |
ast.Walk(&capturer, doc.Body) |
| 974 |
} |
| 975 |
commentPaths := []string{} |
| 976 |
for i := 0; i < capturer.capturedNum; i++ { |
| 977 |
if capturer.orderedTypes[i] == ast.CommentType { |
| 978 |
commentPaths = append(commentPaths, capturer.orderedPaths[i]) |
| 979 |
} |
| 980 |
} |
| 981 |
expectedPaths := []string{ |
| 982 |
"$.a", |
| 983 |
"$.a.b", |
| 984 |
"$.a.b.c", |
| 985 |
"$.a.b.d", |
| 986 |
"$.a.b.e", |
| 987 |
"$.a.f", |
| 988 |
"$.a.f.g", |
| 989 |
"$.a.h", |
| 990 |
"$.a.h[0]", |
| 991 |
"$.a.h[1]", |
| 992 |
"$.a.h[2]", |
| 993 |
"$.a.i", |
| 994 |
"$.j", |
| 995 |
"$.'k.l.m.n'", |
| 996 |
"$.o#p", |
| 997 |
"$.'q#.r'", |
| 998 |
} |
| 999 |
if !reflect.DeepEqual(expectedPaths, commentPaths) { |
| 1000 |
t.Fatalf("failed to get YAMLPath to the comment node:\nexpected[%s]\ngot [%s]", expectedPaths, commentPaths) |
| 1001 |
} |
| 1002 |
} |
| 1003 |
|
| 1004 |
type pathCapturer struct { |
| 1005 |
capturedNum int |
| 1006 |
orderedPaths []string |
| 1007 |
orderedTypes []ast.NodeType |
| 1008 |
orderedTokens []*token.Token |
| 1009 |
} |
| 1010 |
|
| 1011 |
func (c *pathCapturer) Visit(node ast.Node) ast.Visitor { |
| 1012 |
c.capturedNum++ |
| 1013 |
c.orderedPaths = append(c.orderedPaths, node.GetPath()) |
| 1014 |
c.orderedTypes = append(c.orderedTypes, node.Type()) |
| 1015 |
c.orderedTokens = append(c.orderedTokens, node.GetToken()) |
| 1016 |
return c |
| 1017 |
} |
| 1018 |
|
| 1019 |
type Visitor struct { |
| 1020 |
} |
| 1021 |
|
| 1022 |
func (v *Visitor) Visit(node ast.Node) ast.Visitor { |
| 1023 |
tk := node.GetToken() |
| 1024 |
tk.Prev = nil |
| 1025 |
tk.Next = nil |
| 1026 |
return v |
| 1027 |
} |