-
Notifications
You must be signed in to change notification settings - Fork 207
stdlib_io: add Python-like input() function #1070
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
b6c0d10
f2284c8
92516a4
fb7572b
53a495d
82430b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| program example_input | ||
| use stdlib_io, only : input | ||
| implicit none(type, external) | ||
|
|
||
| character(len=:), allocatable :: name | ||
|
|
||
| name = input("Enter your name: ") | ||
| print *, "Hello:", name | ||
|
|
||
| end program example_input |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,3 +17,4 @@ ADDTEST(get_line) | |
| ADDTEST(npy) | ||
| ADDTEST(open) | ||
| ADDTEST(parse_mode) | ||
| ADDTEST(input) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| module test_input | ||
| use testdrive, only : new_unittest, unittest_type, error_type, check | ||
| use stdlib_io, only : input | ||
| implicit none | ||
| private | ||
| public :: collect | ||
|
|
||
| contains | ||
|
|
||
| subroutine collect(tests) | ||
| type(unittest_type), allocatable, intent(out) :: tests(:) | ||
|
|
||
| tests = [ & | ||
| new_unittest("input preserves whitespace", test_input_whitespace), & | ||
| new_unittest("input with prompt", test_input_prompt), & | ||
| new_unittest("input with iostat", test_input_iostat), & | ||
| new_unittest("input with iomsg", test_input_iomsg), & | ||
| new_unittest("input without optional args", test_input_no_args) & | ||
| ] | ||
| end subroutine collect | ||
|
|
||
|
|
||
| subroutine test_input_whitespace(error) | ||
| type(error_type), allocatable, intent(out) :: error | ||
| character(len=:), allocatable :: s | ||
|
|
||
| call feed_stdin(" abc ") | ||
Brijesh-Thakkar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| s = input() | ||
| call assert_equal(error, s, " abc ") | ||
| end subroutine test_input_whitespace | ||
|
|
||
|
|
||
| subroutine test_input_prompt(error) | ||
| type(error_type), allocatable, intent(out) :: error | ||
| character(len=:), allocatable :: s | ||
|
|
||
| call write_test_input("abc") | ||
|
||
| s = input("Enter value: ") | ||
| call assert_equal(error, s, "abc") | ||
| end subroutine test_input_prompt | ||
|
|
||
|
|
||
| subroutine test_input_iostat(error) | ||
| type(error_type), allocatable, intent(out) :: error | ||
| character(len=:), allocatable :: s | ||
| integer :: ios | ||
|
|
||
| call write_test_input("abc") | ||
| s = input(iostat=ios) | ||
| call assert_equal(error, ios, 0) | ||
| call assert_equal(error, s, "abc") | ||
| end subroutine test_input_iostat | ||
|
|
||
|
|
||
| subroutine test_input_iomsg(error) | ||
| type(error_type), allocatable, intent(out) :: error | ||
| character(len=:), allocatable :: s | ||
| character(len=:), allocatable :: msg | ||
|
|
||
| call write_test_input("abc") | ||
| s = input(iomsg=msg) | ||
| call assert_equal(error, s, "abc") | ||
| end subroutine test_input_iomsg | ||
|
|
||
|
|
||
| subroutine test_input_no_args(error) | ||
| type(error_type), allocatable, intent(out) :: error | ||
| character(len=:), allocatable :: s | ||
|
|
||
| call write_test_input("abc") | ||
| s = input() | ||
| call assert_equal(error, s, "abc") | ||
| end subroutine test_input_no_args | ||
|
|
||
| end module test_input | ||
|
|
||
|
|
||
| program run_test_input | ||
| use testdrive, only : run_testsuite | ||
| use test_input, only : collect | ||
| implicit none | ||
|
|
||
| call run_testsuite(collect) | ||
| end program run_test_input | ||
Uh oh!
There was an error while loading. Please reload this page.