aboutsummaryrefslogtreecommitdiffstats
path: root/tests/common/check_stdin.c
blob: 0aea58d2f11738036043f26471280d219ac5195c (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
/*
 * blogc: A blog compiler.
 * Copyright (C) 2014-2018 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)
{
    will_return(__wrap_fgetc, EOF);
    char *t = bc_stdin_read();
    assert_non_null(t);
    assert_string_equal(t, "");
    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();
    assert_non_null(t);
    assert_string_equal(t, "bola");
    free(t);
}


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