about summary refs log tree commit diff
path: root/tests/runtests.sh
blob: 75f1f3c18d23a0daf4ba1823955245c6e985457e (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env bash

PLC="nix run"

globallog=runtests.log
rm -f $globallog
error=
keep=

# Destination directory for generated files (log excepted)
mkdir -p out

# Set time limit
ulimit -t 5

Usage() {
    echo "Usage: runtests.sh [options] [files]"
    echo "-k    Keep intermediate files"
    echo "-h    Print this help"
    exit 1
}

# NoteGen <filename>
# Remember that the given filename was generated so we can clean up
NoteGen() {
  generatedfiles="$generatedfiles $1"
}

SignalError() {
    if [ -z "$error" ] ; then
        echo "FAILED"
        error=1
    fi
    echo "  $1"
}

# Compare <outfile> <reffile> <difffile>
# Compares the outfile with reffile.  Differences, if any, written to difffile
Compare() {
    NoteGen "$3"
    if ! [ -f "$2" ] ; then
      SignalError "$2 not found"	
      echo "$2 not found" 1>&2
    else
	echo diff -b $1 $2 ">" $3 1>&2
	diff -b "$1" "$2" > "$3" 2>&1 || {
	    SignalError "$1 differs"
	    echo "FAILED $1 differs from $2" 1>&2
	    cat $3 >&2
	}
    fi
}

# Run <args>
# Report the command, run it, and report any errors
Run() {
    echo $* 1>&2
    eval $* || {
	SignalError "failed: $*"
	return 1
    }
}

# RunFail <args>
# Report the command, run it, and expect an error
RunFail() {
    echo $* 1>&2
    eval $* && {
	SignalError "failed: $* did not report an error"
	return 1
    }
    return 0
}

# Run the parser on the given file
Check() {
    error=
    basename=`basename $1 | sed 's/[.][^.]*$//'`
    reffile=`echo $1 | sed 's/[.][^.]*$//'`
    basedir=`dirname $1`

    echo -n "$basename..."

    echo 1>&2
    echo "###### Testing $basename" 1>&2

    generatedfiles=

    # Run the parser
    result="out/${basename}.out"
    reference="${reffile}.out"
    diff="out/${basename}.diff"
    NoteGen "${result} ${diff}"
    
    Run $PLC "<" "$1" ">" "${result}" &&
    Compare "${result}" "${reference}" "${diff}"

    if [ -z "$error" ] ; then
	if [ -z "$keep" ] ; then
	    rm -f $generatedfiles
	fi
	echo "OK"
	echo "###### SUCCESS" 1>&2
    else
	echo "###### FAILED" 1>&2
	globalerror=$error
    fi
}

# Run the compiler and expect an error
CheckFail() {
    error=
    basename=`basename $1 | sed 's/[.][^.]*$//'`
    reffile=`echo $1 | sed 's/[.][^.]*$//'`
    basedir=`dirname $1`

    echo -n "$basename..."

    echo 1>&2
    echo "###### Testing failure $basename" 1>&2

    generatedfiles=

    # Run the parser

    parsed="out/${basename}.lam"
    difffile="out/${basename}.diff"
    NoteGen "${parsed} ${difffile}"
    RunFail $PLC "<" "$1" ">" "${parsed}" "2>" "${errfile}" &&
    Compare "${errfile}" "${reffile}.err" "${difffile}"

    # Report the status and clean up the generated files

    if [ -z "$error" ] ; then
	if [ -z "$keep" ] ; then
	    rm -f $generatedfiles
	fi
	echo "OK"
	echo "###### SUCCESS" 1>&2
    else
	echo "###### FAILED" 1>&2
	globalerror=$error
    fi
}

while getopts kh c; do
    case $c in
	k) # Keep intermediate files
	    keep=1
	    ;;
	h) # Help
	    Usage
	    ;;
    esac
done

shift `expr $OPTIND - 1`

if [ $# -ge 1 ]
then
    files=$@
else
    files="examples/*.lam"
fi

for file in $files
do
  case "$file" in
    *-fail.lam)
	  CheckFail "$file" 2>> $globallog
	  ;;
    *)
	  Check "$file" 2>> $globallog
	  ;;
  esac    
done

exit $globalerror