forked from ooni/probe-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperimentbuilder_test.go
190 lines (185 loc) · 4.91 KB
/
experimentbuilder_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package engine
import (
"testing"
"github.com/ooni/probe-engine/experiment/example"
)
func TestExperimentBuilderOptions(t *testing.T) {
t.Run("when config is not a pointer", func(t *testing.T) {
b := &ExperimentBuilder{
config: 17,
}
options, err := b.Options()
if err == nil {
t.Fatal("expected an error here")
}
if options != nil {
t.Fatal("expected nil here")
}
})
t.Run("when config is not a struct", func(t *testing.T) {
number := 17
b := &ExperimentBuilder{
config: &number,
}
options, err := b.Options()
if err == nil {
t.Fatal("expected an error here")
}
if options != nil {
t.Fatal("expected nil here")
}
})
}
func TestExperimentBuilderSetOption(t *testing.T) {
t.Run("when config is not a pointer", func(t *testing.T) {
b := &ExperimentBuilder{
config: 17,
}
if err := b.SetOptionBool("antani", false); err == nil {
t.Fatal("expected an error here")
}
})
t.Run("when config is not a struct", func(t *testing.T) {
number := 17
b := &ExperimentBuilder{
config: &number,
}
if err := b.SetOptionBool("antani", false); err == nil {
t.Fatal("expected an error here")
}
})
t.Run("when field is not valid", func(t *testing.T) {
b := &ExperimentBuilder{
config: &ExperimentBuilder{},
}
if err := b.SetOptionBool("antani", false); err == nil {
t.Fatal("expected an error here")
}
})
t.Run("when field is not bool", func(t *testing.T) {
b := &ExperimentBuilder{
config: new(example.Config),
}
if err := b.SetOptionBool("Message", false); err == nil {
t.Fatal("expected an error here")
}
})
t.Run("when field is not string", func(t *testing.T) {
b := &ExperimentBuilder{
config: new(example.Config),
}
if err := b.SetOptionString("ReturnError", "xx"); err == nil {
t.Fatal("expected an error here")
}
})
t.Run("when field is not int", func(t *testing.T) {
b := &ExperimentBuilder{
config: new(example.Config),
}
if err := b.SetOptionInt("ReturnError", 17); err == nil {
t.Fatal("expected an error here")
}
})
t.Run("when int field does not exist", func(t *testing.T) {
b := &ExperimentBuilder{
config: new(example.Config),
}
if err := b.SetOptionInt("antani", 17); err == nil {
t.Fatal("expected an error here")
}
})
t.Run("when string field does not exist", func(t *testing.T) {
b := &ExperimentBuilder{
config: new(example.Config),
}
if err := b.SetOptionString("antani", "xx"); err == nil {
t.Fatal("expected an error here")
}
})
}
func TestExperimentBuilderSetOptionGuessType(t *testing.T) {
type fiction struct {
String string
Truth bool
Value int64
}
b := &ExperimentBuilder{config: &fiction{}}
t.Run("we correctly guess a boolean", func(t *testing.T) {
if err := b.SetOptionGuessType("Truth", "true"); err != nil {
t.Fatal(err)
}
if err := b.SetOptionGuessType("Truth", "false"); err != nil {
t.Fatal(err)
}
if err := b.SetOptionGuessType("Truth", "1234"); err == nil {
t.Fatal("expected an error here")
}
if err := b.SetOptionGuessType("Truth", "yoloyolo"); err == nil {
t.Fatal("expected an error here")
}
})
t.Run("we correctly guess an integer", func(t *testing.T) {
if err := b.SetOptionGuessType("Value", "true"); err == nil {
t.Fatal("expected an error here")
}
if err := b.SetOptionGuessType("Value", "false"); err == nil {
t.Fatal("expected an error here")
}
if err := b.SetOptionGuessType("Value", "1234"); err != nil {
t.Fatal(err)
}
if err := b.SetOptionGuessType("Value", "yoloyolo"); err == nil {
t.Fatal("expected an error here")
}
})
t.Run("we correctly guess a string", func(t *testing.T) {
if err := b.SetOptionGuessType("String", "true"); err == nil {
t.Fatal("expected an error here")
}
if err := b.SetOptionGuessType("String", "false"); err == nil {
t.Fatal("expected an error here")
}
if err := b.SetOptionGuessType("String", "1234"); err == nil {
t.Fatal("expected an error here")
}
if err := b.SetOptionGuessType("String", "yoloyolo"); err != nil {
t.Fatal(err)
}
})
t.Run("we correctly handle an empty map", func(t *testing.T) {
if err := b.SetOptionsGuessType(nil); err != nil {
t.Fatal(err)
}
})
t.Run("we correctly handle a map containing options", func(t *testing.T) {
f := &fiction{}
privateb := &ExperimentBuilder{config: f}
opts := map[string]string{
"String": "yoloyolo",
"Value": "174",
"Truth": "true",
}
if err := privateb.SetOptionsGuessType(opts); err != nil {
t.Fatal(err)
}
if f.String != "yoloyolo" {
t.Fatal("cannot set string value")
}
if f.Value != 174 {
t.Fatal("cannot set integer value")
}
if f.Truth != true {
t.Fatal("cannot set bool value")
}
})
t.Run("we handle mistakes in a map containing options", func(t *testing.T) {
opts := map[string]string{
"String": "yoloyolo",
"Value": "antani;",
"Truth": "true",
}
if err := b.SetOptionsGuessType(opts); err == nil {
t.Fatal("expected an error here")
}
})
}