forked from google/go-jsonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonnet_test.go
132 lines (121 loc) · 3.62 KB
/
jsonnet_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
package jsonnet
import (
"bytes"
"testing"
"unicode/utf8"
)
type errorFormattingTest struct {
name string
input string
errString string
}
func genericTestErrorMessage(t *testing.T, tests []errorFormattingTest, format func(RuntimeError) string) {
for _, test := range tests {
vm := MakeVM()
rawOutput, err := vm.evaluateSnippet(test.name, test.input, evalKindRegular)
var errString string
if err != nil {
switch typedErr := err.(type) {
case RuntimeError:
errString = format(typedErr)
default:
t.Errorf("%s: unexpected error: %v", test.name, err)
}
}
output := rawOutput.(string)
if errString != test.errString {
t.Errorf("%s: error result does not match. got\n\t%+#v\nexpected\n\t%+#v",
test.name, errString, test.errString)
}
if err == nil {
t.Errorf("%s, Expected error, but execution succeded and the here's the result:\n %v\n", test.name, output)
}
}
}
// TODO(sbarzowski) Perhaps we should have just one set of tests with all the variants?
// TODO(sbarzowski) Perhaps this should be handled in external tests?
var oneLineTests = []errorFormattingTest{
{"error", `error "x"`, "RUNTIME ERROR: x"},
}
func TestOneLineError(t *testing.T) {
genericTestErrorMessage(t, oneLineTests, func(r RuntimeError) string {
return r.Error()
})
}
// TODO(sbarzowski) checking if the whitespace is right is quite unpleasant, what can we do about it?
var minimalErrorTests = []errorFormattingTest{
{"error", `error "x"`, "RUNTIME ERROR: x\n" +
" error:1:1-10 $\n" + // TODO(sbarzowski) if seems we have off-by-one in location
" During evaluation \n" +
""},
{"error_in_func", `local x(n) = if n == 0 then error "x" else x(n - 1); x(3)`, "RUNTIME ERROR: x\n" +
" error_in_func:1:29-38 function <x>\n" +
" error_in_func:1:44-52 function <x>\n" +
" error_in_func:1:44-52 function <x>\n" +
" error_in_func:1:44-52 function <x>\n" +
" error_in_func:1:54-58 $\n" +
" During evaluation \n" +
""},
{"error_in_error", `error (error "x")`, "RUNTIME ERROR: x\n" +
" error_in_error:1:8-17 $\n" +
" During evaluation \n" +
""},
}
func TestMinimalError(t *testing.T) {
formatter := termErrorFormatter{maxStackTraceSize: 20}
genericTestErrorMessage(t, minimalErrorTests, func(r RuntimeError) string {
return formatter.Format(r)
})
}
// TODO(sbarzowski) test pretty errors once they are stable-ish
// probably "golden" pattern is the right one for that
func removeExcessiveWhitespace(s string) string {
var buf bytes.Buffer
separated := true
for i, w := 0, 0; i < len(s); i += w {
runeValue, width := utf8.DecodeRuneInString(s[i:])
if runeValue == '\n' || runeValue == ' ' {
if !separated {
buf.WriteString(" ")
separated = true
}
} else {
buf.WriteRune(runeValue)
separated = false
}
w = width
}
return buf.String()
}
func TestCustomImporter(t *testing.T) {
vm := MakeVM()
vm.Importer(&MemoryImporter{
map[string]Contents{
"a.jsonnet": MakeContents("2 + 2"),
"b.jsonnet": MakeContents("3 + 3"),
},
})
input := `[import "a.jsonnet", importstr "b.jsonnet"]`
expected := `[ 4, "3 + 3" ] `
actual, err := vm.EvaluateSnippet("custom_import.jsonnet", input)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
actual = removeExcessiveWhitespace(actual)
if actual != expected {
t.Errorf("Expected %q, but got %q", expected, actual)
}
}
func TestContents(t *testing.T) {
a := "aaa"
c1 := MakeContents(a)
a = "bbb"
if c1.String() != "aaa" {
t.Errorf("Contents should be immutable")
}
c2 := MakeContents(a)
c3 := MakeContents(a)
if c2 == c3 {
t.Errorf("Contents should distinguish between different instances even if they have the same data inside")
}
}