json_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. d := Directive{}
  28. SetEscapeToken(DefaultEscapeToken, &d)
  29. if node, _, err := parseJSON(json, &d); err != nil {
  30. t.Fatalf("%q should be a valid JSON array of strings, but wasn't! (err: %q)", json, err)
  31. } else {
  32. i := 0
  33. for node != nil {
  34. if i >= len(expected) {
  35. t.Fatalf("expected result is shorter than parsed result (%d vs %d+) in %q", len(expected), i+1, json)
  36. }
  37. if node.Value != expected[i] {
  38. t.Fatalf("expected %q (not %q) in %q at pos %d", expected[i], node.Value, json, i)
  39. }
  40. node = node.Next
  41. i++
  42. }
  43. if i != len(expected) {
  44. t.Fatalf("expected result is longer than parsed result (%d vs %d) in %q", len(expected), i+1, json)
  45. }
  46. }
  47. }
  48. for _, json := range invalidJSONArraysOfStrings {
  49. d := Directive{}
  50. SetEscapeToken(DefaultEscapeToken, &d)
  51. if _, _, err := parseJSON(json, &d); err != errDockerfileNotStringArray {
  52. t.Fatalf("%q should be an invalid JSON array of strings, but wasn't!", json)
  53. }
  54. }
  55. }