Skip to content

Commit acf5aa8

Browse files
committed
add error test
1 parent 68b9027 commit acf5aa8

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

jsonschema/infer_test.go

Lines changed: 34 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

2627
type S struct {
@@ -127,6 +128,38 @@ func TestForType(t *testing.T) {
127128
}
128129
}
129130

131+
func forErr[T any]() error {
132+
_, err := jsonschema.For[T]()
133+
return err
134+
}
135+
136+
func TestForErrors(t *testing.T) {
137+
type (
138+
s1 struct {
139+
Empty int `jsonschema:""`
140+
}
141+
s2 struct {
142+
Bad int `jsonschema:"$foo=1,bar"`
143+
}
144+
)
145+
146+
for _, tt := range []struct {
147+
got error
148+
want string
149+
}{
150+
{forErr[map[int]int](), "unsupported map key type"},
151+
{forErr[s1](), "empty jsonschema tag"},
152+
{forErr[s2](), "must not begin with"},
153+
{forErr[func()](), "unsupported"},
154+
} {
155+
if tt.got == nil {
156+
t.Errorf("got nil, want error containing %q", tt.want)
157+
} else if !strings.Contains(tt.got.Error(), tt.want) {
158+
t.Errorf("got %q\nwant it to contain %q", tt.got, tt.want)
159+
}
160+
}
161+
}
162+
130163
func falseSchema() *jsonschema.Schema {
131164
return &jsonschema.Schema{Not: &jsonschema.Schema{}}
132165
}

0 commit comments

Comments
 (0)