json_test.go 1.5 KB

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