forked from jmespath-community/go-jmespath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jp_test.go
120 lines (106 loc) · 3.05 KB
/
jp_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package jmespath
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/kyverno/go-community-jmespath/pkg/parsing"
"github.com/stretchr/testify/assert"
)
type TestSuite struct {
Given interface{}
TestCases []TestCase `json:"cases"`
Comment string
}
type TestCase struct {
Comment string
Expression string
Result interface{}
Error string
}
var excludeList = []string{
"legacy/legacy-literal.json",
"benchmarks.json",
}
func excluded(path string) bool {
for _, el := range excludeList {
if strings.HasSuffix(path, el) {
return true
}
}
return false
}
func TestCompliance(t *testing.T) {
assert := assert.New(t)
var complianceFiles []string
err := filepath.Walk("compliance/tests", func(path string, _ os.FileInfo, _ error) error {
if strings.HasSuffix(path, ".json") && !excluded(path) {
complianceFiles = append(complianceFiles, path)
}
return nil
})
if assert.Nil(err) {
for _, filename := range complianceFiles {
runComplianceTest(assert, filename)
}
}
}
func runComplianceTest(assert *assert.Assertions, filename string) {
var testSuites []TestSuite
data, err := os.ReadFile(filename)
if assert.Nil(err) {
err := json.Unmarshal(data, &testSuites)
if assert.Nil(err) {
for _, testsuite := range testSuites {
runTestSuite(assert, testsuite, filename)
}
}
}
}
func runTestSuite(assert *assert.Assertions, testsuite TestSuite, filename string) {
for _, testcase := range testsuite.TestCases {
if testcase.Error != "" {
// This is a test case that verifies we error out properly.
runSyntaxTestCase(assert, testsuite.Given, testcase, filename)
} else {
runTestCase(assert, testsuite.Given, testcase, filename)
}
}
}
func runSyntaxTestCase(assert *assert.Assertions, given interface{}, testcase TestCase, filename string) {
// Anything with an .Error means that we expect that JMESPath should return
// an error when we try to evaluate the expression.
// fmt.Println(fmt.Sprintf("%s: %s", filename, testcase.Expression))
_, err := Search(testcase.Expression, given)
if _, ok := err.(NotFoundError); ok {
err = nil
}
assert.NotNil(err, fmt.Sprintf("Expression: %s", testcase.Expression))
}
func runTestCase(assert *assert.Assertions, given interface{}, testcase TestCase, filename string) {
lexer := parsing.NewLexer()
var err error
_, err = lexer.Tokenize(testcase.Expression)
if err != nil {
errMsg := fmt.Sprintf("(%s) Could not lex expression: %s -- %s", filename, testcase.Expression, err.Error())
assert.Fail(errMsg)
return
}
parser := parsing.NewParser()
_, err = parser.Parse(testcase.Expression)
if err != nil {
errMsg := fmt.Sprintf("(%s) Could not parse expression: %s -- %s", filename, testcase.Expression, err.Error())
assert.Fail(errMsg)
return
}
actual, err := Search(testcase.Expression, given)
if _, ok := err.(NotFoundError); ok {
err = nil
actual = nil
}
if assert.Nil(err, fmt.Sprintf("Expression: %s", testcase.Expression)) {
assert.Equal(testcase.Result, actual, fmt.Sprintf("Expression: %s", testcase.Expression))
}
}