-
Notifications
You must be signed in to change notification settings - Fork 0
/
value_test.go
139 lines (114 loc) · 3.78 KB
/
value_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
/*
Copyright 2023 Alexandre Mahdhaoui.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package di_test
import (
"github.com/alexandremahdhaoui/di" //nolint:depguard
. "github.com/onsi/ginkgo/v2" //nolint:depguard
. "github.com/onsi/gomega" //nolint:depguard
)
type (
testStruct struct {
a int
}
testConcreteIface struct {
b string
}
testIface interface {
test() string
}
)
func (t *testConcreteIface) test() string {
return t.b
}
var _ = Describe("Value", func() {
Describe("NewValue", func() {
It("should return a Value[T]", func() {})
It("should panic", func() {})
})
// Describe helps organize our Specs.
// Describe node's closure can contain any number of Setup nodes (e.g. BeforeEach, AfterEach, JustBeforeEach), and
// Subject nodes (i.e. It).
// Context and When nodes are aliases for Describe - use whichever gives your suite a better narrative flow.
// It is idiomatic to Describe the behavior of an object or function and, within that Describe, outline a number of
// Contexts and Whens.
Describe("Value[T]", func() {
var v0Inner []bool
// v1Inner is not referenced
var v2Inner testStruct
var v3Inner testIface
var v0InnerPointer *[]bool
var v2InnerPointer *testStruct
var v3InnerPointer *testIface
var value0 di.Value[[]bool]
var value1 di.Value[string]
var value2 di.Value[testStruct]
var value3 di.Value[testIface]
// We instantiate states of our Specs
BeforeEach(func() {
v0Inner = []bool{true, false}
// v1Inner is not referenced
v2Inner = testStruct{a: 2}
v3Inner = testIface(&testConcreteIface{b: "value3"})
v0InnerPointer = &v0Inner
v2InnerPointer = &v2Inner
v3InnerPointer = &v3Inner
value0 = di.NewValue[[]bool]("value0", v0InnerPointer)
value1 = di.NewValue[string]("value1", nil)
value2 = di.NewValue[testStruct]("value2", v2InnerPointer)
value3 = di.NewValue[testIface]("value3", v3InnerPointer)
})
// Context is an alias to Describe but is used to structure the Specs
Context("Key", func() {
// "It" nodes are subject nodes that contains the Spec code & assertions
It("should return the exact same key", func() {
Expect(value0.Key()).To(Equal("value0"))
})
})
Context("Ptr", func() {
It("should return the exact same pointer", func() {
ptr0, err := value0.Ptr()
Expect(err).ShouldNot(HaveOccurred())
Expect(ptr0).To(BeIdenticalTo(v0InnerPointer))
ptr2, err := value2.Ptr()
Expect(err).ShouldNot(HaveOccurred())
Expect(ptr2).To(BeIdenticalTo(v2InnerPointer))
ptr3, err := value3.Ptr()
Expect(err).ShouldNot(HaveOccurred())
Expect(ptr3).To(BeIdenticalTo(v3InnerPointer))
})
It("should return a pointer to a new string", func() {
ptr1, err := value1.Ptr()
Expect(err).ShouldNot(HaveOccurred())
newStringPtr := new(string)
Expect(ptr1).To(Equal(newStringPtr))
})
})
Context("Set", func() {
It("should have occurred", func() {
newItem := []bool{false}
err := value0.Set(newItem)
Expect(err).ShouldNot(HaveOccurred())
})
})
Context("Value", func() {
It("should return the same value", func() {
v0, err := value0.Value()
Expect(err).ShouldNot(HaveOccurred())
Expect(v0).To(Equal(v0Inner))
v1, err := value1.Value()
Expect(err).ShouldNot(HaveOccurred())
Expect(v1).To(Equal(*new(string)))
})
})
})
})