aboutsummaryrefslogtreecommitdiffstats
path: root/tests/common
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2016-09-03 21:23:27 +0200
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2016-09-03 21:57:04 +0200
commite5f9cd349da231eae42ec9d35e3ebcd0d8fff973 (patch)
treeca81ecd0548cafdefffc4a61b3e1dd2afac32d16 /tests/common
parenta319fe44f6d50d25f546e17ad9907eedd9a358a0 (diff)
downloadblogc-e5f9cd349da231eae42ec9d35e3ebcd0d8fff973.tar.gz
blogc-e5f9cd349da231eae42ec9d35e3ebcd0d8fff973.tar.bz2
blogc-e5f9cd349da231eae42ec9d35e3ebcd0d8fff973.zip
*: moved error handling to src/common/
Diffstat (limited to 'tests/common')
-rw-r--r--tests/common/check_error.c109
1 files changed, 109 insertions, 0 deletions
diff --git a/tests/common/check_error.c b/tests/common/check_error.c
new file mode 100644
index 0000000..9c72916
--- /dev/null
+++ b/tests/common/check_error.c
@@ -0,0 +1,109 @@
+/*
+ * blogc: A blog compiler.
+ * Copyright (C) 2015-2016 Rafael G. Martins <rafael@rafaelmartins.eng.br>
+ *
+ * This program can be distributed under the terms of the BSD License.
+ * See the file LICENSE.
+ */
+
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+#include <string.h>
+#include "../../src/common/error.h"
+
+
+static void
+test_error_new(void **state)
+{
+ bc_error_t *error = bc_error_new(1, "bola %s");
+ assert_non_null(error);
+ assert_int_equal(error->type, 1);
+ assert_string_equal(error->msg, "bola %s");
+ bc_error_free(error);
+}
+
+
+static void
+test_error_new_printf(void **state)
+{
+ bc_error_t *error = bc_error_new_printf(2, "bola %s", "guda");
+ assert_non_null(error);
+ assert_int_equal(error->type, 2);
+ assert_string_equal(error->msg, "bola guda");
+ bc_error_free(error);
+}
+
+
+static void
+test_error_parser(void **state)
+{
+ const char *a = "bola\nguda\nchunda\n";
+ bc_error_t *error = bc_error_parser(1, a, strlen(a), 11, "asd %d", 10);
+ assert_non_null(error);
+ assert_int_equal(error->type, 1);
+ assert_string_equal(error->msg,
+ "asd 10\nError occurred near line 3, position 2: chunda");
+ bc_error_free(error);
+ a = "bola\nguda\nchunda";
+ error = bc_error_parser(1, a, strlen(a), 11, "asd %d", 10);
+ assert_non_null(error);
+ assert_int_equal(error->type, 1);
+ assert_string_equal(error->msg,
+ "asd 10\nError occurred near line 3, position 2: chunda");
+ bc_error_free(error);
+ a = "bola\nguda\nchunda";
+ error = bc_error_parser(1, a, strlen(a), 0, "asd %d", 10);
+ assert_non_null(error);
+ assert_int_equal(error->type, 1);
+ assert_string_equal(error->msg,
+ "asd 10\nError occurred near line 1, position 1: bola");
+ bc_error_free(error);
+ a = "";
+ error = bc_error_parser(1, a, strlen(a), 0, "asd %d", 10);
+ assert_non_null(error);
+ assert_int_equal(error->type, 1);
+ assert_string_equal(error->msg, "asd 10");
+ bc_error_free(error);
+}
+
+
+static void
+test_error_parser_crlf(void **state)
+{
+ const char *a = "bola\r\nguda\r\nchunda\r\n";
+ bc_error_t *error = bc_error_parser(1, a, strlen(a), 13, "asd %d", 10);
+ assert_non_null(error);
+ assert_int_equal(error->type, 1);
+ assert_string_equal(error->msg,
+ "asd 10\nError occurred near line 3, position 2: chunda");
+ bc_error_free(error);
+ a = "bola\r\nguda\r\nchunda";
+ error = bc_error_parser(1, a, strlen(a), 13, "asd %d", 10);
+ assert_non_null(error);
+ assert_int_equal(error->type, 1);
+ assert_string_equal(error->msg,
+ "asd 10\nError occurred near line 3, position 2: chunda");
+ bc_error_free(error);
+ a = "bola\r\nguda\r\nchunda";
+ error = bc_error_parser(1, a, strlen(a), 0, "asd %d", 10);
+ assert_non_null(error);
+ assert_int_equal(error->type, 1);
+ assert_string_equal(error->msg,
+ "asd 10\nError occurred near line 1, position 1: bola");
+ bc_error_free(error);
+}
+
+
+int
+main(void)
+{
+ const UnitTest tests[] = {
+ unit_test(test_error_new),
+ unit_test(test_error_new_printf),
+ unit_test(test_error_parser),
+ unit_test(test_error_parser_crlf),
+ };
+ return run_tests(tests);
+}