aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Wellnhofer <wellnhofer@aevum.de>2014-11-18 18:37:56 +0100
committerNick Wellnhofer <wellnhofer@aevum.de>2014-11-18 18:43:04 +0100
commit8dba3f7f3591f4271522835cb2057acf4ae56f53 (patch)
tree8bf044f7c0fa0e13925db0c64e8720a86c0b2336
parent3f9fec6998fccabe5d61bfe84b9d2431f9d5ae53 (diff)
Start with tests for the C API
The C API tests can be run individually via build/api_test/api_test Or together with the spec tests via cmake --build build --target test
-rw-r--r--CMakeLists.txt2
-rw-r--r--api_test/CMakeLists.txt11
-rw-r--r--api_test/harness.c102
-rw-r--r--api_test/harness.h34
-rw-r--r--api_test/main.c57
-rw-r--r--src/node.h2
6 files changed, 207 insertions, 1 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 105c257..9e2adbd 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -13,6 +13,7 @@ set(PROJECT_VERSION_PATCH 1)
set(PROJECT_VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH} )
add_subdirectory(src)
+add_subdirectory(api_test)
if(UNIX)
INSTALL(FILES man/man1/cmark.1 DESTINATION share/man/man1)
@@ -24,6 +25,7 @@ enable_testing()
add_test(spectest
perl "${CMAKE_SOURCE_DIR}/runtests.pl" "${CMAKE_SOURCE_DIR}/spec.txt" "${CMAKE_BINARY_DIR}/src/cmark"
)
+add_test(NAME api_test COMMAND api_test)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING
diff --git a/api_test/CMakeLists.txt b/api_test/CMakeLists.txt
new file mode 100644
index 0000000..7b6c3d6
--- /dev/null
+++ b/api_test/CMakeLists.txt
@@ -0,0 +1,11 @@
+add_executable(api_test
+ harness.c
+ main.c
+)
+target_include_directories(api_test
+ PRIVATE
+ ${PROJECT_SOURCE_DIR}/src
+ ${PROJECT_BINARY_DIR}/src
+)
+target_link_libraries(api_test libcmark)
+
diff --git a/api_test/harness.c b/api_test/harness.c
new file mode 100644
index 0000000..6b38d41
--- /dev/null
+++ b/api_test/harness.c
@@ -0,0 +1,102 @@
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "harness.h"
+
+test_batch_runner*
+test_batch_runner_new()
+{
+ return (test_batch_runner *)calloc(1, sizeof(test_batch_runner));
+}
+
+static void
+test_result(test_batch_runner *runner, int cond, const char *msg, va_list ap)
+{
+ ++runner->test_num;
+
+ if (cond) {
+ ++runner->num_passed;
+ }
+ else {
+ fprintf(stderr, "FAILED test %d: ", runner->test_num);
+ vfprintf(stderr, msg, ap);
+ fprintf(stderr, "\n");
+ ++runner->num_failed;
+ }
+}
+
+void
+SKIP(test_batch_runner *runner, int num_tests)
+{
+ runner->test_num += num_tests;
+ runner->num_skipped += num_tests;
+}
+
+void
+OK(test_batch_runner *runner, int cond, const char *msg, ...)
+{
+ va_list ap;
+ va_start(ap, msg);
+ test_result(runner, cond, msg, ap);
+ va_end(ap);
+}
+
+void
+INT_EQ(test_batch_runner *runner, int got, int expected, const char *msg, ...)
+{
+ int cond = got == expected;
+
+ va_list ap;
+ va_start(ap, msg);
+ test_result(runner, cond, msg, ap);
+ va_end(ap);
+
+ if (!cond) {
+ fprintf(stderr, " Got: %d\n", got);
+ fprintf(stderr, " Expected: %d\n", expected);
+ }
+}
+
+void
+STR_EQ(test_batch_runner *runner, const char *got, const char *expected,
+ const char *msg, ...)
+{
+ int cond = strcmp(got, expected) == 0;
+
+ va_list ap;
+ va_start(ap, msg);
+ test_result(runner, cond, msg, ap);
+ va_end(ap);
+
+ if (!cond) {
+ fprintf(stderr, " Got: \"%s\"\n", got);
+ fprintf(stderr, " Expected: \"%s\"\n", expected);
+ }
+}
+
+int
+test_ok(test_batch_runner *runner)
+{
+ return runner->num_failed == 0;
+}
+
+void
+test_print_summary(test_batch_runner *runner)
+{
+ int num_passed = runner->num_passed;
+ int num_skipped = runner->num_skipped;
+ int num_failed = runner->num_failed;
+
+ fprintf(stderr, "%d tests passed, %d failed, %d skipped\n",
+ num_passed, num_skipped, num_failed);
+
+ if (test_ok(runner)) {
+ fprintf(stderr, "PASS\n");
+ }
+ else {
+ fprintf(stderr, "FAIL\n");
+ }
+}
+
diff --git a/api_test/harness.h b/api_test/harness.h
new file mode 100644
index 0000000..5fdc7ae
--- /dev/null
+++ b/api_test/harness.h
@@ -0,0 +1,34 @@
+#ifndef CMARK_API_TEST_HARNESS_H
+#define CMARK_API_TEST_HARNESS_H
+
+typedef struct {
+ int test_num;
+ int num_passed;
+ int num_failed;
+ int num_skipped;
+} test_batch_runner;
+
+test_batch_runner*
+test_batch_runner_new();
+
+void
+SKIP(test_batch_runner *runner, int num_tests);
+
+void
+OK(test_batch_runner *runner, int cond, const char *msg, ...);
+
+void
+INT_EQ(test_batch_runner *runner, int got, int expected, const char *msg, ...);
+
+void
+STR_EQ(test_batch_runner *runner, const char *got, const char *expected,
+ const char *msg, ...);
+
+int
+test_ok(test_batch_runner *runner);
+
+void
+test_print_summary(test_batch_runner *runner);
+
+#endif
+
diff --git a/api_test/main.c b/api_test/main.c
new file mode 100644
index 0000000..051d32b
--- /dev/null
+++ b/api_test/main.c
@@ -0,0 +1,57 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <cmark.h>
+
+#include "harness.h"
+
+static void
+create_tree(test_batch_runner *runner)
+{
+ cmark_node *doc = cmark_node_new(CMARK_NODE_DOCUMENT);
+
+ cmark_node *p = cmark_node_new(CMARK_NODE_PARAGRAPH);
+ OK(runner, cmark_node_append_child(doc, p), "append1");
+ INT_EQ(runner, cmark_node_check(doc), 0, "append1 consistent");
+
+ cmark_node *emph = cmark_node_new(CMARK_NODE_EMPH);
+ OK(runner, cmark_node_prepend_child(p, emph), "prepend1");
+ INT_EQ(runner, cmark_node_check(doc), 0, "prepend1 consistent");
+
+ cmark_node *str1 = cmark_node_new(CMARK_NODE_STRING);
+ cmark_node_set_content(str1, "Hello, ");
+ OK(runner, cmark_node_prepend_child(p, str1), "prepend2");
+ INT_EQ(runner, cmark_node_check(doc), 0, "prepend2 consistent");
+
+ cmark_node *str3 = cmark_node_new(CMARK_NODE_STRING);
+ cmark_node_set_content(str3, "!");
+ OK(runner, cmark_node_append_child(p, str3), "append2");
+ INT_EQ(runner, cmark_node_check(doc), 0, "append2 consistent");
+
+ cmark_node *str2 = cmark_node_new(CMARK_NODE_STRING);
+ cmark_node_set_content(str2, "world");
+ OK(runner, cmark_node_append_child(emph, str2), "append3");
+ INT_EQ(runner, cmark_node_check(doc), 0, "append3 consistent");
+
+ char *html = cmark_render_html(doc);
+ STR_EQ(runner, html, "<p>Hello, <em>world</em>!</p>\n",
+ "render_html");
+ free(html);
+
+ cmark_node_destroy(doc);
+}
+
+int main() {
+ int retval;
+ test_batch_runner *runner = test_batch_runner_new();
+
+ create_tree(runner);
+
+ test_print_summary(runner);
+ retval = test_ok(runner) ? 0 : 1;
+ free(runner);
+
+ return retval;
+}
+
diff --git a/src/node.h b/src/node.h
index 533406b..2d7f0a1 100644
--- a/src/node.h
+++ b/src/node.h
@@ -61,7 +61,7 @@ struct cmark_node {
} as;
};
-int
+CMARK_EXPORT int
cmark_node_check(cmark_node *node);
#ifdef __cplusplus