forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnull_int_test.go
145 lines (125 loc) · 3.62 KB
/
null_int_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package types_test
import (
. "code.cloudfoundry.org/cli/types"
"github.com/jessevdk/go-flags"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
)
var _ = Describe("NullInt", func() {
var nullInt NullInt
BeforeEach(func() {
nullInt = NullInt{
IsSet: true,
Value: 0xBAD,
}
})
Describe("IsValidValue", func() {
var (
input string
executeErr error
)
JustBeforeEach(func() {
executeErr = nullInt.IsValidValue(input)
})
When("the value is a positive integer", func() {
BeforeEach(func() {
input = "1"
})
It("does not error", func() {
Expect(executeErr).ToNot(HaveOccurred())
})
})
When("the value is a negative integer", func() {
BeforeEach(func() {
input = "-21"
})
It("does not error", func() {
Expect(executeErr).ToNot(HaveOccurred())
})
})
When("the value is a non integer", func() {
BeforeEach(func() {
input = "not-a-integer"
})
It("returns an error", func() {
Expect(executeErr).To(MatchError("invalid integer value `not-a-integer`"))
})
})
})
Describe("ParseIntValue", func() {
When("nil is provided", func() {
It("sets IsSet to false", func() {
nullInt.ParseIntValue(nil)
Expect(nullInt).To(Equal(NullInt{Value: 0, IsSet: false}))
})
})
When("non-nil pointer is provided", func() {
It("sets IsSet to true and Value to provided value", func() {
n := 5
nullInt.ParseIntValue(&n)
Expect(nullInt).To(Equal(NullInt{Value: 5, IsSet: true}))
})
})
})
Describe("ParseStringValue", func() {
When("the empty string is provided", func() {
It("sets IsSet to false", func() {
err := nullInt.ParseStringValue("")
Expect(err).ToNot(HaveOccurred())
Expect(nullInt).To(Equal(NullInt{Value: 0, IsSet: false}))
})
})
When("an invalid integer is provided", func() {
It("returns an error", func() {
err := nullInt.ParseStringValue("abcdef")
Expect(err).To(MatchError(&flags.Error{
Type: flags.ErrMarshal,
Message: "invalid integer value `abcdef`",
}))
Expect(nullInt).To(Equal(NullInt{Value: 0, IsSet: false}))
})
})
When("a valid integer is provided", func() {
It("stores the integer and sets IsSet to true", func() {
err := nullInt.ParseStringValue("0")
Expect(err).ToNot(HaveOccurred())
Expect(nullInt).To(Equal(NullInt{Value: 0, IsSet: true}))
})
})
})
Describe("UnmarshalJSON", func() {
When("integer value is provided", func() {
It("parses JSON number correctly", func() {
err := nullInt.UnmarshalJSON([]byte("42"))
Expect(err).ToNot(HaveOccurred())
Expect(nullInt).To(Equal(NullInt{Value: 42, IsSet: true}))
})
})
When("a null value is provided", func() {
It("returns an unset NullInt", func() {
err := nullInt.UnmarshalJSON([]byte("null"))
Expect(err).ToNot(HaveOccurred())
Expect(nullInt).To(Equal(NullInt{Value: 0, IsSet: false}))
})
})
When("an empty string is provided", func() {
It("returns an unset NullInt", func() {
err := nullInt.UnmarshalJSON([]byte(""))
Expect(err).ToNot(HaveOccurred())
Expect(nullInt).To(Equal(NullInt{Value: 0, IsSet: false}))
})
})
})
DescribeTable("MarshalJSON",
func(nullInt NullInt, expectedBytes []byte) {
bytes, err := nullInt.MarshalJSON()
Expect(err).ToNot(HaveOccurred())
Expect(bytes).To(Equal(expectedBytes))
},
Entry("negative number", NullInt{IsSet: true, Value: -1}, []byte("-1")),
Entry("positive number", NullInt{IsSet: true, Value: 1}, []byte("1")),
Entry("0", NullInt{IsSet: true, Value: 0}, []byte("0")),
Entry("no value", NullInt{IsSet: false}, []byte("null")),
)
})