From 71ce86b48b60040835531493465be02923b21acb Mon Sep 17 00:00:00 2001 From: Lloyd Hilaiel Date: Sat, 15 Feb 2014 10:14:45 +0100 Subject: add a custom api test facility --- CMakeLists.txt | 6 ++++-- src/yajl_gen.c | 2 +- test/CMakeLists.txt | 16 ++++++++++++++++ test/api/CMakeLists.txt | 25 +++++++++++++++++++++++++ test/api/gen-extra-close.c | 19 +++++++++++++++++++ test/api/run_tests.sh | 23 +++++++++++++++++++++++ test/parsing/CMakeLists.txt | 4 ++-- test/parsing/run_tests.sh | 10 +++++----- 8 files changed, 95 insertions(+), 10 deletions(-) create mode 100644 test/CMakeLists.txt create mode 100644 test/api/CMakeLists.txt create mode 100644 test/api/gen-extra-close.c create mode 100755 test/api/run_tests.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index 7190682..535b0fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -74,6 +74,8 @@ INCLUDE(YAJLDoc.cmake) # a test target ADD_CUSTOM_TARGET(test - ./run_tests.sh ${CMAKE_CURRENT_BINARY_DIR}/test/yajl_test - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test) + ./run_tests.sh ${CMAKE_CURRENT_BINARY_DIR}/test/parsing/yajl_test + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test/parsing) +ADD_CUSTOM_TARGET(test-api ${CMAKE_CURRENT_SOURCE_DIR}/test/api/run_tests.sh + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/test/api) diff --git a/src/yajl_gen.c b/src/yajl_gen.c index ed2b7aa..86b5b46 100644 --- a/src/yajl_gen.c +++ b/src/yajl_gen.c @@ -172,7 +172,7 @@ yajl_gen_free(yajl_gen g) if (++(g->depth) >= YAJL_MAX_DEPTH) return yajl_max_depth_exceeded; #define DECREMENT_DEPTH \ - if (--(g->depth) >= YAJL_MAX_DEPTH) return yajl_gen_error; + if (--(g->depth) >= YAJL_MAX_DEPTH) return yajl_gen_generation_complete; #define APPENDED_ATOM \ switch (g->state[g->depth]) { \ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..ea83430 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,16 @@ +# Copyright (c) 2007-2014, Lloyd Hilaiel +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +ADD_SUBDIRECTORY(parsing) +ADD_SUBDIRECTORY(api) diff --git a/test/api/CMakeLists.txt b/test/api/CMakeLists.txt new file mode 100644 index 0000000..e3d07d7 --- /dev/null +++ b/test/api/CMakeLists.txt @@ -0,0 +1,25 @@ +# Copyright (c) 2007-2014, Lloyd Hilaiel +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +SET (TESTS gen-extra-close.c +) +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}/../../${YAJL_DIST_NAME}/include) +LINK_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}/../../${YAJL_DIST_NAME}/lib) + +# for each test, we'll create a target, and make the api-tests target depend on it +FOREACH (test ${TESTS}) + GET_FILENAME_COMPONENT(testProg ${test} NAME_WE) + ADD_EXECUTABLE(${testProg} ${test}) + TARGET_LINK_LIBRARIES(${testProg} yajl) +ENDFOREACH() diff --git a/test/api/gen-extra-close.c b/test/api/gen-extra-close.c new file mode 100644 index 0000000..85f6925 --- /dev/null +++ b/test/api/gen-extra-close.c @@ -0,0 +1,19 @@ +/* ensure that if we try to generate an extra closing brace + * we get the expected error */ + +#include +#include + +#define CHK(x) if (x != yajl_gen_status_ok) return 1; + +int main(void) { + yajl_gen yg; + yajl_gen_status s; + + yg = yajl_gen_alloc(NULL); + CHK(yajl_gen_map_open(yg)); + CHK(yajl_gen_map_close(yg)); + s = yajl_gen_map_close(yg); + + return (yajl_gen_generation_complete == s); +} diff --git a/test/api/run_tests.sh b/test/api/run_tests.sh new file mode 100755 index 0000000..19e8e17 --- /dev/null +++ b/test/api/run_tests.sh @@ -0,0 +1,23 @@ +#!/bin/sh + +echo Running api tests: + +tests=0 +passed=0 + +for file in `ls`; do + [ ! -x $file -o -d $file ] && continue + tests=`expr 1 + $tests` + printf " %s:\t" $file + ./$file + if [ $? ]; then + passed=`expr 1 + $passed` + echo 'SUCCESS' + else + echo 'FAILURE' + fi +done + +echo "$passed/$tests passed" + +exit 0 diff --git a/test/parsing/CMakeLists.txt b/test/parsing/CMakeLists.txt index 153288c..6ea3e93 100644 --- a/test/parsing/CMakeLists.txt +++ b/test/parsing/CMakeLists.txt @@ -15,8 +15,8 @@ SET (SRCS yajl_test.c) # use the library we build, duh. -INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}/../${YAJL_DIST_NAME}/include) -LINK_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}/../${YAJL_DIST_NAME}/lib) +INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}/../../${YAJL_DIST_NAME}/include) +LINK_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}/../../${YAJL_DIST_NAME}/lib) ADD_EXECUTABLE(yajl_test ${SRCS}) diff --git a/test/parsing/run_tests.sh b/test/parsing/run_tests.sh index af64e15..b37e4dd 100755 --- a/test/parsing/run_tests.sh +++ b/test/parsing/run_tests.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/sh ECHO=`which echo` @@ -16,13 +16,13 @@ fi # find test binary on both platforms. allow the caller to force a # particular test binary (useful for non-cmake build systems). if [ -z "$testBin" ]; then - testBin="../build/test/Release/yajl_test.exe" + testBin="../build/test/parsing/Release/yajl_test.exe" if [ ! -x $testBin ] ; then - testBin="../build/test/Debug/yajl_test.exe" + testBin="../build/test/parsing/Debug/yajl_test.exe" if [ ! -x $testBin ] ; then - testBin="../build/test/yajl_test" + testBin="../build/test/parsing/yajl_test" if [ ! -x $testBin ] ; then - ${ECHO} "cannot execute test binary: '$testBin'" + ${ECHO} "cannot execute test binary: '$testBin'" exit 1; fi fi -- cgit v1.2.1