-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec_test.go
123 lines (115 loc) · 3.43 KB
/
spec_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package ocfl_test
import (
"context"
"fmt"
"testing"
"github.com/carlmjohnson/be"
"github.com/srerickson/ocfl-go"
"github.com/srerickson/ocfl-go/backend/local"
)
func TestSpecCmp(t *testing.T) {
type testCase struct {
v1 ocfl.Spec
v2 ocfl.Spec
wantResult int
wantPanic bool
}
testCases := []testCase{
{v1: ocfl.Spec1_1, v2: ocfl.Spec1_0, wantResult: 1},
{v1: ocfl.Spec1_0, v2: ocfl.Spec1_1, wantResult: -1},
{v1: ocfl.Spec1_1, v2: ocfl.Spec1_1, wantResult: 0},
{v1: ocfl.Spec("2.0"), v2: ocfl.Spec1_1, wantResult: 1},
{v1: ocfl.Spec("1.1"), v2: ocfl.Spec("1.1-draft"), wantResult: 1},
{v1: ocfl.Spec("2.2-draft"), v2: ocfl.Spec("2.2"), wantResult: -1},
{v1: ocfl.Spec("1.1-draft"), v2: ocfl.Spec("1.1-new"), wantResult: 0},
{v1: ocfl.Spec(""), v2: ocfl.Spec1_0, wantResult: -1},
{v1: ocfl.Spec1_0, v2: ocfl.Spec("1"), wantResult: 1},
{v1: ocfl.Spec("invalid"), v2: ocfl.Spec("bad"), wantPanic: true},
}
for i, tcase := range testCases {
t.Run(fmt.Sprintf("case%d--%v vs %v", i, tcase.v1, tcase.v2), func(t *testing.T) {
defer func() {
be.Equal(t, tcase.wantPanic, recover() != nil)
}()
be.Equal(t, tcase.wantResult, tcase.v1.Cmp(tcase.v2))
})
}
}
func TestSpecValid(t *testing.T) {
type testCase struct {
val ocfl.Spec
isValid bool
}
testCases := []testCase{
// valid
{val: ocfl.Spec1_1, isValid: true},
{val: ocfl.Spec1_0, isValid: true},
{val: ocfl.Spec("2.3"), isValid: true},
{val: ocfl.Spec("1.55-test"), isValid: true},
// invalid
{val: ocfl.Spec(""), isValid: false},
{val: ocfl.Spec("1"), isValid: false},
{val: ocfl.Spec("1.-fun"), isValid: false},
{val: ocfl.Spec("a.12"), isValid: false},
{val: ocfl.Spec("."), isValid: false},
{val: ocfl.Spec("1.b"), isValid: false},
{val: ocfl.Spec("1-"), isValid: false},
}
for i, tcase := range testCases {
t.Run(fmt.Sprintf("case%d--%v", i, tcase.val), func(t *testing.T) {
be.Equal(t, tcase.isValid, tcase.val.Valid() == nil)
})
}
}
func TestSpecEmpty(t *testing.T) {
be.True(t, ocfl.Spec("").Empty())
be.False(t, ocfl.Spec1_0.Empty())
}
func TestParseInventoryType(t *testing.T) {
type testCase struct {
in string
out ocfl.Spec
valid bool
}
testCases := []testCase{
// valid
{"https://ocfl.io/1.0/spec/#inventory", ocfl.Spec1_0, true},
{"https://ocfl.io/1.1/spec/#inventory", ocfl.Spec1_1, true},
{"https://ocfl.io/2.0-draft/spec/#inventory", ocfl.Spec("2.0-draft"), true},
// invalid
{"https://ocfl.io/./spec/#inventory", ocfl.Spec(""), false},
{"https://ocfl.io/spec/#inventory", ocfl.Spec(""), false},
}
for i, tcase := range testCases {
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
inv := ocfl.InventoryType{}
err := inv.UnmarshalText([]byte(tcase.in))
if tcase.valid {
be.NilErr(t, err)
} else {
be.True(t, err != nil)
}
be.Equal(t, inv.Spec, tcase.out)
})
}
}
func TestWriteSpecFile(t *testing.T) {
ctx := context.Background()
fsys, err := local.NewFS(t.TempDir())
be.NilErr(t, err)
test := func(t *testing.T, spec ocfl.Spec) {
name, err := ocfl.WriteSpecFile(ctx, fsys, "dir1", spec)
be.NilErr(t, err)
f, err := fsys.OpenFile(ctx, name)
be.NilErr(t, err)
defer be.NilErr(t, f.Close())
// again
_, err = ocfl.WriteSpecFile(ctx, fsys, "dir1", spec)
be.True(t, err != nil)
}
test(t, ocfl.Spec1_0)
test(t, ocfl.Spec1_1)
// expect an error
_, err = ocfl.WriteSpecFile(ctx, fsys, "dir1", ocfl.Spec("3.0"))
be.True(t, err != nil)
}