-
-
Notifications
You must be signed in to change notification settings - Fork 708
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add isAtSameInstantAs to OffsetDateTime assertion
- Loading branch information
1 parent
25ab08f
commit a12dc51
Showing
5 changed files
with
187 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
47 changes: 47 additions & 0 deletions
47
src/main/java/org/assertj/core/error/ShouldBeAtSameInstant.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* 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-2019 the original author or authors. | ||
*/ | ||
package org.assertj.core.error; | ||
|
||
import java.time.OffsetDateTime; | ||
|
||
/** | ||
* Creates an error message indicating that an assertion that verifies that two {@link OffsetDateTime OffsetDateTimes} | ||
* have the same {@link java.time.Instant}, failed. | ||
* | ||
* @author Raymond Pressly | ||
*/ | ||
public class ShouldBeAtSameInstant extends BasicErrorMessageFactory { | ||
|
||
/** | ||
* Creates a new <code>{@link ShouldBeAtSameInstant}</code>. | ||
* @param actual the actual value in the failed assertion. | ||
* @param other the expected value in the failed assertion. | ||
* @return the created {@code ErrorMessageFactory}. | ||
*/ | ||
public static ErrorMessageFactory shouldBeAtSameInstant(OffsetDateTime actual, OffsetDateTime other) { | ||
return new ShouldBeAtSameInstant(actual, other); | ||
} | ||
|
||
private ShouldBeAtSameInstant(OffsetDateTime actual, OffsetDateTime expected) { | ||
super("%nExpecting%n" + | ||
" <%s>%n" + | ||
"to be at the same instant as:%n" + | ||
" <%s>%n" + | ||
"but actual instance was%n" + | ||
" <%s>%n" + | ||
"and expected instant was:%n" + | ||
" <%s>", | ||
actual, expected, actual.toInstant(), expected.toInstant()); | ||
} | ||
|
||
} |
This file contains 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
60 changes: 60 additions & 0 deletions
60
...java/org/assertj/core/api/offsetdatetime/OffsetDateTimeAssert_isAtSameInstantAs_Test.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* 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-2019 the original author or authors. | ||
*/ | ||
package org.assertj.core.api.offsetdatetime; | ||
|
||
import static java.time.OffsetDateTime.now; | ||
import static org.assertj.core.api.AbstractOffsetDateTimeAssert.NULL_OFFSET_DATE_TIME_PARAMETER_MESSAGE; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; | ||
import static org.assertj.core.error.ShouldBeAtSameInstant.shouldBeAtSameInstant; | ||
import static org.assertj.core.util.AssertionsUtil.expectAssertionError; | ||
import static org.assertj.core.util.FailureMessages.actualIsNull; | ||
|
||
import java.time.OffsetDateTime; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@DisplayName("OffsetDateTimeAssert isAtSameInstantAs") | ||
class OffsetDateTimeAssert_isAtSameInstantAs_Test extends OffsetDateTimeAssertBaseTest { | ||
|
||
@Test | ||
void should_pass_if_at_the_same_instant() { | ||
assertThat(REFERENCE).isAtSameInstantAs(SAME_INSTANT) | ||
.isAtSameInstantAs(REFERENCE); | ||
} | ||
|
||
@Test | ||
void should_fail_if_at_a_different_instant() { | ||
// WHEN | ||
AssertionError assertionError = expectAssertionError(() -> assertThat(REFERENCE).isAtSameInstantAs(DIFFERENT_INSTANT)); | ||
// THEN | ||
assertThat(assertionError).hasMessage(shouldBeAtSameInstant(REFERENCE, DIFFERENT_INSTANT).create()); | ||
} | ||
|
||
@Test | ||
void should_fail_if_actual_is_null() { | ||
// GIVEN | ||
OffsetDateTime actual = null; | ||
// WHEN | ||
AssertionError assertionError = expectAssertionError(() -> assertThat(actual).isAtSameInstantAs(now())); | ||
// THEN | ||
assertThat(assertionError).hasMessage(actualIsNull()); | ||
} | ||
|
||
@Test | ||
void should_fail_if_given_OffsetDateTime_is_null() { | ||
assertThatIllegalArgumentException().isThrownBy(() -> assertThat(REFERENCE).isAtSameInstantAs(null)) | ||
.withMessage(NULL_OFFSET_DATE_TIME_PARAMETER_MESSAGE); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/test/java/org/assertj/core/error/ShouldBeAtSameInstant_create_Test.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* 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-2019 the original author or authors. | ||
*/ | ||
package org.assertj.core.error; | ||
|
||
import static java.lang.String.format; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.error.ShouldBeAtSameInstant.shouldBeAtSameInstant; | ||
import static org.assertj.core.presentation.StandardRepresentation.STANDARD_REPRESENTATION; | ||
|
||
import java.time.OffsetDateTime; | ||
import java.time.ZoneOffset; | ||
|
||
import org.assertj.core.description.TextDescription; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@DisplayName("ShouldBeAtSameInstant create") | ||
class ShouldBeAtSameInstant_create_Test { | ||
|
||
@Test | ||
void should_create_error_message() { | ||
// GIVEN | ||
OffsetDateTime actual = OffsetDateTime.of(2000, 12, 14, 0, 0, 0, 0, ZoneOffset.UTC); | ||
OffsetDateTime other = OffsetDateTime.of(2000, 12, 14, 0, 0, 0, 0, ZoneOffset.ofHours(-3)); | ||
// WHEN | ||
String message = shouldBeAtSameInstant(actual, other).create(new TextDescription("Test"), STANDARD_REPRESENTATION); | ||
// THEN | ||
assertThat(message).isEqualTo(format("[Test] %n" + | ||
"Expecting%n" + | ||
" <2000-12-14T00:00Z>%n" + | ||
"to be at the same instant as:%n" + | ||
" <2000-12-14T00:00-03:00>%n" + | ||
"but actual instance was%n" + | ||
" <2000-12-14T00:00:00Z>%n" + | ||
"and expected instant was:%n" + | ||
" <2000-12-14T03:00:00Z>")); | ||
} | ||
} |