Skip to content

Commit 3381a3c

Browse files
committed
Initial commit of compliance test repo
This has extracted from the boto/jmespath repo and pulled into its own repo.
0 parents  commit 3381a3c

14 files changed

+4661
-0
lines changed

README.rst

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
JMESPath Compliance Tests
2+
=========================
3+
4+
This repo contains a suite of JMESPath compliance tests. JMESPath
5+
implementations can use these tests in order to verify their implementation
6+
adheres to the JMESPath spec.
7+
8+
Test Organization
9+
=================
10+
11+
The ``test/`` directory contains JSON files containing the JMESPath testcase.
12+
Each JSON file represents a JMESPath feature. Each JSON file is
13+
a JSON list containing one or more tests suites::
14+
15+
[
16+
<test suite 1>,
17+
<test suite 2>,
18+
]
19+
20+
Each test suite is a JSON object that has the following keys:
21+
22+
* ``given`` - The input data from which the JMESPath expression is evaluated.
23+
* ``cases`` - A list of test cases.
24+
* ``comment`` - An optional field containing a description of the test suite.
25+
26+
Each JMESPath test case can have the following keys:
27+
28+
* ``expression`` - The JMESPath expression being tested.
29+
* ``result`` - The expected result from evaluating the JMESPath expression
30+
against the ``given`` input.
31+
* ``error`` - The type of error that should be raised as a result of evaluating
32+
the JMESPath expression.
33+
* ``comment`` - An optional comment containing a description of the specific
34+
test case.
35+
36+
For each test case, either ``result`` or ``error`` must be specified. Both
37+
keys cannot be present in a single test case.
38+
39+
The error type (if the ``error`` key is present) indicates the type of error
40+
that an implementation should raise, but it does not indicate **when** this
41+
error should be raised. For example, a value of ``"error": "syntax"`` does not
42+
require that the syntax error be raised when the expression is compiled. If an
43+
implementation does not have a separate compilation step this won't even be
44+
possible. Similar for type errors, implementations are free to check for type
45+
errors during compilation or at run time (when the parsed expression is
46+
evaluated). As long as an implementation can detect that this error occured at
47+
any point during the evaluation of a JMESPath expression, this is considered
48+
sufficient.
49+
50+
Below are a few examples::
51+
52+
[{
53+
"given":
54+
{"foo": {"bar": {"baz": "correct"}}},
55+
"cases": [
56+
{
57+
"expression": "foo",
58+
"result": {"bar": {"baz": "correct"}}
59+
},
60+
{
61+
"expression": "foo.1",
62+
"error": "syntax"
63+
},
64+
]
65+
}]
66+
67+
This above JSON document specifies 1 test suite that contains 2 test cases.
68+
The two test cases are:
69+
70+
* Given the input ``{"foo": {"bar": {"baz": "correct"}}}``, the expression
71+
``foo`` should have a result of ``{"bar": {"baz": "correct"}}``.
72+
* Given the input ``{"foo": {"bar": {"baz": "correct"}}}``, the expression
73+
``foo.1`` should generate a syntax error.
74+
75+
76+
Utility Tools
77+
=============
78+
79+
Most languages have test frameworks that are capable of reading the JSON test
80+
descriptions and generating testcases. However, a ``jp-compliance`` tool is
81+
provided to help with any implementation that does not have an available test
82+
framework to generate test cases. The ``jp-compliance`` tool takes the name of
83+
a jmespath executable and will evaluate all the compliance tests using this
84+
provided executable. This way all that's needed to verify your JMESPath
85+
implementation is for you to write a basic exectuable. This executable must
86+
have the following interface:
87+
88+
* Accept the input JSON data on stdin.
89+
* Accept the jmespath expression as an argument.
90+
* Print the jmespath result as JSON on stdout.
91+
* If an error occurred, it must write the error name to sys.stderr. The error
92+
name must be the first thing written to stderr, but it is free to write
93+
information after the error name. For example "syntax: Expression did not
94+
compile 'foo.bar.'" would be a valid message to indicate that a syntax error
95+
occurred because it starts with "syntax".
96+
97+
.. note::
98+
99+
This will be substantially slower than using a test framework. Using
100+
``jp-compliance`` each test case is evaluated by executing a new process.
101+
102+
You can run the ``bin/jp-compliance --help`` for more information and for
103+
examples on how to use this tool.

tests/basic.json

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
[{
2+
"given":
3+
{"foo": {"bar": {"baz": "correct"}}},
4+
"cases": [
5+
{
6+
"expression": "foo",
7+
"result": {"bar": {"baz": "correct"}}
8+
},
9+
{
10+
"expression": "foo.bar",
11+
"result": {"baz": "correct"}
12+
},
13+
{
14+
"expression": "foo.bar.baz",
15+
"result": "correct"
16+
},
17+
{
18+
"expression": "foo.bar.baz.bad",
19+
"result": null
20+
},
21+
{
22+
"expression": "foo.bar.bad",
23+
"result": null
24+
},
25+
{
26+
"expression": "foo.bad",
27+
"result": null
28+
},
29+
{
30+
"expression": "bad",
31+
"result": null
32+
},
33+
{
34+
"expression": "bad.morebad.morebad",
35+
"result": null
36+
}
37+
]
38+
},
39+
{
40+
"given":
41+
{"foo": {"bar": ["one", "two", "three"]}},
42+
"cases": [
43+
{
44+
"expression": "foo",
45+
"result": {"bar": ["one", "two", "three"]}
46+
},
47+
{
48+
"expression": "foo.bar",
49+
"result": ["one", "two", "three"]
50+
}
51+
]
52+
},
53+
{
54+
"given": ["one", "two", "three"],
55+
"cases": [
56+
{
57+
"expression": "one",
58+
"result": null
59+
},
60+
{
61+
"expression": "two",
62+
"result": null
63+
},
64+
{
65+
"expression": "three",
66+
"result": null
67+
},
68+
{
69+
"expression": "one.two",
70+
"result": null
71+
}
72+
]
73+
},
74+
{
75+
"given":
76+
{"foo": {"1": ["one", "two", "three"], "-1": "bar"}},
77+
"cases": [
78+
{
79+
"expression": "foo.\"1\"",
80+
"result": ["one", "two", "three"]
81+
},
82+
{
83+
"expression": "foo.\"1\"[0]",
84+
"result": "one"
85+
},
86+
{
87+
"expression": "foo.\"-1\"",
88+
"result": "bar"
89+
}
90+
]
91+
}
92+
]

tests/escape.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
[{
2+
"given": {
3+
"foo.bar": "dot",
4+
"foo bar": "space",
5+
"foo\nbar": "newline",
6+
"foo\"bar": "doublequote",
7+
"c:\\\\windows\\path": "windows",
8+
"/unix/path": "unix",
9+
"\"\"\"": "threequotes",
10+
"bar": {"baz": "qux"}
11+
},
12+
"cases": [
13+
{
14+
"expression": "\"foo.bar\"",
15+
"result": "dot"
16+
},
17+
{
18+
"expression": "\"foo bar\"",
19+
"result": "space"
20+
},
21+
{
22+
"expression": "\"foo\\nbar\"",
23+
"result": "newline"
24+
},
25+
{
26+
"expression": "\"foo\\\"bar\"",
27+
"result": "doublequote"
28+
},
29+
{
30+
"expression": "\"c:\\\\\\\\windows\\\\path\"",
31+
"result": "windows"
32+
},
33+
{
34+
"expression": "\"/unix/path\"",
35+
"result": "unix"
36+
},
37+
{
38+
"expression": "\"\\\"\\\"\\\"\"",
39+
"result": "threequotes"
40+
},
41+
{
42+
"expression": "\"bar\".\"baz\"",
43+
"result": "qux"
44+
}
45+
]
46+
}]

0 commit comments

Comments
 (0)