@@ -4,9 +4,10 @@ module testsuite
4
4
implicit none
5
5
private
6
6
7
- public :: run_testsuite, new_unittest, test_failed
7
+ public :: run_testsuite, run_selected, new_unittest, new_testsuite, test_failed
8
+ public :: select_test, select_suite
8
9
public :: check_string
9
- public :: unittest_t, error_t
10
+ public :: unittest_t, testsuite_t, error_t
10
11
11
12
12
13
abstract interface
@@ -48,6 +49,22 @@ end subroutine collect_interface
48
49
end interface
49
50
50
51
52
+ ! > Collection of unit tests
53
+ type :: testsuite_t
54
+
55
+ ! > Name of the testsuite
56
+ character (len= :), allocatable :: name
57
+
58
+ ! > Entry point of the test
59
+ procedure (collect_interface), pointer , nopass :: collect = > null ()
60
+
61
+ end type testsuite_t
62
+
63
+
64
+ character (len=* ), parameter :: fmt = ' ("#", *(1x, a))'
65
+ character (len=* ), parameter :: indent = repeat (" " , 5 ) // repeat (" ." , 3 )
66
+
67
+
51
68
contains
52
69
53
70
@@ -64,9 +81,6 @@ subroutine run_testsuite(collect, unit, stat)
64
81
integer , intent (out ) :: stat
65
82
66
83
type (unittest_t), allocatable :: testsuite(:)
67
- character (len=* ), parameter :: fmt = ' ("#", *(1x, a))'
68
- character (len=* ), parameter :: indent = repeat (" " , 5 ) // repeat (" ." , 3 )
69
- type (error_t), allocatable :: error
70
84
integer :: ii
71
85
72
86
stat = 0
@@ -76,27 +90,133 @@ subroutine run_testsuite(collect, unit, stat)
76
90
do ii = 1 , size (testsuite)
77
91
write (unit, ' ("#", 3(1x, a), 1x, "(", i0, "/", i0, ")")' ) &
78
92
& " Starting" , testsuite(ii)% name, " ..." , ii, size (testsuite)
79
- call testsuite(ii)% test(error)
80
- if (allocated (error) .neqv. testsuite(ii)% should_fail) then
81
- if (testsuite(ii)% should_fail) then
82
- write (unit, fmt) indent, testsuite(ii)% name, " [UNEXPECTED PASS]"
83
- else
84
- write (unit, fmt) indent, testsuite(ii)% name, " [FAILED]"
85
- end if
86
- stat = stat + 1
93
+ call run_unittest(testsuite(ii), unit, stat)
94
+ end do
95
+
96
+ end subroutine run_testsuite
97
+
98
+
99
+ ! > Driver for selective testing
100
+ subroutine run_selected (collect , name , unit , stat )
101
+
102
+ ! > Collect tests
103
+ procedure (collect_interface) :: collect
104
+
105
+ ! > Name of the selected test
106
+ character (len=* ), intent (in ) :: name
107
+
108
+ ! > Unit for IO
109
+ integer , intent (in ) :: unit
110
+
111
+ ! > Number of failed tests
112
+ integer , intent (out ) :: stat
113
+
114
+ type (unittest_t), allocatable :: testsuite(:)
115
+ integer :: ii
116
+
117
+ stat = 0
118
+
119
+ call collect(testsuite)
120
+
121
+ ii = select_test(testsuite, name)
122
+
123
+ if (ii > 0 .and. ii <= size (testsuite)) then
124
+ call run_unittest(testsuite(ii), unit, stat)
125
+ else
126
+ write (unit, fmt) " Available tests:"
127
+ do ii = 1 , size (testsuite)
128
+ write (unit, fmt) " -" , testsuite(ii)% name
129
+ end do
130
+ stat = - 1
131
+ end if
132
+
133
+ end subroutine run_selected
134
+
135
+
136
+ ! > Run a selected unit test
137
+ subroutine run_unittest (test , unit , stat )
138
+
139
+ ! > Unit test
140
+ type (unittest_t), intent (in ) :: test
141
+
142
+ ! > Unit for IO
143
+ integer , intent (in ) :: unit
144
+
145
+ ! > Number of failed tests
146
+ integer , intent (inout ) :: stat
147
+
148
+ type (error_t), allocatable :: error
149
+
150
+ call test% test(error)
151
+ if (allocated (error) .neqv. test% should_fail) then
152
+ if (test% should_fail) then
153
+ write (unit, fmt) indent, test% name, " [UNEXPECTED PASS]"
87
154
else
88
- if (testsuite(ii)% should_fail) then
89
- write (unit, fmt) indent, testsuite(ii)% name, " [EXPECTED FAIL]"
90
- else
91
- write (unit, fmt) indent, testsuite(ii)% name, " [PASSED]"
92
- end if
155
+ write (unit, fmt) indent, test% name, " [FAILED]"
93
156
end if
94
- if (allocated (error)) then
95
- write (unit, fmt) " Message:" , error% message
157
+ stat = stat + 1
158
+ else
159
+ if (test% should_fail) then
160
+ write (unit, fmt) indent, test% name, " [EXPECTED FAIL]"
161
+ else
162
+ write (unit, fmt) indent, test% name, " [PASSED]"
163
+ end if
164
+ end if
165
+ if (allocated (error)) then
166
+ write (unit, fmt) " Message:" , error% message
167
+ end if
168
+
169
+ end subroutine run_unittest
170
+
171
+
172
+ ! > Select a unit test from all available tests
173
+ function select_test (tests , name ) result(pos)
174
+
175
+ ! > Name identifying the test suite
176
+ character (len=* ), intent (in ) :: name
177
+
178
+ ! > Available unit tests
179
+ type (unittest_t) :: tests(:)
180
+
181
+ ! > Selected test suite
182
+ integer :: pos
183
+
184
+ integer :: it
185
+
186
+ pos = 0
187
+ do it = 1 , size (tests)
188
+ if (name == tests(it)% name) then
189
+ pos = it
190
+ exit
96
191
end if
97
192
end do
98
193
99
- end subroutine run_testsuite
194
+ end function select_test
195
+
196
+
197
+ ! > Select a test suite from all available suites
198
+ function select_suite (suites , name ) result(pos)
199
+
200
+ ! > Name identifying the test suite
201
+ character (len=* ), intent (in ) :: name
202
+
203
+ ! > Available test suites
204
+ type (testsuite_t) :: suites(:)
205
+
206
+ ! > Selected test suite
207
+ integer :: pos
208
+
209
+ integer :: it
210
+
211
+ pos = 0
212
+ do it = 1 , size (suites)
213
+ if (name == suites(it)% name) then
214
+ pos = it
215
+ exit
216
+ end if
217
+ end do
218
+
219
+ end function select_suite
100
220
101
221
102
222
! > Register a new unit test
@@ -121,6 +241,24 @@ function new_unittest(name, test, should_fail) result(self)
121
241
end function new_unittest
122
242
123
243
244
+ ! > Register a new testsuite
245
+ function new_testsuite (name , collect ) result(self)
246
+
247
+ ! > Name of the testsuite
248
+ character (len=* ), intent (in ) :: name
249
+
250
+ ! > Entry point to collect tests
251
+ procedure (collect_interface) :: collect
252
+
253
+ ! > Newly registered testsuite
254
+ type (testsuite_t) :: self
255
+
256
+ self% name = name
257
+ self% collect = > collect
258
+
259
+ end function new_testsuite
260
+
261
+
124
262
! > Check a deferred length character variable against a reference value
125
263
subroutine check_string (error , actual , expected , name )
126
264
0 commit comments