Skip to content

Commit 7a3e134

Browse files
imetaxasimetaxas
imetaxas
authored and
imetaxas
committed
add object check
1 parent 1ac8d25 commit 7a3e134

File tree

6 files changed

+131
-6
lines changed

6 files changed

+131
-6
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[![Build Status](https://travis-ci.org/imetaxas/realitycheck.svg?branch=master)](https://travis-ci.org/imetaxas/realitycheck)
22
[![CodeCov Coverage](https://codecov.io/gh/imetaxas/realitycheck/graph/badge.svg?branch=master)](https://codecov.io/gh/imetaxas/realitycheck?branch=master)
33
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.yanimetaxas/realitycheck/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.yanimetaxas/realitycheck/)
4-
[![GitHub release](http://github-release-version.herokuapp.com/github/imetaxas/realitycheck/release.svg)](https://github.com/imetaxas/realitycheck/releases/latest)
54
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-green.svg)](http://makeapullrequest.com)
65
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
76

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.yanimetaxas.realitycheck;
2+
3+
/**
4+
* @author yanimetaxas
5+
* @since 24-Mar-18
6+
*/
7+
public final class ObjectCheck extends AbstractCheck<ObjectCheck, Object> {
8+
9+
ObjectCheck(Object object) {
10+
super(object, null);
11+
}
12+
13+
ObjectCheck(Object object, String customMessage) {
14+
super(object, customMessage);
15+
}
16+
}

src/main/java/com/yanimetaxas/realitycheck/Reality.java

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public static IntegerCheck checkThat(Integer integer) throws AssertionError {
5454
return new IntegerCheck(integer);
5555
}
5656

57+
public static ObjectCheck checkThat(Object object) throws AssertionError {
58+
return new ObjectCheck(object);
59+
}
60+
5761
public static BooleanCheck checkThat(Boolean bool) throws AssertionError {
5862
return new BooleanCheck(bool);
5963
}

src/main/java/com/yanimetaxas/realitycheck/StatementBuilder.java

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ public final IntegerCheck that(Integer actual) {
2323
return new IntegerCheck(actual, message);
2424
}
2525

26+
public final ObjectCheck that(Object actual) {
27+
return new ObjectCheck(actual, message);
28+
}
29+
2630
public final BooleanCheck that(Boolean actual) {
2731
return new BooleanCheck(actual, message);
2832
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.yanimetaxas.realitycheck;
2+
3+
import static org.junit.Assert.assertNotNull;
4+
5+
import java.io.Serializable;
6+
import java.math.BigDecimal;
7+
import java.util.Currency;
8+
import org.junit.Test;
9+
10+
/**
11+
* @author yanimetaxas
12+
* @since 24-Mar-18
13+
*/
14+
public class ObjectCheckTest {
15+
16+
@Test
17+
public void objectIsEqualTo() throws Exception {
18+
Object object = new Object();
19+
assertNotNull(new ObjectCheck(object).isEqualTo(object));
20+
}
21+
22+
@Test(expected = AssertionError.class)
23+
public void objectIsEqualTo_WhenIsNot() throws Exception {
24+
Object object1 = new Object();
25+
Object object2 = new Object();
26+
assertNotNull(new ObjectCheck(object1).isEqualTo(object2));
27+
}
28+
29+
@Test
30+
public void customObjectIsEqualTo() throws Exception {
31+
Money money = new Money(new BigDecimal(10.1), Currency.getInstance("EUR"));
32+
assertNotNull(new ObjectCheck(money).isEqualTo(money));
33+
}
34+
35+
@Test(expected = AssertionError.class)
36+
public void customObjectIsEqualTo_WhenIsNot() throws Exception {
37+
Money moneyEUR = new Money(new BigDecimal(10.1), Currency.getInstance("EUR"));
38+
Money moneySEK = new Money(new BigDecimal(10.1), Currency.getInstance("SEK"));
39+
assertNotNull(new ObjectCheck(moneyEUR).isEqualTo(moneySEK));
40+
}
41+
}
42+
43+
class Money implements Serializable, Comparable<Money> {
44+
45+
private BigDecimal amount;
46+
private Currency currency;
47+
48+
Money(BigDecimal amount, Currency currency) {
49+
this.amount = amount;
50+
this.currency = currency;
51+
}
52+
53+
private BigDecimal getAmount() {
54+
return amount;
55+
}
56+
57+
@Override
58+
public int compareTo(Money obj) {
59+
return getAmount().compareTo(obj.getAmount());
60+
}
61+
62+
@Override
63+
public boolean equals(Object o) {
64+
if (this == o) {
65+
return true;
66+
}
67+
if (o == null || getClass() != o.getClass()) {
68+
return false;
69+
}
70+
Money money = (Money) o;
71+
72+
return amount.equals(money.amount) && currency.equals(money.currency);
73+
}
74+
75+
@Override
76+
public int hashCode() {
77+
int result = amount.hashCode();
78+
result = 31 * result + currency.hashCode();
79+
return result;
80+
}
81+
}

src/test/java/com/yanimetaxas/realitycheck/RealityTest.java

+26-5
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,6 @@ public void checkWithMessageThatBooleanIsFalse_WhenIsNot() throws Exception {
115115
checkWithMessage("Boolean is false").that(true).isFalse();
116116
}
117117

118-
@Test
119-
public void checkWithMessageThatIntegerIsEqualTo() throws Exception {
120-
assertNotNull(checkWithMessage("Integer is 1").that(1).isEqualTo(1));
121-
}
122-
123118
@Test
124119
public void checkThatStringEqualsIgnoreCase() throws Exception {
125120
String string1 = "RandomString";
@@ -128,6 +123,11 @@ public void checkThatStringEqualsIgnoreCase() throws Exception {
128123
assertNotNull(checkThat(string1).equalsIgnoreCase(string2));
129124
}
130125

126+
@Test
127+
public void checkWithMessageThatIntegerIsEqualTo() throws Exception {
128+
assertNotNull(checkWithMessage("Integer is 1").that(1).isEqualTo(1));
129+
}
130+
131131
@Test
132132
public void checkWithMessageThatIntegerIsNotEqualTo_WhenIs() throws Exception {
133133
expectedEx.expect(AssertionError.class);
@@ -136,6 +136,21 @@ public void checkWithMessageThatIntegerIsNotEqualTo_WhenIs() throws Exception {
136136
checkWithMessage("Integer is wrong").that(1).isNotEqualTo(1);
137137
}
138138

139+
@Test
140+
public void checkWithMessageThatObjectIsEqualTo() throws Exception {
141+
Object object = new Object();
142+
assertNotNull(checkWithMessage("Object equals shelf").that(object).isEqualTo(object));
143+
}
144+
145+
@Test
146+
public void checkWithMessageThatObjectIsNotEqualTo_WhenIs() throws Exception {
147+
expectedEx.expect(AssertionError.class);
148+
expectedEx.expectMessage("Object is NOT equals shelf");
149+
150+
Object object = new Object();
151+
checkWithMessage("Object is NOT equals shelf").that(object).isNotEqualTo(object);
152+
}
153+
139154
@Test
140155
public void checkWithMessageThatInputStreamIsNullWhenIsNot() throws Exception {
141156
expectedEx.expect(AssertionError.class);
@@ -227,6 +242,12 @@ public void checkThatIntegerIsOne() throws Exception {
227242
assertNotNull(checkThat(1).isEqualTo(1).isNotNull());
228243
}
229244

245+
@Test
246+
public void checkThatObjectIsEqualTo() throws Exception {
247+
Object object = new Object();
248+
assertNotNull(checkThat(object).isEqualTo(object).isNotNull());
249+
}
250+
230251
@Test
231252
public void checkWithMessageThatCsvResourceHasNotSameContentAs() throws Exception {
232253
expectedEx.expect(AssertionError.class);

0 commit comments

Comments
 (0)