-
Notifications
You must be signed in to change notification settings - Fork 208
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
Open
Brijesh-Thakkar
wants to merge
4
commits into
fortran-lang:master
Choose a base branch
from
Brijesh-Thakkar:feature/input-function
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+183
−2
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b6c0d10
stdlib_io: add Python-like input() function
Brijesh-Thakkar f2284c8
stdlib_io: add Python-like input() function Fixes #259
Brijesh-Thakkar 92516a4
test(io): move input test to top-level for fpm compatibility (fixes #…
Brijesh-Thakkar fb7572b
io: implement input() with tests and documentation (fixes #259)
Brijesh-Thakkar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 ") | ||
| 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.