compliance_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package jmespath
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. type TestSuite struct {
  12. Given interface{}
  13. TestCases []TestCase `json:"cases"`
  14. Comment string
  15. }
  16. type TestCase struct {
  17. Comment string
  18. Expression string
  19. Result interface{}
  20. Error string
  21. }
  22. var whiteListed = []string{
  23. "compliance/basic.json",
  24. "compliance/current.json",
  25. "compliance/escape.json",
  26. "compliance/filters.json",
  27. "compliance/functions.json",
  28. "compliance/identifiers.json",
  29. "compliance/indices.json",
  30. "compliance/literal.json",
  31. "compliance/multiselect.json",
  32. "compliance/ormatch.json",
  33. "compliance/pipe.json",
  34. "compliance/slice.json",
  35. "compliance/syntax.json",
  36. "compliance/unicode.json",
  37. "compliance/wildcard.json",
  38. "compliance/boolean.json",
  39. }
  40. func allowed(path string) bool {
  41. for _, el := range whiteListed {
  42. if el == path {
  43. return true
  44. }
  45. }
  46. return false
  47. }
  48. func TestCompliance(t *testing.T) {
  49. assert := assert.New(t)
  50. var complianceFiles []string
  51. err := filepath.Walk("compliance", func(path string, _ os.FileInfo, _ error) error {
  52. //if strings.HasSuffix(path, ".json") {
  53. if allowed(path) {
  54. complianceFiles = append(complianceFiles, path)
  55. }
  56. return nil
  57. })
  58. if assert.Nil(err) {
  59. for _, filename := range complianceFiles {
  60. runComplianceTest(assert, filename)
  61. }
  62. }
  63. }
  64. func runComplianceTest(assert *assert.Assertions, filename string) {
  65. var testSuites []TestSuite
  66. data, err := ioutil.ReadFile(filename)
  67. if assert.Nil(err) {
  68. err := json.Unmarshal(data, &testSuites)
  69. if assert.Nil(err) {
  70. for _, testsuite := range testSuites {
  71. runTestSuite(assert, testsuite, filename)
  72. }
  73. }
  74. }
  75. }
  76. func runTestSuite(assert *assert.Assertions, testsuite TestSuite, filename string) {
  77. for _, testcase := range testsuite.TestCases {
  78. if testcase.Error != "" {
  79. // This is a test case that verifies we error out properly.
  80. runSyntaxTestCase(assert, testsuite.Given, testcase, filename)
  81. } else {
  82. runTestCase(assert, testsuite.Given, testcase, filename)
  83. }
  84. }
  85. }
  86. func runSyntaxTestCase(assert *assert.Assertions, given interface{}, testcase TestCase, filename string) {
  87. // Anything with an .Error means that we expect that JMESPath should return
  88. // an error when we try to evaluate the expression.
  89. _, err := Search(testcase.Expression, given)
  90. assert.NotNil(err, fmt.Sprintf("Expression: %s", testcase.Expression))
  91. }
  92. func runTestCase(assert *assert.Assertions, given interface{}, testcase TestCase, filename string) {
  93. lexer := NewLexer()
  94. var err error
  95. _, err = lexer.tokenize(testcase.Expression)
  96. if err != nil {
  97. errMsg := fmt.Sprintf("(%s) Could not lex expression: %s -- %s", filename, testcase.Expression, err.Error())
  98. assert.Fail(errMsg)
  99. return
  100. }
  101. parser := NewParser()
  102. _, err = parser.Parse(testcase.Expression)
  103. if err != nil {
  104. errMsg := fmt.Sprintf("(%s) Could not parse expression: %s -- %s", filename, testcase.Expression, err.Error())
  105. assert.Fail(errMsg)
  106. return
  107. }
  108. actual, err := Search(testcase.Expression, given)
  109. if assert.Nil(err, fmt.Sprintf("Expression: %s", testcase.Expression)) {
  110. assert.Equal(testcase.Result, actual, fmt.Sprintf("Expression: %s", testcase.Expression))
  111. }
  112. }