Skip to content

Commit 275e446

Browse files
committed
test(command_line): check present arg, missing val
1 parent 5e73f88 commit 275e446

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
program check_command_line_argument
2+
!! This program serves the dual purposes of
3+
!! 1. Showing how to use the command_line_t derived type to check whether a
4+
!! command-line argument is present and
5+
!! 2. Supporting the test suite verification of this same behavior.
6+
!!
7+
!! Running this program as follows with the command
8+
!!
9+
!! fpm run --example check-command-line-argument -- --some-argument
10+
!!
11+
!! should result in normal termination.
12+
use assert_m, only : assert
13+
use sourcery_m, only : command_line_t
14+
implicit none
15+
16+
type(command_line_t) command_line
17+
logical argument_passed
18+
19+
argument_passed = command_line%argument_present(["--some-argument"])
20+
21+
call assert(argument_passed, "check_command_line_argument: argument present")
22+
end program

example/handle-missing-flag.f90

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
program handle_missing_flag
2-
!! Verify that a missing command-line flag value returns a zero-length string.
2+
!! This program serves the dual purposes of
3+
!!
4+
!! 1. Showing an example of a command-line with an expected flag missing an
5+
!! 2. Supporting the test suite check that the returned value has zero length.
6+
!!
37
!! Running this program as follows with the command
48
!!
59
!! fpm run --example handle-missing-flag -- --empty-flag

test/command_line_test.F90

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,19 @@ function results() result(test_results)
2929
#ifndef __GFORTRAN__
3030
test_descriptions = [ &
3131
test_description_t(string_t("returning the value passed after a command-line flag"), check_flag_value), &
32-
test_description_t(string_t("returning an empty string when a flag value is missing"), handle_missing_flag_value) &
32+
test_description_t(string_t("returning an empty string when a flag value is missing"), handle_missing_flag_value), &
33+
test_description_t(string_t("detecting a present command-line argument"), check_command_line_argument) &
3334
]
3435
#else
3536
! Work around missing Fortran 2008 feature: associating a procedure actual argument with a procedure pointer dummy argument:
36-
procedure(test_function_i), pointer :: check_flag_ptr, handle_missing_value_ptr
37+
procedure(test_function_i), pointer :: check_flag_ptr, handle_missing_value_ptr, check_command_ptr
3738
check_flag_ptr => check_flag_value
3839
handle_missing_value_ptr => handle_missing_flag_value
40+
check_command_ptr => check_command_line_argument
3941
test_descriptions = [ &
4042
test_description_t(string_t("returning the value passed after a command-line flag"), check_flag_ptr), &
41-
test_description_t(string_t("returning an empty string when a flag value is missing"), handle_missing_value_ptr) &
43+
test_description_t(string_t("returning an empty string when a flag value is missing"), handle_missing_value_ptr), &
44+
test_description_t(string_t("detecting a present command-line argument"), check_command_ptr) &
4245
]
4346
#endif
4447
test_descriptions = pack(test_descriptions, &
@@ -71,4 +74,16 @@ function handle_missing_flag_value() result(test_passes)
7174
test_passes = exit_status == 0
7275
end function
7376

77+
function check_command_line_argument() result(test_passes)
78+
logical test_passes
79+
integer exit_status, command_status
80+
character(len=132) command_message
81+
82+
call execute_command_line( &
83+
command = "fpm run --example check-command-line-argument -- --some-argument", &
84+
wait = .true., exitstat = exit_status, cmdstat = command_status, cmdmsg = command_message &
85+
)
86+
test_passes = exit_status == 0
87+
end function
88+
7489
end module command_line_test_m

0 commit comments

Comments
 (0)