Skip to content

Commit d78d6a9

Browse files
committed
test(command_line): handle missing flag values
This commit adds a test that verifies the handling of cases in which a value associated with a command-line flag is not present. The motivating case is supporiting a statement like type(command_line_t) command_line flag_value = command_line%flag_value("--foo") when the --foo flag is missing. This commit adds a test ensuring that the flag_value() function resultis is then the empty string.
1 parent 0ecdd54 commit d78d6a9

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

example/handle-missing-flag.f90

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
program handle_missing_flag
2+
!! Verify that a missing command-line flag value returns a zero-length string.
3+
!! Running this program as follows with the command
4+
!!
5+
!! fpm run --example handle-missing-flag -- --empty-flag
6+
!!
7+
!! should result in normal termination.
8+
use assert_m, only : assert
9+
use sourcery_m, only : command_line_t
10+
implicit none
11+
12+
type(command_line_t) command_line
13+
character(len=:), allocatable :: flag_value
14+
character(len=*), parameter :: expected_name=""
15+
16+
flag_value = command_line%flag_value("--empty-flag")
17+
18+
call assert(flag_value==expected_name,"handle_missing_flag: expected empty flag value", flag_value)
19+
end program

test/command_line_test.F90

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@ function results() result(test_results)
2828
type(test_description_t), allocatable :: test_descriptions(:)
2929
#ifndef __GFORTRAN__
3030
test_descriptions = [ &
31-
test_description_t(string_t("returning the value passed after a command-line flag"), check_flag_value) &
31+
test_description_t(string_t("returning the value passed after a command-line flag"), check_flag_value), &
32+
test_description_t(string_t("return an empty string when a flag value is missing"), handle_missing_flag_value) &
3233
]
3334
#else
3435
! Work around missing Fortran 2008 feature: associating a procedure actual argument with a procedure pointer dummy argument:
35-
procedure(test_function_i), pointer :: check_flag_ptr
36+
procedure(test_function_i), pointer :: check_flag_ptr, handle_missing_value_ptr
3637
check_flag_ptr => check_flag_value
38+
handle_missing_value_ptr => handle_missing_flag_value
3739
test_descriptions = [ &
38-
test_description_t(string_t("returning the value passed after a command-line flag"), check_flag_ptr) &
40+
test_description_t(string_t("returning the value passed after a command-line flag"), check_flag_ptr), &
41+
test_description_t(string_t("return an empty string when a flag value is missing"), handle_missing_value_ptr) &
3942
]
4043
#endif
4144
test_descriptions = pack(test_descriptions, &
@@ -46,7 +49,6 @@ function results() result(test_results)
4649

4750
function check_flag_value() result(test_passes)
4851
logical test_passes
49-
5052
integer exit_status, command_status
5153
character(len=132) command_message
5254

@@ -55,7 +57,18 @@ function check_flag_value() result(test_passes)
5557
wait = .true., exitstat = exit_status, cmdstat = command_status, cmdmsg = command_message &
5658
)
5759
test_passes = exit_status == 0
60+
end function
61+
62+
function handle_missing_flag_value() result(test_passes)
63+
logical test_passes
64+
integer exit_status, command_status
65+
character(len=132) command_message
5866

67+
call execute_command_line( &
68+
command = "fpm run --example handle-missing-flag -- --empty-flag", &
69+
wait = .true., exitstat = exit_status, cmdstat = command_status, cmdmsg = command_message &
70+
)
71+
test_passes = exit_status == 0
5972
end function
6073

6174
end module command_line_test_m

0 commit comments

Comments
 (0)