forked from docopt/docopts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_json_load.go
74 lines (63 loc) · 1.67 KB
/
test_json_load.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
package test_json_loader
import (
"encoding/json"
"fmt"
"io/ioutil"
)
type TestString struct {
Input map[string]interface{}
Expect_args []string
Expect_global []string
Expect_global_prefix []string
}
func (t TestString) ToString() string {
return t.toString()
}
// some debug
func (t TestString) toString() (str string) {
var key string
var val interface{}
str = fmt.Sprintf("input: {\n")
for key, val = range t.Input {
str += fmt.Sprintf(" key : %s , value : %v\n", key, val)
}
str += fmt.Sprintf("}\n")
str += fmt.Sprintf("Expect_args {\n")
for _, v := range t.Expect_args {
str += fmt.Sprintf("%v\n", v)
}
str += fmt.Sprintf("}\n")
str += fmt.Sprintf("Expect_global : %v\n", t.Expect_global)
str += fmt.Sprintf("Expect_global_prefix : %v\n", t.Expect_global_prefix)
return str
}
func Load_json(filename string) ([]TestString, error) {
raw, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
var c []TestString
json.Unmarshal(raw, &c)
// loop over result and convert type when needed to match expected type in tests
for _, ts := range c {
for k, v := range ts.Input {
switch v.(type) {
case []interface{}:
// converts []interface{} to []string
aInterface := v.([]interface{})
aString := make([]string, len(aInterface))
// copy and change type
for i, str_v := range aInterface {
aString[i] = str_v.(string)
}
// overwrite old key with the new converted array
ts.Input[k] = aString
case float64:
// json.Unmarshal read all number to float64
// See: https://golang.org/pkg/encoding/json/#Unmarshal
ts.Input[k] = int(v.(float64))
}
}
}
return c, nil
}