aboutsummaryrefslogtreecommitdiffstats
path: root/tests/blogc/check_funcvars.c
blob: 9d094d58be018f31599c2c44d2bc876cd85b6c6e (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
/*
 * blogc: A blog compiler.
 * Copyright (C) 2014-2019 Rafael G. Martins <rafael@rafaelmartins.eng.br>
 *
 * This program can be distributed under the terms of the BSD License.
 * See the file LICENSE.
 */

#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "../../src/common/error.h"
#include "../../src/common/utils.h"
#include "../../src/blogc/funcvars.h"


char*
__wrap_bc_file_get_contents(const char *path, bool utf8, size_t *len, bc_error_t **err)
{
    assert_string_equal(path, "/proc/1/cgroup");
    assert_false(utf8);
    char *rv = mock_type(char*);
    *len = strlen(rv);
    return rv;
}


static void
test_funcvars_eval(void **state)
{
    bc_trie_t *t = bc_trie_new(free);
    blogc_funcvars_eval(t, NULL);
    blogc_funcvars_eval(t, "");
    blogc_funcvars_eval(t, "BOLA");
    assert_int_equal(bc_trie_size(t), 0);

    bc_trie_insert(t, "BOLA", bc_strdup("GUDA"));
    blogc_funcvars_eval(t, "BOLA");
    assert_int_equal(bc_trie_size(t), 1);

    bc_trie_free(t);
}


static void
test_funcvars_eval_mocked(void **state)
{
    bc_trie_t *t = bc_trie_new(free);

    // this is the only function that isn't hidden behind conditional macros
    // as of when this test was written. the other functions should be tested
    // separately
    will_return(__wrap_bc_file_get_contents, bc_strdup("asd/docker/asd"));
    blogc_funcvars_eval(t, "BLOGC_SYSINFO_INSIDE_DOCKER");
    assert_string_equal(bc_trie_lookup(t, "BLOGC_SYSINFO_INSIDE_DOCKER"), "1");
    assert_int_equal(bc_trie_size(t), 1);

    // this specific function call is cached, so calling it again should not
    // call bc_file_get_contents_again
    blogc_funcvars_eval(t, "BLOGC_SYSINFO_INSIDE_DOCKER");
    assert_string_equal(bc_trie_lookup(t, "BLOGC_SYSINFO_INSIDE_DOCKER"), "1");
    assert_int_equal(bc_trie_size(t), 1);

    bc_trie_free(t);
}


int
main(void)
{
    const UnitTest tests[] = {
        unit_test(test_funcvars_eval),
        unit_test(test_funcvars_eval_mocked),
    };
    return run_tests(tests);
}