Skip to content

Commit 2d58dee

Browse files
Merge pull request #20 from everythingfunctional/vegetables
Vegetables Presentation
2 parents d020f5f + 859e915 commit 2d58dee

File tree

11 files changed

+253
-0
lines changed

11 files changed

+253
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/*
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# is_leap_year
2+
3+
Example project for demonstrating good and bad testing practices.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name = "is_leap_year"
2+
version = "0.1.0"
3+
license = "MIT"
4+
author = "Brad Richardson"
5+
maintainer = "[email protected]"
6+
copyright = "Copyright 2021, Brad Richardson"
7+
8+
[dev-dependencies]
9+
vegetables = { git = "https://gitlab.com/everythingfunctional/vegetables.git", tag = "v7.2.1" }
10+
strff = { git = "https://gitlab.com/everythingfunctional/strff.git", tag = "v2.1.0" }
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module is_leap_year_m
2+
implicit none
3+
private
4+
public :: is_leap_year
5+
contains
6+
pure function is_leap_year(year)
7+
integer, intent(in) :: year
8+
logical :: is_leap_year
9+
10+
is_leap_year = mod(year, 4) == 0
11+
end function
12+
end module
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module bad_test
2+
use is_leap_year_m, only: is_leap_year
3+
use vegetables, only: &
4+
result_t, test_item_t, assert_not, assert_that, describe, it
5+
6+
implicit none
7+
private
8+
public :: test_is_leap_year
9+
contains
10+
function test_is_leap_year() result(tests)
11+
type(test_item_t) :: tests
12+
13+
tests = describe(&
14+
"is_leap_year", &
15+
[ it("is true for leap years", check_leap_year) &
16+
, it("is false for non leap years", check_non_leap_year) &
17+
])
18+
end function
19+
20+
function check_leap_year() result(result_)
21+
type(result_t) :: result_
22+
23+
result_ = &
24+
assert_that(is_leap_year(2016), "2016") &
25+
.and.assert_that(is_leap_year(2000), "2000")
26+
end function
27+
28+
function check_non_leap_year() result(result_)
29+
type(result_t) :: result_
30+
31+
result_ = &
32+
assert_not(is_leap_year(1999), "1999") &
33+
.and.assert_not(is_leap_year(1900), "1900")
34+
end function
35+
end module
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
module fancy_test
2+
use is_leap_year_m, only: is_leap_year
3+
use strff, only: to_string
4+
use vegetables, only: &
5+
example_t, &
6+
input_t, &
7+
integer_input_t, &
8+
result_t, &
9+
test_item_t, &
10+
assert_not, &
11+
assert_that, &
12+
describe, &
13+
fail, &
14+
it
15+
16+
implicit none
17+
private
18+
public :: test_is_leap_year
19+
contains
20+
function test_is_leap_year() result(tests)
21+
type(test_item_t) :: tests
22+
23+
tests = describe(&
24+
"is_leap_year", &
25+
[ it( &
26+
"returns false for years that are not divisible by 4", &
27+
[ example_t(integer_input_t(2002)) &
28+
, example_t(integer_input_t(2003)) &
29+
], &
30+
check_not_leap_year) &
31+
, it( &
32+
"returns true for years that are divisible by 4 but not by 100", &
33+
[ example_t(integer_input_t(2004)) &
34+
, example_t(integer_input_t(2008)) &
35+
], &
36+
check_leap_year) &
37+
, it( &
38+
"returns false for years that are divisible by 100 but not by 400", &
39+
[ example_t(integer_input_t(1900)) &
40+
, example_t(integer_input_t(2100)) &
41+
], &
42+
check_not_leap_year) &
43+
, it( &
44+
"returns true for years that are divisible by 400", &
45+
[ example_t(integer_input_t(2000)) &
46+
, example_t(integer_input_t(2400)) &
47+
], &
48+
check_leap_year) &
49+
])
50+
end function
51+
52+
function check_not_leap_year(input) result(result_)
53+
class(input_t), intent(in) :: input
54+
type(result_t) :: result_
55+
56+
select type (input)
57+
type is (integer_input_t)
58+
associate(year => input%input())
59+
result_ = assert_not(is_leap_year(year), to_string(year))
60+
end associate
61+
class default
62+
result_ = fail("Didn't get an integer_input_t")
63+
end select
64+
end function
65+
66+
function check_leap_year(input) result(result_)
67+
class(input_t), intent(in) :: input
68+
type(result_t) :: result_
69+
70+
select type (input)
71+
type is (integer_input_t)
72+
associate(year => input%input())
73+
result_ = assert_that(is_leap_year(year), to_string(year))
74+
end associate
75+
class default
76+
result_ = fail("Didn't get an integer_input_t")
77+
end select
78+
end function
79+
end module
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
module good_test
2+
use is_leap_year_m, only: is_leap_year
3+
use vegetables, only: &
4+
result_t, test_item_t, assert_not, assert_that, describe, it
5+
6+
implicit none
7+
private
8+
public :: test_is_leap_year
9+
contains
10+
function test_is_leap_year() result(tests)
11+
type(test_item_t) :: tests
12+
13+
tests = describe(&
14+
"is_leap_year", &
15+
[ it( &
16+
"returns false for years that are not divisible by 4", &
17+
check_not_divisible_by_4) &
18+
, it( &
19+
"returns true for years that are divisible by 4 but not by 100", &
20+
check_divisible_by_4_but_not_100) &
21+
, it( &
22+
"returns false for years that are divisible by 100 but not by 400", &
23+
check_divisible_by_100_but_not_400) &
24+
, it( &
25+
"returns true for years that are divisible by 400", &
26+
check_divisible_by_400) &
27+
])
28+
end function
29+
30+
function check_not_divisible_by_4() result(result_)
31+
type(result_t) :: result_
32+
33+
result_ = &
34+
assert_not(is_leap_year(2002), "2002") &
35+
.and.assert_not(is_leap_year(2003), "2003")
36+
end function
37+
38+
function check_divisible_by_4_but_not_100() result(result_)
39+
type(result_t) :: result_
40+
41+
result_ = &
42+
assert_that(is_leap_year(2004), "2004") &
43+
.and.assert_that(is_leap_year(2008), "2008")
44+
end function
45+
46+
function check_divisible_by_100_but_not_400() result(result_)
47+
type(result_t) :: result_
48+
49+
result_ = &
50+
assert_not(is_leap_year(1900), "1900") &
51+
.and.assert_not(is_leap_year(2100), "2100")
52+
end function
53+
54+
function check_divisible_by_400() result(result_)
55+
type(result_t) :: result_
56+
57+
result_ = &
58+
assert_that(is_leap_year(2000), "2000") &
59+
.and.assert_that(is_leap_year(2400), "2400")
60+
end function
61+
end module
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
! Generated by make_vegetable_driver. DO NOT EDIT
2+
program main
3+
implicit none
4+
5+
call run()
6+
contains
7+
subroutine run()
8+
use bad_test, only: &
9+
bad_is_leap_year => test_is_leap_year
10+
use fancy_test, only: &
11+
fancy_is_leap_year => test_is_leap_year
12+
use good_test, only: &
13+
good_is_leap_year => test_is_leap_year
14+
use ugly_test, only: &
15+
ugly_is_leap_year => test_is_leap_year
16+
use vegetables, only: test_item_t, test_that, run_tests
17+
18+
type(test_item_t) :: tests
19+
type(test_item_t) :: individual_tests(4)
20+
21+
individual_tests(1) = bad_is_leap_year()
22+
individual_tests(2) = fancy_is_leap_year()
23+
individual_tests(3) = good_is_leap_year()
24+
individual_tests(4) = ugly_is_leap_year()
25+
tests = test_that(individual_tests)
26+
27+
call run_tests(tests)
28+
end subroutine
29+
end program
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module ugly_test
2+
use is_leap_year_m, only: is_leap_year
3+
use vegetables, only: &
4+
result_t, test_item_t, assert_not, assert_that, describe, it
5+
6+
implicit none
7+
private
8+
public :: test_is_leap_year
9+
contains
10+
function test_is_leap_year() result(tests)
11+
type(test_item_t) :: tests
12+
13+
tests = describe("is_leap_year", [it("works", check_is_leap_year)])
14+
end function
15+
16+
function check_is_leap_year() result(result_)
17+
type(result_t) :: result_
18+
19+
result_ = &
20+
assert_not(is_leap_year(1)) &
21+
.and.assert_that(is_leap_year(4))
22+
end function
23+
end module
4.64 MB
Binary file not shown.

0 commit comments

Comments
 (0)