Skip to content

Commit f5f4a4d

Browse files
committed
error_stop: improve test by using a driver program
1 parent e230a25 commit f5f4a4d

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

src/tests/CMakeLists.txt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
macro(ADDTEST name)
22
add_executable(test_${name} test_${name}.f90)
3-
target_link_libraries(test_${name} ${PROJECT_NAME})
3+
target_link_libraries(test_${name} PRIVATE ${PROJECT_NAME})
44
add_test(NAME ${name}
55
COMMAND $<TARGET_FILE:test_${name}> ${CMAKE_CURRENT_BINARY_DIR}
66
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
@@ -19,7 +19,14 @@ add_subdirectory(system)
1919
add_subdirectory(quadrature)
2020
add_subdirectory(math)
2121

22-
ADDTEST(always_skip)
23-
set_tests_properties(always_skip PROPERTIES SKIP_RETURN_CODE 77)
24-
ADDTEST(always_fail)
25-
set_tests_properties(always_fail PROPERTIES WILL_FAIL true)
22+
add_executable(test_error_handling test_error_driver.f90)
23+
24+
add_executable(test_always_77 test_always_77.f90)
25+
target_link_libraries(test_always_77 PRIVATE ${PROJECT_NAME})
26+
add_test(NAME test_error_77
27+
COMMAND $<TARGET_FILE:test_error_handling> $<TARGET_FILE:test_always_77> 77)
28+
29+
add_executable(test_always_1 test_always_fail.f90)
30+
target_link_libraries(test_always_1 PRIVATE ${PROJECT_NAME})
31+
add_test(NAME test_error_1
32+
COMMAND $<TARGET_FILE:test_error_handling> $<TARGET_FILE:test_always_1> 1)

src/tests/test_always_skip.f90 renamed to src/tests/test_always_77.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
program test_always_skip
1+
program test_always_77
22

33
use stdlib_error, only: check
44
implicit none

src/tests/test_error_driver.f90

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
program test_driver
2+
!! tests return codes from programs.
3+
!! useful for checking "error stop" return codes.
4+
!!
5+
!! Arguments:
6+
!! prog: program to run and catch return code
7+
8+
implicit none
9+
10+
integer :: ierr, ok_code, cmderr
11+
character(1024) :: prog
12+
character(3) :: ok, actual
13+
14+
call get_command_argument(1, prog, status=ierr)
15+
if(ierr/=0) error stop "please specify a program to catch return codes from"
16+
17+
call get_command_argument(2, ok, status=ierr)
18+
if(ierr/=0) error stop "please specify the expected return code"
19+
20+
read(ok, '(i3)') ok_code
21+
22+
call execute_command_line(trim(prog), exitstat=ierr, cmdstat=cmderr)
23+
if(cmderr/=0) error stop "test_driver had problem running always-fail program"
24+
25+
write(actual, '(i3)') ierr
26+
if(ierr /= ok_code) error stop "expected return code "//ok//", got "//actual//" instead"
27+
28+
end program

0 commit comments

Comments
 (0)