parser_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package jmespath
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. var parsingErrorTests = []struct {
  8. expression string
  9. msg string
  10. }{
  11. {"foo.", "Incopmlete expression"},
  12. {"[foo", "Incopmlete expression"},
  13. {"]", "Invalid"},
  14. {")", "Invalid"},
  15. {"}", "Invalid"},
  16. {"foo..bar", "Invalid"},
  17. {`foo."bar`, "Forwards lexer errors"},
  18. {`{foo: bar`, "Incomplete expression"},
  19. {`{foo bar}`, "Invalid"},
  20. {`[foo bar]`, "Invalid"},
  21. {`foo@`, "Invalid"},
  22. {`&&&&&&&&&&&&t(`, "Invalid"},
  23. {`[*][`, "Invalid"},
  24. }
  25. func TestParsingErrors(t *testing.T) {
  26. assert := assert.New(t)
  27. parser := NewParser()
  28. for _, tt := range parsingErrorTests {
  29. _, err := parser.Parse(tt.expression)
  30. assert.NotNil(err, fmt.Sprintf("Expected parsing error: %s, for expression: %s", tt.msg, tt.expression))
  31. }
  32. }
  33. var prettyPrinted = `ASTProjection {
  34. children: {
  35. ASTField {
  36. value: "foo"
  37. }
  38. ASTSubexpression {
  39. children: {
  40. ASTSubexpression {
  41. children: {
  42. ASTField {
  43. value: "bar"
  44. }
  45. ASTField {
  46. value: "baz"
  47. }
  48. }
  49. ASTField {
  50. value: "qux"
  51. }
  52. }
  53. }
  54. `
  55. var prettyPrintedCompNode = `ASTFilterProjection {
  56. children: {
  57. ASTField {
  58. value: "a"
  59. }
  60. ASTIdentity {
  61. }
  62. ASTComparator {
  63. value: tLTE
  64. children: {
  65. ASTField {
  66. value: "b"
  67. }
  68. ASTField {
  69. value: "c"
  70. }
  71. }
  72. }
  73. `
  74. func TestPrettyPrintedAST(t *testing.T) {
  75. assert := assert.New(t)
  76. parser := NewParser()
  77. parsed, _ := parser.Parse("foo[*].bar.baz.qux")
  78. assert.Equal(parsed.PrettyPrint(0), prettyPrinted)
  79. }
  80. func TestPrettyPrintedCompNode(t *testing.T) {
  81. assert := assert.New(t)
  82. parser := NewParser()
  83. parsed, _ := parser.Parse("a[?b<=c]")
  84. assert.Equal(parsed.PrettyPrint(0), prettyPrintedCompNode)
  85. }
  86. func BenchmarkParseIdentifier(b *testing.B) {
  87. runParseBenchmark(b, exprIdentifier)
  88. }
  89. func BenchmarkParseSubexpression(b *testing.B) {
  90. runParseBenchmark(b, exprSubexpr)
  91. }
  92. func BenchmarkParseDeeplyNested50(b *testing.B) {
  93. runParseBenchmark(b, deeplyNested50)
  94. }
  95. func BenchmarkParseDeepNested50Pipe(b *testing.B) {
  96. runParseBenchmark(b, deeplyNested50Pipe)
  97. }
  98. func BenchmarkParseDeepNested50Index(b *testing.B) {
  99. runParseBenchmark(b, deeplyNested50Index)
  100. }
  101. func BenchmarkParseQuotedIdentifier(b *testing.B) {
  102. runParseBenchmark(b, exprQuotedIdentifier)
  103. }
  104. func BenchmarkParseQuotedIdentifierEscapes(b *testing.B) {
  105. runParseBenchmark(b, quotedIdentifierEscapes)
  106. }
  107. func BenchmarkParseRawStringLiteral(b *testing.B) {
  108. runParseBenchmark(b, rawStringLiteral)
  109. }
  110. func BenchmarkParseDeepProjection104(b *testing.B) {
  111. runParseBenchmark(b, deepProjection104)
  112. }
  113. func runParseBenchmark(b *testing.B, expression string) {
  114. parser := NewParser()
  115. for i := 0; i < b.N; i++ {
  116. parser.Parse(expression)
  117. }
  118. }