aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/driver.c12
-rw-r--r--tests/test_strfmt.c52
-rw-r--r--tests/tests.c2
3 files changed, 61 insertions, 5 deletions
diff --git a/tests/driver.c b/tests/driver.c
index d3739b5..4d998b8 100644
--- a/tests/driver.c
+++ b/tests/driver.c
@@ -6,9 +6,8 @@
#include "../src/extlib.h"
#include "test_driver.h"
-const char* success = "\033[1;32m";
-const char* fail = " \033[1;31m";
-const char *reset = "\033[0m";
+char* success;
+char* fail;
int tests_count;
struct test_t** registered_tests;
@@ -19,9 +18,9 @@ test_runner (struct test_t *test, int n)
printf ("Test Case %d: %s -- %s\n", n, test->test_name, test->test_desc);
int test_result = test->test_func();
if (test_result == 0)
- printf ("Test Case %d: %s -- %sSUCCESS%s\n\n", n, test->test_name, success, reset);
+ printf ("Test Case %d: %s -- %s\n\n", n, test->test_name, success);
else
- printf ("Test Case %d: %s -- %sFAIL %d%s\n\n", n, test->test_name, fail, test_result, reset);
+ printf ("Test Case %d: %s -- %s %d\n\n", n, test->test_name, fail, test_result);
}
void
@@ -36,6 +35,9 @@ register_test (struct test_t *test)
int
main (int argc, char *argv[])
{
+ success = strfmt("SUCCESS", (struct style) {.color = GREEN, .background = false, .styles = BOLD|UNDERLINE});
+ fail = strfmt("FAIL", (struct style) {.color = RED, .background = false, .styles = BOLD|UNDERLINE});
+
registered_tests = malloc (8);
tests_entrypoint();
for (int i = 0; i < tests_count; i++)
diff --git a/tests/test_strfmt.c b/tests/test_strfmt.c
new file mode 100644
index 0000000..c8ab08e
--- /dev/null
+++ b/tests/test_strfmt.c
@@ -0,0 +1,52 @@
+#include <string.h>
+#define USE_SECURE_MEM
+#include "../src/extlib.h"
+#include "stdbool.h"
+
+int
+test_strfmt ()
+{
+ int errno = 0;
+ char *test_string_1 = strfmt ("meow", (struct style) {0, false, BOLD});
+ printf ("\e[1mtest_strfmt\e[0m:: No Color, Bold: %s\n", test_string_1);
+ if (strcmp (test_string_1, "\e[1mmeow\e[0m") != 0)
+ errno = 1;
+
+ char *test_string_2 = strfmt ("meow", (struct style) {RED, false, 0});
+ printf ("\e[1mtest_strfmt\e[0m:: Red, No style: %s\n", test_string_2);
+ if (strcmp (test_string_2, "\e[38;5;1mmeow\e[0m") != 0)
+ errno = 2;
+
+ char *test_string_3 = strfmt ("meow", (struct style) {RED, false, BOLD});
+ printf ("\e[1mtest_strfmt\e[0m:: Red, Bold: %s\n", test_string_3);
+ if (strcmp (test_string_3, "\e[38;5;1m\e[1mmeow\e[0m") != 0)
+ errno = 3;
+
+ char *test_string_4 = strfmt ("meow", (struct style) {RED, true, 0});
+ printf ("\e[1mtest_strfmt\e[0m:: Red background, No style: %s\n", test_string_4);
+ if (strcmp (test_string_4, "\e[48;5;1mmeow\e[0m") != 0)
+ errno = 4;
+
+ char *test_string_5 = strfmt ("meow", (struct style) {RED, true, BOLD});
+ printf ("\e[1mtest_strfmt\e[0m:: Red background, Bold: %s\n", test_string_5);
+ if (strcmp (test_string_5, "\e[48;5;1m\e[1mmeow\e[0m") != 0)
+ errno = 5;
+
+ free_secure ((void **) &test_string_1, strlen (test_string_1));
+ free_secure ((void **) &test_string_2, strlen (test_string_2));
+ free_secure ((void **) &test_string_3, strlen (test_string_3));
+ free_secure ((void **) &test_string_4, strlen (test_string_4));
+ free_secure ((void **) &test_string_5, strlen (test_string_5));
+ return errno;
+}
+
+struct test_t*
+test_strfmt_t ()
+{
+ struct test_t* test = malloc (sizeof (struct test_t));
+ test->test_func=test_strfmt;
+ test->test_name="test_strfmt";
+ test->test_desc="strfmt";
+ return test;
+}
+
diff --git a/tests/tests.c b/tests/tests.c
index 73dd1af..295cbc8 100644
--- a/tests/tests.c
+++ b/tests/tests.c
@@ -7,6 +7,7 @@
#include "test_replace_str.c"
#include "test_strlwr_strupr.c"
#include "test_trim.c"
+#include "test_strfmt.c"
inline void
tests_entrypoint ()
@@ -19,4 +20,5 @@ tests_entrypoint ()
register_test (test_replace_str_t ());
register_test (test_strlwr_strupr_t ());
register_test (test_trim_t ());
+ register_test (test_strfmt_t ());
}