Skip to content

Commit 2b794ed

Browse files
committed
encoding/json: expand and modernize TestInterfaceSet
Add more test cases to cover a wider range of edge cases. Use a generic addr function to take the address of a value. Even though redudant, explicitly include a cast to the top-level Go type so that it is more readable what the expected input and ouput types are. Change-Id: I3ef68df6f1beb903ae237cd49f3dcb91e5270fe7 Reviewed-on: https://go-review.googlesource.com/c/go/+/638256 Reviewed-by: Daniel Martí <[email protected]> Reviewed-by: Damien Neil <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent e3cd55e commit 2b794ed

File tree

1 file changed

+46
-19
lines changed

1 file changed

+46
-19
lines changed

src/encoding/json/decode_test.go

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,19 +1797,12 @@ func TestNullString(t *testing.T) {
17971797
}
17981798
}
17991799

1800-
func intp(x int) *int {
1801-
p := new(int)
1802-
*p = x
1803-
return p
1804-
}
1805-
1806-
func intpp(x *int) **int {
1807-
pp := new(*int)
1808-
*pp = x
1809-
return pp
1800+
func addr[T any](v T) *T {
1801+
return &v
18101802
}
18111803

18121804
func TestInterfaceSet(t *testing.T) {
1805+
errUnmarshal := &UnmarshalTypeError{Value: "object", Offset: 6, Type: reflect.TypeFor[int](), Field: "X"}
18131806
tests := []struct {
18141807
CaseName
18151808
pre any
@@ -1820,21 +1813,55 @@ func TestInterfaceSet(t *testing.T) {
18201813
{Name(""), "foo", `2`, 2.0},
18211814
{Name(""), "foo", `true`, true},
18221815
{Name(""), "foo", `null`, nil},
1823-
1824-
{Name(""), nil, `null`, nil},
1825-
{Name(""), new(int), `null`, nil},
1826-
{Name(""), (*int)(nil), `null`, nil},
1827-
{Name(""), new(*int), `null`, new(*int)},
1828-
{Name(""), (**int)(nil), `null`, nil},
1829-
{Name(""), intp(1), `null`, nil},
1830-
{Name(""), intpp(nil), `null`, intpp(nil)},
1831-
{Name(""), intpp(intp(1)), `null`, intpp(nil)},
1816+
{Name(""), map[string]any{}, `true`, true},
1817+
{Name(""), []string{}, `true`, true},
1818+
1819+
{Name(""), any(nil), `null`, any(nil)},
1820+
{Name(""), (*int)(nil), `null`, any(nil)},
1821+
{Name(""), (*int)(addr(0)), `null`, any(nil)},
1822+
{Name(""), (*int)(addr(1)), `null`, any(nil)},
1823+
{Name(""), (**int)(nil), `null`, any(nil)},
1824+
{Name(""), (**int)(addr[*int](nil)), `null`, (**int)(addr[*int](nil))},
1825+
{Name(""), (**int)(addr(addr(1))), `null`, (**int)(addr[*int](nil))},
1826+
{Name(""), (***int)(nil), `null`, any(nil)},
1827+
{Name(""), (***int)(addr[**int](nil)), `null`, (***int)(addr[**int](nil))},
1828+
{Name(""), (***int)(addr(addr[*int](nil))), `null`, (***int)(addr[**int](nil))},
1829+
{Name(""), (***int)(addr(addr(addr(1)))), `null`, (***int)(addr[**int](nil))},
1830+
1831+
{Name(""), any(nil), `2`, float64(2)},
1832+
{Name(""), (int)(1), `2`, float64(2)},
1833+
{Name(""), (*int)(nil), `2`, float64(2)},
1834+
{Name(""), (*int)(addr(0)), `2`, (*int)(addr(2))},
1835+
{Name(""), (*int)(addr(1)), `2`, (*int)(addr(2))},
1836+
{Name(""), (**int)(nil), `2`, float64(2)},
1837+
{Name(""), (**int)(addr[*int](nil)), `2`, (**int)(addr(addr(2)))},
1838+
{Name(""), (**int)(addr(addr(1))), `2`, (**int)(addr(addr(2)))},
1839+
{Name(""), (***int)(nil), `2`, float64(2)},
1840+
{Name(""), (***int)(addr[**int](nil)), `2`, (***int)(addr(addr(addr(2))))},
1841+
{Name(""), (***int)(addr(addr[*int](nil))), `2`, (***int)(addr(addr(addr(2))))},
1842+
{Name(""), (***int)(addr(addr(addr(1)))), `2`, (***int)(addr(addr(addr(2))))},
1843+
1844+
{Name(""), any(nil), `{}`, map[string]any{}},
1845+
{Name(""), (int)(1), `{}`, map[string]any{}},
1846+
{Name(""), (*int)(nil), `{}`, map[string]any{}},
1847+
{Name(""), (*int)(addr(0)), `{}`, errUnmarshal},
1848+
{Name(""), (*int)(addr(1)), `{}`, errUnmarshal},
1849+
{Name(""), (**int)(nil), `{}`, map[string]any{}},
1850+
{Name(""), (**int)(addr[*int](nil)), `{}`, errUnmarshal},
1851+
{Name(""), (**int)(addr(addr(1))), `{}`, errUnmarshal},
1852+
{Name(""), (***int)(nil), `{}`, map[string]any{}},
1853+
{Name(""), (***int)(addr[**int](nil)), `{}`, errUnmarshal},
1854+
{Name(""), (***int)(addr(addr[*int](nil))), `{}`, errUnmarshal},
1855+
{Name(""), (***int)(addr(addr(addr(1)))), `{}`, errUnmarshal},
18321856
}
18331857
for _, tt := range tests {
18341858
t.Run(tt.Name, func(t *testing.T) {
18351859
b := struct{ X any }{tt.pre}
18361860
blob := `{"X":` + tt.json + `}`
18371861
if err := Unmarshal([]byte(blob), &b); err != nil {
1862+
if wantErr, _ := tt.post.(error); equalError(err, wantErr) {
1863+
return
1864+
}
18381865
t.Fatalf("%s: Unmarshal(%#q) error: %v", tt.Where, blob, err)
18391866
}
18401867
if !reflect.DeepEqual(b.X, tt.post) {

0 commit comments

Comments
 (0)