summaryrefslogtreecommitdiff
path: root/tests/testing.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/testing.c')
-rw-r--r--tests/testing.c82
1 files changed, 79 insertions, 3 deletions
diff --git a/tests/testing.c b/tests/testing.c
index 14e545d..21e0264 100644
--- a/tests/testing.c
+++ b/tests/testing.c
@@ -1,6 +1,16 @@
#include "../src/testing.c"
#include <assert.h>
+#include <string.h>
+
+#include "../src/logerr.h"
+#include "slurp.h"
+
+
+
+static const char
+FNAME[] = __FILE__ ".txt";
+
int
@@ -8,10 +18,25 @@ main(void) {
int rc = EXIT_FAILURE;
test_start("testing.c");
+ testing("output varying on $NO_COLOUR value");
+
+ FILE *file = NULL;
+ char *str = NULL;
+
+ file = fopen(FNAME, "w");
+ if (file == NULL) {
+ logerr("fopen(FNAME, \"w\")");
+ goto out;
+ }
+ test_set_stream(file);
+
+ test_start("testing.c");
const bool should_overwrite = true;
+ const char *const orig = getenv(ENVVAR_NAME);
+
if (unsetenv(ENVVAR_NAME)) {
- perror("unsetenv(\"NO_COLOUR\")");
+ logerr("unsetenv(\"NO_COLOUR\")");
goto out;
}
{
@@ -21,7 +46,7 @@ main(void) {
}
if (setenv(ENVVAR_NAME, "", should_overwrite)) {
- perror("setenv(\"NO_COLOUR\", \"\", 1)");
+ logerr("setenv(\"NO_COLOUR\", \"\", 1)");
goto out;
}
{
@@ -31,7 +56,7 @@ main(void) {
}
if (setenv(ENVVAR_NAME, "something", should_overwrite)) {
- perror("setenv(\"NO_COLOUR\", \"something\", 1)");
+ logerr("setenv(\"NO_COLOUR\", \"something\", 1)");
goto out;
}
{
@@ -40,7 +65,58 @@ main(void) {
test_ok();
}
+ test_set_stream(NULL);
+ const int ret = fclose(file);
+ file = NULL;
+ if (ret) {
+ logerr("fclose(file)");
+ goto out;
+ }
+
+ if (orig == NULL) {
+ if (unsetenv(ENVVAR_NAME)) {
+ logerr("unsetenv(\"NO_COLOUR\")");
+ goto out;
+ }
+ } else {
+ if (setenv(ENVVAR_NAME, orig, true)) {
+ logerr("setenv(\"NO_COLOUR\", orig, true)");
+ goto out;
+ }
+ }
+
+ if (slurp_for_tests(FNAME, &str)) {
+ logerr("slurp_for_tests()");
+ goto out;
+ }
+
+ const char expected[] =
+ "testing.c:\n"
+ "\033[0;33mtesting\033[0m: unset NO_COLOUR... "
+ "\033[0;32mOK\033[0m.\n"
+ "\033[0;33mtesting\033[0m: empty NO_COLOUR... "
+ "\033[0;32mOK\033[0m.\n"
+ "testing: defined NO_COLOUR... OK.\n"
+ ;
+
+ assert(strcmp(expected, str) == 0);
+
+ free(str);
+ str = NULL;
+
+ test_ok();
+
rc = EXIT_SUCCESS;
out:
+ if (str != NULL) {
+ free(str);
+ str = NULL;
+ }
+ if (file != NULL) {
+ if (fclose(file)) {
+ logerr("flose()");
+ rc = EXIT_FAILURE;
+ }
+ }
return rc;
}