summaryrefslogtreecommitdiff
path: root/src/logging/logging.test.c
blob: 01c8b202fe8fa98b96f0583dd2a4abd0769ef016 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <honeysuckle.h>
#include "test/honey-test.h"

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * include C file and declare mocks
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */
#define vfprintf(file, fmt, vl) mock_vfprintf(file, fmt, vl)
void mock_vfprintf(FILE*, const char*, va_list vl);
#include "logging.c"
#undef vfprintf


/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * define mock functions
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

MOCK(mock_vfprintf)(FILE *file, const char *fmt, va_list vl)
{
	/* to avoid basically just re-implementing printf parsing here,
	   i am limiting this function to be able to receive strings only */
	
	/* count format specifiers */
	char *ptr = strchr(fmt, '%');
	int n_args = 0;
	while (ptr != NULL) {
		n_args += 1;
		ptr = strchr(ptr+1, '%');
	}

	/* store arguments */
	struct lily_mock_arg_t args[] = {
		{ sizeof(FILE*), &file },
		{ sizeof(const char*), &fmt },
		{ sizeof(int), &n_args },
	};
	lily_mock_call(mock_vfprintf_data, args);

	/* store format arguments */
	lily_queue_t *queue = mock_vfprintf_data->values;
	for (int i=0; i<n_args; i++) {
		char *str = va_arg(vl, char*);
		lily_enqueue(queue, char*, str);
	}
}


/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * declare tests and define suite
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

void clean_mock(lily_mock_t **m)
{
	if (*m != NULL) {
		lily_mock_destroy(*m);
		*m = NULL;
	}
}

void level_fatal_log_fatal_succeeds();
void level_neg_log_fatal_fails();
void test_honey_log_fatal();
void test_honey_log_fatal_fail();

void suite_logging()
{
	lily_run_test(level_neg_log_fatal_fails);
	lily_run_test(level_fatal_log_fatal_succeeds);
    lily_run_test(test_honey_log_fatal);

	CLEAN_MOCK(mock_vfprintf);
}


/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * define tests
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

void level_fatal_log_fatal_succeeds()
{
	USE_MOCK(mock_vfprintf);

	honey_set_log_level(HONEY_FATAL);
	honey_log_fatal("some message");
	lily_assert_int_equal(mock_vfprintf_data->n_calls, 1);

	FILE *file; const char *fmt; int n_strings;
	struct lily_mock_arg_t args[] = {
		{ sizeof(FILE*), &file },
		{ sizeof(const char*), &fmt },
		{ sizeof(int), &n_strings },
	};
	lily_get_call(mock_vfprintf_data, args, 0);

	lily_assert_ptr_equal(file, stderr);
	lily_assert_string_equal((char*) fmt, "[FATAL] some message");
	lily_assert_int_equal(n_strings, 0);
}


void level_neg_log_fatal_fails()
{
	USE_MOCK(mock_vfprintf);

	honey_set_log_level(-1);
	honey_log_fatal("some message");
	lily_assert_int_equal(mock_vfprintf_data->n_calls, 0);
}

//error logging test functions
void test_honey_log_fatal()
{
    USE_MOCK(mock_vfprintf);    
    //first test (with no error surpressions)
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);
    lua_pushcfunction(L, log_fatal); //function is on the stack
    lua_pushstring(L, "fatal error test");
    int result = hs_call(L, 1, 0);
    if (result != 0) {
        const char *error = lua_tostring(L, -1);
        fprintf(stderr, "%s", error);
    }
    lily_assert_int_equal(result, 0); //testing that error function got string pushed to it
    lily_assert_int_equal(mock_vfprintf_data->n_calls, 1); //testing that error printing function works
}

void test_honey_log_fatal_fail()
{
    USE_MOCK(mock_vfprintf);
    honey_set_log_level(-1);
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);
    lua_pushcfunction(L, log_fatal);
    lua_pushstring(L, "fatal error test");
    int result = hs_call(L, 1, 0);
    if (result != 0) {
        const char *error = lua_tostring(L, -1);
        fprintf(stderr, "%s", error);
    }
    lily_assert_int_equal(result, 0);
    lily_assert_int_equal(mock_vfprintf_data->n_calls, 0);
}