-
Notifications
You must be signed in to change notification settings - Fork 0
/
Serializer_test.go
95 lines (84 loc) · 2.88 KB
/
Serializer_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 gosoon_test
import (
. "gosoon"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Gosoon serializer", func() {
var serialize func(interface{})
var mock MockJsonWriter
BeforeEach(func() {
mock = MockJsonWriter{}
serialize = MakeSerializer(&mock)
})
Context("Given an object with no fields", func() {
BeforeEach(func() {
serialize(&struct{}{})
})
It("Should serialize it as an empty JSON object", func() {
Expect(mock.json).To(Equal("{}"))
})
})
Context("Given an object with one defaulted string field", func() {
BeforeEach(func() {
serialize(&struct{ Foo string }{})
})
It("Should serialize it as JSON object with one field", func() {
Expect(mock.json).To(Equal(`{Foo""}`))
})
})
Context("Given an object with two defaulted string fields", func() {
BeforeEach(func() {
serialize(&struct{ Foo string; Bar string }{})
})
It("Should serialize it as JSON object with two fields", func() {
Expect(mock.json).To(Equal(`{Foo"",Bar""}`))
})
})
Context("Given an object with a string field set to a nondefault value", func() {
BeforeEach(func() {
object := struct{ Foo string }{}
object.Foo = "bar"
serialize(&object)
})
It("Should serialize it as JSON object with one field", func() {
Expect(mock.json).To(Equal(`{Foo"bar"}`))
})
})
Context("Given an object with a defaulted integer field", func() {
BeforeEach(func() {
serialize(&struct{ Foo int }{})
})
It("Should serialize it as JSON object with one field, set to zero", func() {
Expect(mock.json).To(Equal(`{Foo0}`))
})
})
Context("Given an object with an integer field set to 4", func() {
BeforeEach(func() {
object := struct{ Foo int }{}
object.Foo = 4
serialize(&object)
})
It("Should serialize it as JSON object with one field, set to zero", func() {
Expect(mock.json).To(Equal(`{Foo4}`))
})
})
Context("Given an object with a defaulted boolean field", func() {
BeforeEach(func() {
serialize(&struct{ Foo bool }{})
})
It("Should serialize it as JSON object with one field, set to false", func() {
Expect(mock.json).To(Equal(`{Foofalse}`))
})
})
Context("Given an object with a boolean field set to true", func() {
BeforeEach(func() {
object := struct{ Foo bool }{}
object.Foo = true
serialize(&object)
})
It("Should serialize it as JSON object with one field, set to true", func() {
Expect(mock.json).To(Equal(`{Footrue}`))
})
})
})