forked from hyperworks/go-getstream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slug_test.go
67 lines (54 loc) · 1.47 KB
/
slug_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
package getstream
import (
"encoding/json"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
var _ json.Marshaler = Slug{}
var _ json.Unmarshaler = &Slug{}
var _ fmt.Stringer = Slug{}
func TestSlug(t *testing.T) {
assert.Equal(t, "test:456", Slug{"test", "456", ""}.String())
assert.Equal(t, "test:123 token", Slug{"test", "123", "token"}.String())
}
func TestSlug_Valid(t *testing.T) {
valids := []Slug{
Slug{"feed", "123", ""},
Slug{"feed", "456", "token"},
}
for _, slug := range valids {
assert.True(t, slug.Valid())
}
invalids := []Slug{
Slug{"", "", ""},
Slug{"feed", "", ""},
Slug{"", "123", ""},
Slug{"", "", "token"},
}
for _, slug := range invalids {
assert.False(t, slug.Valid())
}
}
func TestSlug_JSON(t *testing.T) {
marshals := map[Slug]string{
Slug{"slug", "123", ""}: `"slug:123"`,
Slug{"slug", "123", "signature"}: `"slug:123 signature"`,
}
for slug, str := range marshals {
bytes, e := json.Marshal(slug)
assert.NoError(t, e, "failed to marshal slug: "+slug.String())
assert.Equal(t, str, string(bytes))
}
unmarshals := map[string]Slug{
`"slug:123"`: Slug{"slug", "123", ""},
`"slug:123 signature"`: Slug{"slug", "123", "signature"},
`["slug:123", "signature"]`: Slug{"slug", "123", "signature"},
}
for str, slug := range unmarshals {
result := Slug{}
e := json.Unmarshal([]byte(str), &result)
assert.NoError(t, e, "failed to unmarshal json: "+str)
assert.Equal(t, slug, result)
}
}