-
Notifications
You must be signed in to change notification settings - Fork 14
/
pattern_test.go
78 lines (68 loc) · 1.98 KB
/
pattern_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
package single
import (
"fmt"
"testing"
"gotest.tools/v3/assert"
)
func TestBookResourceName(t *testing.T) {
t.Run("good", func(t *testing.T) {
const (
shelf = "shelf"
book = "book"
)
pattern := fmt.Sprintf("shelves/%s/books/%s", shelf, book)
var name BookResourceName
err := name.UnmarshalString(pattern)
assert.NilError(t, err)
assert.Equal(t, name.Shelf, shelf)
assert.Equal(t, name.Book, book)
marshalled, err := name.MarshalString()
assert.NilError(t, err)
assert.Equal(t, marshalled, pattern)
})
t.Run("bad top-level", func(t *testing.T) {
var name BookResourceName
err := name.UnmarshalString("books/book")
assert.Error(
t,
err,
"parse resource name 'books/book' with pattern 'shelves/{shelf}/books/{book}': segment shelves: got books",
)
})
t.Run("bad wrong parent", func(t *testing.T) {
var name BookResourceName
err := name.UnmarshalString("others/other/books/book")
assert.Error(
t,
err,
"parse resource name 'others/other/books/book' with pattern 'shelves/{shelf}/books/{book}': segment shelves: got others",
)
})
}
func TestShelfResourceName(t *testing.T) {
t.Run("good", func(t *testing.T) {
const shelf = "shelf"
pattern := fmt.Sprintf("shelves/%s", shelf)
var name ShelfResourceName
err := name.UnmarshalString(pattern)
assert.NilError(t, err)
assert.Equal(t, name.Shelf, shelf)
marshalled, err := name.MarshalString()
assert.NilError(t, err)
assert.Equal(t, marshalled, pattern)
})
t.Run("invalid", func(t *testing.T) {
var name ShelfResourceName
err := name.UnmarshalString("others/other")
assert.Error(t, err, "parse resource name 'others/other' with pattern 'shelves/{shelf}': segment shelves: got others")
})
t.Run("bad wrong parent", func(t *testing.T) {
var name ShelfResourceName
err := name.UnmarshalString("others/other/shelves/shelf")
assert.Error(
t,
err,
"parse resource name 'others/other/shelves/shelf' with pattern 'shelves/{shelf}': segment shelves: got others",
)
})
}