all repos

json2go @ v0.2.2

convert json to go type annotations

json2go/json2go_test.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
remove dead code, 19 days ago
1
package json2go
2
3
import (
4
	"errors"
5
	"strings"
6
	"testing"
7
)
8
9
func TestTransform(t *testing.T) {
10
	tests := map[string]struct {
11
		input      string
12
		check      func(t *testing.T, result string)
13
		structName string
14
		err        error
15
	}{
16
		"simple object": {
17
			input: `{"name": "Olex", "active": true, "age": 420}`,
18
			check: func(t *testing.T, result string) {
19
				if !strings.Contains(result, "type Out struct") {
20
					t.Errorf("missing Out struct")
21
				}
22
				if !strings.Contains(result, "Name string `json:\"name\"`") {
23
					t.Errorf("missing Name field")
24
				}
25
				if !strings.Contains(result, "Active bool `json:\"active\"`") {
26
					t.Errorf("missing Active field")
27
				}
28
				if !strings.Contains(result, "Age int `json:\"age\"`") {
29
					t.Errorf("missing Age field")
30
				}
31
			},
32
		},
33
		"invalid json": {
34
			err:   ErrInvalidJSON,
35
			input: `{"invalid":json}`,
36
		},
37
		"invalid struct name, starts with number": {
38
			err:        ErrInvalidStructName,
39
			structName: "1Name",
40
		},
41
		"invalid struct name, has space": {
42
			err:        ErrInvalidStructName,
43
			structName: "Name Name2",
44
		},
45
		"invalid struct name, has non letter/number": {
46
			err:        ErrInvalidStructName,
47
			structName: "Name$",
48
		},
49
		"snake_case to CamelCase": {
50
			input: `{"first_name": "Bob", "last_name": "Bobberson"}`,
51
			check: func(t *testing.T, result string) {
52
				if !strings.Contains(result, "FirstName string `json:\"first_name\"`") {
53
					t.Errorf("missing FirstName field")
54
				}
55
				if !strings.Contains(result, "LastName string `json:\"last_name\"`") {
56
					t.Errorf("missing LastName field")
57
				}
58
			},
59
		},
60
		"nested object and array": {
61
			input: `{"user": {"name": "Alice", "score": 95.5}, "tags": ["go", "json"]}`,
62
			check: func(t *testing.T, result string) {
63
				if !strings.Contains(result, "type Out struct") {
64
					t.Errorf("missing Out struct")
65
				}
66
				if !strings.Contains(result, "Tags []string") {
67
					t.Errorf("missing Tags field")
68
				}
69
				if !strings.Contains(result, "User struct {") {
70
					t.Errorf("missing inline User struct")
71
				}
72
				if !strings.Contains(result, "Name string `json:\"name\"`") {
73
					t.Errorf("missing Name field in inline struct")
74
				}
75
			},
76
		},
77
		"empty nested object": {
78
			input: `{"user": {}}`,
79
			check: func(t *testing.T, result string) {
80
				if !strings.Contains(result, "type Out struct") {
81
					t.Errorf("missing Out struct")
82
				}
83
				if !strings.Contains(result, "User struct {") {
84
					t.Errorf("missing inline User struct")
85
				}
86
			},
87
		},
88
		"array of object": {
89
			input: `[{"name": "John"}, {"name": "Jane"}]`,
90
			check: func(t *testing.T, result string) {
91
				if !strings.Contains(result, "type Out []struct {") {
92
					t.Errorf("missing Out array type with inline struct, got: %s", result)
93
				}
94
				if !strings.Contains(result, "Name string `json:\"name\"`") {
95
					t.Errorf("missing Name field")
96
				}
97
			},
98
		},
99
		"empty array": {
100
			input: `{"items": []}`,
101
			check: func(t *testing.T, result string) {
102
				if !strings.Contains(result, "Items []any `json:\"items\"`") {
103
					t.Errorf("missing Items field")
104
				}
105
			},
106
		},
107
		"null": {
108
			input: `{"item": null}`,
109
			check: func(t *testing.T, result string) {
110
				if !strings.Contains(result, "Item any `json:\"item\"`") {
111
					t.Errorf("missing Item field")
112
				}
113
			},
114
		},
115
		"numbers": {
116
			input: `{"pos": 123, "neg": -321, "float": 420.69}`,
117
			check: func(t *testing.T, result string) {
118
				if !strings.Contains(result, "Pos int `json:\"pos\"`") {
119
					t.Errorf("missing Pos field")
120
				}
121
				if !strings.Contains(result, "Neg int `json:\"neg\"`") {
122
					t.Errorf("missing Neg field")
123
				}
124
				if !strings.Contains(result, "Float float64 `json:\"float\"`") {
125
					t.Errorf("missing Float field")
126
				}
127
			},
128
		},
129
	}
130
131
	for tname, tt := range tests {
132
		t.Run(tname, func(t *testing.T) {
133
			sn := "Out"
134
			if tt.structName != "" {
135
				sn = tt.structName
136
			}
137
138
			result, err := Transform(sn, tt.input, true)
139
			assertEqualErr(t, tt.err, err)
140
			if tt.check != nil {
141
				tt.check(t, result)
142
			}
143
		})
144
	}
145
}
146
147
func assertEqualErr(t *testing.T, expected, actual error) {
148
	t.Helper()
149
	if expected == nil && actual == nil {
150
		return
151
	}
152
153
	if expected == nil || actual == nil {
154
		t.Errorf("expected: %v, got: %v", expected, actual)
155
		return
156
	}
157
158
	if !errors.Is(actual, expected) {
159
		t.Errorf("expected error: %v, got: %v", expected, actual)
160
	}
161
}