Skip to content

Commit ca42a3a

Browse files
committed
Move unmarshalJSON unit tests
Move the Test_unmarshalJSON tests and supporting TestUnmarshaler type into a dedicated decode_internal_test.go file and remove the duplicate test code from yaml_test.go. This isolates JSON-unmarshaling-specific tests for unmarshalJSON into a focused test file, improving test organization and making the intent of these tests clearer. The tests cover maps with string, int, and any keys, including a duplicate-key error case, and assert correct conversion to the TestUnmarshaler struct.
1 parent 44937f2 commit ca42a3a

2 files changed

Lines changed: 95 additions & 87 deletions

File tree

decode_internal_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package yaml
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
7+
"go.yaml.in/yaml/v4/internal/testutil/assert"
8+
)
9+
10+
type TestUnmarshaler struct {
11+
Value string
12+
Array []int
13+
Map map[string]int
14+
}
15+
16+
func (t *TestUnmarshaler) UnmarshalJSON(data []byte) error {
17+
type Alias TestUnmarshaler
18+
var aux Alias
19+
if err := json.Unmarshal(data, &aux); err != nil {
20+
return err
21+
}
22+
*t = TestUnmarshaler(aux)
23+
return nil
24+
}
25+
26+
func Test_unmarshalJSON(t *testing.T) {
27+
tests := []struct {
28+
name string
29+
input any
30+
expected any
31+
wantErr string
32+
}{
33+
{
34+
name: "map with string keys",
35+
input: map[string]any{
36+
"Value": "hello",
37+
"Array": []int{1, 2, 3},
38+
"Map": map[string]int{"a": 1, "b": 2},
39+
},
40+
expected: &TestUnmarshaler{
41+
Value: "hello",
42+
Array: []int{1, 2, 3},
43+
Map: map[string]int{"a": 1, "b": 2},
44+
},
45+
},
46+
{
47+
name: "map with int keys",
48+
input: map[string]any{
49+
"Value": "hello",
50+
"Array": []int{1, 2, 3},
51+
"Map": map[int]int{1: 1, 2: 2},
52+
},
53+
expected: &TestUnmarshaler{
54+
Value: "hello",
55+
Array: []int{1, 2, 3},
56+
Map: map[string]int{"1": 1, "2": 2},
57+
},
58+
},
59+
{
60+
name: "map with any keys",
61+
input: map[string]any{
62+
"Value": "hello",
63+
"Array": []int{1, 2, 3},
64+
"Map": map[any]int{1: 1, "b": 2, true: 3},
65+
},
66+
expected: &TestUnmarshaler{
67+
Value: "hello",
68+
Array: []int{1, 2, 3},
69+
Map: map[string]int{"1": 1, "b": 2, "true": 3},
70+
},
71+
},
72+
{
73+
name: "map with duplicate keys",
74+
input: map[string]any{
75+
"Value": "hello",
76+
"Array": []int{1, 2, 3},
77+
"Map": map[any]int{1: 1, "1": 2},
78+
},
79+
wantErr: `duplicate key "1" found when converting to JSON object`,
80+
},
81+
}
82+
for _, tt := range tests {
83+
tt := tt
84+
t.Run(tt.name, func(t *testing.T) {
85+
target := &TestUnmarshaler{}
86+
err := unmarshalJSON(tt.input, target)
87+
if tt.wantErr != "" {
88+
assert.ErrorMatchesf(t, tt.wantErr, err, "unmarshalJSON() error")
89+
return
90+
}
91+
assert.NoError(t, err)
92+
assert.DeepEqualf(t, tt.expected, target, "unmarshalJSON() result")
93+
})
94+
}
95+
}

yaml_test.go

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -455,90 +455,3 @@ func ExampleEncoder_FallbackToJSON() {
455455
//
456456
// true
457457
}
458-
459-
type TestUnmarshaler struct {
460-
Value string
461-
Array []int
462-
Map map[string]int
463-
}
464-
465-
func (t *TestUnmarshaler) UnmarshalJSON(data []byte) error {
466-
type Alias TestUnmarshaler
467-
var aux Alias
468-
if err := json.Unmarshal(data, &aux); err != nil {
469-
return err
470-
}
471-
*t = TestUnmarshaler(aux)
472-
return nil
473-
}
474-
475-
func Test_unmarshalJSON(t *testing.T) {
476-
tests := []struct {
477-
name string
478-
input any
479-
expected any
480-
wantErr string
481-
}{
482-
{
483-
name: "map with string keys",
484-
input: map[string]any{
485-
"Value": "hello",
486-
"Array": []int{1, 2, 3},
487-
"Map": map[string]int{"a": 1, "b": 2},
488-
},
489-
expected: &TestUnmarshaler{
490-
Value: "hello",
491-
Array: []int{1, 2, 3},
492-
Map: map[string]int{"a": 1, "b": 2},
493-
},
494-
},
495-
{
496-
name: "map with int keys",
497-
input: map[string]any{
498-
"Value": "hello",
499-
"Array": []int{1, 2, 3},
500-
"Map": map[int]int{1: 1, 2: 2},
501-
},
502-
expected: &TestUnmarshaler{
503-
Value: "hello",
504-
Array: []int{1, 2, 3},
505-
Map: map[string]int{"1": 1, "2": 2},
506-
},
507-
},
508-
{
509-
name: "map with any keys",
510-
input: map[string]any{
511-
"Value": "hello",
512-
"Array": []int{1, 2, 3},
513-
"Map": map[any]int{1: 1, "b": 2, true: 3},
514-
},
515-
expected: &TestUnmarshaler{
516-
Value: "hello",
517-
Array: []int{1, 2, 3},
518-
Map: map[string]int{"1": 1, "b": 2, "true": 3},
519-
},
520-
},
521-
{
522-
name: "map with duplicate keys",
523-
input: map[string]any{
524-
"Value": "hello",
525-
"Array": []int{1, 2, 3},
526-
"Map": map[any]int{1: 1, "1": 2},
527-
},
528-
wantErr: `duplicate key "1" found when converting to JSON object`,
529-
},
530-
}
531-
for _, tt := range tests {
532-
tt := tt
533-
t.Run(tt.name, func(t *testing.T) {
534-
target := &TestUnmarshaler{}
535-
err := unmarshalJSON(tt.input, target)
536-
if tt.wantErr != "" {
537-
assert.ErrorMatchesf(t, tt.wantErr, err, "unmarshalJSON() error")
538-
return
539-
}
540-
assert.NoError(t, err)
541-
assert.DeepEqualf(t, tt.expected, target, "unmarshalJSON() result")
542-
})
543-
}
544-
}

0 commit comments

Comments
 (0)