| 
									
										
										
										
											2019-10-16 18:21:20 +09:00
										 |  |  | package parser_test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2019-12-22 11:50:07 +09:00
										 |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2021-09-07 17:31:17 +09:00
										 |  |  | 	"reflect" | 
					
						
							| 
									
										
										
										
											2019-10-16 18:21:20 +09:00
										 |  |  | 	"strings" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 12:11:17 +09:00
										 |  |  | 	"github.com/goccy/go-yaml" | 
					
						
							| 
									
										
										
										
											2019-10-16 18:21:20 +09:00
										 |  |  | 	"github.com/goccy/go-yaml/ast" | 
					
						
							|  |  |  | 	"github.com/goccy/go-yaml/lexer" | 
					
						
							|  |  |  | 	"github.com/goccy/go-yaml/parser" | 
					
						
							| 
									
										
										
										
											2021-09-07 17:31:17 +09:00
										 |  |  | 	"github.com/goccy/go-yaml/token" | 
					
						
							| 
									
										
										
										
											2019-10-16 18:21:20 +09:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestParser(t *testing.T) { | 
					
						
							|  |  |  | 	sources := []string{ | 
					
						
							|  |  |  | 		"null\n", | 
					
						
							| 
									
										
										
										
											2024-11-02 18:44:43 +09:00
										 |  |  | 		"0_", | 
					
						
							| 
									
										
										
										
											2019-10-16 18:21:20 +09:00
										 |  |  | 		"{}\n", | 
					
						
							|  |  |  | 		"v: hi\n", | 
					
						
							|  |  |  | 		"v: \"true\"\n", | 
					
						
							|  |  |  | 		"v: \"false\"\n", | 
					
						
							|  |  |  | 		"v: true\n", | 
					
						
							|  |  |  | 		"v: false\n", | 
					
						
							|  |  |  | 		"v: 10\n", | 
					
						
							|  |  |  | 		"v: -10\n", | 
					
						
							|  |  |  | 		"v: 42\n", | 
					
						
							|  |  |  | 		"v: 4294967296\n", | 
					
						
							|  |  |  | 		"v: \"10\"\n", | 
					
						
							|  |  |  | 		"v: 0.1\n", | 
					
						
							|  |  |  | 		"v: 0.99\n", | 
					
						
							|  |  |  | 		"v: -0.1\n", | 
					
						
							|  |  |  | 		"v: .inf\n", | 
					
						
							|  |  |  | 		"v: -.inf\n", | 
					
						
							|  |  |  | 		"v: .nan\n", | 
					
						
							|  |  |  | 		"v: null\n", | 
					
						
							|  |  |  | 		"v: \"\"\n", | 
					
						
							|  |  |  | 		"v:\n- A\n- B\n", | 
					
						
							|  |  |  | 		"a: '-'\n", | 
					
						
							|  |  |  | 		"123\n", | 
					
						
							|  |  |  | 		"hello: world\n", | 
					
						
							|  |  |  | 		"a: null\n", | 
					
						
							|  |  |  | 		"v:\n- A\n- 1\n- B:\n  - 2\n  - 3\n", | 
					
						
							|  |  |  | 		"a:\n  b: c\n", | 
					
						
							|  |  |  | 		"a: {x: 1}\n", | 
					
						
							|  |  |  | 		"t2: 2018-01-09T10:40:47Z\nt4: 2098-01-09T10:40:47Z\n", | 
					
						
							|  |  |  | 		"a: [1, 2]\n", | 
					
						
							|  |  |  | 		"a: {b: c, d: e}\n", | 
					
						
							|  |  |  | 		"a: 3s\n", | 
					
						
							|  |  |  | 		"a: <foo>\n", | 
					
						
							|  |  |  | 		"a: \"1:1\"\n", | 
					
						
							|  |  |  | 		"a: 1.2.3.4\n", | 
					
						
							|  |  |  | 		"a: \"2015-02-24T18:19:39Z\"\n", | 
					
						
							|  |  |  | 		"a: 'b: c'\n", | 
					
						
							|  |  |  | 		"a: 'Hello #comment'\n", | 
					
						
							| 
									
										
										
										
											2021-03-01 17:32:11 +09:00
										 |  |  | 		"a: abc <<def>> ghi", | 
					
						
							|  |  |  | 		"a: <<abcd", | 
					
						
							|  |  |  | 		"a: <<:abcd", | 
					
						
							|  |  |  | 		"a: <<  :abcd", | 
					
						
							| 
									
										
										
										
											2019-10-16 18:21:20 +09:00
										 |  |  | 		"a: 100.5\n", | 
					
						
							|  |  |  | 		"a: bogus\n", | 
					
						
							|  |  |  | 		"a: \"\\0\"\n", | 
					
						
							|  |  |  | 		"b: 2\na: 1\nd: 4\nc: 3\nsub:\n  e: 5\n", | 
					
						
							|  |  |  | 		"       a       :          b        \n", | 
					
						
							|  |  |  | 		"a: b # comment\nb: c\n", | 
					
						
							|  |  |  | 		"---\na: b\n", | 
					
						
							|  |  |  | 		"a: b\n...\n", | 
					
						
							|  |  |  | 		"%YAML 1.2\n---\n", | 
					
						
							|  |  |  | 		"a: !!binary gIGC\n", | 
					
						
							|  |  |  | 		"a: !!binary |\n  " + strings.Repeat("kJCQ", 17) + "kJ\n  CQ\n", | 
					
						
							|  |  |  | 		"- !tag\n  a: b\n  c: d\n", | 
					
						
							|  |  |  | 		"v:\n- A\n- |-\n  B\n  C\n", | 
					
						
							|  |  |  | 		"v:\n- A\n- >-\n  B\n  C\n", | 
					
						
							| 
									
										
										
										
											2020-06-01 12:54:23 +09:00
										 |  |  | 		"v: |-\n  0\n", | 
					
						
							|  |  |  | 		"v: |-\n  0\nx: 0", | 
					
						
							| 
									
										
										
										
											2020-06-02 13:06:46 +09:00
										 |  |  | 		`"a\n1\nb"`, | 
					
						
							| 
									
										
										
										
											2020-07-02 12:49:18 +09:00
										 |  |  | 		`{"a":"b"}`, | 
					
						
							| 
									
										
										
										
											2020-07-02 17:25:31 +09:00
										 |  |  | 		`!!map { | 
					
						
							|  |  |  |   ? !!str "explicit":!!str "entry", | 
					
						
							|  |  |  |   ? !!str "implicit" : !!str "entry", | 
					
						
							|  |  |  |   ? !!null "" : !!null "", | 
					
						
							|  |  |  | }`, | 
					
						
							| 
									
										
										
										
											2022-12-02 03:56:31 +09:00
										 |  |  | 		"\"a\": a\n\"b\": b", | 
					
						
							|  |  |  | 		"'a': a\n'b': b", | 
					
						
							| 
									
										
										
										
											2024-07-16 06:17:13 -04:00
										 |  |  | 		"a: \r\n  b: 1\r\n", | 
					
						
							|  |  |  | 		"a_ok: \r  bc: 2\r", | 
					
						
							|  |  |  | 		"a_mk: \n  bd: 3\n", | 
					
						
							| 
									
										
										
										
											2024-10-28 18:59:54 +03:00
										 |  |  | 		"a: :a", | 
					
						
							| 
									
										
										
										
											2024-10-30 19:48:44 +09:00
										 |  |  | 		"{a: , b: c}", | 
					
						
							| 
									
										
										
										
											2024-11-01 15:03:27 +09:00
										 |  |  | 		"value: >\n", | 
					
						
							|  |  |  | 		"value: >\n\n", | 
					
						
							|  |  |  | 		"value: >\nother:", | 
					
						
							|  |  |  | 		"value: >\n\nother:", | 
					
						
							| 
									
										
										
										
											2019-10-16 18:21:20 +09:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	for _, src := range sources { | 
					
						
							| 
									
										
										
										
											2020-07-02 12:49:05 +09:00
										 |  |  | 		if _, err := parser.Parse(lexer.Tokenize(src), 0); err != nil { | 
					
						
							|  |  |  | 			t.Fatalf("parse error: source [%s]: %+v", src, err) | 
					
						
							| 
									
										
										
										
											2019-10-16 18:21:20 +09:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestParseComplicatedDocument(t *testing.T) { | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		source string | 
					
						
							|  |  |  | 		expect string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | american: | 
					
						
							|  |  |  |   - Boston Red Sox | 
					
						
							|  |  |  |   - Detroit Tigers | 
					
						
							|  |  |  |   - New York Yankees | 
					
						
							|  |  |  | national: | 
					
						
							|  |  |  |   - New York Mets | 
					
						
							|  |  |  |   - Chicago Cubs | 
					
						
							|  |  |  |   - Atlanta Braves | 
					
						
							|  |  |  | `, ` | 
					
						
							|  |  |  | american: | 
					
						
							|  |  |  |   - Boston Red Sox | 
					
						
							|  |  |  |   - Detroit Tigers | 
					
						
							|  |  |  |   - New York Yankees | 
					
						
							|  |  |  | national: | 
					
						
							|  |  |  |   - New York Mets | 
					
						
							|  |  |  |   - Chicago Cubs | 
					
						
							|  |  |  |   - Atlanta Braves | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  |   b: c | 
					
						
							|  |  |  |   d: e | 
					
						
							|  |  |  |   f: g | 
					
						
							|  |  |  | h: | 
					
						
							|  |  |  |   i: j | 
					
						
							|  |  |  |   k: | 
					
						
							|  |  |  |     l: m | 
					
						
							|  |  |  |     n: o | 
					
						
							|  |  |  |   p: q | 
					
						
							|  |  |  | r: s | 
					
						
							|  |  |  | `, ` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  |   b: c | 
					
						
							|  |  |  |   d: e | 
					
						
							|  |  |  |   f: g | 
					
						
							|  |  |  | h: | 
					
						
							|  |  |  |   i: j | 
					
						
							|  |  |  |   k: | 
					
						
							|  |  |  |     l: m | 
					
						
							|  |  |  |     n: o | 
					
						
							|  |  |  |   p: q | 
					
						
							|  |  |  | r: s | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | - a: | 
					
						
							|  |  |  |   - b | 
					
						
							|  |  |  |   - c | 
					
						
							|  |  |  | - d | 
					
						
							|  |  |  | `, ` | 
					
						
							|  |  |  | - a: | 
					
						
							|  |  |  |   - b | 
					
						
							|  |  |  |   - c | 
					
						
							|  |  |  | - d | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | - a | 
					
						
							|  |  |  | - b | 
					
						
							|  |  |  | - c | 
					
						
							|  |  |  |  - d | 
					
						
							|  |  |  |  - e | 
					
						
							|  |  |  | - f | 
					
						
							|  |  |  | `, ` | 
					
						
							|  |  |  | - a | 
					
						
							|  |  |  | - b | 
					
						
							|  |  |  | - c - d - e | 
					
						
							|  |  |  | - f | 
					
						
							| 
									
										
										
										
											2019-12-04 23:58:06 +09:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: 0 - 1 | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: 0 - 1 | 
					
						
							| 
									
										
										
										
											2019-10-16 18:21:20 +09:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{` | 
					
						
							|  |  |  | - a: | 
					
						
							|  |  |  |    b: c | 
					
						
							|  |  |  |    d: e | 
					
						
							|  |  |  | - f: | 
					
						
							|  |  |  |   g: h | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | - a: | 
					
						
							|  |  |  |    b: c | 
					
						
							|  |  |  |    d: e | 
					
						
							| 
									
										
										
										
											2020-06-03 17:57:26 +09:00
										 |  |  | - f: null | 
					
						
							| 
									
										
										
										
											2019-10-16 18:21:20 +09:00
										 |  |  |   g: h | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  |  b | 
					
						
							|  |  |  |  c | 
					
						
							|  |  |  | d: e | 
					
						
							|  |  |  | `, ` | 
					
						
							|  |  |  | a: b c | 
					
						
							|  |  |  | d: e | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a | 
					
						
							|  |  |  | b | 
					
						
							|  |  |  | c | 
					
						
							|  |  |  | `, ` | 
					
						
							|  |  |  | a b c | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  |  - b | 
					
						
							|  |  |  |  - c | 
					
						
							|  |  |  | `, ` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  |  - b | 
					
						
							|  |  |  |  - c | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | -     a     : | 
					
						
							|  |  |  |       b: c | 
					
						
							|  |  |  | `, ` | 
					
						
							| 
									
										
										
										
											2020-06-03 17:57:26 +09:00
										 |  |  | - a: null | 
					
						
							| 
									
										
										
										
											2019-10-16 18:21:20 +09:00
										 |  |  |   b: c | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | - a: | 
					
						
							|  |  |  |    b | 
					
						
							|  |  |  |    c | 
					
						
							|  |  |  |    d | 
					
						
							|  |  |  |   hoge: fuga | 
					
						
							|  |  |  | `, ` | 
					
						
							|  |  |  | - a: b c d | 
					
						
							|  |  |  |   hoge: fuga | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | - a # ' " # - : % | 
					
						
							|  |  |  | - b # " # - : % ' | 
					
						
							|  |  |  | - c # # - : % ' " | 
					
						
							|  |  |  | - d # - : % ' " # | 
					
						
							|  |  |  | - e # : % ' " # - | 
					
						
							|  |  |  | - f # % ' : # - : | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | - a | 
					
						
							|  |  |  | - b | 
					
						
							|  |  |  | - c | 
					
						
							|  |  |  | - d | 
					
						
							|  |  |  | - e | 
					
						
							|  |  |  | - f | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | # comment | 
					
						
							|  |  |  | a: # comment | 
					
						
							|  |  |  | # comment | 
					
						
							|  |  |  |  b: c # comment | 
					
						
							|  |  |  |  # comment | 
					
						
							|  |  |  | d: e # comment | 
					
						
							|  |  |  | # comment | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  |  b: c | 
					
						
							|  |  |  | d: e | 
					
						
							| 
									
										
										
										
											2019-11-12 19:50:58 +09:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: b#notcomment | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: b#notcomment | 
					
						
							| 
									
										
										
										
											2019-10-16 18:21:20 +09:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | anchored: &anchor foo | 
					
						
							|  |  |  | aliased: *anchor | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | anchored: &anchor foo | 
					
						
							|  |  |  | aliased: *anchor | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | --- | 
					
						
							|  |  |  | - &CENTER { x: 1, y: 2 } | 
					
						
							|  |  |  | - &LEFT { x: 0, y: 2 } | 
					
						
							|  |  |  | - &BIG { r: 10 } | 
					
						
							|  |  |  | - &SMALL { r: 1 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # All the following maps are equal: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - # Explicit keys | 
					
						
							|  |  |  |   x: 1 | 
					
						
							|  |  |  |   y: 2 | 
					
						
							|  |  |  |   r: 10 | 
					
						
							|  |  |  |   label: center/big | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - # Merge one map | 
					
						
							|  |  |  |   << : *CENTER | 
					
						
							|  |  |  |   r: 10 | 
					
						
							|  |  |  |   label: center/big | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - # Merge multiple maps | 
					
						
							|  |  |  |   << : [ *CENTER, *BIG ] | 
					
						
							|  |  |  |   label: center/big | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - # Override | 
					
						
							|  |  |  |   << : [ *BIG, *LEFT, *SMALL ] | 
					
						
							|  |  |  |   x: 1 | 
					
						
							|  |  |  |   label: center/big | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							| 
									
										
										
										
											2019-11-05 17:02:55 +09:00
										 |  |  | --- | 
					
						
							| 
									
										
										
										
											2019-10-16 18:21:20 +09:00
										 |  |  | - &CENTER {x: 1, y: 2} | 
					
						
							|  |  |  | - &LEFT {x: 0, y: 2} | 
					
						
							|  |  |  | - &BIG {r: 10} | 
					
						
							|  |  |  | - &SMALL {r: 1} | 
					
						
							|  |  |  | - x: 1 | 
					
						
							|  |  |  |   y: 2 | 
					
						
							|  |  |  |   r: 10 | 
					
						
							|  |  |  |   label: center/big | 
					
						
							|  |  |  | - <<: *CENTER | 
					
						
							|  |  |  |   r: 10 | 
					
						
							|  |  |  |   label: center/big | 
					
						
							|  |  |  | - <<: [*CENTER, *BIG] | 
					
						
							|  |  |  |   label: center/big | 
					
						
							|  |  |  | - <<: [*BIG, *LEFT, *SMALL] | 
					
						
							|  |  |  |   x: 1 | 
					
						
							|  |  |  |   label: center/big | 
					
						
							| 
									
										
										
										
											2019-10-23 20:22:14 +09:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  | - - b | 
					
						
							|  |  |  | - - c | 
					
						
							|  |  |  |   - d | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  | - - b | 
					
						
							|  |  |  | - - c | 
					
						
							|  |  |  |   - d | 
					
						
							| 
									
										
										
										
											2019-10-24 13:52:12 +09:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  |   b: | 
					
						
							|  |  |  |     c: d | 
					
						
							|  |  |  |   e: | 
					
						
							|  |  |  |     f: g | 
					
						
							|  |  |  |     h: i | 
					
						
							|  |  |  | j: k | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  |   b: | 
					
						
							|  |  |  |     c: d | 
					
						
							|  |  |  |   e: | 
					
						
							|  |  |  |     f: g | 
					
						
							|  |  |  |     h: i | 
					
						
							|  |  |  | j: k | 
					
						
							| 
									
										
										
										
											2019-11-05 17:02:55 +09:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | --- | 
					
						
							|  |  |  | a: 1 | 
					
						
							|  |  |  | b: 2 | 
					
						
							|  |  |  | ... | 
					
						
							|  |  |  | --- | 
					
						
							|  |  |  | c: 3 | 
					
						
							|  |  |  | d: 4 | 
					
						
							|  |  |  | ... | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | --- | 
					
						
							|  |  |  | a: 1 | 
					
						
							|  |  |  | b: 2 | 
					
						
							|  |  |  | ... | 
					
						
							|  |  |  | --- | 
					
						
							|  |  |  | c: 3 | 
					
						
							|  |  |  | d: 4 | 
					
						
							|  |  |  | ... | 
					
						
							| 
									
										
										
										
											2019-11-07 23:45:39 +09:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  |   b: | | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       [ 1, 2 ] | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   c: d | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  |   b: | | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       [ 1, 2 ] | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   c: d | 
					
						
							| 
									
										
										
										
											2019-11-08 16:48:54 +09:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | | | 
					
						
							|  |  |  |     hoge | 
					
						
							|  |  |  |     fuga | 
					
						
							|  |  |  |     piyo`, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | | | 
					
						
							|  |  |  |     hoge | 
					
						
							|  |  |  |     fuga | 
					
						
							|  |  |  |     piyo | 
					
						
							| 
									
										
										
										
											2019-11-19 18:03:24 +09:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2024-10-28 17:07:44 +09:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | v: | | 
					
						
							|  |  |  |  a | 
					
						
							|  |  |  |  b | 
					
						
							|  |  |  |  c`, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | v: | | 
					
						
							|  |  |  |  a | 
					
						
							|  |  |  |  b | 
					
						
							|  |  |  |  c | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-19 18:03:24 +09:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | | 
					
						
							|  |  |  |    bbbbbbb | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    ccccccc | 
					
						
							|  |  |  | d: eeeeeeeeeeeeeeeee | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | | 
					
						
							|  |  |  |    bbbbbbb | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    ccccccc | 
					
						
							|  |  |  | d: eeeeeeeeeeeeeeeee | 
					
						
							| 
									
										
										
										
											2019-12-11 17:17:05 +09:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: b     | 
					
						
							|  |  |  |   c | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: b c | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a:     | 
					
						
							|  |  |  |   b: c | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  |   b: c | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: b     | 
					
						
							|  |  |  | c: d | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: b | 
					
						
							|  |  |  | c: d | 
					
						
							| 
									
										
										
										
											2020-03-07 12:03:06 +09:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | - ab - cd | 
					
						
							|  |  |  | - ef - gh | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | - ab - cd | 
					
						
							|  |  |  | - ef - gh | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | - 0 - 1 | 
					
						
							|  |  |  |  - 2 - 3 | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | - 0 - 1 - 2 - 3 | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a - b - c: value | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a - b - c: value | 
					
						
							| 
									
										
										
										
											2020-03-07 20:17:54 +09:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  | - | 
					
						
							|  |  |  |   b: c | 
					
						
							|  |  |  |   d: e | 
					
						
							|  |  |  | - | 
					
						
							|  |  |  |   f: g | 
					
						
							|  |  |  |   h: i | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  | - b: c | 
					
						
							|  |  |  |   d: e | 
					
						
							|  |  |  | - f: g | 
					
						
							|  |  |  |   h: i | 
					
						
							| 
									
										
										
										
											2020-06-15 20:16:51 +09:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: |- | 
					
						
							|  |  |  |   value | 
					
						
							|  |  |  | b: c | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: |- | 
					
						
							|  |  |  |   value | 
					
						
							|  |  |  | b: c | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a:  |+ | 
					
						
							|  |  |  |   value | 
					
						
							|  |  |  | b: c | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: |+ | 
					
						
							|  |  |  |   value | 
					
						
							|  |  |  | b: c | 
					
						
							| 
									
										
										
										
											2022-01-11 20:37:31 +09:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | - key1: val | 
					
						
							|  |  |  |   key2: | 
					
						
							|  |  |  |     ( | 
					
						
							|  |  |  |       foo | 
					
						
							|  |  |  |       + | 
					
						
							|  |  |  |       bar | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | - key1: val | 
					
						
							|  |  |  |   key2: ( foo + bar ) | 
					
						
							| 
									
										
										
										
											2022-12-02 03:56:31 +09:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | "a": b | 
					
						
							|  |  |  | 'c': d | 
					
						
							|  |  |  | "e": "f" | 
					
						
							|  |  |  | g: "h" | 
					
						
							|  |  |  | i: 'j' | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | "a": b | 
					
						
							|  |  |  | 'c': d | 
					
						
							|  |  |  | "e": "f" | 
					
						
							|  |  |  | g: "h" | 
					
						
							|  |  |  | i: 'j' | 
					
						
							| 
									
										
										
										
											2019-10-16 18:21:20 +09:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-09-09 20:31:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-16 18:21:20 +09:00
										 |  |  | 	for _, test := range tests { | 
					
						
							| 
									
										
										
										
											2022-01-11 20:37:31 +09:00
										 |  |  | 		t.Run(test.source, func(t *testing.T) { | 
					
						
							|  |  |  | 			tokens := lexer.Tokenize(test.source) | 
					
						
							|  |  |  | 			f, err := parser.Parse(tokens, 0) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				t.Fatalf("%+v", err) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2024-11-02 05:24:28 +00:00
										 |  |  | 			got := f.String() | 
					
						
							|  |  |  | 			if got != strings.TrimPrefix(test.expect, "\n") { | 
					
						
							|  |  |  | 				t.Fatalf("failed to parse comment:\nexpected:\n%s\ngot:\n%s", strings.TrimPrefix(test.expect, "\n"), got) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2022-01-11 20:37:31 +09:00
										 |  |  | 			var v Visitor | 
					
						
							|  |  |  | 			for _, doc := range f.Docs { | 
					
						
							|  |  |  | 				ast.Walk(&v, doc.Body) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2022-12-08 22:48:12 +09:00
										 |  |  | 			expect := fmt.Sprintf("\n%+v", f) | 
					
						
							| 
									
										
										
										
											2022-01-11 20:37:31 +09:00
										 |  |  | 			if test.expect != expect { | 
					
						
							|  |  |  | 				tokens.Dump() | 
					
						
							|  |  |  | 				t.Fatalf("unexpected output: [%s] != [%s]", test.expect, expect) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2019-10-16 18:21:20 +09:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-02 05:24:28 +00:00
										 |  |  | func TestParseWhitespace(t *testing.T) { | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		source string | 
					
						
							|  |  |  | 		expect string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: b | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | c: d | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | e: f | 
					
						
							|  |  |  | g: h | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: b | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | c: d | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | e: f | 
					
						
							|  |  |  | g: h | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  |   - b: c | 
					
						
							|  |  |  |     d: e | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   - f: g | 
					
						
							|  |  |  |     h: i | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  |   - b: c | 
					
						
							|  |  |  |     d: e | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   - f: g | 
					
						
							|  |  |  |     h: i | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  |   - b: c | 
					
						
							|  |  |  |     d: e | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   - f: g | 
					
						
							|  |  |  |     h: i | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  |   - b: c | 
					
						
							|  |  |  |     d: e | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   - f: g | 
					
						
							|  |  |  |     h: i | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  | - b: c | 
					
						
							|  |  |  |   d: e | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - f: g | 
					
						
							|  |  |  |   h: i | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  | - b: c | 
					
						
							|  |  |  |   d: e | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - f: g | 
					
						
							|  |  |  |   h: i | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  | # comment 1 | 
					
						
							|  |  |  | - b: c | 
					
						
							|  |  |  |   d: e | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # comment 2 | 
					
						
							|  |  |  | - f: g | 
					
						
							|  |  |  |   h: i | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  | # comment 1 | 
					
						
							|  |  |  | - b: c | 
					
						
							|  |  |  |   d: e | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # comment 2 | 
					
						
							|  |  |  | - f: g | 
					
						
							|  |  |  |   h: i | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  |   # comment 1 | 
					
						
							|  |  |  |   - b: c | 
					
						
							|  |  |  |     # comment 2 | 
					
						
							|  |  |  |     d: e | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # comment 3 | 
					
						
							|  |  |  |   # comment 4 | 
					
						
							|  |  |  |   - f: g | 
					
						
							|  |  |  |     h: i # comment 5 | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  |   # comment 1 | 
					
						
							|  |  |  |   - b: c | 
					
						
							|  |  |  |     # comment 2 | 
					
						
							|  |  |  |     d: e | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # comment 3 | 
					
						
							|  |  |  |   # comment 4 | 
					
						
							|  |  |  |   - f: g | 
					
						
							|  |  |  |     h: i # comment 5 | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  |   # comment 1 | 
					
						
							|  |  |  |   - b: c | 
					
						
							|  |  |  |     # comment 2 | 
					
						
							|  |  |  |     d: e | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # comment 3 | 
					
						
							|  |  |  |   # comment 4 | 
					
						
							|  |  |  |   - f: | | 
					
						
							|  |  |  |       g | 
					
						
							|  |  |  |       g | 
					
						
							|  |  |  |     h: i # comment 5 | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  |   # comment 1 | 
					
						
							|  |  |  |   - b: c | 
					
						
							|  |  |  |     # comment 2 | 
					
						
							|  |  |  |     d: e | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # comment 3 | 
					
						
							|  |  |  |   # comment 4 | 
					
						
							|  |  |  |   - f: | | 
					
						
							|  |  |  |       g | 
					
						
							|  |  |  |       g | 
					
						
							|  |  |  |     h: i # comment 5 | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  |   # comment 1 | 
					
						
							|  |  |  |   - b: c | 
					
						
							|  |  |  |     # comment 2 | 
					
						
							|  |  |  |     d: e | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # comment 3 | 
					
						
							|  |  |  |   # comment 4 | 
					
						
							|  |  |  |   - f: | | 
					
						
							|  |  |  |       asd | 
					
						
							|  |  |  |       def | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     h: i # comment 5 | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  |   # comment 1 | 
					
						
							|  |  |  |   - b: c | 
					
						
							|  |  |  |     # comment 2 | 
					
						
							|  |  |  |     d: e | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # comment 3 | 
					
						
							|  |  |  |   # comment 4 | 
					
						
							|  |  |  |   - f: | | 
					
						
							|  |  |  |       asd | 
					
						
							|  |  |  |       def | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     h: i # comment 5 | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | - b: c | 
					
						
							|  |  |  |   d: e | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - f: g | 
					
						
							|  |  |  |   h: i # comment 4 | 
					
						
							|  |  |  | 		`, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | - b: c | 
					
						
							|  |  |  |   d: e | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - f: g | 
					
						
							|  |  |  |   h: i # comment 4 | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: null | 
					
						
							|  |  |  | b: null | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | d: e | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: null | 
					
						
							|  |  |  | b: null | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | d: e | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | foo: | 
					
						
							|  |  |  |   bar: null # comment | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   baz: 1 | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | foo: | 
					
						
							|  |  |  |   bar: null # comment | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   baz: 1 | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | foo: | 
					
						
							|  |  |  |   bar: null # comment | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | baz: 1 | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | foo: | 
					
						
							|  |  |  |   bar: null # comment | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | baz: 1 | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, test := range tests { | 
					
						
							|  |  |  | 		t.Run(test.source, func(t *testing.T) { | 
					
						
							|  |  |  | 			f, err := parser.ParseBytes([]byte(test.source), parser.ParseComments) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				t.Fatal(err) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			got := f.String() | 
					
						
							|  |  |  | 			if got != strings.TrimPrefix(test.expect, "\n") { | 
					
						
							|  |  |  | 				t.Fatalf("failed to parse comment:\nexpected:\n%s\ngot:\n%s", strings.TrimPrefix(test.expect, "\n"), got) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-22 11:50:07 +09:00
										 |  |  | func TestNewLineChar(t *testing.T) { | 
					
						
							|  |  |  | 	for _, f := range []string{ | 
					
						
							|  |  |  | 		"lf.yml", | 
					
						
							|  |  |  | 		"cr.yml", | 
					
						
							|  |  |  | 		"crlf.yml", | 
					
						
							|  |  |  | 	} { | 
					
						
							| 
									
										
										
										
											2019-12-22 12:08:45 +09:00
										 |  |  | 		ast, err := parser.ParseFile(filepath.Join("testdata", f), 0) | 
					
						
							| 
									
										
										
										
											2019-12-22 11:59:24 +09:00
										 |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2019-12-22 11:50:07 +09:00
										 |  |  | 			t.Fatalf("%+v", err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-12-08 22:48:12 +09:00
										 |  |  | 		actual := fmt.Sprintf("%v", ast) | 
					
						
							| 
									
										
										
										
											2019-12-22 11:59:24 +09:00
										 |  |  | 		expect := `a: "a" | 
					
						
							| 
									
										
										
										
											2024-11-02 05:24:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-22 11:59:24 +09:00
										 |  |  | b: 1 | 
					
						
							|  |  |  | ` | 
					
						
							|  |  |  | 		if expect != actual { | 
					
						
							| 
									
										
										
										
											2024-11-02 05:24:28 +00:00
										 |  |  | 			t.Fatalf("unexpected result\nexpected:\n%s\ngot:\n%s", expect, actual) | 
					
						
							| 
									
										
										
										
											2019-12-22 11:59:24 +09:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-12-22 11:50:07 +09:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-23 03:21:42 +09:00
										 |  |  | func TestSyntaxError(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2020-07-02 14:38:34 +09:00
										 |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		source string | 
					
						
							|  |  |  | 		expect string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  | - b | 
					
						
							|  |  |  |   c: d | 
					
						
							|  |  |  |   e: f | 
					
						
							|  |  |  |   g: h`, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | [3:3] unexpected key name | 
					
						
							|  |  |  |    2 | a: | 
					
						
							|  |  |  | >  3 | - b | 
					
						
							|  |  |  |    4 |   c: d | 
					
						
							| 
									
										
										
										
											2020-07-02 12:49:05 +09:00
										 |  |  |          ^ | 
					
						
							| 
									
										
										
										
											2020-07-02 14:38:34 +09:00
										 |  |  |    5 |   e: f | 
					
						
							|  |  |  |    6 |   g: h`, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a | 
					
						
							|  |  |  | - b: c`, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | [2:1] unexpected key name | 
					
						
							|  |  |  | >  2 | a | 
					
						
							|  |  |  |    3 | - b: c | 
					
						
							|  |  |  |        ^ | 
					
						
							| 
									
										
										
										
											2020-09-09 20:31:42 +03:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`%YAML 1.1 {}`, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | [1:2] unexpected directive value. document not started | 
					
						
							|  |  |  | >  1 | %YAML 1.1 {} | 
					
						
							|  |  |  |         ^ | 
					
						
							| 
									
										
										
										
											2021-03-01 17:58:17 +09:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`{invalid`, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | [1:2] unexpected map | 
					
						
							|  |  |  | >  1 | {invalid | 
					
						
							|  |  |  |         ^ | 
					
						
							| 
									
										
										
										
											2021-03-04 11:44:55 -08:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`{ "key": "value" `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | [1:1] unterminated flow mapping | 
					
						
							|  |  |  | >  1 | { "key": "value" | 
					
						
							|  |  |  |        ^ | 
					
						
							| 
									
										
										
										
											2021-12-07 16:13:35 -08:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: | 
					
						
							|  |  |  | - b: c | 
					
						
							|  |  |  | - `, | 
					
						
							|  |  |  | 			` | 
					
						
							| 
									
										
										
										
											2024-10-31 16:05:37 +09:00
										 |  |  | [4:1] empty sequence value | 
					
						
							| 
									
										
										
										
											2021-12-07 16:13:35 -08:00
										 |  |  |    2 | a: | 
					
						
							|  |  |  |    3 | - b: c | 
					
						
							|  |  |  | >  4 | - | 
					
						
							|  |  |  |        ^ | 
					
						
							| 
									
										
										
										
											2020-07-02 14:38:34 +09:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2024-10-29 20:00:48 +09:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: |invalidopt | 
					
						
							|  |  |  |   foo | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | [2:4] found invalid token | 
					
						
							| 
									
										
										
										
											2024-10-31 22:54:26 +09:00
										 |  |  | >  2 | a: |invalidopt | 
					
						
							| 
									
										
										
										
											2024-10-29 20:00:48 +09:00
										 |  |  |           ^ | 
					
						
							|  |  |  |    3 |   foo`, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2024-10-31 15:35:43 +09:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: 1 | 
					
						
							|  |  |  | b | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | [3:1] required ':' and map value | 
					
						
							|  |  |  |    2 | a: 1 | 
					
						
							|  |  |  | >  3 | b | 
					
						
							|  |  |  |        ^ | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: 1 | 
					
						
							|  |  |  | b | 
					
						
							|  |  |  | - c | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | [4:1] required ':' and map value | 
					
						
							|  |  |  |    2 | a: 1 | 
					
						
							|  |  |  |    3 | b | 
					
						
							|  |  |  | >  4 | - c | 
					
						
							|  |  |  |        ^ | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`a: [`, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | [1:4] sequence end token ']' not found | 
					
						
							|  |  |  | >  1 | a: [ | 
					
						
							|  |  |  |           ^ | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`a: ]`, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | [1:4] could not find '[' character corresponding to ']' | 
					
						
							|  |  |  | >  1 | a: ] | 
					
						
							|  |  |  |           ^ | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`a: [ [1] [2] [3] ]`, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | [1:10] ',' or ']' must be specified | 
					
						
							|  |  |  | >  1 | a: [ [1] [2] [3] ] | 
					
						
							|  |  |  |                 ^ | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2024-10-31 16:05:37 +09:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: - | 
					
						
							|  |  |  | b: - | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | [3:4] empty sequence value | 
					
						
							|  |  |  |    2 | a: - | 
					
						
							|  |  |  | >  3 | b: - | 
					
						
							|  |  |  |           ^ | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | a: - 1 | 
					
						
							|  |  |  | b: - 2 | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | [2:4] block sequence entries are not allowed in this context | 
					
						
							|  |  |  | >  2 | a: - 1 | 
					
						
							|  |  |  |           ^ | 
					
						
							|  |  |  |    3 | b: - 2`, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2024-10-31 22:54:26 +09:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			`a: 'foobarbaz`, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | [1:4] found invalid token | 
					
						
							|  |  |  | >  1 | a: 'foobarbaz | 
					
						
							|  |  |  |           ^ | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`a: "\"key\": \"value:\"`, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | [1:4] found invalid token | 
					
						
							|  |  |  | >  1 | a: "\"key\": \"value:\" | 
					
						
							|  |  |  |           ^ | 
					
						
							| 
									
										
										
										
											2024-11-01 15:03:27 +09:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`foo: [${should not be allowed}]`, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | [1:8] ',' or ']' must be specified | 
					
						
							|  |  |  | >  1 | foo: [${should not be allowed}] | 
					
						
							|  |  |  |               ^ | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			`foo: [$[should not be allowed]]`, | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | [1:8] ',' or ']' must be specified | 
					
						
							|  |  |  | >  1 | foo: [$[should not be allowed]] | 
					
						
							|  |  |  |               ^ | 
					
						
							| 
									
										
										
										
											2024-11-02 19:29:48 +09:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			">\n>", | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | [2:1] found invalid token | 
					
						
							|  |  |  |    1 | > | 
					
						
							|  |  |  | >  2 | > | 
					
						
							|  |  |  |        ^ | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			">\n1", | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | [2:1] found invalid token | 
					
						
							|  |  |  |    1 | > | 
					
						
							|  |  |  | >  2 | 1 | 
					
						
							|  |  |  |        ^ | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			"|\n1", | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | [2:1] found invalid token | 
					
						
							|  |  |  |    1 | | | 
					
						
							|  |  |  | >  2 | 1 | 
					
						
							|  |  |  |        ^ | 
					
						
							| 
									
										
										
										
											2024-11-03 02:11:50 +09:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			"a: >3\n  1", | 
					
						
							|  |  |  | 			` | 
					
						
							|  |  |  | [2:3] found invalid token | 
					
						
							|  |  |  |    1 | a: >3 | 
					
						
							|  |  |  | >  2 |   1 | 
					
						
							|  |  |  |          ^ | 
					
						
							| 
									
										
										
										
											2024-10-31 22:54:26 +09:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2020-07-02 14:38:34 +09:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	for _, test := range tests { | 
					
						
							|  |  |  | 		t.Run(test.source, func(t *testing.T) { | 
					
						
							|  |  |  | 			_, err := parser.ParseBytes([]byte(test.source), 0) | 
					
						
							|  |  |  | 			if err == nil { | 
					
						
							|  |  |  | 				t.Fatal("cannot catch syntax error") | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			actual := "\n" + err.Error() | 
					
						
							|  |  |  | 			if test.expect != actual { | 
					
						
							|  |  |  | 				t.Fatalf("expected: [%s] but got [%s]", test.expect, actual) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2019-10-23 03:21:42 +09:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-28 21:39:06 +09:00
										 |  |  | func TestComment(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2020-05-29 11:49:32 +09:00
										 |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		name string | 
					
						
							|  |  |  | 		yaml string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name: "map with comment", | 
					
						
							|  |  |  | 			yaml: ` | 
					
						
							| 
									
										
										
										
											2020-05-28 21:39:06 +09:00
										 |  |  | # commentA | 
					
						
							|  |  |  | a: #commentB | 
					
						
							|  |  |  |   # commentC | 
					
						
							|  |  |  |   b: c # commentD | 
					
						
							|  |  |  |   # commentE | 
					
						
							|  |  |  |   d: e # commentF | 
					
						
							|  |  |  |   # commentG | 
					
						
							|  |  |  |   f: g # commentH | 
					
						
							|  |  |  | # commentI | 
					
						
							|  |  |  | f: g # commentJ | 
					
						
							|  |  |  | # commentK | 
					
						
							| 
									
										
										
										
											2020-05-29 11:49:32 +09:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name: "sequence with comment", | 
					
						
							|  |  |  | 			yaml: ` | 
					
						
							|  |  |  | # commentA | 
					
						
							|  |  |  | - a # commentB | 
					
						
							|  |  |  | # commentC | 
					
						
							|  |  |  | - b: # commentD | 
					
						
							|  |  |  |   # commentE | 
					
						
							|  |  |  |   - d # commentF | 
					
						
							|  |  |  |   - e # commentG | 
					
						
							|  |  |  | # commentH | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name: "anchor and alias", | 
					
						
							|  |  |  | 			yaml: ` | 
					
						
							|  |  |  | a: &x b # commentA | 
					
						
							|  |  |  | c: *x # commentB | 
					
						
							| 
									
										
										
										
											2021-07-16 21:49:17 +09:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name: "multiline", | 
					
						
							|  |  |  | 			yaml: ` | 
					
						
							|  |  |  | # foo comment | 
					
						
							|  |  |  | # foo comment2 | 
					
						
							|  |  |  | foo: # map key comment | 
					
						
							|  |  |  |   # bar above comment | 
					
						
							|  |  |  |   # bar above comment2 | 
					
						
							|  |  |  |   bar: 10 # comment for bar | 
					
						
							|  |  |  |   # baz above comment | 
					
						
							|  |  |  |   # baz above comment2 | 
					
						
							|  |  |  |   baz: bbbb # comment for baz | 
					
						
							|  |  |  |   piyo: # sequence key comment | 
					
						
							|  |  |  |   # sequence1 above comment 1 | 
					
						
							|  |  |  |   # sequence1 above comment 2 | 
					
						
							|  |  |  |   - sequence1 # sequence1 | 
					
						
							|  |  |  |   # sequence2 above comment 1 | 
					
						
							|  |  |  |   # sequence2 above comment 2 | 
					
						
							|  |  |  |   - sequence2 # sequence2 | 
					
						
							|  |  |  |   # sequence3 above comment 1 | 
					
						
							|  |  |  |   # sequence3 above comment 2 | 
					
						
							|  |  |  |   - false # sequence3 | 
					
						
							|  |  |  | # foo2 comment | 
					
						
							|  |  |  | # foo2 comment2 | 
					
						
							|  |  |  | foo2: &anchor text # anchor comment | 
					
						
							|  |  |  | # foo3 comment | 
					
						
							|  |  |  | # foo3 comment2 | 
					
						
							|  |  |  | foo3: *anchor # alias comment | 
					
						
							| 
									
										
										
										
											2021-07-19 18:48:09 +09:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name: "literal", | 
					
						
							|  |  |  | 			yaml: ` | 
					
						
							|  |  |  | foo: | # comment | 
					
						
							|  |  |  |   x: 42 | 
					
						
							|  |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name: "folded", | 
					
						
							|  |  |  | 			yaml: ` | 
					
						
							|  |  |  | foo: > # comment | 
					
						
							|  |  |  |   x: 42 | 
					
						
							| 
									
										
										
										
											2020-05-29 11:49:32 +09:00
										 |  |  | `, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2020-05-28 21:39:06 +09:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-05-29 11:49:32 +09:00
										 |  |  | 	for _, test := range tests { | 
					
						
							|  |  |  | 		t.Run(test.name, func(t *testing.T) { | 
					
						
							|  |  |  | 			f, err := parser.ParseBytes([]byte(test.yaml), parser.ParseComments) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				t.Fatalf("%+v", err) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2022-12-08 22:48:12 +09:00
										 |  |  | 			got := "\n" + f.String() | 
					
						
							| 
									
										
										
										
											2021-07-16 21:49:17 +09:00
										 |  |  | 			if test.yaml != got { | 
					
						
							|  |  |  | 				t.Fatalf("expected:%s\ngot:%s", test.yaml, got) | 
					
						
							| 
									
										
										
										
											2020-05-29 11:49:32 +09:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2020-05-28 21:39:06 +09:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-14 12:25:21 +09:00
										 |  |  | func TestCommentWithNull(t *testing.T) { | 
					
						
							|  |  |  | 	t.Run("same line", func(t *testing.T) { | 
					
						
							|  |  |  | 		content := ` | 
					
						
							|  |  |  | foo: | 
					
						
							|  |  |  |   bar: # comment | 
					
						
							|  |  |  |   baz: 1 | 
					
						
							|  |  |  | ` | 
					
						
							|  |  |  | 		expected := ` | 
					
						
							|  |  |  | foo: | 
					
						
							|  |  |  |   bar: null # comment | 
					
						
							|  |  |  |   baz: 1` | 
					
						
							|  |  |  | 		f, err := parser.ParseBytes([]byte(content), parser.ParseComments) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Fatal(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if len(f.Docs) != 1 { | 
					
						
							|  |  |  | 			t.Fatal("failed to parse content with same line comment") | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-11-02 05:24:28 +00:00
										 |  |  | 		got := f.Docs[0].String() | 
					
						
							|  |  |  | 		if got != strings.TrimPrefix(expected, "\n") { | 
					
						
							|  |  |  | 			t.Fatalf("failed to parse comment:\nexpected:\n%s\ngot:\n%s", strings.TrimPrefix(expected, "\n"), got) | 
					
						
							| 
									
										
										
										
											2023-09-14 12:25:21 +09:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	t.Run("next line", func(t *testing.T) { | 
					
						
							|  |  |  | 		content := ` | 
					
						
							|  |  |  | foo: | 
					
						
							|  |  |  |   bar: | 
					
						
							|  |  |  |     # comment | 
					
						
							|  |  |  |   baz: 1 | 
					
						
							|  |  |  | ` | 
					
						
							|  |  |  | 		expected := ` | 
					
						
							|  |  |  | foo: | 
					
						
							|  |  |  |   bar: null # comment | 
					
						
							|  |  |  |   baz: 1` | 
					
						
							|  |  |  | 		f, err := parser.ParseBytes([]byte(content), parser.ParseComments) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Fatal(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if len(f.Docs) != 1 { | 
					
						
							|  |  |  | 			t.Fatal("failed to parse content with next line comment") | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-11-02 05:24:28 +00:00
										 |  |  | 		got := f.Docs[0].String() | 
					
						
							|  |  |  | 		if got != strings.TrimPrefix(expected, "\n") { | 
					
						
							|  |  |  | 			t.Fatalf("failed to parse comment:\nexpected:\n%s\ngot:\n%s", strings.TrimPrefix(expected, "\n"), got) | 
					
						
							| 
									
										
										
										
											2023-09-14 12:25:21 +09:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	t.Run("next line and different indent", func(t *testing.T) { | 
					
						
							|  |  |  | 		content := ` | 
					
						
							|  |  |  | foo: | 
					
						
							|  |  |  |   bar: | 
					
						
							|  |  |  |  # comment | 
					
						
							|  |  |  | baz: 1` | 
					
						
							|  |  |  | 		f, err := parser.ParseBytes([]byte(content), parser.ParseComments) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Fatal(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if len(f.Docs) != 1 { | 
					
						
							|  |  |  | 			t.Fatal("failed to parse content with next line comment") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		expected := ` | 
					
						
							|  |  |  | foo: | 
					
						
							|  |  |  |   bar: null # comment | 
					
						
							|  |  |  | baz: 1` | 
					
						
							| 
									
										
										
										
											2024-11-02 05:24:28 +00:00
										 |  |  | 		got := f.Docs[0].String() | 
					
						
							|  |  |  | 		if got != strings.TrimPrefix(expected, "\n") { | 
					
						
							|  |  |  | 			t.Fatalf("failed to parse comment:\nexpected:\n%s\ngot:\n%s", strings.TrimPrefix(expected, "\n"), got) | 
					
						
							| 
									
										
										
										
											2023-09-14 12:25:21 +09:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2023-09-15 12:11:17 +09:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-09-14 12:25:21 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 12:11:17 +09:00
										 |  |  | func TestSequenceComment(t *testing.T) { | 
					
						
							|  |  |  | 	content := ` | 
					
						
							|  |  |  | foo: | 
					
						
							|  |  |  |   - # comment | 
					
						
							|  |  |  |     bar: 1 | 
					
						
							|  |  |  | baz: | 
					
						
							|  |  |  |   - xxx | 
					
						
							|  |  |  | ` | 
					
						
							|  |  |  | 	f, err := parser.ParseBytes([]byte(content), parser.ParseComments) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if len(f.Docs) != 1 { | 
					
						
							|  |  |  | 		t.Fatal("failed to parse content with next line with sequence") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	expected := ` | 
					
						
							|  |  |  | foo: | 
					
						
							|  |  |  |   # comment | 
					
						
							|  |  |  |   - bar: 1 | 
					
						
							|  |  |  | baz: | 
					
						
							|  |  |  |   - xxx` | 
					
						
							| 
									
										
										
										
											2024-11-02 05:24:28 +00:00
										 |  |  | 	got := f.Docs[0].String() | 
					
						
							|  |  |  | 	if got != strings.TrimPrefix(expected, "\n") { | 
					
						
							|  |  |  | 		t.Fatalf("failed to parse comment:\nexpected:\n%s\ngot:\n%s", strings.TrimPrefix(expected, "\n"), got) | 
					
						
							| 
									
										
										
										
											2023-09-15 12:11:17 +09:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	t.Run("foo[0].bar", func(t *testing.T) { | 
					
						
							|  |  |  | 		path, err := yaml.PathString("$.foo[0].bar") | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Fatal(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		v, err := path.FilterFile(f) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Fatal(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if v.String() != "1" { | 
					
						
							|  |  |  | 			t.Fatal("failed to get foo[0].bar value") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	t.Run("baz[0]", func(t *testing.T) { | 
					
						
							|  |  |  | 		path, err := yaml.PathString("$.baz[0]") | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Fatal(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		v, err := path.FilterFile(f) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			t.Fatal(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if v.String() != "xxx" { | 
					
						
							|  |  |  | 			t.Fatal("failed to get baz[0] value") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2023-09-14 12:25:21 +09:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-07 17:31:17 +09:00
										 |  |  | func TestNodePath(t *testing.T) { | 
					
						
							|  |  |  | 	yml := ` | 
					
						
							|  |  |  | a: # commentA | 
					
						
							|  |  |  |   b: # commentB | 
					
						
							|  |  |  |     c: foo # commentC | 
					
						
							|  |  |  |     d: bar # commentD | 
					
						
							|  |  |  |     e: baz # commentE | 
					
						
							|  |  |  |   f: # commentF | 
					
						
							|  |  |  |     g: hoge # commentG | 
					
						
							|  |  |  |   h: # commentH | 
					
						
							|  |  |  |    - list1 # comment list1 | 
					
						
							|  |  |  |    - list2 # comment list2 | 
					
						
							|  |  |  |    - list3 # comment list3 | 
					
						
							|  |  |  |   i: fuga # commentI | 
					
						
							|  |  |  | j: piyo # commentJ | 
					
						
							| 
									
										
										
										
											2021-09-07 19:43:04 +09:00
										 |  |  | k.l.m.n: moge # commentKLMN | 
					
						
							| 
									
										
										
										
											2023-09-14 21:33:31 -05:00
										 |  |  | o#p: hogera # commentOP | 
					
						
							|  |  |  | q#.r: hogehoge # commentQR | 
					
						
							| 
									
										
										
										
											2021-09-07 17:31:17 +09:00
										 |  |  | ` | 
					
						
							|  |  |  | 	f, err := parser.ParseBytes([]byte(yml), parser.ParseComments) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Fatalf("%+v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	var capturer pathCapturer | 
					
						
							|  |  |  | 	for _, doc := range f.Docs { | 
					
						
							|  |  |  | 		ast.Walk(&capturer, doc.Body) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	commentPaths := []string{} | 
					
						
							|  |  |  | 	for i := 0; i < capturer.capturedNum; i++ { | 
					
						
							|  |  |  | 		if capturer.orderedTypes[i] == ast.CommentType { | 
					
						
							|  |  |  | 			commentPaths = append(commentPaths, capturer.orderedPaths[i]) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	expectedPaths := []string{ | 
					
						
							|  |  |  | 		"$.a", | 
					
						
							|  |  |  | 		"$.a.b", | 
					
						
							|  |  |  | 		"$.a.b.c", | 
					
						
							|  |  |  | 		"$.a.b.d", | 
					
						
							|  |  |  | 		"$.a.b.e", | 
					
						
							|  |  |  | 		"$.a.f", | 
					
						
							|  |  |  | 		"$.a.f.g", | 
					
						
							|  |  |  | 		"$.a.h", | 
					
						
							|  |  |  | 		"$.a.h[0]", | 
					
						
							|  |  |  | 		"$.a.h[1]", | 
					
						
							|  |  |  | 		"$.a.h[2]", | 
					
						
							|  |  |  | 		"$.a.i", | 
					
						
							|  |  |  | 		"$.j", | 
					
						
							| 
									
										
										
										
											2021-09-07 19:43:04 +09:00
										 |  |  | 		"$.'k.l.m.n'", | 
					
						
							| 
									
										
										
										
											2023-09-14 21:33:31 -05:00
										 |  |  | 		"$.o#p", | 
					
						
							|  |  |  | 		"$.'q#.r'", | 
					
						
							| 
									
										
										
										
											2021-09-07 17:31:17 +09:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if !reflect.DeepEqual(expectedPaths, commentPaths) { | 
					
						
							|  |  |  | 		t.Fatalf("failed to get YAMLPath to the comment node:\nexpected[%s]\ngot     [%s]", expectedPaths, commentPaths) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type pathCapturer struct { | 
					
						
							|  |  |  | 	capturedNum   int | 
					
						
							|  |  |  | 	orderedPaths  []string | 
					
						
							|  |  |  | 	orderedTypes  []ast.NodeType | 
					
						
							|  |  |  | 	orderedTokens []*token.Token | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (c *pathCapturer) Visit(node ast.Node) ast.Visitor { | 
					
						
							|  |  |  | 	c.capturedNum++ | 
					
						
							|  |  |  | 	c.orderedPaths = append(c.orderedPaths, node.GetPath()) | 
					
						
							|  |  |  | 	c.orderedTypes = append(c.orderedTypes, node.Type()) | 
					
						
							|  |  |  | 	c.orderedTokens = append(c.orderedTokens, node.GetToken()) | 
					
						
							|  |  |  | 	return c | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-02 05:24:28 +00:00
										 |  |  | type Visitor struct{} | 
					
						
							| 
									
										
										
										
											2019-10-16 18:21:20 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | func (v *Visitor) Visit(node ast.Node) ast.Visitor { | 
					
						
							| 
									
										
										
										
											2019-10-31 13:06:35 +09:00
										 |  |  | 	tk := node.GetToken() | 
					
						
							|  |  |  | 	tk.Prev = nil | 
					
						
							|  |  |  | 	tk.Next = nil | 
					
						
							| 
									
										
										
										
											2019-10-16 18:21:20 +09:00
										 |  |  | 	return v | 
					
						
							|  |  |  | } |