Skip to content

Commit 9ca9800

Browse files
committed
feat: add minimalistic Vegetables replacement
This commit adds module/submodule pairs defining an abstract test_t type and a non-abstract test_result_t type that encapsulate test methods and results-reporting.
1 parent 5b6d987 commit 9ca9800

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

src/test_m.F90

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module test_m
2+
!! Define an abstract test_t type with deferred bindings ("subject" and "results")
3+
!! used by a type-bound procedure ("report") for reporting test results. The "report"
4+
!! procedure thus represents an implementation of the Template Method pattern.
5+
use test_result_m, only : test_result_t
6+
implicit none
7+
8+
private
9+
public :: test_t
10+
11+
type, abstract :: test_t
12+
!! Facilitate testing and test reporting
13+
contains
14+
procedure(subject_interface), nopass, deferred :: subject
15+
procedure(results_interface), nopass, deferred :: results
16+
procedure :: report
17+
end type
18+
19+
abstract interface
20+
21+
pure function subject_interface() result(specimen)
22+
!! The result is the name of the test specimen (the subject of testing)
23+
character(len=:), allocatable :: specimen
24+
end function
25+
26+
function results_interface() result(test_results)
27+
!! The result is an array of test outcomes for reporting
28+
import test_result_t
29+
type(test_result_t), allocatable :: test_results(:)
30+
end function
31+
32+
end interface
33+
34+
interface
35+
36+
module subroutine report(test)
37+
!! Report test results
38+
implicit none
39+
class(test_t), intent(in) :: test
40+
end subroutine
41+
42+
end interface
43+
44+
end module test_m

src/test_result_m.f90

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
module test_result_m
2+
!! Define an abstraction for describing test intentions and results
3+
implicit none
4+
5+
private
6+
public :: test_result_t
7+
8+
type test_result_t
9+
!! Encapsulate test descriptions and outcomes and reporting
10+
private
11+
character(len=:), allocatable :: description_
12+
logical outcome_
13+
contains
14+
procedure :: characterize
15+
end type
16+
17+
interface test_result_t
18+
19+
pure module function construct(description, outcome) result(test_result)
20+
!! The result is a test_result_t object with the components defined by the dummy arguments
21+
implicit none
22+
character(len=*), intent(in) :: description
23+
logical, intent(in) :: outcome
24+
type(test_result_t) test_result
25+
end function
26+
27+
end interface
28+
29+
interface
30+
31+
pure module function characterize(self) result(characterization)
32+
!! The result is a character test description and its outcome
33+
implicit none
34+
class(test_result_t), intent(in) :: self
35+
character(len=:), allocatable :: characterization
36+
end function
37+
38+
end interface
39+
40+
end module test_result_m

src/test_result_s.f90

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
submodule(test_result_m) test_result_s
2+
implicit none
3+
4+
contains
5+
6+
module procedure construct
7+
test_result%description_ = description
8+
test_result%outcome_ = outcome
9+
end procedure
10+
11+
module procedure characterize
12+
characterization = merge("Pass: ", "Fail: ", self%outcome_) // self%description_
13+
end procedure
14+
15+
end submodule test_result_s

src/test_s.F90

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
submodule(test_m) test_s
2+
#ifdef XLF
3+
use test_result_m, only : test_result_t
4+
#endif
5+
implicit none
6+
7+
contains
8+
9+
module procedure report
10+
integer i
11+
#ifdef XLF
12+
type(test_result_t), allocatable :: test_results(:)
13+
test_results = test%results()
14+
#else
15+
associate(test_results => test%results())
16+
#endif
17+
18+
print *
19+
print *, test%subject()
20+
21+
do i=1,size(test_results)
22+
print *," ",test_results(i)%characterize()
23+
end do
24+
#ifndef XLF
25+
end associate
26+
#endif
27+
end procedure
28+
29+
end submodule test_s

0 commit comments

Comments
 (0)