|
| 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. |
0 commit comments