|
1 | | -call write_test_input(" abc ") |
2 | | -s = input() |
3 | | -call assert_equal(s, " abc ") |
| 1 | +module test_input |
| 2 | + use testdrive, only : new_unittest, unittest_type, error_type, assert_equal |
| 3 | + use stdlib_io, only : input |
| 4 | + use stdlib_test_utils, only : write_test_input |
| 5 | + implicit none |
| 6 | + private |
| 7 | + public :: collect |
| 8 | + |
| 9 | +contains |
| 10 | + |
| 11 | + subroutine collect(tests) |
| 12 | + type(unittest_type), allocatable, intent(out) :: tests(:) |
| 13 | + |
| 14 | + tests = [ & |
| 15 | + new_unittest("input preserves whitespace", test_input_whitespace), & |
| 16 | + new_unittest("input with prompt", test_input_prompt), & |
| 17 | + new_unittest("input with iostat", test_input_iostat), & |
| 18 | + new_unittest("input with iomsg", test_input_iomsg), & |
| 19 | + new_unittest("input without optional args", test_input_no_args) & |
| 20 | + ] |
| 21 | + end subroutine collect |
| 22 | + |
| 23 | + |
| 24 | + subroutine test_input_whitespace(error) |
| 25 | + type(error_type), allocatable, intent(out) :: error |
| 26 | + character(len=:), allocatable :: s |
| 27 | + |
| 28 | + call write_test_input(" abc ") |
| 29 | + s = input() |
| 30 | + call assert_equal(error, s, " abc ") |
| 31 | + end subroutine test_input_whitespace |
| 32 | + |
| 33 | + |
| 34 | + subroutine test_input_prompt(error) |
| 35 | + type(error_type), allocatable, intent(out) :: error |
| 36 | + character(len=:), allocatable :: s |
| 37 | + |
| 38 | + call write_test_input("abc") |
| 39 | + s = input("Enter value: ") |
| 40 | + call assert_equal(error, s, "abc") |
| 41 | + end subroutine test_input_prompt |
| 42 | + |
| 43 | + |
| 44 | + subroutine test_input_iostat(error) |
| 45 | + type(error_type), allocatable, intent(out) :: error |
| 46 | + character(len=:), allocatable :: s |
| 47 | + integer :: ios |
| 48 | + |
| 49 | + call write_test_input("abc") |
| 50 | + s = input(iostat=ios) |
| 51 | + call assert_equal(error, ios, 0) |
| 52 | + call assert_equal(error, s, "abc") |
| 53 | + end subroutine test_input_iostat |
| 54 | + |
| 55 | + |
| 56 | + subroutine test_input_iomsg(error) |
| 57 | + type(error_type), allocatable, intent(out) :: error |
| 58 | + character(len=:), allocatable :: s |
| 59 | + character(len=:), allocatable :: msg |
| 60 | + |
| 61 | + call write_test_input("abc") |
| 62 | + s = input(iomsg=msg) |
| 63 | + call assert_equal(error, s, "abc") |
| 64 | + end subroutine test_input_iomsg |
| 65 | + |
| 66 | + |
| 67 | + subroutine test_input_no_args(error) |
| 68 | + type(error_type), allocatable, intent(out) :: error |
| 69 | + character(len=:), allocatable :: s |
| 70 | + |
| 71 | + call write_test_input("abc") |
| 72 | + s = input() |
| 73 | + call assert_equal(error, s, "abc") |
| 74 | + end subroutine test_input_no_args |
| 75 | + |
| 76 | +end module test_input |
| 77 | + |
| 78 | + |
| 79 | +program run_test_input |
| 80 | + use testdrive, only : run_testsuite |
| 81 | + use test_input, only : collect |
| 82 | + implicit none |
| 83 | + |
| 84 | + call run_testsuite(collect) |
| 85 | +end program run_test_input |
0 commit comments