Skip to content

Commit

Permalink
Change ValueWrapper.create() to not wrap another ValueWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
leonard84 authored and marcphilipp committed Apr 18, 2019
1 parent 2ee4080 commit 96502f5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
15 changes: 14 additions & 1 deletion src/main/java/org/opentest4j/ValueWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,16 @@ public final class ValueWrapper implements Serializable {
*
* <p>If the supplied {@code value} is {@code null}, this method will return a
* cached {@code ValueWrapper} suitable for all {@code null} values.
* If the supplied {@code value} is already an instance of {@link ValueWrapper},
* it will be returned as is.
*
* @param value the value to wrap; may be {@code null}
* @return a wrapper for the supplied value; never {@code null}
*/
public static ValueWrapper create(Object value) {
return (value == null ? nullValueWrapper : new ValueWrapper(value));
if (value instanceof ValueWrapper)
return (ValueWrapper) value;
return (value == null) ? nullValueWrapper : new ValueWrapper(value);
}

/**
Expand All @@ -66,6 +70,10 @@ public static ValueWrapper create(Object value) {
*
* <p>If the supplied {@code value} is {@code null}, this method will return a
* cached {@code ValueWrapper} suitable for all {@code null} values.
* If the supplied {@code value} is already an instance of {@link ValueWrapper},
* it will be returned as is if the {@code stringRepresentation} match, otherwise
* the original value will be unwrapped and a new {@code ValueWrapper} with the
* new {@code stringRepresentation} will be created.
*
* @param value the value to wrap; may be {@code null}
* @param stringRepresentation a custom rendering of the value; will fallback to
Expand All @@ -74,6 +82,11 @@ public static ValueWrapper create(Object value) {
* @since 1.2
*/
public static ValueWrapper create(Object value, String stringRepresentation) {
if (value instanceof ValueWrapper) {
ValueWrapper wrapper = (ValueWrapper) value;
return wrapper.stringRepresentation.equals(stringRepresentation) ? wrapper
: create(wrapper.value, stringRepresentation);
}
return (value == null ? nullValueWrapper : new ValueWrapper(value, stringRepresentation));
}

Expand Down
33 changes: 28 additions & 5 deletions src/test/java/org/opentest4j/ValueWrapperTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@

package org.opentest4j;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

import java.io.Serializable;

Expand Down Expand Up @@ -73,6 +69,33 @@ public void acceptsCustomStringRepresentation() {
assertTrue(wrapper.toString().endsWith(")"));
}

@Test
public void doesNotWrapAnotherValueWrapper() {
ValueWrapper wrapper = ValueWrapper.create(1.2d);

ValueWrapper same = ValueWrapper.create(wrapper);

assertSame(wrapper, same);
}

@Test
public void doesNotWrapAnotherValueWrapperWithSameCustomRepresentation() {
ValueWrapper wrapper = ValueWrapper.create(1.2d, "1,20");

ValueWrapper same = ValueWrapper.create(wrapper, "1,20");

assertSame(wrapper, same);
}

@Test
public void doesRepackageValueWrapperWithDifferentStringRepresentation() {
ValueWrapper wrapper = ValueWrapper.create(1.2d);

ValueWrapper same = ValueWrapper.create(wrapper, "1,20");

assertNotSame(wrapper, same);
}

@Test
public void nullForCustomStringRepresentationFallsBackToDefaultToString() {
ValueWrapper wrapper = ValueWrapper.create(1.2d, null);
Expand Down

0 comments on commit 96502f5

Please sign in to comment.