-
Notifications
You must be signed in to change notification settings - Fork 0
/
operationSerial_test.go
83 lines (78 loc) · 2.08 KB
/
operationSerial_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
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 TestOperationSerialization(t *testing.T) {
atl := atlas.MustBuild(
Operation_AtlasEntry,
SlotRef_AtlasEntry,
FormulaAction_AtlasEntry,
FormulaUserinfo_AtlasEntry,
)
t.Run("zero operation should roundtrip", func(t *testing.T) {
obj := Operation{}
canon := `{"inputs":null,"action":{}}`
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 := Operation{}
err := refmt.UnmarshalAtlased(json.DecodeOptions{}, bytes.NewBufferString(canon).Bytes(), &targ, atl)
Wish(t, err, ShouldEqual, nil)
Wish(t, targ, ShouldEqual, obj)
})
t.Run("unmarshal blank", func(t *testing.T) {
targ := Operation{}
canon := `{}`
err := refmt.UnmarshalAtlased(json.DecodeOptions{}, bytes.NewBufferString(canon).Bytes(), &targ, atl)
Wish(t, err, ShouldEqual, nil)
Wish(t, targ, ShouldEqual, obj)
})
})
t.Run("examplary operation should roundtrip", func(t *testing.T) {
obj := Operation{
Inputs: map[AbsPath]SlotRef{
"/": SlotRef{"", "foo"},
},
Action: FormulaAction{
Exec: []string{"/script"},
},
Outputs: map[SlotName]AbsPath{
"bar": "/bar",
},
}
canon := Dedent(`
{
"inputs": {
"/": "foo"
},
"action": {
"exec": [
"/script"
]
},
"outputs": {
"bar": "/bar"
}
}
`)
t.Run("marshal", func(t *testing.T) {
bs, err := refmt.MarshalAtlased(json.EncodeOptions{Line: []byte{'\n'}, Indent: []byte{'\t'}}, obj, atl)
Wish(t, err, ShouldEqual, nil)
Wish(t, string(bs), ShouldEqual, canon)
})
t.Run("unmarshal", func(t *testing.T) {
targ := Operation{}
err := refmt.UnmarshalAtlased(json.DecodeOptions{}, bytes.NewBufferString(canon).Bytes(), &targ, atl)
Wish(t, err, ShouldEqual, nil)
Wish(t, targ, ShouldEqual, obj)
})
})
}