aboutsummaryrefslogtreecommitdiff
path: root/testsuite/libdejagnu/unit.exp
blob: d572f473709b5d589d1ac874c824fe2531bcacd5 (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
# Copyright (C) 2022 Free Software Foundation, Inc.
#
# This file is part of DejaGnu.
#
# DejaGnu is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# DejaGnu is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with DejaGnu; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.

# This file is a test driver for the unit test protocol library.

# Each test program must accept test outcomes on the command line, and
# produce exactly those results in that order.

proc test_libdejagnu_unit { language tests } {
    set test_program [testsuite file -object -test "unit-${language}"]

    # map messages from dejagnu.h:totals() to result types
    array set expected_totals_map {
	passed	pass	"real failed"	fail
	"unexpected passes"	xpass
	"expected failures"	xfail
	untested		untested
	unresolved		unresolved
    }

    foreach test $tests {
	array set expected_totals {
	    pass     0	fail       0
	    xpass    0	xfail      0
	    untested 0	unresolved 0
	}
	set test_idx 0
	set result pass

	verbose -log "Spawning $test_program $test ..."
	eval [list spawn $test_program] $test

	expect_after {
	    -re {^[^\n]*\n} { exp_continue }
	    full_buffer {
		perror "Expect matching buffer overrun during test."
	    }
	    eof {
		# No end marker?  ->  Fail!
		verbose -log " unit test did not emit an end marker"
		set result fail
	    }
	    timeout {
		catch close
		set result unresolved
	    }
	}

	# Check that the reported results match.
	expect {
	    -re {^[\r\n]*Totals:[\r\n]+} {
		# done with results, but fail if there should be more
		if { [lindex $test $test_idx] ne "" } {
		    verbose -log " expected [lindex $test $test_idx]"
		    set result fail
		}
	    }
	    -re {(?:\A|\n)\t([][[:upper:]]+):([^\n]+)\n} {
		# above pattern copied from lib/dejagnu.exp:host_execute
		switch -- [lindex $test $test_idx] {
		    pass	{ set expected PASSED     }
		    fail	{ set expected FAILED     }
		    xpass	{ set expected XPASSED    }
		    xfail	{ set expected XFAILED    }
		    untested	{ set expected UNTESTED   }
		    unresolved	{ set expected UNRESOLVED }
		}
		if { [info exists expected_totals([lindex $test $test_idx])]} {
		    incr expected_totals([lindex $test $test_idx])
		}
		if { $expected ne $expect_out(1,string) } {
		    set result fail
		}
		incr test_idx
		exp_continue
	    }
	}
	# Now ensure that the reported totals are as expected.
	expect {
	    -re {^\n*\t#([^:]+):\t+([[:digit:]]+)\r*\n} {
		set type $expected_totals_map($expect_out(1,string))
		set count $expect_out(2,string)

		if { $expected_totals($type) != $count } {
		    verbose -log " expected $expected_totals($type)\
						 $type; got $count"
		    set result fail
		}
		unset expected_totals($type)
		exp_continue
	    }
	    -re {^\n*\tEND:[^\n]+\n} {
		# flush to EOF
		expect -re {.+} { exp_continue } eof
	    }
	}
	catch close
	wait -nowait

	foreach { type count } [array get expected_totals] {
	    if { $count == 0 } { continue }
	    verbose -log " expected $count $type; got no report"
	    set result fail
	}

	$result "test $test_program with: $test"
    }
}

test_libdejagnu_unit c {
    pass fail xpass xfail untested unresolved
}