-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembedded_test.go
76 lines (63 loc) · 1.63 KB
/
embedded_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
package siren
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
type Prop1 struct {
X int
}
func TestEmbedJSON(t *testing.T) {
assert := assert.New(t)
siren := NewSiren().SetTitle("Welcome")
str, err := json.Marshal(siren)
assert.Nil(err)
assert.Equal(`{"title":"Welcome"}`, string(str))
child := NewSirenP(Prop1{X: 5}).SetTitle("I'm a child")
siren.AddEntity(child, "home")
siren.Entities[0].AddClass("foo")
str, err = json.Marshal(siren)
assert.Nil(err)
assert.Equal(`{"title":"Welcome","entities":[{"rel":["home"],"href":"#/child","title":"I'm a child","class":["foo"],"properties":{"X":5}}]}`, string(str))
siren2, err := Parse(string(str))
assert.Nil(err)
str2, err := json.Marshal(siren2)
assert.Nil(err)
assert.Equal(str, str2)
}
func TestContruction(t *testing.T) {
assert := assert.New(t)
siren := NewSiren().
SetTitle("Welcome").
AddEntity(NewSiren().
SetTitle("Child").
AddEntity(NewSiren().
SetTitle("Grandchild"), "item"),
"item")
str, err := json.Marshal(siren)
assert.Nil(err)
assert.Equal(`{"title":"Welcome","entities":[{"rel":["item"],"href":"#/child","title":"Child","entities":[{"rel":["item"],"href":"#/grandchild","title":"Grandchild"}]}]}`, string(str))
str, err = json.MarshalIndent(siren, "", " ")
assert.Nil(err)
assert.Equal(`{
"title": "Welcome",
"entities": [
{
"rel": [
"item"
],
"href": "#/child",
"title": "Child",
"entities": [
{
"rel": [
"item"
],
"href": "#/grandchild",
"title": "Grandchild"
}
]
}
]
}`, string(str))
}