
dtonhofer
Functional Programming in Java, Second Edition: Functional Programming in Java, Second Edition: JUnit code improvements for Chapter 11, pages 184 ff “Refactoring More Complex loops”
The same remarks apply as for “Refactoring the Traditional for Loop”
- No
init()
- Classes under test implement a common interface that is called in tests.
- Negative tests which apply for input less than 1900, resulting in exceptions.
package chapter11;
import org.junit.jupiter.api.Test;
import java.time.Year;
import java.util.stream.IntStream;
import static org.junit.jupiter.api.Assertions.*;
// Everything is inside the MoreComplexLoopTest class for convenience.
// Documentation for java.time.Year.isLeap()
// https://docs.oracle.com/javase/8/docs/api/java/time/Year.html#isLeap--
public class MoreComplexLoopTest {
interface LeapYears {
int countFrom1900(int upTo);
}
static class LeapYearCountBefore implements LeapYears {
public int countFrom1900(int upTo) {
if (upTo < 1900) {
throw new IllegalArgumentException("The passed value is < 1900: " + upTo);
}
int count = 0;
for (int year = 1900; year <= upTo; year += 4) {
if (Year.isLeap(year)) {
count++;
}
}
return count;
}
}
static class LeapYearCountAfter implements LeapYears {
public int countFrom1900(int upTo) {
if (upTo < 1900) {
throw new IllegalArgumentException("The passed value is < 1900: " + upTo);
}
return (int) IntStream.iterate(1900, year -> year <= upTo, year -> year + 4)
.filter(Year::isLeap)
.count();
}
}
private static void commonLeapYearsTests(final LeapYears leapYears) {
assertAll(
() -> assertEquals(0, leapYears.countFrom1900(1900)),
() -> assertEquals(25, leapYears.countFrom1900(2000)),
() -> assertEquals(27, leapYears.countFrom1900(2010)),
() -> assertEquals(31, leapYears.countFrom1900(2025)),
() -> assertEquals(49, leapYears.countFrom1900(2100)),
() -> assertEquals(267, leapYears.countFrom1900(3000)),
() -> assertThrows(IllegalArgumentException.class, () -> leapYears.countFrom1900(1800))
);
}
@Test
void leapYearCountBefore() {
commonLeapYearsTests(new LeapYearCountBefore());
}
@Test
void leapYearCountAfter() {
commonLeapYearsTests(new LeapYearCountAfter());
}
}
Popular Pragmatic Bookshelf topics

Hi everyone!
There is an error on the page 71 in the book “Programming machine learning from coding to depp learning” P. Perrotta. You c...
New

Hi,
build fails on:
bracket-lib = “~0.8.1”
when running on Mac Mini M1 Rust version 1.5.0:
Compiling winit v0.22.2
error[E0308]: mi...
New

The generated iex result below should list products instead of product for the metadata. (page 67)
iex> product = %Product{}
%Pento....
New

Title: Intuitive Python: docker run… denied error (page 2)
Attempted to run the docker command in both CLI and Powershell
PS C:\Users\r...
New

I ran this command after installing the sample application:
$ cards add do something --owner Brian
And got a file not found error:
Fil...
New

I’m running Android Studio “Arctic Fox” 2020.3.1 Patch 2, and I’m embarrassed to admit that I only made it to page 8 before running into ...
New

When I run the coverage example to report on missing lines, I get:
pytest --cov=cards --report=term-missing ch7
ERROR: usage: pytest [op...
New

Is the book’s epub format available to read on Google Play Books?
New

Hey there,
I’m enjoying this book and have learned a few things alredayd. However, in Chapter 4 I believe we are meant to see the “>...
New

From page 13:
On Python 3.7, you can install the libraries with pip by running these commands inside a Python venv using Visual Studio ...
New
Other popular topics

Reading something? Working on something? Planning something? Changing jobs even!?
If you’re up for sharing, please let us know what you’...
New

Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it.
Does your monitor have eye p...
New

New
New

“Finding the Boundaries” Hero’s Journey with Noel Rappin @noelrappin
Even when you’re ultimately right about what the future ho...
New

Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New

Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
New

Here’s the story how one of the world’s first production deployments of LiveView came to be - and how trying to improve it almost caused ...
New

If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol:
bre...
New

Author Spotlight:
Peter Ullrich
@PJUllrich
Data is at the core of every business, but it is useless if nobody can access and analyze ...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /ruby
- /wasm
- /erlang
- /phoenix
- /keyboards
- /rails
- /js
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /emacs
- /java
- /haskell
- /onivim
- /svelte
- /typescript
- /crystal
- /c-plus-plus
- /kotlin
- /tailwind
- /gleam
- /react
- /ocaml
- /elm
- /flutter
- /vscode
- /ash
- /opensuse
- /centos
- /php
- /deepseek
- /html
- /scala
- /zig
- /textmate
- /sublime-text
- /debian
- /nixos
- /lisp
- /agda
- /react-native
- /kubuntu
- /arch-linux
- /ubuntu
- /revery
- /django
- /manjaro
- /spring
- /diversity
- /nodejs
- /lua
- /c
- /slackware
- /julia
- /neovim