Skip to content

Commit 9e10ef9

Browse files
committed
test(test_description_t): add unit test
1 parent 5c67bc7 commit 9e10ef9

File tree

4 files changed

+105
-17
lines changed

4 files changed

+105
-17
lines changed

src/sourcery/sourcery_test_description_m.F90

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,28 @@ function test_function_i() result(passes)
2525
contains
2626
procedure run
2727
procedure contains_text
28+
generic :: operator(==) => equals
29+
procedure, private :: equals
2830
end type
2931

3032
interface test_description_t
3133

32-
module function construct(description, test_function) result(test_description)
34+
module function construct_from_string_t(description, test_function) result(test_description)
3335
!! The result is a test_description_t object with the components defined by the dummy arguments
3436
implicit none
3537
type(string_t), intent(in) :: description
3638
procedure(test_function_i), intent(in), pointer :: test_function
3739
type(test_description_t) test_description
3840
end function
3941

42+
module function construct_from_character(description, test_function) result(test_description)
43+
!! The result is a test_description_t object with the components defined by the dummy arguments
44+
implicit none
45+
character(len=*), intent(in) :: description
46+
procedure(test_function_i), intent(in), pointer :: test_function
47+
type(test_description_t) test_description
48+
end function
49+
4050
end interface
4151

4252
interface
@@ -56,6 +66,13 @@ impure elemental module function contains_text(self, substring) result(match)
5666
logical match
5767
end function
5868

69+
elemental module function equals(lhs, rhs) result(lhs_eq_rhs)
70+
!! The result is .true. if the components of the lhs & rhs are equal
71+
implicit none
72+
class(test_description_t), intent(in) :: lhs, rhs
73+
logical lhs_eq_rhs
74+
end function
75+
5976
end interface
6077

6178
end module sourcery_test_description_m

src/sourcery/sourcery_test_description_s.f90

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
submodule(sourcery_test_description_m) sourcery_test_description_s
22
implicit none
3-
43
contains
4+
module procedure construct_from_character
5+
test_description%description_ = description
6+
test_description%test_function_ => test_function
7+
end procedure
58

6-
module procedure construct
9+
module procedure construct_from_string_t
710
test_description%description_ = description
811
test_description%test_function_ => test_function
912
end procedure
@@ -16,4 +19,7 @@
1619
match = index(self%description_%string(), substring%string()) /= 0
1720
end procedure
1821

22+
module procedure equals
23+
lhs_eq_rhs = (lhs%description_ == rhs%description_) .and. associated(lhs%test_function_, rhs%test_function_)
24+
end procedure
1925
end submodule sourcery_test_description_s

test/main.f90

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
program main
2-
use user_defined_collectives_test_m, only : collectives_test_t
3-
use data_partition_test_m, only : data_partition_test_t
42
use bin_test_m, only : bin_test_t
5-
use object_m_test_m, only : object_test_t
6-
use formats_test_m, only : formats_test_t
7-
use test_result_test_m, only : test_result_test_t
83
use command_line_test_m, only : command_line_test_t
4+
use data_partition_test_m, only : data_partition_test_t
5+
use formats_test_m, only : formats_test_t
6+
use object_m_test_m, only : object_test_t
97
use string_test_m, only : string_test_t
108
use sourcery_m, only : command_line_t
9+
use test_result_test_m, only : test_result_test_t
10+
use test_description_test_m, only : test_description_test_t
11+
use user_defined_collectives_test_m, only : collectives_test_t
1112
implicit none
1213

14+
type(bin_test_t) bin_test
15+
type(command_line_test_t) command_line_test
1316
type(collectives_test_t) collectives_test
1417
type(data_partition_test_t) data_partition_test
15-
type(bin_test_t) bin_test
1618
type(formats_test_t) formats_test
1719
type(object_test_t) object_test
18-
type(test_result_test_t) test_result_test
19-
type(command_line_test_t) command_line_test
2020
type(string_test_t) string_test
21+
type(test_result_test_t) test_result_test
22+
type(test_description_test_t) test_description_test
2123

2224
integer :: passes=0, tests=0
2325

@@ -34,19 +36,17 @@ program main
3436
end block
3537

3638
call bin_test%report(passes, tests)
37-
call data_partition_test%report(passes, tests)
3839
call collectives_test%report(passes, tests)
39-
call object_test%report(passes, tests)
40-
call test_result_test%report(passes, tests)
40+
call data_partition_test%report(passes, tests)
4141
call formats_test%report(passes, tests)
42+
call object_test%report(passes, tests)
4243
call string_test%report(passes, tests)
43-
44+
call test_result_test%report(passes, tests)
45+
call test_description_test%report(passes, tests)
4446
if (.not. GitHub_CI()) call command_line_test%report(passes, tests)
4547

4648
if (this_image()==1) print *, new_line('a'), "_________ In total, ",passes," of ",tests, " tests pass. _________"
47-
4849
if (passes /= tests) error stop
49-
5050
contains
5151

5252
logical function GitHub_CI()

test/test_description_test.F90

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
module test_description_test_m
2+
!! Verify test_description_t object behavior
3+
use sourcery_m, only : string_t, test_result_t, test_description_t, test_t, test_description_substring
4+
#ifdef __GFORTRAN__
5+
use sourcery_m, only : test_function_i
6+
#endif
7+
implicit none
8+
9+
private
10+
public :: test_description_test_t
11+
12+
type, extends(test_t) :: test_description_test_t
13+
contains
14+
procedure, nopass :: subject
15+
procedure, nopass :: results
16+
end type
17+
18+
contains
19+
20+
pure function subject() result(specimen)
21+
character(len=:), allocatable :: specimen
22+
specimen = "The test_description_t type"
23+
end function
24+
25+
function results() result(test_results)
26+
type(test_result_t), allocatable :: test_results(:)
27+
type(test_description_t), allocatable :: test_descriptions(:)
28+
29+
#ifndef __GFORTRAN__
30+
test_descriptions = [ &
31+
test_description_t("identical construction from string_t or character arguments", check_character_constructor) &
32+
]
33+
#else
34+
! Work around missing Fortran 2008 feature: associating a procedure actual argument with a procedure pointer dummy argument:
35+
procedure(test_function_i), pointer :: check_character_ptr
36+
check_character_ptr => check_character_constructor
37+
test_descriptions = [ &
38+
test_description_t("identical construction from string_t or character arguments", check_character_ptr) &
39+
]
40+
#endif
41+
associate( &
42+
substring_in_subject => index(subject(), test_description_substring) /= 0, &
43+
substring_in_description => test_descriptions%contains_text(string_t(test_description_substring)) &
44+
)
45+
test_descriptions = pack(test_descriptions, substring_in_subject .or. substring_in_description)
46+
end associate
47+
test_results = test_descriptions%run()
48+
end function
49+
50+
function check_character_constructor() result(passed)
51+
logical passed
52+
#ifndef __GFORTRAN__
53+
passed = test_description_t("foo", tautology) == test_description_t(string_t("foo"), tautology)
54+
#else
55+
procedure(test_function_i), pointer :: test_function_ptr
56+
test_function_ptr => tautology
57+
passed = test_description_t("foo", test_function_ptr) == test_description_t(string_t("foo"), test_function_ptr)
58+
#endif
59+
contains
60+
logical function tautology()
61+
tautology = .true.
62+
end function
63+
end function
64+
65+
end module test_description_test_m

0 commit comments

Comments
 (0)