-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlicense_struct_test.go
69 lines (58 loc) · 1.58 KB
/
license_struct_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package openapi_test
import (
"testing"
"github.com/MarkRosemaker/openapi"
)
func TestLicense_JSON(t *testing.T) {
t.Parallel()
testJSON(t, []byte(`{
"name": "Apache 2.0",
"identifier": "Apache-2.0"
}`), &openapi.License{})
testJSON(t, []byte(`{
"name": "Apache 2.0",
"identifier": "Apache-2.0",
"x-foo": true,
"x-bar": ["one", "two"]
}`), &openapi.License{})
}
func TestLicense_Validate(t *testing.T) {
t.Parallel()
t.Run("empty", func(t *testing.T) {
l := openapi.License{}
if err := l.Validate(); err == nil {
t.Fatal("expected error")
} else if want := `name is required`; err.Error() != want {
t.Fatalf("got: %v, want: %v", err, want)
}
})
t.Run("valid", func(t *testing.T) {
l := openapi.License{Name: "Apache 2.0"}
if err := l.Validate(); err != nil {
t.Fatal(err)
}
})
t.Run("both URL and identifier", func(t *testing.T) {
l := openapi.License{
Name: "Apache 2.0",
Identifier: "Apache-2.0",
URL: mustParseURL("https://www.apache.org/licenses/LICENSE-2.0"),
}
if err := l.Validate(); err == nil {
t.Fatal("expected error")
} else if want := `url and identifier are mutually exclusive`; err.Error() != want {
t.Fatalf("got: %v, want: %v", err, want)
}
})
t.Run("invalid extension", func(t *testing.T) {
l := openapi.License{
Name: "Apache 2.0",
Extensions: openapi.Extensions(`{"foo":"bar"}`),
}
if err := l.Validate(); err == nil {
t.Fatal("expected error")
} else if want := `foo: ` + openapi.ErrUnknownField.Error(); err.Error() != want {
t.Fatalf("got: %v, want: %v", err, want)
}
})
}