aboutsummaryrefslogtreecommitdiff
path: root/tests/common.sh
blob: 965975c5978b27641a76d93626153d00714a9727 (plain) (blame)
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
#!/bin/bash

function assert_exit_status() {
    build/cupcc -c "$1" -o build/test.nasm
    make build/test.out -s

    set +e
    build/test.out
    res=$?
    set -e
    if [ $res -ne $2 ]
    then
        echo ""
        echo "----------------------------------" 
        echo "Test failed: expected $2, got $res"
        echo "----------------------------------"
        echo "$1"
        exit 1
    fi
    echo -n "."
}

function assert_exit_status_stdin() {
    code=$(</dev/stdin)
    assert_exit_status "$code" $1
}

function assert_compile_failure_stdin() {
    code=$(</dev/stdin)
    set +e
    build/cupcc -c "$code" -o build/test.nasm >/dev/null 2>&1
    res=$?
    set -e
    if [ $res -eq 0 ]
    then
        echo ""
        echo "----------------------------------------------" 
        echo "Test failed: expected compilation, got success"
        echo "----------------------------------------------"
        echo "$code"
        exit 1
    fi
    echo -n "."
}

function assert_stdout_text() {
    build/cupcc -c "$1" -o build/test.nasm
    make build/test.out -s

    set +e
    output=$(build/test.out)
    res=$?
    set -e
    if [ $res -ne 0 ]
    then
        echo ""
        echo "----------------------------------------------" 
        echo "Test failed: executable returned non-0 code"
        echo "----------------------------------------------"
        echo "$1"
        exit 1
    fi
    if [[ "$output" != $2 ]]
    then
        echo ""
        echo "----------------------------------------------" 
        echo "Test failed: Did not get expected output"
        echo "----------------------------------------------"
        echo "$code"
        echo "----------------------------------------------"
        echo "Expected:"
        echo "$2"
        echo "----------------------------------------------"
        echo "Got:"
        echo "$output"
        exit 1
    fi
    echo -n "."
}