Skip to content

Commit

Permalink
add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy committed Dec 1, 2022
1 parent 7b77440 commit 045101d
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
23 changes: 23 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2144,6 +2144,29 @@ b: *a
t.Fatal("failed to unmarshal")
}
})
t.Run("quoted map keys", func(t *testing.T) {
t.Parallel()
yml := `
a:
"b": 2
'c': true
`
var v struct {
A struct {
B int
C bool
}
}
if err := yaml.Unmarshal([]byte(yml), &v); err != nil {
t.Fatalf("failed to unmarshal %v", err)
}
if v.A.B != 2 {
t.Fatalf("expected a.b to equal 2 but was %d", v.A.B)
}
if !v.A.C {
t.Fatal("expected a.c to be true but was false")
}
})
}

type unmarshalablePtrStringContainer struct {
Expand Down
111 changes: 111 additions & 0 deletions lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func TestTokenize(t *testing.T) {
"a: 'Hello #comment'\n",
"a: 100.5\n",
"a: bogus\n",
"\"a\": double quoted map key",
"'a': single quoted map key",
"a: \"double quoted\"\nb: \"value map\"",
"a: 'single quoted'\nb: 'value map'",
}
for _, src := range sources {
lexer.Tokenize(src).Dump()
Expand Down Expand Up @@ -231,6 +235,60 @@ func TestSingleLineToken_ValueLineColumnPosition(t *testing.T) {
15: "]",
},
},
{
name: "double quote key",
src: `"a": b`,
expect: map[int]string{
1: "a",
4: ":",
6: "b",
},
},
{
name: "single quote key",
src: `'a': b`,
expect: map[int]string{
1: "a",
4: ":",
6: "b",
},
},
{
name: "double quote key and value",
src: `"a": "b"`,
expect: map[int]string{
1: "a",
4: ":",
6: "b",
},
},
{
name: "single quote key and value",
src: `'a': 'b'`,
expect: map[int]string{
1: "a",
4: ":",
6: "b",
},
},
{
name: "double quote key, single quote value",
src: `"a": 'b'`,
expect: map[int]string{
1: "a",
4: ":",
6: "b",
},
},
{
name: "single quote key, double quote value",
src: `'a': "b"`,
expect: map[int]string{
1: "a",
4: ":",
6: "b",
},
},
}

for _, tc := range tests {
Expand Down Expand Up @@ -432,6 +490,59 @@ foo2: 'bar2'`,
},
},
},
{
name: "single and double quote map keys",
src: `"a": test
'b': 1
c: true`,
expect: []testToken{
{
line: 1,
column: 1,
value: "a",
},
{
line: 1,
column: 4,
value: ":",
},
{
line: 1,
column: 6,
value: "test",
},
{
line: 2,
column: 1,
value: "b",
},
{
line: 2,
column: 4,
value: ":",
},
{
line: 2,
column: 6,
value: "1",
},
{
line: 3,
column: 1,
value: "c",
},
{
line: 3,
column: 2,
value: ":",
},
{
line: 3,
column: 4,
value: "true",
},
},
},
}

for _, tc := range tests {
Expand Down
18 changes: 18 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ func TestParser(t *testing.T) {
? !!str "implicit" : !!str "entry",
? !!null "" : !!null "",
}`,
"\"a\": a\n\"b\": b",
"'a': a\n'b': b",
}
for _, src := range sources {
if _, err := parser.Parse(lexer.Tokenize(src), 0); err != nil {
Expand Down Expand Up @@ -562,6 +564,22 @@ b: c
`
- key1: val
key2: ( foo + bar )
`,
},
{
`
"a": b
'c': d
"e": "f"
g: "h"
i: 'j'
`,
`
"a": b
'c': d
"e": "f"
g: "h"
i: 'j'
`,
},
}
Expand Down

0 comments on commit 045101d

Please sign in to comment.