parser_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package jsonpath
  14. import (
  15. "testing"
  16. )
  17. type parserTest struct {
  18. name string
  19. text string
  20. nodes []Node
  21. shouldError bool
  22. }
  23. var parserTests = []parserTest{
  24. {"plain", `hello jsonpath`, []Node{newText("hello jsonpath")}, false},
  25. {"variable", `hello {.jsonpath}`,
  26. []Node{newText("hello "), newList(), newField("jsonpath")}, false},
  27. {"arrayfiled", `hello {['jsonpath']}`,
  28. []Node{newText("hello "), newList(), newField("jsonpath")}, false},
  29. {"quote", `{"{"}`, []Node{newList(), newText("{")}, false},
  30. {"array", `{[1:3]}`, []Node{newList(),
  31. newArray([3]ParamsEntry{{1, true}, {3, true}, {0, false}})}, false},
  32. {"allarray", `{.book[*].author}`,
  33. []Node{newList(), newField("book"),
  34. newArray([3]ParamsEntry{{0, false}, {0, false}, {0, false}}), newField("author")}, false},
  35. {"wildcard", `{.bicycle.*}`,
  36. []Node{newList(), newField("bicycle"), newWildcard()}, false},
  37. {"filter", `{[?(@.price<3)]}`,
  38. []Node{newList(), newFilter(newList(), newList(), "<"),
  39. newList(), newField("price"), newList(), newInt(3)}, false},
  40. {"recursive", `{..}`, []Node{newList(), newRecursive()}, false},
  41. {"recurField", `{..price}`,
  42. []Node{newList(), newRecursive(), newField("price")}, false},
  43. {"arraydict", `{['book.price']}`, []Node{newList(),
  44. newField("book"), newField("price"),
  45. }, false},
  46. {"union", `{['bicycle.price', 3, 'book.price']}`, []Node{newList(), newUnion([]*ListNode{}),
  47. newList(), newField("bicycle"), newField("price"),
  48. newList(), newArray([3]ParamsEntry{{3, true}, {4, true}, {0, false}}),
  49. newList(), newField("book"), newField("price"),
  50. }, false},
  51. {"range", `{range .items}{.name},{end}`, []Node{
  52. newList(), newIdentifier("range"), newField("items"),
  53. newList(), newField("name"), newText(","),
  54. newList(), newIdentifier("end"),
  55. }, false},
  56. {"malformat input", `{\\\}`, []Node{}, true},
  57. }
  58. func collectNode(nodes []Node, cur Node) []Node {
  59. nodes = append(nodes, cur)
  60. switch cur.Type() {
  61. case NodeList:
  62. for _, node := range cur.(*ListNode).Nodes {
  63. nodes = collectNode(nodes, node)
  64. }
  65. case NodeFilter:
  66. nodes = collectNode(nodes, cur.(*FilterNode).Left)
  67. nodes = collectNode(nodes, cur.(*FilterNode).Right)
  68. case NodeUnion:
  69. for _, node := range cur.(*UnionNode).Nodes {
  70. nodes = collectNode(nodes, node)
  71. }
  72. }
  73. return nodes
  74. }
  75. func TestParser(t *testing.T) {
  76. for _, test := range parserTests {
  77. parser, err := Parse(test.name, test.text)
  78. if test.shouldError {
  79. if err == nil {
  80. t.Errorf("unexpected non-error when parsing %s", test.name)
  81. }
  82. continue
  83. }
  84. if err != nil {
  85. t.Errorf("parse %s error %v", test.name, err)
  86. }
  87. result := collectNode([]Node{}, parser.Root)[1:]
  88. if len(result) != len(test.nodes) {
  89. t.Errorf("in %s, expect to get %d nodes, got %d nodes", test.name, len(test.nodes), len(result))
  90. t.Error(result)
  91. }
  92. for i, expect := range test.nodes {
  93. if result[i].String() != expect.String() {
  94. t.Errorf("in %s, %dth node, expect %v, got %v", test.name, i, expect, result[i])
  95. }
  96. }
  97. }
  98. }
  99. type failParserTest struct {
  100. name string
  101. text string
  102. err string
  103. }
  104. func TestFailParser(t *testing.T) {
  105. failParserTests := []failParserTest{
  106. {"unclosed action", "{.hello", "unclosed action"},
  107. {"unrecognized character", "{*}", "unrecognized character in action: U+002A '*'"},
  108. {"invalid number", "{+12.3.0}", "cannot parse number +12.3.0"},
  109. {"unterminated array", "{[1}", "unterminated array"},
  110. {"invalid index", "{[::-1]}", "invalid array index ::-1"},
  111. {"unterminated filter", "{[?(.price]}", "unterminated filter"},
  112. }
  113. for _, test := range failParserTests {
  114. _, err := Parse(test.name, test.text)
  115. var out string
  116. if err == nil {
  117. out = "nil"
  118. } else {
  119. out = err.Error()
  120. }
  121. if out != test.err {
  122. t.Errorf("in %s, expect to get error %v, got %v", test.name, test.err, out)
  123. }
  124. }
  125. }