forked from pelletier/go-toml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toml_testgen_support_test.go
119 lines (104 loc) · 2.99 KB
/
toml_testgen_support_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
// This is a support file for toml_testgen_test.go
package toml
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"testing"
"time"
"github.com/davecgh/go-spew/spew"
)
func testgenInvalid(t *testing.T, input string) {
t.Logf("Input TOML:\n%s", input)
tree, err := Load(input)
if err != nil {
return
}
typedTree := testgenTranslate(*tree)
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(typedTree); err != nil {
return
}
t.Fatalf("test did not fail. resulting tree:\n%s", buf.String())
}
func testgenValid(t *testing.T, input string, jsonRef string) {
t.Logf("Input TOML:\n%s", input)
tree, err := Load(input)
if err != nil {
t.Fatalf("failed parsing toml: %s", err)
}
typedTree := testgenTranslate(*tree)
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(typedTree); err != nil {
t.Fatalf("failed translating to JSON: %s", err)
}
var jsonTest interface{}
if err := json.NewDecoder(buf).Decode(&jsonTest); err != nil {
t.Logf("translated JSON:\n%s", buf.String())
t.Fatalf("failed decoding translated JSON: %s", err)
}
var jsonExpected interface{}
if err := json.NewDecoder(bytes.NewBufferString(jsonRef)).Decode(&jsonExpected); err != nil {
t.Logf("reference JSON:\n%s", jsonRef)
t.Fatalf("failed decoding reference JSON: %s", err)
}
if !reflect.DeepEqual(jsonExpected, jsonTest) {
t.Logf("Diff:\n%s", spew.Sdump(jsonExpected, jsonTest))
t.Fatal("parsed TOML tree is different than expected structure")
}
}
func testgenTranslate(tomlData interface{}) interface{} {
switch orig := tomlData.(type) {
case map[string]interface{}:
typed := make(map[string]interface{}, len(orig))
for k, v := range orig {
typed[k] = testgenTranslate(v)
}
return typed
case *Tree:
return testgenTranslate(*orig)
case Tree:
keys := orig.Keys()
typed := make(map[string]interface{}, len(keys))
for _, k := range keys {
typed[k] = testgenTranslate(orig.GetPath([]string{k}))
}
return typed
case []*Tree:
typed := make([]map[string]interface{}, len(orig))
for i, v := range orig {
typed[i] = testgenTranslate(v).(map[string]interface{})
}
return typed
case []map[string]interface{}:
typed := make([]map[string]interface{}, len(orig))
for i, v := range orig {
typed[i] = testgenTranslate(v).(map[string]interface{})
}
return typed
case []interface{}:
typed := make([]interface{}, len(orig))
for i, v := range orig {
typed[i] = testgenTranslate(v)
}
return testgenTag("array", typed)
case time.Time:
return testgenTag("datetime", orig.Format("2006-01-02T15:04:05Z"))
case bool:
return testgenTag("bool", fmt.Sprintf("%v", orig))
case int64:
return testgenTag("integer", fmt.Sprintf("%d", orig))
case float64:
return testgenTag("float", fmt.Sprintf("%v", orig))
case string:
return testgenTag("string", orig)
}
panic(fmt.Sprintf("Unknown type: %T", tomlData))
}
func testgenTag(typeName string, data interface{}) map[string]interface{} {
return map[string]interface{}{
"type": typeName,
"value": data,
}
}