github.com/tompao/docker@v1.9.1/builder/dockerfile/parser/json_test.go (about) 1 package parser 2 3 import ( 4 "testing" 5 ) 6 7 var invalidJSONArraysOfStrings = []string{ 8 `["a",42,"b"]`, 9 `["a",123.456,"b"]`, 10 `["a",{},"b"]`, 11 `["a",{"c": "d"},"b"]`, 12 `["a",["c"],"b"]`, 13 `["a",true,"b"]`, 14 `["a",false,"b"]`, 15 `["a",null,"b"]`, 16 } 17 18 var validJSONArraysOfStrings = map[string][]string{ 19 `[]`: {}, 20 `[""]`: {""}, 21 `["a"]`: {"a"}, 22 `["a","b"]`: {"a", "b"}, 23 `[ "a", "b" ]`: {"a", "b"}, 24 `[ "a", "b" ]`: {"a", "b"}, 25 ` [ "a", "b" ] `: {"a", "b"}, 26 `["abc 123", "♥", "☃", "\" \\ \/ \b \f \n \r \t \u0000"]`: {"abc 123", "♥", "☃", "\" \\ / \b \f \n \r \t \u0000"}, 27 } 28 29 func TestJSONArraysOfStrings(t *testing.T) { 30 for json, expected := range validJSONArraysOfStrings { 31 if node, _, err := parseJSON(json); err != nil { 32 t.Fatalf("%q should be a valid JSON array of strings, but wasn't! (err: %q)", json, err) 33 } else { 34 i := 0 35 for node != nil { 36 if i >= len(expected) { 37 t.Fatalf("expected result is shorter than parsed result (%d vs %d+) in %q", len(expected), i+1, json) 38 } 39 if node.Value != expected[i] { 40 t.Fatalf("expected %q (not %q) in %q at pos %d", expected[i], node.Value, json, i) 41 } 42 node = node.Next 43 i++ 44 } 45 if i != len(expected) { 46 t.Fatalf("expected result is longer than parsed result (%d vs %d) in %q", len(expected), i+1, json) 47 } 48 } 49 } 50 for _, json := range invalidJSONArraysOfStrings { 51 if _, _, err := parseJSON(json); err != errDockerfileNotStringArray { 52 t.Fatalf("%q should be an invalid JSON array of strings, but wasn't!", json) 53 } 54 } 55 }