Skip to content

Commit

Permalink
Switch to assertj for exceptions (#2502)
Browse files Browse the repository at this point in the history
* make error handling use AssertJ for exceptions

* Adhere to coding style

* Completely switch to AssertJ

* make import explicit

* Switch to AssertJ in n-th prime

* Use AssertJ in the forth exercise

* Use AssertJ in the ledger exercise

* Use AssertJ in the OCR exercise

* Use AssertJ in the palindrom calculator exercise

* Use AssertJ in the phone number exercise

* Use AssertJ in sgf parsing exercise

* Use AssertJ in simple linked list exercise

* Use AssertJ in triangle exercise

* Add missing paranthesis

* Use AssertJ in Custom Set exercise

* Use AssertJ in Leap exercise

* Use AssertJ in Luhn Validator exercise

* Use AssertJ in Bracket Checker exercise

* Use AssertJ in Pangram Checker exercise

* Use AssertJ in Parallel Letter Frequency exercise

* Use AssertJ in Queen Attack Calculator exercise

* Update exercises/practice/sgf-parsing/src/test/java/SgfParsingTest.java

Co-authored-by: Sander Ploegsma <[email protected]>

* Update exercises/practice/sgf-parsing/src/test/java/SgfParsingTest.java

Co-authored-by: Sander Ploegsma <[email protected]>

* Remove debug output

* Remove BDD-style comments

* be more verbose in tests

---------

Co-authored-by: Oliver Keszöcze <[email protected]>
Co-authored-by: Sander Ploegsma <[email protected]>
  • Loading branch information
3 people authored Sep 29, 2023
1 parent eca5f62 commit 529b75a
Show file tree
Hide file tree
Showing 17 changed files with 447 additions and 723 deletions.
130 changes: 66 additions & 64 deletions exercises/practice/custom-set/src/test/java/CustomSetTest.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

import org.junit.Ignore;
import org.junit.Test;
Expand Down
337 changes: 100 additions & 237 deletions exercises/practice/forth/src/test/java/ForthEvaluatorTest.java

Large diffs are not rendered by default.

21 changes: 10 additions & 11 deletions exercises/practice/leap/src/test/java/LeapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;

public class LeapTest {

Expand All @@ -16,55 +15,55 @@ public void setup() {

@Test
public void testYearNotDivBy4InCommonYear() {
assertFalse(leap.isLeapYear(2015));
assertThat(leap.isLeapYear(2015)).isFalse();
}

@Ignore("Remove to run test")
@Test
public void testYearDivBy2NotDivBy4InCommonYear() {
assertFalse(leap.isLeapYear(1970));
assertThat(leap.isLeapYear(1970)).isFalse();
}

@Ignore("Remove to run test")
@Test
public void testYearDivBy4NotDivBy100InLeapYear() {
assertTrue(leap.isLeapYear(1996));
assertThat(leap.isLeapYear(1996)).isTrue();
}

@Ignore("Remove to run test")
@Test
public void testYearDivBy4And5InLeapYear() {
assertTrue(leap.isLeapYear(1960));
assertThat(leap.isLeapYear(1960)).isTrue();
}

@Ignore("Remove to run test")
@Test
public void testYearDivBy100NotDivBy400InCommonYear() {
assertFalse(leap.isLeapYear(2100));
assertThat(leap.isLeapYear(2100)).isFalse();
}

@Ignore("Remove to run test")
@Test
public void testYearDivBy100NotDivBy3IsNotLeapYear() {
assertFalse(leap.isLeapYear(1900));
assertThat(leap.isLeapYear(1900)).isFalse();
}

@Ignore("Remove to run test")
@Test
public void testYearDivBy400InLeapYear() {
assertTrue(leap.isLeapYear(2000));
assertThat(leap.isLeapYear(2000)).isTrue();
}

@Ignore("Remove to run test")
@Test
public void testYearDivBy400NotDivBy125IsLeapYear() {
assertTrue(leap.isLeapYear(2400));
assertThat(leap.isLeapYear(2400)).isTrue();
}

@Ignore("Remove to run test")
@Test
public void testYearDivBy200NotDivBy400InCommonYear() {
assertFalse(leap.isLeapYear(1800));
assertThat(leap.isLeapYear(1800)).isFalse();
}

}
84 changes: 20 additions & 64 deletions exercises/practice/ledger/src/test/java/LedgerTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;


public class LedgerTest {

Expand All @@ -24,232 +24,188 @@ public void setUp() throws Exception {
@Ignore("Remove to run test")
@Test
public void emptyLedgerUS() {
// given
var entries = new Ledger.LedgerEntry[]{};

// when
String actual = ledger.format(USD_CURRENCY, US_LOCALE, entries);

// then
String expected = "Date | Description | Change ";
Assert.assertEquals(expected, actual);
assertThat(actual).isEqualTo(expected);
}

@Ignore("Remove to run test")
@Test
public void emptyLedgerNL() {
// given
var entries = new Ledger.LedgerEntry[]{};

// when
String actual = ledger.format(EUR_CURRENCY, NL_LOCALE, entries);

// then
String expected = "Datum | Omschrijving | Verandering ";
Assert.assertEquals(expected, actual);
assertThat(actual).isEqualTo(expected);
}

@Ignore("Remove to run test")
@Test
public void oneEntry() {
// given
var entries = new Ledger.LedgerEntry[]{
ledger.createLedgerEntry("2015-02-01", "Buy present", -10.00)
};

// when
String actual = ledger.format(USD_CURRENCY, US_LOCALE, entries);

// then
String expected = "Date | Description | Change \n"
+ "02/01/2015 | Buy present | -$10.00";
Assert.assertEquals(expected, actual);
assertThat(actual).isEqualTo(expected);
}

@Ignore("Remove to run test")
@Test
public void creditAndDebit() {
// given
var entries = new Ledger.LedgerEntry[]{
ledger.createLedgerEntry("2015-01-02", "Get present", 10.00),
ledger.createLedgerEntry("2015-01-01", "Buy present", -10.00)
};

// when
String actual = ledger.format(USD_CURRENCY, US_LOCALE, entries);

// then
String expected = "Date | Description | Change \n" +
"01/01/2015 | Buy present | -$10.00\n" +
"01/02/2015 | Get present | $10.00";

Assert.assertEquals(expected, actual);
assertThat(actual).isEqualTo(expected);
}

@Ignore("Remove to run test")
@Test
public void multipleEntriesOnSameDateOrderedByDescription() {
// given
var entries = new Ledger.LedgerEntry[]{
ledger.createLedgerEntry("2015-01-01", "Get present", 10.00),
ledger.createLedgerEntry("2015-01-01", "Buy present", -10.00)
};

// when
String actual = ledger.format(USD_CURRENCY, US_LOCALE, entries);

// then
String expected = "Date | Description | Change \n" +
"01/01/2015 | Buy present | -$10.00\n" +
"01/01/2015 | Get present | $10.00";

Assert.assertEquals(expected, actual);
assertThat(actual).isEqualTo(expected);
}

@Ignore("Remove to run test")
@Test
public void finalOrderTieBreakerIsChange() {
// given
var entries = new Ledger.LedgerEntry[]{
ledger.createLedgerEntry("2015-01-01", "Something", 0),
ledger.createLedgerEntry("2015-01-01", "Something", -0.01),
ledger.createLedgerEntry("2015-01-01", "Something", 0.01)
};

// when
String actual = ledger.format(USD_CURRENCY, US_LOCALE, entries);

// then
String expected = "Date | Description | Change \n" +
"01/01/2015 | Something | -$0.01\n" +
"01/01/2015 | Something | $0.00\n" +
"01/01/2015 | Something | $0.01";
;

Assert.assertEquals(expected, actual);
assertThat(actual).isEqualTo(expected);
}

@Ignore("Remove to run test")
@Test
public void overlongDescriptions() {
// given
var entries = new Ledger.LedgerEntry[]{
ledger.createLedgerEntry("2015-01-01", "Freude schoner Gotterfunken", -1234.56)
};

// when
String actual = ledger.format(USD_CURRENCY, US_LOCALE, entries);

// then
String expected = "Date | Description | Change \n" +
"01/01/2015 | Freude schoner Gotterf... | -$1,234.56";

Assert.assertEquals(expected, actual);
assertThat(actual).isEqualTo(expected);
}

@Ignore("Remove to run test")
@Test
public void euros() {
// given
var entries = new Ledger.LedgerEntry[]{
ledger.createLedgerEntry("2015-01-01", "Buy present", -10.00)
};

// when
String actual = ledger.format(EUR_CURRENCY, US_LOCALE, entries);

// then
var expected = "Date | Description | Change \n" +
"01/01/2015 | Buy present | -€10.00";

Assert.assertEquals(expected, actual);
assertThat(actual).isEqualTo(expected);
}

@Ignore("Remove to run test")
@Test
public void dutchLocale() {
// given
var entries = new Ledger.LedgerEntry[]{
ledger.createLedgerEntry("2015-03-12", "Buy present", 1234.56)
};

// when
String actual = ledger.format(USD_CURRENCY, NL_LOCALE, entries);

// then
var expected = "Datum | Omschrijving | Verandering \n" +
"12/03/2015 | Buy present | $1.234,56";

Assert.assertEquals(expected, actual);
assertThat(actual).isEqualTo(expected);
}

@Ignore("Remove to run test")
@Test
public void dutchNegativeNumberWith3DigitsBeforeDecimalPoint() {
// given
var entries = new Ledger.LedgerEntry[]{
ledger.createLedgerEntry("2015-03-12", "Buy present", -123.45)
};

// when
String actual = ledger.format(USD_CURRENCY, NL_LOCALE, entries);

// then
var expected = "Datum | Omschrijving | Verandering \n" +
"12/03/2015 | Buy present | -$123,45";

Assert.assertEquals(expected, actual);
assertThat(actual).isEqualTo(expected);
}

@Ignore("Remove to run test")
@Test
public void americanNegativeNumberWith3DigitsBeforeDecimalPoint() {

// given
var entries = new Ledger.LedgerEntry[]{
ledger.createLedgerEntry("2015-03-12", "Buy present", -123.45)
};

// when
String actual = ledger.format(USD_CURRENCY, US_LOCALE, entries);

// then
var expected = "Date | Description | Change \n" +
"03/12/2015 | Buy present | -$123.45";

Assert.assertEquals(expected, actual);
assertThat(actual).isEqualTo(expected);
}

@Ignore("Remove to run test")
@Test
public void givenInvalidLocale_ThenException() {
// given
var entries = new Ledger.LedgerEntry[0];
var locale = "it-IT";

// when
IllegalArgumentException illegalArgumentException = assertThrows(
IllegalArgumentException.class,
() -> ledger.format(EUR_CURRENCY, locale, entries));

// then
assertEquals("Invalid locale", illegalArgumentException.getMessage());
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> ledger.format(EUR_CURRENCY, locale, entries))
.withMessage("Invalid locale");

}

@Ignore("Remove to run test")
@Test
public void givenInvalidCurrency_ThenException() {
// given
var entries = new Ledger.LedgerEntry[0];
var currency = "Pieces o' Eight";

// when
IllegalArgumentException illegalArgumentException = assertThrows(
IllegalArgumentException.class,
() -> ledger.format(currency, US_LOCALE, entries));

// then
assertEquals("Invalid currency", illegalArgumentException.getMessage());
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> ledger.format(currency, US_LOCALE, entries))
.withMessage("Invalid currency");
}
}
Loading

0 comments on commit 529b75a

Please sign in to comment.