summaryrefslogtreecommitdiff
path: root/src/tests/hs_create_enum_tests.c
blob: b24ec71e5b7577f171f895d6a6ff4c625e7442d8 (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
#include "hs_tests.h"

#define check(string, value) do {				\
    lua_getfield(L, index, string);					\
    mu_assert("field '" string "' is not an integer!", lua_isnumber(L, -1)); \
    int enum_value = lua_tointeger(L, -1);				\
    mu_assert("field '" string "' does not match expected value!", enum_value == (value)); \
    lua_pop(L, -1);							\
    lua_rawgeti(L, index, value);					\
    mu_assert("enum field for '" string "' is not a string!", lua_isstring(L, -1)); \
    const char *str = lua_tostring(L, -1);				\
    mu_assert("enum field for '" string "' does not match expected value!", strcmp(str, string) == 0); \
    lua_pop(L, -1);							\
    } while(0)


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

enum test_enum { VAL0, VAL1, VAL2, VAL3, N_VALS };
		

TEST(enum_test)
{
    int index = hs_create_enum
	(L,
	 "zero",  VAL0,
	 "one",   VAL1,
	 "two",   VAL2,
	 "three", VAL3,
	 HS_END);
    mu_assert("failed to return correct index!", index == lua_gettop(L));
    check("zero", VAL0);
    check("one", VAL1);
    check("two", VAL2);
    check("three", VAL3);
    return 0;
}

TEST(int_test)
{
    int index = hs_create_enum
	(L,
	 "someValue", 23,
	 "anotherValue", 444,
	 "???", -5,
	 "217", 217,
	 HS_END);
    mu_assert("failed to return correct index!", index == lua_gettop(L));
    check("someValue", 23);
    check("anotherValue", 444);
    check("???", -5);
    check("217", 217);
    return 0;
}


/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * test suite
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

void hs_create_enum_tests()
{
    printf("running hs_create_enum() tests...\n");
    mu_run_test("create enum table from enum", enum_test);
    mu_run_test("create enum table from integers", int_test);
}