aboutsummaryrefslogtreecommitdiffstats
path: root/t/unit-tests/clar/test/suites
diff options
context:
space:
mode:
Diffstat (limited to 't/unit-tests/clar/test/suites')
-rw-r--r--t/unit-tests/clar/test/suites/CMakeLists.txt53
-rw-r--r--t/unit-tests/clar/test/suites/combined.c85
-rw-r--r--t/unit-tests/clar/test/suites/main.c27
-rw-r--r--t/unit-tests/clar/test/suites/pointer.c13
-rw-r--r--t/unit-tests/clar/test/suites/resources/test/file1
5 files changed, 179 insertions, 0 deletions
diff --git a/t/unit-tests/clar/test/suites/CMakeLists.txt b/t/unit-tests/clar/test/suites/CMakeLists.txt
new file mode 100644
index 0000000000..fa8ab9416a
--- /dev/null
+++ b/t/unit-tests/clar/test/suites/CMakeLists.txt
@@ -0,0 +1,53 @@
+list(APPEND suites
+ "combined"
+ "pointer"
+)
+
+foreach(suite IN LISTS suites)
+ add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${suite}/clar.suite"
+ COMMAND "${Python_EXECUTABLE}"
+ "${CMAKE_SOURCE_DIR}/generate.py"
+ "${CMAKE_CURRENT_SOURCE_DIR}/${suite}.c"
+ --output "${CMAKE_CURRENT_BINARY_DIR}/${suite}"
+ DEPENDS ${suite}.c
+ WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
+ )
+
+ add_executable(${suite}_suite)
+ set_target_properties(${suite}_suite PROPERTIES
+ C_STANDARD 90
+ C_STANDARD_REQUIRED ON
+ C_EXTENSIONS OFF
+ )
+
+ # MSVC generates all kinds of warnings. We may want to fix these in the future
+ # and then unconditionally treat warnings as errors.
+ if(NOT MSVC)
+ set_target_properties(${suite}_suite PROPERTIES
+ COMPILE_WARNING_AS_ERROR ON
+ )
+ endif()
+
+ target_sources(${suite}_suite PRIVATE
+ main.c
+ ${suite}.c
+ "${CMAKE_CURRENT_BINARY_DIR}/${suite}/clar.suite"
+ )
+ target_compile_definitions(${suite}_suite PRIVATE
+ CLAR_FIXTURE_PATH="${CMAKE_CURRENT_SOURCE_DIR}/resources/"
+ CLAR_SELFTEST
+ )
+ target_compile_options(${suite}_suite PRIVATE
+ $<IF:$<CXX_COMPILER_ID:MSVC>,/W4,-Wall>
+ )
+ target_include_directories(${suite}_suite PRIVATE
+ "${CMAKE_SOURCE_DIR}"
+ "${CMAKE_CURRENT_BINARY_DIR}/${suite}"
+ )
+ target_link_libraries(${suite}_suite clar)
+
+ add_test(NAME build_${suite}_suite
+ COMMAND "${CMAKE_COMMAND}" --build "${CMAKE_BINARY_DIR}" --config "$<CONFIG>" --target selftest
+ )
+ set_tests_properties(build_${suite}_suite PROPERTIES FIXTURES_SETUP clar_test_fixture)
+endforeach()
diff --git a/t/unit-tests/clar/test/suites/combined.c b/t/unit-tests/clar/test/suites/combined.c
new file mode 100644
index 0000000000..e8b41c98c3
--- /dev/null
+++ b/t/unit-tests/clar/test/suites/combined.c
@@ -0,0 +1,85 @@
+#include <sys/stat.h>
+
+#include "clar.h"
+
+static int file_size(const char *filename)
+{
+ struct stat st;
+
+ if (stat(filename, &st) == 0)
+ return (int)st.st_size;
+ return -1;
+}
+
+void test_combined__cleanup(void)
+{
+ cl_fixture_cleanup("test");
+
+ cl_assert(file_size("test/file") == -1);
+}
+
+void test_combined__1(void)
+{
+ cl_assert(1);
+ cl_must_pass(0); /* 0 == success */
+ cl_must_fail(-1); /* <0 == failure */
+ cl_must_pass(-1); /* demonstrate a failing call */
+}
+
+void test_combined__2(void)
+{
+ cl_fixture_sandbox("test");
+
+ cl_assert(file_size("test/nonexistent") == -1);
+ cl_assert(file_size("test/file") > 0);
+ cl_assert(100 == 101);
+}
+
+void test_combined__strings(void)
+{
+ const char *actual = "expected";
+ cl_assert_equal_s("expected", actual);
+ cl_assert_equal_s_("expected", actual, "second try with annotation");
+ cl_assert_equal_s_("mismatched", actual, "this one fails");
+}
+
+void test_combined__strings_with_length(void)
+{
+ const char *actual = "expected";
+ cl_assert_equal_strn("expected_", actual, 8);
+ cl_assert_equal_strn("exactly", actual, 2);
+ cl_assert_equal_strn_("expected_", actual, 8, "with annotation");
+ cl_assert_equal_strn_("exactly", actual, 3, "this one fails");
+}
+
+void test_combined__int(void)
+{
+ int value = 100;
+ cl_assert_equal_i(100, value);
+ cl_assert_equal_i_(101, value, "extra note on failing test");
+}
+
+void test_combined__int_fmt(void)
+{
+ int value = 100;
+ cl_assert_equal_i_fmt(022, value, "%04o");
+}
+
+void test_combined__bool(void)
+{
+ int value = 100;
+ cl_assert_equal_b(1, value); /* test equality as booleans */
+ cl_assert_equal_b(0, value);
+}
+
+void test_combined__multiline_description(void)
+{
+ cl_must_pass_(-1, "description line 1\ndescription line 2");
+}
+
+void test_combined__null_string(void)
+{
+ const char *actual = NULL;
+ cl_assert_equal_s(actual, actual);
+ cl_assert_equal_s_("expected", actual, "this one fails");
+}
diff --git a/t/unit-tests/clar/test/suites/main.c b/t/unit-tests/clar/test/suites/main.c
new file mode 100644
index 0000000000..3ab581d390
--- /dev/null
+++ b/t/unit-tests/clar/test/suites/main.c
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) Vicent Marti. All rights reserved.
+ *
+ * This file is part of clar, distributed under the ISC license.
+ * For full terms see the included COPYING file.
+ */
+
+#include "clar.h"
+
+/*
+ * Selftest main() for clar tests.
+ *
+ * You should write your own main routine for clar tests that does specific
+ * setup and teardown as necessary for your application. The only required
+ * line is the call to `clar_test(argc, argv)`, which will execute the test
+ * suite. If you want to check the return value of the test application,
+ * your main() should return the same value returned by clar_test().
+ */
+
+#ifdef _WIN32
+int __cdecl main(int argc, char *argv[])
+#else
+int main(int argc, char *argv[])
+#endif
+{
+ return clar_test(argc, argv);
+}
diff --git a/t/unit-tests/clar/test/suites/pointer.c b/t/unit-tests/clar/test/suites/pointer.c
new file mode 100644
index 0000000000..20535b159e
--- /dev/null
+++ b/t/unit-tests/clar/test/suites/pointer.c
@@ -0,0 +1,13 @@
+#include "clar.h"
+
+void test_pointer__equal(void)
+{
+ void *p1 = (void *)0x1;
+ cl_assert_equal_p(p1, p1);
+}
+
+void test_pointer__unequal(void)
+{
+ void *p1 = (void *)0x1, *p2 = (void *)0x2;
+ cl_assert_equal_p(p1, p2);
+}
diff --git a/t/unit-tests/clar/test/suites/resources/test/file b/t/unit-tests/clar/test/suites/resources/test/file
new file mode 100644
index 0000000000..220f4aa98a
--- /dev/null
+++ b/t/unit-tests/clar/test/suites/resources/test/file
@@ -0,0 +1 @@
+File