Skip to content

Commit 5ebb338

Browse files
committed
add error test
1 parent a1a3510 commit 5ebb338

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

jsonschema/infer_test.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package jsonschema_test
66

77
import (
8+
"strings"
89
"testing"
910

1011
"github.com/google/go-cmp/cmp"
@@ -20,7 +21,7 @@ func forType[T any]() *jsonschema.Schema {
2021
return s
2122
}
2223

23-
func TestForType(t *testing.T) {
24+
func TestFor(t *testing.T) {
2425
type schema = jsonschema.Schema
2526
tests := []struct {
2627
name string
@@ -92,6 +93,38 @@ func TestForType(t *testing.T) {
9293
}
9394
}
9495

96+
func forErr[T any]() error {
97+
_, err := jsonschema.For[T]()
98+
return err
99+
}
100+
101+
func TestForErrors(t *testing.T) {
102+
type (
103+
s1 struct {
104+
Empty int `jsonschema:""`
105+
}
106+
s2 struct {
107+
Bad int `jsonschema:"$foo=1,bar"`
108+
}
109+
)
110+
111+
for _, tt := range []struct {
112+
got error
113+
want string
114+
}{
115+
{forErr[map[int]int](), "unsupported map key type"},
116+
{forErr[s1](), "empty jsonschema tag"},
117+
{forErr[s2](), "must not begin with"},
118+
{forErr[func()](), "unsupported"},
119+
} {
120+
if tt.got == nil {
121+
t.Errorf("got nil, want error containing %q", tt.want)
122+
} else if !strings.Contains(tt.got.Error(), tt.want) {
123+
t.Errorf("got %q\nwant it to contain %q", tt.got, tt.want)
124+
}
125+
}
126+
}
127+
95128
func TestForWithMutation(t *testing.T) {
96129
// This test ensures that the cached schema is not mutated when the caller
97130
// mutates the returned schema.
@@ -184,3 +217,7 @@ func TestForWithCycle(t *testing.T) {
184217
})
185218
}
186219
}
220+
221+
func falseSchema() *jsonschema.Schema {
222+
return &jsonschema.Schema{Not: &jsonschema.Schema{}}
223+
}

0 commit comments

Comments
 (0)