-
-
Save SDiamante13/a49a578af8698152a919a0e08e6e67c4 to your computer and use it in GitHub Desktop.
Extract a method called isDivisibleBy. This will increase the readability of the code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package tech.pathtoprogramming.tdd; | |
| import org.junit.jupiter.params.ParameterizedTest; | |
| import org.junit.jupiter.params.provider.CsvSource; | |
| import static org.assertj.core.api.Assertions.assertThat; | |
| class AYearShould { | |
| @ParameterizedTest | |
| @CsvSource({ | |
| "2001,false", | |
| "1996,true", | |
| "1992,true", | |
| "1900,false", | |
| "2000,true" | |
| }) | |
| void determineWhetherAGivenYearIsALeapYearOrNot(int givenYear, boolean isExpectedLeapYear) { | |
| boolean isLeapYear = Year.isLeapYear(givenYear); | |
| assertThat(isLeapYear).isEqualTo(isExpectedLeapYear); | |
| } | |
| } | |
| class Year { | |
| public static boolean isLeapYear(int year) { | |
| if (isDivisibleBy(year, 4) && (year % 100 != 0 || isDivisibleBy(year, 400))) { | |
| return true; | |
| } | |
| return false; | |
| } | |
| private static boolean isDivisibleBy(int year, int four) { | |
| return year % four == 0; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment