Debian Codegraph @

test/fpm_test/testsuite.f90

raw
1 !> Define some procedures to automate collecting and launching of tests
2 module testsuite
3 use fpm_error, only : error_t, test_failed => fatal_error
4 implicit none
5 private
6
7 public :: run_testsuite, run_selected, new_unittest, new_testsuite, test_failed
8 public :: select_test, select_suite
9 public :: check_string
10 public :: unittest_t, testsuite_t, error_t
11
12
13 abstract interface
14 !> Entry point for tests
15 subroutine test_interface(error)
16 import :: error_t
17
18 !> Error handling
19 type(error_t), allocatable, intent(out) :: error
20
21 end subroutine test_interface
22 end interface
23
24
25 !> Declaration of a unit test
26 type :: unittest_t
27
28 !> Name of the test
29 character(len=:), allocatable :: name
30
31 !> Entry point of the test
32 procedure(test_interface), pointer, nopass :: test => null()
33
34 !> Whether test is supposed to fail
35 logical :: should_fail = .false.
36
37 end type unittest_t
38
39
40 abstract interface
41 !> Collect all tests
42 subroutine collect_interface(testsuite)
43 import :: unittest_t
44
45 !> Collection of tests
46 type(unittest_t), allocatable, intent(out) :: testsuite(:)
47
48 end subroutine collect_interface
49 end interface
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
68 contains
69
70
71 !> Driver for testsuite
72 subroutine run_testsuite(collect, unit, stat)
73
74 !> Collect tests
75 procedure(collect_interface) :: collect
76
77 !> Unit for IO
78 integer, intent(in) :: unit
79
80 !> Number of failed tests
81 integer, intent(inout) :: stat
82
83 type(unittest_t), allocatable :: testsuite(:)
84 integer :: ii
85
86 call collect(testsuite)
87
88 do ii = 1, size(testsuite)
89 write(unit, '("#", 3(1x, a), 1x, "(", i0, "/", i0, ")")') &
90 & "Starting", testsuite(ii)%name, "...", ii, size(testsuite)
91 call run_unittest(testsuite(ii), unit, stat)
92 end do
93
94 end subroutine run_testsuite
95
96
97 !> Driver for selective testing
98 subroutine run_selected(collect, name, unit, stat)
99
100 !> Collect tests
101 procedure(collect_interface) :: collect
102
103 !> Name of the selected test
104 character(len=*), intent(in) :: name
105
106 !> Unit for IO
107 integer, intent(in) :: unit
108
109 !> Number of failed tests
110 integer, intent(inout) :: stat
111
112 type(unittest_t), allocatable :: testsuite(:)
113 integer :: ii
114
115 call collect(testsuite)
116
117 ii = select_test(testsuite, name)
118
119 if (ii > 0 .and. ii <= size(testsuite)) then
120 call run_unittest(testsuite(ii), unit, stat)
121 else
122 write(unit, fmt) "Available tests:"
123 do ii = 1, size(testsuite)
124 write(unit, fmt) "-", testsuite(ii)%name
125 end do
126 stat = -huge(ii)
127 end if
128
129 end subroutine run_selected
130
131
132 !> Run a selected unit test
133 subroutine run_unittest(test, unit, stat)
134
135 !> Unit test
136 type(unittest_t), intent(in) :: test
137
138 !> Unit for IO
139 integer, intent(in) :: unit
140
141 !> Number of failed tests
142 integer, intent(inout) :: stat
143
144 type(error_t), allocatable :: error
145
146 call test%test(error)
147 if (allocated(error) .neqv. test%should_fail) then
148 if (test%should_fail) then
149 write(unit, fmt) indent, test%name, "[UNEXPECTED PASS]"
150 else
151 write(unit, fmt) indent, test%name, "[FAILED]"
152 end if
153 stat = stat + 1
154 else
155 if (test%should_fail) then
156 write(unit, fmt) indent, test%name, "[EXPECTED FAIL]"
157 else
158 write(unit, fmt) indent, test%name, "[PASSED]"
159 end if
160 end if
161 if (allocated(error)) then
162 write(unit, fmt) "Message:", error%message
163 end if
164
165 end subroutine run_unittest
166
167
168 !> Select a unit test from all available tests
169 function select_test(tests, name) result(pos)
170
171 !> Name identifying the test suite
172 character(len=*), intent(in) :: name
173
174 !> Available unit tests
175 type(unittest_t) :: tests(:)
176
177 !> Selected test suite
178 integer :: pos
179
180 integer :: it
181
182 pos = 0
183 do it = 1, size(tests)
184 if (name == tests(it)%name) then
185 pos = it
186 exit
187 end if
188 end do
189
190 end function select_test
191
192
193 !> Select a test suite from all available suites
194 function select_suite(suites, name) result(pos)
195
196 !> Name identifying the test suite
197 character(len=*), intent(in) :: name
198
199 !> Available test suites
200 type(testsuite_t) :: suites(:)
201
202 !> Selected test suite
203 integer :: pos
204
205 integer :: it
206
207 pos = 0
208 do it = 1, size(suites)
209 if (name == suites(it)%name) then
210 pos = it
211 exit
212 end if
213 end do
214
215 end function select_suite
216
217
218 !> Register a new unit test
219 function new_unittest(name, test, should_fail) result(self)
220
221 !> Name of the test
222 character(len=*), intent(in) :: name
223
224 !> Entry point for the test
225 procedure(test_interface) :: test
226
227 !> Whether test is supposed to error or not
228 logical, intent(in), optional :: should_fail
229
230 !> Newly registered test
231 type(unittest_t) :: self
232
233 self%name = name
234 self%test => test
235 if (present(should_fail)) self%should_fail = should_fail
236
237 end function new_unittest
238
239
240 !> Register a new testsuite
241 function new_testsuite(name, collect) result(self)
242
243 !> Name of the testsuite
244 character(len=*), intent(in) :: name
245
246 !> Entry point to collect tests
247 procedure(collect_interface) :: collect
248
249 !> Newly registered testsuite
250 type(testsuite_t) :: self
251
252 self%name = name
253 self%collect => collect
254
255 end function new_testsuite
256
257
258 !> Check a deferred length character variable against a reference value
259 subroutine check_string(error, actual, expected, name)
260
261 !> Error handling
262 type(error_t), allocatable, intent(out) :: error
263
264 !> Actual string value
265 character(len=:), allocatable, intent(in) :: actual
266
267 !> Expected string value
268 character(len=*), intent(in) :: expected
269
270 !> Name of the string to check
271 character(len=*), intent(in) :: name
272
273 if (.not.allocated(actual)) then
274 call test_failed(error, name//" is not set correctly")
275 return
276 end if
277
278 if (actual /= expected) then
279 call test_failed(error, name//" is "//actual// &
280 & " but should be "//expected)
281 end if
282
283 end subroutine check_string
284
285
286 end module testsuite