aboutsummaryrefslogtreecommitdiffstats
path: root/tests/common/check_stdin.c
blob: 3d902029f3b289829cd4539f1502f74477b945a2 (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
/*
 * 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/stdin.h"


int
__wrap_fgetc(FILE *stream)
{
    assert_int_equal(fileno(stream), fileno(stdin));
    return mock_type(int);
}


static void
test_read(void **state)
{
    assert_null(bc_stdin_read(NULL));
    will_return(__wrap_fgetc, EOF);
    size_t len;
    char *t = bc_stdin_read(&len);
    assert_non_null(t);
    assert_string_equal(t, "");
    assert_int_equal(len, 0);
    free(t);
    will_return(__wrap_fgetc, 'b');
    will_return(__wrap_fgetc, 'o');
    will_return(__wrap_fgetc, 'l');
    will_return(__wrap_fgetc, 'a');
    will_return(__wrap_fgetc, EOF);
    t = bc_stdin_read(&len);
    assert_non_null(t);
    assert_string_equal(t, "bola");
    assert_int_equal(len, 4);
    free(t);
}


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