mirror of
https://github.com/google/go-jsonnet.git
synced 2025-09-29 17:31:02 +02:00
commit
8c8668650d
@ -1,8 +1,7 @@
|
||||
language: go
|
||||
sudo: false
|
||||
go:
|
||||
- 1.4
|
||||
- 1.5
|
||||
- 1.8
|
||||
- tip
|
||||
before_install:
|
||||
- go get github.com/axw/gocov/gocov
|
||||
|
106
main_test.go
106
main_test.go
@ -17,53 +17,85 @@ limitations under the License.
|
||||
package jsonnet
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/sergi/go-diff/diffmatchpatch"
|
||||
)
|
||||
|
||||
var update = flag.Bool("update", false, "update .golden files")
|
||||
|
||||
// Just a few simple sanity tests for now. Eventually we'll share end-to-end tests with the C++
|
||||
// implementation but unsure if that should be done here or via some external framework.
|
||||
|
||||
type mainTest struct {
|
||||
name string
|
||||
input string
|
||||
golden string
|
||||
errString string
|
||||
}
|
||||
|
||||
var mainTests = []mainTest{
|
||||
{"numeric_literal", "100", "100", ""},
|
||||
{"boolean_literal", "true", "true", ""},
|
||||
{"simple_arith1", "3 + 3", "6", ""},
|
||||
{"simple_arith2", "3 + 3 + 3", "9", ""},
|
||||
{"simple_arith3", "(3 + 3) + (3 + 3)", "12", ""},
|
||||
{"unicode", `"\u263A"`, `"☺"`, ""},
|
||||
{"unicode2", `"\u263a"`, `"☺"`, ""},
|
||||
{"escaped_single_quote", `"\\'"`, `"\\'"`, ""},
|
||||
{"simple_arith_string", "\"aaa\" + \"bbb\"", "\"aaabbb\"", ""},
|
||||
{"simple_arith_string2", "\"aaa\" + \"\"", "\"aaa\"", ""},
|
||||
{"simple_arith_string3", "\"\" + \"bbb\"", "\"bbb\"", ""},
|
||||
{"simple_arith_string_empty", "\"\" + \"\"", "\"\"", ""},
|
||||
{"verbatim_string", `@"blah ☺"`, `"blah ☺"`, ""},
|
||||
{"empty_array", "[]", "[ ]", ""},
|
||||
{"array", "[1, 2, 1 + 2]", "[\n 1,\n 2,\n 3\n]", ""},
|
||||
{"empty_object", "{}", "{ }", ""},
|
||||
{"object", `{"x": 1+1}`, "{\n \"x\": 2\n}", ""},
|
||||
name string
|
||||
input string
|
||||
golden string
|
||||
}
|
||||
|
||||
func TestMain(t *testing.T) {
|
||||
flag.Parse()
|
||||
var mainTests []mainTest
|
||||
match, err := filepath.Glob("testdata/*.input")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, input := range match {
|
||||
golden := input
|
||||
name := input
|
||||
if strings.HasSuffix(input, ".input") {
|
||||
name = input[:len(input)-len(".input")]
|
||||
golden = name + ".golden"
|
||||
}
|
||||
mainTests = append(mainTests, mainTest{name: name, input: input, golden: golden})
|
||||
}
|
||||
for _, test := range mainTests {
|
||||
vm := MakeVM()
|
||||
output, err := vm.EvaluateSnippet(test.name, test.input)
|
||||
var errString string
|
||||
if err != nil {
|
||||
errString = err.Error()
|
||||
}
|
||||
if errString != test.errString {
|
||||
t.Errorf("%s: error result does not match. got\n\t%+v\nexpected\n\t%+v",
|
||||
test.input, errString, test.errString)
|
||||
}
|
||||
if err == nil && output != test.golden {
|
||||
t.Errorf("%s: got\n\t%+v\nexpected\n\t%+v", test.name, output, test.golden)
|
||||
}
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
vm := MakeVM()
|
||||
read := func(file string) []byte {
|
||||
bytz, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
t.Fatalf("reading file: %s: %v", file, err)
|
||||
}
|
||||
return bytz
|
||||
}
|
||||
|
||||
input := read(test.input)
|
||||
output, err := vm.EvaluateSnippet(test.name, string(input))
|
||||
if err != nil {
|
||||
t.Fail()
|
||||
t.Errorf("evaluate snippet: %v", err)
|
||||
}
|
||||
if *update {
|
||||
err := ioutil.WriteFile(test.golden, []byte(output), 0666)
|
||||
if err != nil {
|
||||
t.Errorf("error updating golden files: %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
golden := read(test.golden)
|
||||
if bytes.Compare(golden, []byte(output)) != 0 {
|
||||
t.Fail()
|
||||
t.Errorf("%s.input != %s\n", test.name, test.golden)
|
||||
data := diff( output, string(golden),)
|
||||
if err != nil {
|
||||
t.Errorf("computing diff: %s", err)
|
||||
}
|
||||
t.Errorf("diff %s jsonnet %s.input\n", test.golden, test.name)
|
||||
t.Errorf(string(data))
|
||||
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func diff(a, b string) string {
|
||||
dmp := diffmatchpatch.New()
|
||||
diffs := dmp.DiffMain(a, b, false)
|
||||
return dmp.DiffPrettyText(diffs)
|
||||
}
|
||||
|
5
testdata/array.golden
vendored
Normal file
5
testdata/array.golden
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
[
|
||||
1,
|
||||
2,
|
||||
3
|
||||
]
|
1
testdata/array.input
vendored
Normal file
1
testdata/array.input
vendored
Normal file
@ -0,0 +1 @@
|
||||
[1, 2, 1 + 2]
|
1
testdata/boolean_literal.golden
vendored
Normal file
1
testdata/boolean_literal.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
true
|
1
testdata/boolean_literal.input
vendored
Normal file
1
testdata/boolean_literal.input
vendored
Normal file
@ -0,0 +1 @@
|
||||
true
|
1
testdata/empty_array.golden
vendored
Normal file
1
testdata/empty_array.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
[ ]
|
1
testdata/empty_array.input
vendored
Normal file
1
testdata/empty_array.input
vendored
Normal file
@ -0,0 +1 @@
|
||||
[]
|
1
testdata/empty_object.golden
vendored
Normal file
1
testdata/empty_object.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
{ }
|
1
testdata/empty_object.input
vendored
Normal file
1
testdata/empty_object.input
vendored
Normal file
@ -0,0 +1 @@
|
||||
{}
|
1
testdata/escaped_single_quote.golden
vendored
Normal file
1
testdata/escaped_single_quote.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
"\\'"
|
1
testdata/escaped_single_quote.input
vendored
Normal file
1
testdata/escaped_single_quote.input
vendored
Normal file
@ -0,0 +1 @@
|
||||
"\\'"
|
1
testdata/numeric_literal.golden
vendored
Normal file
1
testdata/numeric_literal.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
100
|
1
testdata/numeric_literal.input
vendored
Normal file
1
testdata/numeric_literal.input
vendored
Normal file
@ -0,0 +1 @@
|
||||
100
|
3
testdata/object.golden
vendored
Normal file
3
testdata/object.golden
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"x": 2
|
||||
}
|
1
testdata/object.input
vendored
Normal file
1
testdata/object.input
vendored
Normal file
@ -0,0 +1 @@
|
||||
{"x": 1+1}
|
1
testdata/simple_arith1.golden
vendored
Normal file
1
testdata/simple_arith1.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
6
|
1
testdata/simple_arith1.input
vendored
Normal file
1
testdata/simple_arith1.input
vendored
Normal file
@ -0,0 +1 @@
|
||||
3 + 3
|
1
testdata/simple_arith2.golden
vendored
Normal file
1
testdata/simple_arith2.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
9
|
1
testdata/simple_arith2.input
vendored
Normal file
1
testdata/simple_arith2.input
vendored
Normal file
@ -0,0 +1 @@
|
||||
3 + 3 + 3
|
1
testdata/simple_arith3.golden
vendored
Normal file
1
testdata/simple_arith3.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
12
|
1
testdata/simple_arith3.input
vendored
Normal file
1
testdata/simple_arith3.input
vendored
Normal file
@ -0,0 +1 @@
|
||||
(3 + 3) + (3 + 3)
|
1
testdata/simple_arith_string.golden
vendored
Normal file
1
testdata/simple_arith_string.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
"aaabbb"
|
1
testdata/simple_arith_string.input
vendored
Normal file
1
testdata/simple_arith_string.input
vendored
Normal file
@ -0,0 +1 @@
|
||||
"aaa" + "bbb"
|
1
testdata/simple_arith_string2.golden
vendored
Normal file
1
testdata/simple_arith_string2.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
"aaa"
|
1
testdata/simple_arith_string2.input
vendored
Normal file
1
testdata/simple_arith_string2.input
vendored
Normal file
@ -0,0 +1 @@
|
||||
"aaa" + ""
|
1
testdata/simple_arith_string3.golden
vendored
Normal file
1
testdata/simple_arith_string3.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
"bbb"
|
1
testdata/simple_arith_string3.input
vendored
Normal file
1
testdata/simple_arith_string3.input
vendored
Normal file
@ -0,0 +1 @@
|
||||
"" + "bbb"
|
1
testdata/simple_arith_string_empty.golden
vendored
Normal file
1
testdata/simple_arith_string_empty.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
""
|
1
testdata/simple_arith_string_empty.input
vendored
Normal file
1
testdata/simple_arith_string_empty.input
vendored
Normal file
@ -0,0 +1 @@
|
||||
"" + ""
|
1
testdata/unicode.golden
vendored
Normal file
1
testdata/unicode.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
"☺"
|
1
testdata/unicode.input
vendored
Normal file
1
testdata/unicode.input
vendored
Normal file
@ -0,0 +1 @@
|
||||
"\u263A"
|
1
testdata/unicode2.golden
vendored
Normal file
1
testdata/unicode2.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
"☺"
|
1
testdata/unicode2.input
vendored
Normal file
1
testdata/unicode2.input
vendored
Normal file
@ -0,0 +1 @@
|
||||
"\u263a"
|
1
testdata/verbatim_string.golden
vendored
Normal file
1
testdata/verbatim_string.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
"blah ☺"
|
1
testdata/verbatim_string.input
vendored
Normal file
1
testdata/verbatim_string.input
vendored
Normal file
@ -0,0 +1 @@
|
||||
@"blah ☺"
|
Loading…
x
Reference in New Issue
Block a user