Skip to content

Commit

Permalink
github #74 ObjectAssert lenient equals assertions ignore inherited fi…
Browse files Browse the repository at this point in the history
…elds
  • Loading branch information
joel-costigliola committed Sep 9, 2012
1 parent e6c191d commit adf6064
Show file tree
Hide file tree
Showing 7 changed files with 288 additions and 103 deletions.
47 changes: 27 additions & 20 deletions src/main/java/org/fest/assertions/api/ObjectAssert.java
@@ -1,14 +1,14 @@
/*
* Created on Dec 26, 2010
*
* 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
* 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.
* 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 @2010-2011 the original author or authors.
*/
Expand All @@ -34,10 +34,11 @@ protected ObjectAssert(T actual) {
}

/**
* Assert that the actual object is lenient equals to given one by only comparing actual and <b>not null</b> other fields.
* Assert that the actual object is lenient equals to given one by comparing only actual and <b>not null</b> other
* fields (including inherited fields).
* <p>
* It means that if an actual field is not null and the corresponding field in other is null, field will be ignored by lenient
* comparison, but the inverse will make assertion fail (null field in actual, not null in other).
* It means that if an actual field is not null and the corresponding field in other is null, field will be ignored by
* lenient comparison, but the inverse will make assertion fail (null field in actual, not null in other).
*
* <pre>
* Example:
Expand Down Expand Up @@ -65,20 +66,20 @@ public ObjectAssert<T> isLenientEqualsToByIgnoringNullFields(T other) {
}

/**
* Assert that the actual object is lenient equals to given one by only comparing actual and other on the given "accepted"
* fields only.
* Assert that the actual object is lenient equals to given one by only comparing actual and other on the given
* "accepted" fields only ("accepted" fields can be inherited fields).
* <p>
* Example:
*
* <pre>
* Example:
*
* TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);
* TolkienCharacter sam = new TolkienCharacter("Sam", 38, HOBBIT);
* TolkienCharacter frodo = new TolkienCharacter(&quot;Frodo&quot;, 33, HOBBIT);
* TolkienCharacter sam = new TolkienCharacter(&quot;Sam&quot;, 38, HOBBIT);
*
* // frodo and sam both are hobbits, so they are lenient equals on race
* assertThat(frodo).isLenientEqualsToByAcceptingFields(sam, "race"); //=> OK
* assertThat(frodo).isLenientEqualsToByAcceptingFields(sam, &quot;race&quot;); // =&gt; OK
*
* // ... but not when accepting name and race
* assertThat(frodo).isLenientEqualsToByAcceptingFields(sam, "name", "race"); //=> FAIL
* assertThat(frodo).isLenientEqualsToByAcceptingFields(sam, &quot;name&quot;, &quot;race&quot;); // =&gt; FAIL
*
* </pre>
*
Expand All @@ -96,8 +97,8 @@ public ObjectAssert<T> isLenientEqualsToByAcceptingFields(T other, String... fie
}

/**
* Assert that the actual object is lenient equals to given one by comparing actual and other fields except the given "ignored"
* fields.
* Assert that the actual object is lenient equals to given one by comparing actual and other fields (including
* inherited fields) except the given "ignored" fields.
*
* <pre>
* Example:
Expand Down Expand Up @@ -126,16 +127,22 @@ public ObjectAssert<T> isLenientEqualsToByIgnoringFields(T other, String... fiel
}

/**
* Assert that the actual object is equals fields by fields to another object.
* Assert that the actual object is equals fields by fields to another object, inherited fields are taken into
* account.
* <p>
* This can be handy if <code>equals</code> implementation of objects to compare does not suit you.
*
* <pre>
* Example:
*
* TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);
* TolkienCharacter frodoClone = new TolkienCharacter("Frodo", 33, HOBBIT);
*
* // Fail if equals has not been overriden in TolkienCharacter as equals default implementation only compares references
* assertThat(frodo).isEqualsTo(frodoClone); // Fail if equals has not been overriden in TolkienCharacter
*
* // frodo and frodoClone are equals by comparing fields
* assertThat(frodo).isLenientEqualsToByIgnoringFields(frodoClone); //=> OK
* assertThat(frodo).isEqualsToByComparingFields(frodoClone); // OK
*
* </pre>
*
Expand Down
42 changes: 30 additions & 12 deletions src/main/java/org/fest/assertions/error/ShouldBeInstance.java
@@ -1,37 +1,55 @@
/*
* Created on Dec 26, 2010
*
* 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
* 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.
* 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 @2010-2011 the original author or authors.
*/
package org.fest.assertions.error;

/**
* Creates an error message indicating that an assertion that verifies that an object is an instance of some type failed.
* Creates an error message indicating that an assertion that verifies that an object is an instance of some type
* failed.
*
* @author Alex Ruiz
* @author Joel Costigliola
*/
public class ShouldBeInstance extends BasicErrorMessageFactory {

/**
* Creates a new </code>{@link ShouldBeInstance}</code>.
* @param actual the actual value in the failed assertion.
* @param type the type {@code actual} is expected to belong to.
*
* @param object the object value in the failed assertion.
* @param type the type {@code object} is expected to belong to.
* @return the created {@code ErrorMessageFactory}.
*/
public static ErrorMessageFactory shouldBeInstance(Object actual, Class<?> type) {
return new ShouldBeInstance(actual, type);
public static ErrorMessageFactory shouldBeInstance(Object object, Class<?> type) {
return new ShouldBeInstance(object, type);
}

private ShouldBeInstance(Object actual, Class<?> type) {
super("expected <%s> to be an instance of:<%s> but was instance of:<%s>", actual, type, actual.getClass());
/**
* Creates a new </code>{@link ShouldBeInstance}</code> when object we want to check type is null.
*
* @param objectDescription the description of the null object we wanted to check type.
* @param type the expected type.
* @return the created {@code ErrorMessageFactory}.
*/
public static ErrorMessageFactory shouldBeInstanceButWasNull(String objectDescription, Class<?> type) {
return new ShouldBeInstance(objectDescription, type);
}

private ShouldBeInstance(Object object, Class<?> type) {
super("expected <%s> to be an instance of:<%s> but was instance of:<%s>", object, type, object.getClass());
}

private ShouldBeInstance(String objectDescription, Class<?> type) {
super("expected %s object to be an instance of:<%s> but was null", objectDescription, type);
}
}

0 comments on commit adf6064

Please sign in to comment.