diff --git a/.travis.yml b/.travis.yml index f8de2ef..0802a6c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/main_test.go b/main_test.go index b1bf2bc..e2b4123 100644 --- a/main_test.go +++ b/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) +} diff --git a/testdata/array.golden b/testdata/array.golden new file mode 100644 index 0000000..c856f5c --- /dev/null +++ b/testdata/array.golden @@ -0,0 +1,5 @@ +[ + 1, + 2, + 3 +] \ No newline at end of file diff --git a/testdata/array.input b/testdata/array.input new file mode 100644 index 0000000..80cc40d --- /dev/null +++ b/testdata/array.input @@ -0,0 +1 @@ +[1, 2, 1 + 2] \ No newline at end of file diff --git a/testdata/boolean_literal.golden b/testdata/boolean_literal.golden new file mode 100644 index 0000000..f32a580 --- /dev/null +++ b/testdata/boolean_literal.golden @@ -0,0 +1 @@ +true \ No newline at end of file diff --git a/testdata/boolean_literal.input b/testdata/boolean_literal.input new file mode 100644 index 0000000..f32a580 --- /dev/null +++ b/testdata/boolean_literal.input @@ -0,0 +1 @@ +true \ No newline at end of file diff --git a/testdata/empty_array.golden b/testdata/empty_array.golden new file mode 100644 index 0000000..8878e54 --- /dev/null +++ b/testdata/empty_array.golden @@ -0,0 +1 @@ +[ ] \ No newline at end of file diff --git a/testdata/empty_array.input b/testdata/empty_array.input new file mode 100644 index 0000000..0637a08 --- /dev/null +++ b/testdata/empty_array.input @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/testdata/empty_object.golden b/testdata/empty_object.golden new file mode 100644 index 0000000..6f31cf5 --- /dev/null +++ b/testdata/empty_object.golden @@ -0,0 +1 @@ +{ } \ No newline at end of file diff --git a/testdata/empty_object.input b/testdata/empty_object.input new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/testdata/empty_object.input @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/testdata/escaped_single_quote.golden b/testdata/escaped_single_quote.golden new file mode 100644 index 0000000..eb16fcd --- /dev/null +++ b/testdata/escaped_single_quote.golden @@ -0,0 +1 @@ +"\\'" \ No newline at end of file diff --git a/testdata/escaped_single_quote.input b/testdata/escaped_single_quote.input new file mode 100644 index 0000000..eb16fcd --- /dev/null +++ b/testdata/escaped_single_quote.input @@ -0,0 +1 @@ +"\\'" \ No newline at end of file diff --git a/testdata/numeric_literal.golden b/testdata/numeric_literal.golden new file mode 100644 index 0000000..105d7d9 --- /dev/null +++ b/testdata/numeric_literal.golden @@ -0,0 +1 @@ +100 \ No newline at end of file diff --git a/testdata/numeric_literal.input b/testdata/numeric_literal.input new file mode 100644 index 0000000..105d7d9 --- /dev/null +++ b/testdata/numeric_literal.input @@ -0,0 +1 @@ +100 \ No newline at end of file diff --git a/testdata/object.golden b/testdata/object.golden new file mode 100644 index 0000000..3c7020e --- /dev/null +++ b/testdata/object.golden @@ -0,0 +1,3 @@ +{ + "x": 2 +} \ No newline at end of file diff --git a/testdata/object.input b/testdata/object.input new file mode 100644 index 0000000..6d7857b --- /dev/null +++ b/testdata/object.input @@ -0,0 +1 @@ +{"x": 1+1} \ No newline at end of file diff --git a/testdata/simple_arith1.golden b/testdata/simple_arith1.golden new file mode 100644 index 0000000..62f9457 --- /dev/null +++ b/testdata/simple_arith1.golden @@ -0,0 +1 @@ +6 \ No newline at end of file diff --git a/testdata/simple_arith1.input b/testdata/simple_arith1.input new file mode 100644 index 0000000..8fc606f --- /dev/null +++ b/testdata/simple_arith1.input @@ -0,0 +1 @@ +3 + 3 \ No newline at end of file diff --git a/testdata/simple_arith2.golden b/testdata/simple_arith2.golden new file mode 100644 index 0000000..f11c82a --- /dev/null +++ b/testdata/simple_arith2.golden @@ -0,0 +1 @@ +9 \ No newline at end of file diff --git a/testdata/simple_arith2.input b/testdata/simple_arith2.input new file mode 100644 index 0000000..28de3de --- /dev/null +++ b/testdata/simple_arith2.input @@ -0,0 +1 @@ +3 + 3 + 3 \ No newline at end of file diff --git a/testdata/simple_arith3.golden b/testdata/simple_arith3.golden new file mode 100644 index 0000000..3cacc0b --- /dev/null +++ b/testdata/simple_arith3.golden @@ -0,0 +1 @@ +12 \ No newline at end of file diff --git a/testdata/simple_arith3.input b/testdata/simple_arith3.input new file mode 100644 index 0000000..97529e6 --- /dev/null +++ b/testdata/simple_arith3.input @@ -0,0 +1 @@ +(3 + 3) + (3 + 3) \ No newline at end of file diff --git a/testdata/simple_arith_string.golden b/testdata/simple_arith_string.golden new file mode 100644 index 0000000..bf056f9 --- /dev/null +++ b/testdata/simple_arith_string.golden @@ -0,0 +1 @@ +"aaabbb" \ No newline at end of file diff --git a/testdata/simple_arith_string.input b/testdata/simple_arith_string.input new file mode 100644 index 0000000..dd1044b --- /dev/null +++ b/testdata/simple_arith_string.input @@ -0,0 +1 @@ +"aaa" + "bbb" \ No newline at end of file diff --git a/testdata/simple_arith_string2.golden b/testdata/simple_arith_string2.golden new file mode 100644 index 0000000..a9cf1d4 --- /dev/null +++ b/testdata/simple_arith_string2.golden @@ -0,0 +1 @@ +"aaa" \ No newline at end of file diff --git a/testdata/simple_arith_string2.input b/testdata/simple_arith_string2.input new file mode 100644 index 0000000..1e9bd2a --- /dev/null +++ b/testdata/simple_arith_string2.input @@ -0,0 +1 @@ +"aaa" + "" \ No newline at end of file diff --git a/testdata/simple_arith_string3.golden b/testdata/simple_arith_string3.golden new file mode 100644 index 0000000..b209038 --- /dev/null +++ b/testdata/simple_arith_string3.golden @@ -0,0 +1 @@ +"bbb" \ No newline at end of file diff --git a/testdata/simple_arith_string3.input b/testdata/simple_arith_string3.input new file mode 100644 index 0000000..1af9d9c --- /dev/null +++ b/testdata/simple_arith_string3.input @@ -0,0 +1 @@ +"" + "bbb" \ No newline at end of file diff --git a/testdata/simple_arith_string_empty.golden b/testdata/simple_arith_string_empty.golden new file mode 100644 index 0000000..3cc762b --- /dev/null +++ b/testdata/simple_arith_string_empty.golden @@ -0,0 +1 @@ +"" \ No newline at end of file diff --git a/testdata/simple_arith_string_empty.input b/testdata/simple_arith_string_empty.input new file mode 100644 index 0000000..d9883df --- /dev/null +++ b/testdata/simple_arith_string_empty.input @@ -0,0 +1 @@ +"" + "" \ No newline at end of file diff --git a/testdata/unicode.golden b/testdata/unicode.golden new file mode 100644 index 0000000..4526bf4 --- /dev/null +++ b/testdata/unicode.golden @@ -0,0 +1 @@ +"☺" \ No newline at end of file diff --git a/testdata/unicode.input b/testdata/unicode.input new file mode 100644 index 0000000..d514a5d --- /dev/null +++ b/testdata/unicode.input @@ -0,0 +1 @@ +"\u263A" \ No newline at end of file diff --git a/testdata/unicode2.golden b/testdata/unicode2.golden new file mode 100644 index 0000000..4526bf4 --- /dev/null +++ b/testdata/unicode2.golden @@ -0,0 +1 @@ +"☺" \ No newline at end of file diff --git a/testdata/unicode2.input b/testdata/unicode2.input new file mode 100644 index 0000000..2402faf --- /dev/null +++ b/testdata/unicode2.input @@ -0,0 +1 @@ +"\u263a" \ No newline at end of file diff --git a/testdata/verbatim_string.golden b/testdata/verbatim_string.golden new file mode 100644 index 0000000..562bed5 --- /dev/null +++ b/testdata/verbatim_string.golden @@ -0,0 +1 @@ +"blah ☺" \ No newline at end of file diff --git a/testdata/verbatim_string.input b/testdata/verbatim_string.input new file mode 100644 index 0000000..ed3f2d6 --- /dev/null +++ b/testdata/verbatim_string.input @@ -0,0 +1 @@ +@"blah ☺" \ No newline at end of file