diff options
author | Petr Štetiar <ynezz@true.cz> | 2019-11-19 16:31:44 +0300 |
---|---|---|
committer | Petr Štetiar <ynezz@true.cz> | 2019-11-24 15:26:58 +0300 |
commit | b0a5cd8a28bf1d1883317ceac6cb8967d840d6ae (patch) | |
tree | 8600f4d551a16671af14f1d086014b111316ea10 /tests/cram | |
parent | 1fefb7c4d7f90464940143c93e1b98f44ecf6590 (diff) |
add cram based unit tests
For improved QA etc. For the start with initial test cases for avl,
base64, jshn and list components. Moved runqueue and blobmsg from
examples to tests. Converted just a few first test cases from
json-script example into the new cram based unit test, more to come.
Signed-off-by: Petr Štetiar <ynezz@true.cz>
Diffstat (limited to 'tests/cram')
-rw-r--r-- | tests/cram/CMakeLists.txt | 22 | ||||
-rw-r--r-- | tests/cram/inputs/json-script.json | 38 | ||||
-rw-r--r-- | tests/cram/test_avl.t | 11 | ||||
-rw-r--r-- | tests/cram/test_base64.t | 21 | ||||
-rw-r--r-- | tests/cram/test_blobmsg.t | 17 | ||||
-rw-r--r-- | tests/cram/test_jshn.t | 25 | ||||
-rw-r--r-- | tests/cram/test_json_script.t | 96 | ||||
-rw-r--r-- | tests/cram/test_list.t | 22 | ||||
-rw-r--r-- | tests/cram/test_runqueue.t | 14 |
9 files changed, 266 insertions, 0 deletions
diff --git a/tests/cram/CMakeLists.txt b/tests/cram/CMakeLists.txt new file mode 100644 index 0000000..bebd821 --- /dev/null +++ b/tests/cram/CMakeLists.txt @@ -0,0 +1,22 @@ +FIND_PACKAGE(PythonInterp 3 REQUIRED) +FILE(GLOB test_cases "test_*.t") + +SET(PYTHON_VENV_DIR "${CMAKE_CURRENT_BINARY_DIR}/.venv") +SET(PYTHON_VENV_PIP "${PYTHON_VENV_DIR}/bin/pip") +SET(PYTHON_VENV_CRAM "${PYTHON_VENV_DIR}/bin/cram") + +ADD_CUSTOM_COMMAND( + OUTPUT ${PYTHON_VENV_CRAM} + COMMAND ${PYTHON_EXECUTABLE} -m venv ${PYTHON_VENV_DIR} + COMMAND ${PYTHON_VENV_PIP} install cram +) +ADD_CUSTOM_TARGET(prepare-cram-venv ALL DEPENDS ${PYTHON_VENV_CRAM}) + +ADD_TEST( + NAME cram + COMMAND ${PYTHON_VENV_CRAM} ${test_cases} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} +) + +SET_PROPERTY(TEST cram APPEND PROPERTY ENVIRONMENT "JSHN=$<TARGET_FILE:jshn>") +SET_PROPERTY(TEST cram APPEND PROPERTY ENVIRONMENT "TEST_BIN_DIR=$<TARGET_FILE_DIR:test-avl>") diff --git a/tests/cram/inputs/json-script.json b/tests/cram/inputs/json-script.json new file mode 100644 index 0000000..5328e59 --- /dev/null +++ b/tests/cram/inputs/json-script.json @@ -0,0 +1,38 @@ +[ + [ "exec", "%EXECVAR%", "/%%/" ], + [ "if", + [ "eq", "EQVAR", "eqval" ], + [ "exec_if", "%VAR%", "%%", "jk" ] + ], + [ "case", "CASEVAR", { + "caseval0": ["cmd_case_0", "cmd_case_arg0", "case_cmd_arg1"], + "caseval1": ["cmd_case_1", "cmd_case_arg0", "case_cmd_arg1"] + } ], + + [ "if", + [ "and", [ "eq", "EQVAR", "eqval" ], + [ "has", "HASVAR" ], + [ "regex", "REGEXVAR0", "regexval" ], + [ "regex", "REGEXVAR1", [ "regexval10", "regexval11" ] ], + [ "not", [ "eq", "NOTEQVAR", "noteqval" ] ] ], + [ "exec_if_and", "%ANDVAR%" ] + ], + + [ "if", + [ "or", [ "eq", "EQVAR", "eqval" ], + [ "has", "HASVAR" ], + [ "regex", "REGEXVAR0", "regexval" ], + [ "regex", "REGEXVAR1", [ "regexval10", "regexval11" ] ], + [ "not", [ "eq", "NOTEQVAR", "noteqval" ] ] ], + [ "exec_if_or", "%ORVAR%" ] + ], + + [ "if", + [ "isdir", "%ISDIRVAR%" ], + [ "exec_isdir", "%ISDIRVAR%" ] + ], + + [ "return", "foobar" ], + + [ "exec_non_reachable", "Arghhh" ] +] diff --git a/tests/cram/test_avl.t b/tests/cram/test_avl.t new file mode 100644 index 0000000..19a8d21 --- /dev/null +++ b/tests/cram/test_avl.t @@ -0,0 +1,11 @@ +check that avl is producing expected results: + + $ [ -n "$TEST_BIN_DIR" ] && export PATH="$TEST_BIN_DIR:$PATH" + $ valgrind --quiet --leak-check=full test-avl + test_basics: insert: 0=zero 0=one 0=two 0=three 0=four 0=five 0=six 0=seven 0=eight 0=nine 0=ten 0=eleven 0=twelve + test_basics: insert duplicate: -1=zero -1=one -1=two -1=three -1=four -1=five -1=six -1=seven -1=eight -1=nine -1=ten -1=eleven -1=twelve + test_basics: first=eight last=zero + test_basics: for each element: eight eleven five four nine one seven six ten three twelve two zero + test_basics: delete 'one' element + test_basics: for each element reverse: zero two twelve three ten six seven nine four five eleven eight + test_basics: delete all elements diff --git a/tests/cram/test_base64.t b/tests/cram/test_base64.t new file mode 100644 index 0000000..4f8809f --- /dev/null +++ b/tests/cram/test_base64.t @@ -0,0 +1,21 @@ +set test bin path: + + $ [ -n "$TEST_BIN_DIR" ] && export PATH="$TEST_BIN_DIR:$PATH" + +check that base64 is producing expected results: + + $ valgrind --quiet --leak-check=full test-b64 + 0 + 4 Zg== + 4 Zm8= + 4 Zm9v + 8 Zm9vYg== + 8 Zm9vYmE= + 8 Zm9vYmFy + 0 + 1 f + 2 fo + 3 foo + 4 foob + 5 fooba + 6 foobar diff --git a/tests/cram/test_blobmsg.t b/tests/cram/test_blobmsg.t new file mode 100644 index 0000000..504a056 --- /dev/null +++ b/tests/cram/test_blobmsg.t @@ -0,0 +1,17 @@ +check that blobmsg is producing expected results: + + $ [ -n "$TEST_BIN_DIR" ] && export PATH="$TEST_BIN_DIR:$PATH" + $ valgrind --quiet --leak-check=full test-blobmsg + Message: Hello, world! + List: { + 0 + 1 + 2 + 133.700000 + } + Testdata: { + \tdouble : 133.700000 (esc) + \thello : 1 (esc) + \tworld : 2 (esc) + } + json: {"message":"Hello, world!","testdata":{"double":133.700000,"hello":1,"world":"2"},"list":[0,1,2,133.700000]} diff --git a/tests/cram/test_jshn.t b/tests/cram/test_jshn.t new file mode 100644 index 0000000..d228f0e --- /dev/null +++ b/tests/cram/test_jshn.t @@ -0,0 +1,25 @@ +set jshn for convenience: + + $ [ -n "$JSHN" ] && export PATH="$(dirname "$JSHN"):$PATH" + $ alias jshn="valgrind --quiet --leak-check=full jshn" + +check usage: + + $ jshn + Usage: jshn [-n] [-i] -r <message>|-R <file>|-w + [2] + +test bad json: + + $ jshn -r '[]' + Failed to parse message data + [1] + +test good json: + + $ jshn -r '{"foo": "bar", "baz": {"next": "meep"}}' + json_init; + json_add_string 'foo' 'bar'; + json_add_object 'baz'; + json_add_string 'next' 'meep'; + json_close_object; diff --git a/tests/cram/test_json_script.t b/tests/cram/test_json_script.t new file mode 100644 index 0000000..3e80a5c --- /dev/null +++ b/tests/cram/test_json_script.t @@ -0,0 +1,96 @@ +set test bin path: + + $ [ -n "$TEST_BIN_DIR" ] && export PATH="$TEST_BIN_DIR:$PATH" + $ export TEST_INPUTS="$TESTDIR/inputs" + $ alias js="valgrind --quiet --leak-check=full test-json-script" + +check that json-script is producing expected results: + + $ js + Usage: test-json-script [VARNAME=value] <filename_json_script> + [254] + + $ echo '}' > test.json; js test.json + load JSON data from test.json failed. + + $ js nada.json 2>&1 | grep load.*failed + load JSON data from nada.json failed. + + $ echo '[ [ ] [ ] ]' > test.json; js test.json + load JSON data from test.json failed. + +check example json-script: + + $ js $TEST_INPUTS/json-script.json + exec /%/ + exec_if_or + + $ js EXECVAR=meh ORVAR=meep $TEST_INPUTS/json-script.json + exec meh /%/ + exec_if_or meep + +check has expression: + + $ echo ' + > [ + > [ "if", + > [ "has", "VAR" ], + > [ "echo", "bar" ], + > [ "echo", "baz" ] + > ] + > ]' > test.json + + $ js VAR=foo test.json + echo bar + + $ js VAR=bar test.json + echo bar + + $ js test.json + echo baz + +check eq expression: + + $ echo ' + > [ + > [ "if", + > [ "eq", "VAR", "bar" ], + > [ "echo", "foo" ], + > [ "echo", "baz" ] + > ] + > ]' > test.json + + $ js VAR=bar test.json + echo foo + + $ js VAR=xxx test.json + echo baz + + $ js test.json + echo baz + +check regex single expression: + + $ echo ' + > [ + > [ "if", + > [ "regex", "VAR", ".ell." ], + > [ "echo", "bar" ], + > [ "echo", "baz" ] + > ] + > ]' > test.json + + $ js VAR=hello test.json + echo bar + + $ js VAR=.ell. test.json + echo bar + + $ js test.json + echo baz + + $ js VAR= test.json + echo baz + + $ js VAR=hell test.json + echo baz diff --git a/tests/cram/test_list.t b/tests/cram/test_list.t new file mode 100644 index 0000000..f7f18bd --- /dev/null +++ b/tests/cram/test_list.t @@ -0,0 +1,22 @@ +check that list is producing expected results: + + $ [ -n "$TEST_BIN_DIR" ] && export PATH="$TEST_BIN_DIR:$PATH" + $ valgrind --quiet --leak-check=full test-list + test_basics: list_empty: yes + test_basics: list_add_tail: zero one two three four five six seven eight nine ten eleven twelve + test_basics: list_empty: no + test_basics: first=zero last=twelve + test_basics: 'zero' is first, yes + test_basics: 'twelve' is last, yes + test_basics: removing 'twelve' and 'zero' + test_basics: first=one last=eleven + test_basics: 'one' is first, yes + test_basics: 'eleven' is last, yes + test_basics: moving 'one' to the tail + test_basics: first=two last=one + test_basics: 'two' is first, yes + test_basics: 'one' is last, yes + test_basics: list_for_each_entry: two three four five six seven eight nine ten eleven one + test_basics: list_for_each_entry_reverse: one eleven ten nine eight seven six five four three two + test_basics: delete all entries + test_basics: list_empty: yes diff --git a/tests/cram/test_runqueue.t b/tests/cram/test_runqueue.t new file mode 100644 index 0000000..4d49110 --- /dev/null +++ b/tests/cram/test_runqueue.t @@ -0,0 +1,14 @@ +check that runqueue is producing expected results: + + $ [ -n "$TEST_BIN_DIR" ] && export PATH="$TEST_BIN_DIR:$PATH" + $ valgrind --quiet --leak-check=full test-runqueue + [1/1] start 'sleep 1' + [1/1] cancel 'sleep 1' + [0/1] finish 'sleep 1' + [1/1] start 'sleep 1' + [1/1] cancel 'sleep 1' + [0/1] finish 'sleep 1' + [1/1] start 'sleep 1' + [1/1] cancel 'sleep 1' + [0/1] finish 'sleep 1' + All done! |