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

jon
Some minor things in the paper edition that says “3 2020” on the title page verso, not mentioned in the book’s errata online: p. 186 But...
New
lirux
Hi Jamis, I think there’s an issue with a test on chapter 6. I own the ebook, version P1.0 Feb. 2019. This test doesn’t pass for me: ...
New
cro
I am working on the “Your Turn” for chapter one and building out the restart button talked about on page 27. It recommends looking into ...
New
gilesdotcodes
In case this helps anyone, I’ve had issues setting up the rails source code. Here were the solutions: In Gemfile, change gem 'rails' t...
New
patoncrispy
I’m new to Rust and am using this book to learn more as well as to feed my interest in game dev. I’ve just finished the flappy dragon exa...
New
jgchristopher
“The ProductLive.Index template calls a helper function, live_component/3, that in turn calls on the modal component. ” Excerpt From: Br...
New
brunogirin
When trying to run tox in parallel as explained on page 151, I got the following error: tox: error: argument -p/–parallel: expected one...
New
oaklandgit
Hi, I completed chapter 6 but am getting the following error when running: thread 'main' panicked at 'Failed to load texture: IoError(O...
New
jwandekoken
Book: Programming Phoenix LiveView, page 142 (157/378), file lib/pento_web/live/product_live/form_component.ex, in the function below: d...
New
redconfetti
Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
New

Other popular topics Top

AstonJ
A thread that every forum needs! Simply post a link to a track on YouTube (or SoundCloud or Vimeo amongst others!) on a separate line an...
New
AstonJ
Inspired by this post from @Carter, which languages, frameworks or other tech or tools do you think is killing it right now? :upside_down...
New
AstonJ
I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me. I think the Planck and the Preonic (o...
New
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
AstonJ
Seems like a lot of people caught it - just wondered whether any of you did? As far as I know I didn’t, but it wouldn’t surprise me if I...
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
rustkas
Intensively researching Erlang books and additional resources on it, I have found that the topic of using Regular Expressions is either c...
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
New
AnfaengerAlex
Hello, I’m a beginner in Android development and I’m facing an issue with my project setup. In my build.gradle.kts file, I have the foll...
New

Latest in Functional Programming in Java, Second Edition

Functional Programming in Java, Second Edition Portal

Sub Categories: