Fix query test

- replaced  assertArrayContainsInAnyOrder with a version which tests the exact order for more strict testing
This commit is contained in:
x-hgg-x
2020-05-17 14:42:51 +02:00
committed by GitHub
parent 8784f9c73a
commit f99d6bbca1
+6 -13
View File
@@ -7,21 +7,14 @@ import (
"github.com/pelletier/go-toml" "github.com/pelletier/go-toml"
) )
func assertArrayContainsInAnyOrder(t *testing.T, array []interface{}, objects ...interface{}) { func assertArrayContainsInOrder(t *testing.T, array []interface{}, objects ...interface{}) {
if len(array) != len(objects) { if len(array) != len(objects) {
t.Fatalf("array contains %d objects but %d are expected", len(array), len(objects)) t.Fatalf("array contains %d objects but %d are expected", len(array), len(objects))
} }
for _, o := range objects { for i := 0; i < len(array); i++ {
found := false if array[i] != objects[i] {
for _, a := range array { t.Fatalf("wanted '%s', have '%s'", objects[i], array[i])
if a == o {
found = true
break
}
}
if !found {
t.Fatal(o, "not found in array", array)
} }
} }
} }
@@ -31,7 +24,7 @@ func checkQuery(t *testing.T, tree *toml.Tree, query string, objects ...interfac
if err != nil { if err != nil {
t.Fatal("unexpected error:", err) t.Fatal("unexpected error:", err)
} }
assertArrayContainsInAnyOrder(t, results.Values(), objects...) assertArrayContainsInOrder(t, results.Values(), objects...)
} }
func TestQueryExample(t *testing.T) { func TestQueryExample(t *testing.T) {
@@ -53,7 +46,7 @@ func TestQueryExample(t *testing.T) {
checkQuery(t, config, "$.book[-1].author", "William Gibson") checkQuery(t, config, "$.book[-1].author", "William Gibson")
checkQuery(t, config, "$.book[1:].author", "Ernest Hemmingway", "William Gibson") checkQuery(t, config, "$.book[1:].author", "Ernest Hemmingway", "William Gibson")
checkQuery(t, config, "$.book[-1:].author", "William Gibson") checkQuery(t, config, "$.book[-1:].author", "William Gibson")
checkQuery(t, config, "$.book[::2].author", "William Gibson", "Stephen King") checkQuery(t, config, "$.book[::2].author", "Stephen King", "William Gibson")
checkQuery(t, config, "$.book[::-1].author", "William Gibson", "Ernest Hemmingway", "Stephen King") checkQuery(t, config, "$.book[::-1].author", "William Gibson", "Ernest Hemmingway", "Stephen King")
checkQuery(t, config, "$.book[:].author", "Stephen King", "Ernest Hemmingway", "William Gibson") checkQuery(t, config, "$.book[:].author", "Stephen King", "Ernest Hemmingway", "William Gibson")
checkQuery(t, config, "$.book[::].author", "Stephen King", "Ernest Hemmingway", "William Gibson") checkQuery(t, config, "$.book[::].author", "Stephen King", "Ernest Hemmingway", "William Gibson")