forked from biased-unit/planout-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_ops_test.go
184 lines (151 loc) · 4.19 KB
/
simple_ops_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
/*
* Copyright 2014 URX
*
* 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 goplanout
import (
"encoding/json"
"io/ioutil"
"reflect"
"testing"
)
func readTest(f string) map[string]interface{} {
data, _ := ioutil.ReadFile(f)
var js map[string]interface{}
json.Unmarshal(data, &js)
return js
}
type Struct struct {
Member int
String string
}
func TestSimpleOps(t *testing.T) {
js := readTest("test/simple_ops.json")
data := Struct{Member: 101, String: "test-string"}
params := make(map[string]interface{})
params["struct"] = data
expt := &Interpreter{
Salt: "global_salt",
Evaluated: false,
Inputs: params,
Outputs: map[string]interface{}{},
Overrides: map[string]interface{}{},
Code: js,
}
output, ok := expt.Run()
if !ok {
t.Errorf("Error running experiment 'test/simple_ops.json'\n")
return
}
a := output["a"]
if compare(a, 10.0) != 0 {
t.Errorf("Variable 'a'. Expected 10. Actual %v\n", a)
}
b := output["b"]
if compare(b, 3.14) != 0 {
t.Errorf("Variable 'b'. Expected 3.14. Actual %v\n", b)
}
c := output["c"]
if compare(c, "hello") != 0 {
t.Errorf("Variable 'c'. Expected 'hello'. Actual %v\n", c)
}
d := output["d"].([]interface{})
if len(d) != 3 {
t.Errorf("Expected length of variable 'd' = 3. Actual len %v\n", len(d))
}
if reflect.DeepEqual(d[0].(float64), 1.0) == false ||
reflect.DeepEqual(d[1].(float64), 2.0) == false ||
reflect.DeepEqual(d[2].(float64), 3.0) == false {
t.Errorf("Variable 'd'. Expected [1 2 3]. Actual %v\n", d)
}
e := output["e"].([]interface{})
if len(e) != 3 {
t.Errorf("Expected length of variable 'e' = 3. Actual len %v\n", len(e))
}
if reflect.DeepEqual(e[0].(float64), 10.0) == false ||
reflect.DeepEqual(e[1].(float64), 3.14) == false {
t.Errorf("Variable 'e'. Expected [10 3.14 [1 2 3]] Actual %v\n", e)
}
if reflect.DeepEqual(e[2], d) == false {
t.Errorf("Variable 'e'. Expected last element to be [1 2 3]] Actual %v\n", e[2])
}
f := output["f"]
if compare(f, 3.0) != 0 {
t.Errorf("Variable 'f'. Expected 3. Actual %v\n", f)
}
g := output["g"]
if compare(g, 1.0) != 0 {
t.Errorf("Variable 'g'. Expected 1.0. Actual %v\n", g)
}
h := output["h"]
if compare(h, 1.0) != 0 {
t.Errorf("Variable 'h'. Expected 1.0. Actual %v\n", h)
}
i := output["i"]
if compare(i, 1.0) != 0 {
t.Errorf("Variable 'i'. Expected 1.0. Actual %v\n", i)
}
j := output["j"]
if compare(j, 13.14) != 0 {
t.Errorf("Variable 'j'. Expected 1.0. Actual %v\n", j)
}
k := output["k"]
if compare(k, 31.4) != 0 {
t.Errorf("Variable 'k'. Expected 31.4. Actual %v\n", k)
}
l := output["l"]
if compare(l, 3.1847) != 0 {
t.Errorf("Variable 'k'. Expected 3.1847. Actual %v\n", l)
}
m := output["m"]
if compare(m, 1.0) != 0 {
t.Errorf("Variable 'm'. Expected 1. Actual %v\n", m)
}
n := output["n"]
if compare(n, 10.0) != 0 {
t.Errorf("Variable 'n'. Expected 10.0. Actual %v\n", n)
}
o := output["o"]
if compare(o, 3.14) != 0 {
t.Errorf("Variable 'o'. Expected 3.14. Actual %v\n", o)
}
s := output["s"]
if s.(bool) == true {
t.Errorf("Variable 's'. Expected false.\n")
}
tval := output["t"]
if tval.(bool) == false {
t.Errorf("Variable 't'. Expected true.\n")
}
u := output["u"]
if u.(bool) == true {
t.Errorf("Variable 'u'. Expected false (NOT p).\n")
}
w := output["w"]
if reflect.DeepEqual(w, d) == false {
t.Errorf("Variable 'w' %v. Expected [1, 2, 3] (same as variable 'd')\n", w)
}
x := output["x"]
if compare(x, 2) != 0 {
t.Errorf("Variable 'x' %v. Expected 2\n", x)
}
z := output["z1"]
if z != 101 {
t.Errorf("Variable 'z1' %v. Expected 101\n", z)
}
z2 := output["z2"]
if z2 != "test-string" {
t.Errorf("Variable 'z' '%v'. Expected 'test-string'\n", z2)
}
}