-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformulaSerial_test.go
95 lines (87 loc) · 3.07 KB
/
formulaSerial_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
package api
import (
"bytes"
"testing"
"github.com/polydawn/refmt"
"github.com/polydawn/refmt/json"
"github.com/polydawn/refmt/obj/atlas"
. "github.com/warpfork/go-wish"
)
func TestFormulaActionSerialization(t *testing.T) {
atl := atlas.MustBuild(
FormulaAction_AtlasEntry,
FormulaUserinfo_AtlasEntry,
)
t.Run("zero FormulaAction should roundtrip", func(t *testing.T) {
obj := FormulaAction{}
canon := `{}`
t.Run("marshal", func(t *testing.T) {
bs, err := refmt.MarshalAtlased(json.EncodeOptions{}, obj, atl)
Wish(t, err, ShouldEqual, nil)
Wish(t, string(bs), ShouldEqual, canon)
})
t.Run("unmarshal", func(t *testing.T) {
targ := FormulaAction{}
err := refmt.UnmarshalAtlased(json.DecodeOptions{}, bytes.NewBufferString(canon).Bytes(), &targ, atl)
Wish(t, err, ShouldEqual, nil)
Wish(t, targ, ShouldEqual, obj)
})
})
t.Run("exciting FormulaAction should roundtrip", func(t *testing.T) {
obj := FormulaAction{
Exec: []string{"/wizz", "bang"},
Env: map[string]string{"FOO": "bar", "BAZ": "quux"},
Userinfo: &FormulaUserinfo{Username: "zoltan"},
}
canon := `{"exec":["/wizz","bang"],"env":{"BAZ":"quux","FOO":"bar"},"userinfo":{"username":"zoltan"}}`
t.Run("marshal", func(t *testing.T) {
bs, err := refmt.MarshalAtlased(json.EncodeOptions{}, obj, atl)
Wish(t, err, ShouldEqual, nil)
Wish(t, string(bs), ShouldEqual, canon)
})
t.Run("unmarshal", func(t *testing.T) {
targ := FormulaAction{}
err := refmt.UnmarshalAtlased(json.DecodeOptions{}, bytes.NewBufferString(canon).Bytes(), &targ, atl)
Wish(t, err, ShouldEqual, nil)
Wish(t, targ, ShouldEqual, obj)
})
})
}
// Test Userinfo serialization.
// Some of these sanity assertions are moderately nontrivial beacuse we
// consider it important to correctly round-trip the unset/default values,
// which for integers we implement as some pointer jiggery.
func TestUserinfoSerialization(t *testing.T) {
atl := atlas.MustBuild(FormulaUserinfo_AtlasEntry)
t.Run("zero userinfo should roundtrip", func(t *testing.T) {
obj := FormulaUserinfo{}
canon := `{}`
t.Run("marshal", func(t *testing.T) {
bs, err := refmt.MarshalAtlased(json.EncodeOptions{}, obj, atl)
Wish(t, err, ShouldEqual, nil)
Wish(t, string(bs), ShouldEqual, canon)
})
t.Run("unmarshal", func(t *testing.T) {
targ := FormulaUserinfo{}
err := refmt.UnmarshalAtlased(json.DecodeOptions{}, bytes.NewBufferString(canon).Bytes(), &targ, atl)
Wish(t, err, ShouldEqual, nil)
Wish(t, targ, ShouldEqual, obj)
})
})
t.Run("userinfo with set zero uid should roundtrip", func(t *testing.T) {
i0 := 0
obj := FormulaUserinfo{Uid: &i0}
canon := `{"uid":0}`
t.Run("marshal", func(t *testing.T) {
bs, err := refmt.MarshalAtlased(json.EncodeOptions{}, obj, atl)
Wish(t, err, ShouldEqual, nil)
Wish(t, string(bs), ShouldEqual, canon)
})
t.Run("unmarshal", func(t *testing.T) {
targ := FormulaUserinfo{}
err := refmt.UnmarshalAtlased(json.DecodeOptions{}, bytes.NewBufferString(canon).Bytes(), &targ, atl)
Wish(t, err, ShouldEqual, nil)
Wish(t, targ, ShouldEqual, obj)
})
})
}