Skip to content

Commit

Permalink
Merge 9109896 into 78e6808
Browse files Browse the repository at this point in the history
  • Loading branch information
VaughanJackson committed Mar 16, 2018
2 parents 78e6808 + 9109896 commit 2afd08d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
10 changes: 10 additions & 0 deletions src/main/java/org/exparity/hamcrest/beans/TheSameAs.java
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,8 @@ private void compareObjects(final Object expected, final Object actual, final St
final Type type = type(klass, new CapitalizedNamingStrategy());
if (type.isArray()) {
compareArrays(expected, actual, path, ctx);
} else if (type.isEnum()) {
compareEnums(expected, actual, path, ctx);
} else if (type.packageName().startsWith("java.lang")) {
compareLangTypes(expected, actual, path, ctx);
} else if (type.is(List.class)) {
Expand Down Expand Up @@ -601,6 +603,14 @@ private void compareArrays(final Object expected, final Object actual, final Str
}
}

private void compareEnums(final Object expected, final Object actual, final String path,
final MismatchContext ctx) {
LOG.debug("Compare path [{}] as enum", path);
if (actual != expected) {
ctx.addMismatch(expected, actual, path);
}
}

private void compareLangTypes(final Object expected, final Object actual, final String path,
final MismatchContext ctx) {
LOG.debug("Compare path [{}] as lang type", path);
Expand Down
22 changes: 15 additions & 7 deletions src/test/java/org/exparity/hamcrest/beans/TheSameAsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@
import org.exparity.hamcrest.BeanMatchers;
import org.exparity.hamcrest.beans.TheSameAs.PropertyComparator;
import org.exparity.hamcrest.beans.comparators.HasPattern;
import org.exparity.hamcrest.beans.testutils.types.ClassContainingNestedClasses;
import org.exparity.hamcrest.beans.testutils.types.NotBean;
import org.exparity.hamcrest.beans.testutils.types.ObjectWithAllTypes;
import org.exparity.hamcrest.beans.testutils.types.OuterClass;
import org.exparity.hamcrest.beans.testutils.types.SimpleType;
import org.exparity.hamcrest.beans.testutils.types.SimpleTypeWithList;
import org.exparity.hamcrest.beans.testutils.types.SimpleTypeWithSimpleType;
import org.exparity.hamcrest.beans.testutils.types.*;
import org.testng.annotations.Test;

/**
Expand Down Expand Up @@ -517,4 +511,18 @@ public void canTestNestedNullObjects() {
assertThat(actual, theSameAs(reference));
}

@Test
public void canCompareSameEnumValues() {
final SimpleEnum reference = SimpleEnum.VALUE_A;
final SimpleEnum actual = SimpleEnum.VALUE_A;
assertThat(actual, theSameAs(reference));
}

@Test(expectedExceptions = AssertionError.class)
public void canCompareDifferentEnumValues() {
final SimpleEnum reference = SimpleEnum.VALUE_A;
final SimpleEnum actual = SimpleEnum.VALUE_B;
assertThat(actual, theSameAs(reference));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.exparity.hamcrest.beans.testutils.types;

public enum SimpleEnum {
VALUE_A, VALUE_B
}

0 comments on commit 2afd08d

Please sign in to comment.