Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added assertions for LocalDate #20

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/java/org/assertj/jodatime/api/Assertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.assertj.jodatime.api;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;

/**
Expand All @@ -31,6 +32,10 @@ public static LocalDateTimeAssert assertThat(LocalDateTime localDateTime) {
return new LocalDateTimeAssert(LocalDateTimeAssert.class, localDateTime);
}

public static LocalDateAssert assertThat(LocalDate localDate) {
return new LocalDateAssert(LocalDateAssert.class, localDate);
}

/** Creates a new <code>{@link Assertions}</code>. */
protected Assertions() {
// empty
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/assertj/jodatime/api/DateTimeAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,15 @@ public DateTimeAssert hasMonthOfYear(int expectedMonthOfYear){
}

/**
* Verifies that the month of the actual {@code DateTime} is equal to the given month
* Verifies that the day of month of the actual {@code DateTime} is equal to the given day of month
* <p>
* Example :
* <pre><code class='java'> assertThat(new DateTime(&quot;2000-01-01&quot;)).hasDayOfMonth(1);</code></pre>
*
* @param expectedDayOfMonth the given month.
* @param expectedDayOfMonth the given day of month.
* @return this assertion object.
* @throws AssertionError if the actual {@code DateTime} is {@code null}.
* @throws AssertionError if the month of the actual {@code DateTime} is not equal to the given month.
* @throws AssertionError if the day of month of the actual {@code DateTime} is not equal to the given day of month.
*/
public DateTimeAssert hasDayOfMonth(int expectedDayOfMonth){
isNotNull();
Expand Down
407 changes: 407 additions & 0 deletions src/main/java/org/assertj/jodatime/api/LocalDateAssert.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected LocalDateTime getActual() {
*
* @param expectedYear the given year.
* @return this assertion object.
* @throws AssertionError if the actual {@code DateTime} is {@code null}.
* @throws AssertionError if the actual {@code LocalDateTime} is {@code null}.
* @throws AssertionError if the year of the actual {@code DateTime} is not equal to the given year.
*/
public LocalDateTimeAssert hasYear(int expectedYear) {
Expand Down Expand Up @@ -96,15 +96,15 @@ public LocalDateTimeAssert hasMonthOfYear(int expectedMonthOfYear) {
}

/**
* Verifies that the month of the actual {@code LocalDateTime} is equal to the given month
* Verifies that the day of month of the actual {@code LocalDateTime} is equal to the given day of month
* <p>
* Example :
* <pre><code class='java'> assertThat(new LocalDateTime(&quot;2000-01-01&quot;)).hasDayOfMonth(1);</code></pre>
*
* @param expectedDayOfMonth the given month.
* @param expectedDayOfMonth the given day of month.
* @return this assertion object.
* @throws AssertionError if the actual {@code LocalDateTime} is {@code null}.
* @throws AssertionError if the month of the actual {@code LocalDateTime} is not equal to the given month.
* @throws AssertionError if the day of month of the actual {@code LocalDateTime} is not equal to the given day of month.
*/
public LocalDateTimeAssert hasDayOfMonth(int expectedDayOfMonth) {
isNotNull();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2018 the original author or authors.
*/
package org.assertj.jodatime.api;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;

import org.joda.time.LocalDate;
import org.junit.Test;

/**
* Tests for <code>{@link Assertions#assertThat(LocalDate)}</code>.
*
* @author Evgenii Strepetov
*/
public class Assertions_assertThat_with_LocalDate_Test {

@Test
public void should_create_Assert() {
LocalDateAssert assertions = Assertions.assertThat(new LocalDate());
assertNotNull(assertions);
}

@Test
public void should_pass_actual() {
LocalDate localDate = new LocalDate();
LocalDateAssert assertions = Assertions.assertThat(localDate);
assertSame(localDate, assertions.getActual());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2018 the original author or authors.
*/
package org.assertj.jodatime.api.localdate;

import org.assertj.jodatime.api.JodaTimeBaseTest;
import org.assertj.jodatime.api.LocalDateAssert;
import org.joda.time.LocalDate;
import org.junit.experimental.theories.DataPoint;

import static org.junit.Assume.assumeTrue;

/**
* Base test class for {@link LocalDateAssert} tests.
*
* @author Evgenii Strepetov
*/
public class LocalDateAssertBaseTest extends JodaTimeBaseTest {

@DataPoint
public static LocalDate localDate1 = new LocalDate(2000, 12, 14);
@DataPoint
public static LocalDate localDate2 = new LocalDate(1999, 12, 14);
@DataPoint
public static LocalDate localDate3 = new LocalDate(1999, 12, 15);
@DataPoint
public static LocalDate localDate4 = new LocalDate(1999, 11, 14);

protected static void testAssumptions(LocalDate reference, LocalDate dateBefore, LocalDate dateAfter) {
assumeTrue(dateBefore.isBefore(reference));
assumeTrue(dateAfter.isAfter(reference));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2018 the original author or authors.
*/
package org.assertj.jodatime.api.localdate;

import org.joda.time.LocalDate;
import org.junit.Test;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;

import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.util.FailureMessages.actualIsNull;
import static org.assertj.jodatime.api.Assertions.assertThat;

/**
* @author Evgenii Strepetov
*/
@RunWith(Theories.class)
public class LocalDateAssert_isAfterOrEqualTo_Test extends LocalDateAssertBaseTest {

@Theory
public void test_isAfterOrEqual_assertion(LocalDate referenceDate, LocalDate dateBefore, LocalDate dateAfter) {
// GIVEN
testAssumptions(referenceDate, dateBefore, dateAfter);
// WHEN
assertThat(dateAfter).isAfterOrEqualTo(referenceDate);
assertThat(dateAfter).isAfterOrEqualTo(referenceDate.toString());
assertThat(referenceDate).isAfterOrEqualTo(referenceDate);
assertThat(referenceDate).isAfterOrEqualTo(referenceDate.toString());
// THEN
verify_that_isAfterOrEqual_assertion_fails_and_throws_AssertionError(dateBefore, referenceDate);
}

@Test
public void test_isAfterOrEqual_assertion_error_message() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since assertj-joda-time relies on assertj-core 3.x, we can take advantage to catchThrowable, that will make the test easier to read and not having to use failBecauseExpectedAssertionErrorWasNotThrown.
I'm keen on using a BBD style test (GIVEN, WHEN THEN).

This comment applies to any other test checking for an expected AssertionError

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored to assertThatThrownBy() everywhere it is applicable

assertThatThrownBy(() -> {
LocalDate actualLocalDate = new LocalDate(2000, 1, 5);
LocalDate localDateToCheck = new LocalDate(2012, 1, 1);
assertThat(actualLocalDate).isAfterOrEqualTo(localDateToCheck);
}).isInstanceOf(AssertionError.class)
.hasMessage(format("%nExpecting:%n <2000-01-05>%nto be after or equals to:%n <2012-01-01>%n"));
}

@Test
public void should_fail_if_both_actual_and_parameter_are_null() {
expectException(AssertionError.class, actualIsNull());
Copy link
Member

@joel-costigliola joel-costigliola Jun 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to use catchThrowable, the test is short enough to be readable (even though I'm not a big fan of expectException coming as the first instruction making the test a THEN, GIVEN, WHEN instead of GIVEN, WHEN, THEN).

LocalDate actual = null;
assertThat(actual).isAfterOrEqualTo((LocalDate) null);
}

@Test
public void should_fail_if_actual_is_null_and_parameter_is_not() {
expectException(AssertionError.class, actualIsNull());
LocalDate actual = null;
assertThat(actual).isAfterOrEqualTo(new LocalDate());
}

@Test
public void should_fail_if_actual_is_null_and_localDate_as_string_parameter_is_not() {
expectException(AssertionError.class, actualIsNull());
LocalDate actual = null;
assertThat(actual).isAfterOrEqualTo(new LocalDate().toString());
}

@Test
public void should_fail_if_localDate_parameter_is_null_but_actual_is_not() {
expectException(IllegalArgumentException.class, "The LocalDate to compare actual with should not be null");
assertThat(new LocalDate()).isAfterOrEqualTo((LocalDate) null);
}

@Test
public void should_fail_if_localDate_as_string_parameter_is_null() {
expectException(IllegalArgumentException.class,
"The String representing the LocalDate to compare actual with should not be null");
assertThat(new LocalDate()).isAfterOrEqualTo((String) null);
assertThat((LocalDate) null).isAfterOrEqualTo((String) null);
}

private static void verify_that_isAfterOrEqual_assertion_fails_and_throws_AssertionError(LocalDate dateToCheck,
LocalDate reference) {
assertThatThrownBy(() -> assertThat(dateToCheck).isAfterOrEqualTo(reference))
.isInstanceOf(AssertionError.class);

assertThatThrownBy(() -> assertThat(dateToCheck).isAfterOrEqualTo(reference.toString()))
.isInstanceOf(AssertionError.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2018 the original author or authors.
*/
package org.assertj.jodatime.api.localdate;

import org.joda.time.LocalDate;
import org.junit.Test;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;

import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.util.FailureMessages.actualIsNull;
import static org.assertj.jodatime.api.Assertions.assertThat;

/**
* @author Evgenii Strepetov
*/
@RunWith(Theories.class)
public class LocalDateAssert_isAfter_Test extends LocalDateAssertBaseTest {

@Theory
public void test_isAfter_assertion(LocalDate referenceDate, LocalDate dateBefore, LocalDate dateAfter) {
// GIVEN
testAssumptions(referenceDate, dateBefore, dateAfter);
// WHEN
assertThat(dateAfter).isAfter(referenceDate);
assertThat(dateAfter).isAfter(referenceDate.toString());
// THEN
verify_that_isAfter_assertion_fails_and_throws_AssertionError(referenceDate, referenceDate);
verify_that_isAfter_assertion_fails_and_throws_AssertionError(dateBefore, referenceDate);
}

@Test
public void test_isAfter_assertion_error_message() {
assertThatThrownBy(() -> {
LocalDate actualLocalDate = new LocalDate(2000, 1, 5);
LocalDate localDateToCheck = new LocalDate(2012, 1, 1);
assertThat(actualLocalDate).isAfter(localDateToCheck);
}).isInstanceOf(AssertionError.class)
.hasMessage(format("%nExpecting:%n <2000-01-05>%nto be strictly after:%n <2012-01-01>%n"));
}

@Test
public void should_fail_if_actual_is_null() {
LocalDate nullLocalDate = null;
LocalDate other = new LocalDate();

assertThatThrownBy(() -> assertThat(nullLocalDate).isAfter(other)).as("other=%s", other)
.hasMessage(actualIsNull());
assertThatThrownBy(() -> assertThat(nullLocalDate).isAfter(other.toString())).as("other=%s", other)
.hasMessage(actualIsNull());
}

@Test
public void should_fail_if_localDate_parameter_is_null() {
expectException(IllegalArgumentException.class, "The LocalDate to compare actual with should not be null");
assertThat(new LocalDate()).isAfter((LocalDate) null);
}

@Test
public void should_fail_if_localDate_as_string_parameter_is_null() {
expectException(IllegalArgumentException.class,
"The String representing the LocalDate to compare actual with should not be null");
assertThat(new LocalDate()).isAfter((String) null);
}

private static void verify_that_isAfter_assertion_fails_and_throws_AssertionError(LocalDate dateToCheck,
LocalDate reference) {
assertThatThrownBy(() -> assertThat(dateToCheck).isAfter(reference))
.isInstanceOf(AssertionError.class);

assertThatThrownBy(() -> assertThat(dateToCheck).isAfter(reference.toString()))
.isInstanceOf(AssertionError.class);
}
}
Loading