-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsone_test.go
118 lines (106 loc) · 3.11 KB
/
jsone_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
package jsone
import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"testing"
yaml "gopkg.in/yaml.v2"
"github.com/stretchr/testify/require"
)
// testCase represents an entry in specification.yml
type testCase struct {
Section string `json:"section"` // nil, if not a section
Title string `json:"title"`
Context map[string]interface{} `json:"context"`
Template interface{} `json:"template"`
Result interface{} `json:"result"`
Error interface{} `json:"error"` // bool, string or nil
Panic interface{} `json:"panic"` // bool, true if panic is expected
}
func (c *testCase) Test(t *testing.T) {
require.Empty(t, c.Section, "sections aren't test cases")
if c.Panic != nil {
require.Panics(t, func() {
Render(c.Template, c.Context)
})
return
}
// Set a fixed 'now', if one isn't specified
context := make(map[string]interface{})
context["now"] = "2017-01-19T16:27:20.974Z"
for k, v := range c.Context {
context[k] = v
}
result, err := Render(c.Template, context)
if c.Error == nil {
require.NoError(t, err)
require.Equal(t, c.Result, result, "expected a different result")
} else {
require.Error(t, err)
}
}
// TestSpec will load specification.yml into testCase structs and call
// testCase.Test(t) for each test case.
func TestSpec(t *testing.T) {
// NOTE: this does some ugly YAML hacks to get around type mismatches
// between YAML and JSON in golang, as well as lack of document support
// in gopkg.in/yaml.v2
// Read specification.yml
data, err := ioutil.ReadFile("specification.yml")
require.NoError(t, err, "failed to read specification.yml")
// Parse as YAML (split for each document)
var rawSpec []interface{}
for _, doc := range strings.Split(string(data), "\n---\n") {
var rawDoc interface{}
err = yaml.Unmarshal([]byte(doc), &rawDoc)
require.NoError(t, err, "failed to parse specification.yml")
rawSpec = append(rawSpec, convertSimpleJSONTypes(rawDoc))
}
// Dump as JSON and parse from JSON to []testCase
data, err = json.Marshal(rawSpec)
require.NoError(t, err, "couldn't dump specification.yml as JSON")
var spec []testCase
err = json.Unmarshal(data, &spec)
require.NoError(t, err, "failed to parse specification from JSON")
// Test for each test case
for i, s := range spec {
if s.Section == "" {
continue
}
t.Run(s.Section, func(t *testing.T) {
for _, c := range spec[i+1:] {
if c.Section != "" {
break
}
t.Run(c.Title, c.Test)
}
})
}
}
// convertSimpleJSONTypes changes types from gopkg.in/yaml.v2 to types used
// for representing JSON in golang, see encoding/json
func convertSimpleJSONTypes(val interface{}) interface{} {
switch val := val.(type) {
case []interface{}:
r := make([]interface{}, len(val))
for i, v := range val {
r[i] = convertSimpleJSONTypes(v)
}
return r
case map[interface{}]interface{}:
r := make(map[string]interface{})
for k, v := range val {
s, ok := k.(string)
if !ok {
s = fmt.Sprintf("%v", k)
}
r[s] = convertSimpleJSONTypes(v)
}
return r
case int:
return float64(val)
default:
return val
}
}