github.com/sams1990/dockerrepo@v17.12.1-ce-rc2+incompatible/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  		d := NewDefaultDirective()
    32  
    33  		if node, _, err := parseJSON(json, d); err != nil {
    34  			t.Fatalf("%q should be a valid JSON array of strings, but wasn't! (err: %q)", json, err)
    35  		} else {
    36  			i := 0
    37  			for node != nil {
    38  				if i >= len(expected) {
    39  					t.Fatalf("expected result is shorter than parsed result (%d vs %d+) in %q", len(expected), i+1, json)
    40  				}
    41  				if node.Value != expected[i] {
    42  					t.Fatalf("expected %q (not %q) in %q at pos %d", expected[i], node.Value, json, i)
    43  				}
    44  				node = node.Next
    45  				i++
    46  			}
    47  			if i != len(expected) {
    48  				t.Fatalf("expected result is longer than parsed result (%d vs %d) in %q", len(expected), i+1, json)
    49  			}
    50  		}
    51  	}
    52  	for _, json := range invalidJSONArraysOfStrings {
    53  		d := NewDefaultDirective()
    54  
    55  		if _, _, err := parseJSON(json, d); err != errDockerfileNotStringArray {
    56  			t.Fatalf("%q should be an invalid JSON array of strings, but wasn't!", json)
    57  		}
    58  	}
    59  }