dtonhofer

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());
    }

}

Where Next?

Popular Pragmatic Bookshelf topics Top

Alexandr
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
Mmm
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
joepstender
The generated iex result below should list products instead of product for the metadata. (page 67) iex&gt; product = %Product{} %Pento....
New
rmurray10127
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
leonW
I ran this command after installing the sample application: $ cards add do something --owner Brian And got a file not found error: Fil...
New
jskubick
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
brunogirin
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
New
dsmith42
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 “&gt;...
New
roadbike
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 Top

Devtalk
Reading something? Working on something? Planning something? Changing jobs even!? If you’re up for sharing, please let us know what you’...
1031 17377 381
New
Exadra37
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
PragmaticBookshelf
“Finding the Boundaries” Hero’s Journey with Noel Rappin @noelrappin Even when you’re ultimately right about what the future ho...
New
AstonJ
Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New
Margaret
Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
1143 25802 759
New
wmnnd
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
AstonJ
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
PragmaticBookshelf
Author Spotlight: Peter Ullrich @PJUllrich Data is at the core of every business, but it is useless if nobody can access and analyze ...
New

Latest in Functional Programming in Java, Second Edition

Functional Programming in Java, Second Edition Portal

Sub Categories: