2018-02-05 16:05:59 -05:00
|
|
|
package parser // import "github.com/docker/docker/builder/dockerfile/parser"
|
2015-01-03 00:40:43 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var invalidJSONArraysOfStrings = []string{
|
|
|
|
`["a",42,"b"]`,
|
|
|
|
`["a",123.456,"b"]`,
|
|
|
|
`["a",{},"b"]`,
|
|
|
|
`["a",{"c": "d"},"b"]`,
|
|
|
|
`["a",["c"],"b"]`,
|
|
|
|
`["a",true,"b"]`,
|
|
|
|
`["a",false,"b"]`,
|
|
|
|
`["a",null,"b"]`,
|
|
|
|
}
|
|
|
|
|
|
|
|
var validJSONArraysOfStrings = map[string][]string{
|
|
|
|
`[]`: {},
|
|
|
|
`[""]`: {""},
|
|
|
|
`["a"]`: {"a"},
|
|
|
|
`["a","b"]`: {"a", "b"},
|
|
|
|
`[ "a", "b" ]`: {"a", "b"},
|
|
|
|
`[ "a", "b" ]`: {"a", "b"},
|
|
|
|
` [ "a", "b" ] `: {"a", "b"},
|
|
|
|
`["abc 123", "♥", "☃", "\" \\ \/ \b \f \n \r \t \u0000"]`: {"abc 123", "♥", "☃", "\" \\ / \b \f \n \r \t \u0000"},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJSONArraysOfStrings(t *testing.T) {
|
|
|
|
for json, expected := range validJSONArraysOfStrings {
|
2017-04-12 13:47:19 -04:00
|
|
|
d := NewDefaultDirective()
|
2016-06-27 16:20:47 -04:00
|
|
|
|
2017-04-12 13:47:19 -04:00
|
|
|
if node, _, err := parseJSON(json, d); err != nil {
|
2015-01-03 00:40:43 -05:00
|
|
|
t.Fatalf("%q should be a valid JSON array of strings, but wasn't! (err: %q)", json, err)
|
|
|
|
} else {
|
|
|
|
i := 0
|
|
|
|
for node != nil {
|
|
|
|
if i >= len(expected) {
|
|
|
|
t.Fatalf("expected result is shorter than parsed result (%d vs %d+) in %q", len(expected), i+1, json)
|
|
|
|
}
|
|
|
|
if node.Value != expected[i] {
|
|
|
|
t.Fatalf("expected %q (not %q) in %q at pos %d", expected[i], node.Value, json, i)
|
|
|
|
}
|
|
|
|
node = node.Next
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
if i != len(expected) {
|
|
|
|
t.Fatalf("expected result is longer than parsed result (%d vs %d) in %q", len(expected), i+1, json)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, json := range invalidJSONArraysOfStrings {
|
2017-04-12 13:47:19 -04:00
|
|
|
d := NewDefaultDirective()
|
2016-06-27 16:20:47 -04:00
|
|
|
|
2017-04-12 13:47:19 -04:00
|
|
|
if _, _, err := parseJSON(json, d); err != errDockerfileNotStringArray {
|
2015-01-03 00:40:43 -05:00
|
|
|
t.Fatalf("%q should be an invalid JSON array of strings, but wasn't!", json)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|