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
|
language: go
|
||||||
sudo: false
|
sudo: false
|
||||||
go:
|
go:
|
||||||
- 1.4
|
- 1.8
|
||||||
- 1.5
|
|
||||||
- tip
|
- tip
|
||||||
before_install:
|
before_install:
|
||||||
- go get github.com/axw/gocov/gocov
|
- 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
|
package jsonnet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"flag"
|
||||||
|
"io/ioutil"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"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++
|
// 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.
|
// implementation but unsure if that should be done here or via some external framework.
|
||||||
|
|
||||||
type mainTest struct {
|
type mainTest struct {
|
||||||
name string
|
name string
|
||||||
input string
|
input string
|
||||||
golden 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}", ""},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMain(t *testing.T) {
|
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 {
|
for _, test := range mainTests {
|
||||||
vm := MakeVM()
|
t.Run(test.name, func(t *testing.T) {
|
||||||
output, err := vm.EvaluateSnippet(test.name, test.input)
|
vm := MakeVM()
|
||||||
var errString string
|
read := func(file string) []byte {
|
||||||
if err != nil {
|
bytz, err := ioutil.ReadFile(file)
|
||||||
errString = err.Error()
|
if err != nil {
|
||||||
}
|
t.Fatalf("reading file: %s: %v", file, err)
|
||||||
if errString != test.errString {
|
}
|
||||||
t.Errorf("%s: error result does not match. got\n\t%+v\nexpected\n\t%+v",
|
return bytz
|
||||||
test.input, errString, test.errString)
|
}
|
||||||
}
|
|
||||||
if err == nil && output != test.golden {
|
input := read(test.input)
|
||||||
t.Errorf("%s: got\n\t%+v\nexpected\n\t%+v", test.name, output, test.golden)
|
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