aboutsummaryrefslogtreecommitdiff
path: root/test/tsupport.h
blob: a7842cc61649244e45fc93c5a1bc516b21676aff (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
// This software disclaims copyright. Do what you want with it. Be gay, do
// crime. Originally written by Alexis Lockwood in 2021. Ⓐ

#ifndef TSUPPORT_H
#define TSUPPORT_H

// --- DEPENDENCIES ------------------------------------------------------------

// Supporting modules
#include "ls.h"

// Standard headers
#include <stdbool.h>
#include <stddef.h>
#include <inttypes.h>
#include <string.h>

// --- PUBLIC MACROS -----------------------------------------------------------

/// Set up the internal jump buffer to catch an error. Normally this is done by
/// the execution engine itself and invisible for this user, but tests focusing
/// on internals will need to use this.
///
/// Used as a block:
///
/// 	LS_TEST_CATCH(ls, evar) {
/// 		handle_error(evar);
/// 	}
///
/// @param ls
/// @param evar - name of variable to create in the block
#define LS_TEST_CATCH(ls) if (setjmp((ls).error_jmp_buf))

/// Execute some code, expecting an error and asserting on it.
///
/// @param ls
/// @param e - error expected
/// @param code - code body to execute
#define LS_EXPECT_ERR(ls, e, code) \
	do {	\
		jmp_buf _holdbuf; \
		memcpy(&_holdbuf, &(ls).error_jmp_buf, sizeof(jmp_buf)); \
		bool got_error = false; \
		if (setjmp((ls).error_jmp_buf)) { \
			munit_assert_int((ls).error, ==, (e)); \
			got_error = true; \
		} else { \
			code ; \
		} \
		munit_assert_true(got_error); \
		memcpy(&(ls).error_jmp_buf, &_holdbuf, sizeof(jmp_buf)); \
	} while (0)

// --- PRIVATE DATATYPES -------------------------------------------------------
// --- PUBLIC DATATYPES --------------------------------------------------------
// --- PUBLIC CONSTANTS --------------------------------------------------------

/// Number of entries in the test data pool
#define LS_TEST_NPOOL 1000

// --- PUBLIC VARIABLES --------------------------------------------------------
// --- PUBLIC FUNCTIONS --------------------------------------------------------

/// Set up a new ls_t with a string as its source. You must also set up the
/// jump buffer, for which you can use LS_TEST_CATCH().
///
/// An internal static pool of LS_TEST_NPOOL will be initialized.
///
/// @param ls
/// @param text - the source text
void ls_test_setup(ls_t * ls, char const * text);

/// Set up the fetcher to return from a string, but don't initialize anything
/// else. Mainly for use with ls_run().
void ls_setup_run(ls_t * ls, char const * text);

#endif // !defined(TSUPPORT_H)