Skip to content

Commit

Permalink
Improves the factoring of IonReaderContinuableTopLevelBinaryTest.java.
Browse files Browse the repository at this point in the history
  • Loading branch information
tgregg committed Oct 17, 2023
1 parent 7f9a8ef commit e01b348
Show file tree
Hide file tree
Showing 4 changed files with 655 additions and 600 deletions.
43 changes: 29 additions & 14 deletions test/com/amazon/ion/impl/IonCursorTestUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,18 @@ static <T extends IonCursorBinary> void assertSequence(T cursor, ExpectationProv
}

/**
* Provides Expectations that verify that advancing the cursor positions it on a container value with a field name
* that matches the given expectation, and that the container's child values match the given expectations, without
* filling the container up-front.
* Provides Expectations that verify that advancing the cursor positions it on a container value that matches the
* given expectation, and that the container's child values match the given expectations, without filling the
* container up-front.
*/
@SafeVarargs
@SuppressWarnings("unchecked")
static <T extends IonCursorBinary> ExpectationProvider<T> containerField(Expectation<T> expectedField, ExpectationProvider<T>... expectations) {
static <T extends IonCursorBinary> ExpectationProvider<T> container(Expectation<T> expectedOnContainer, ExpectationProvider<T>... expectations) {
return consumer -> {
consumer.accept((Expectation<T>) CONTAINER_START);
consumer.accept(expectedField);
if (expectedOnContainer != NO_EXPECTATION) {
consumer.accept(expectedOnContainer);
}
consumer.accept((Expectation<T>) STEP_IN);
for (Consumer<Consumer<Expectation<T>>> expectation : expectations) {
expectation.accept(consumer);
Expand All @@ -125,18 +127,20 @@ static <T extends IonCursorBinary> ExpectationProvider<T> containerField(Expecta
@SafeVarargs
@SuppressWarnings("unchecked")
static <T extends IonCursorBinary> ExpectationProvider<T> container(ExpectationProvider<T>... expectations) {
return containerField((Expectation<T>) NO_EXPECTATION, expectations);
return container((Expectation<T>) NO_EXPECTATION, expectations);
}

/**
* Provides an Expectation that verifies that advancing the cursor positions it on a scalar value with a field name
* that matches the given expectation, without filling that scalar.
* Provides an Expectation that verifies that advancing the cursor positions it on a scalar value that matches the
* given expectation, without filling that scalar.
*/
@SuppressWarnings("unchecked")
static <T extends IonCursorBinary> ExpectationProvider<T> scalarField(Expectation<T> expectedField) {
static <T extends IonCursorBinary> ExpectationProvider<T> scalar(Expectation<T> expectedOnScalar) {
return consumer -> {
consumer.accept((Expectation<T>) SCALAR);
consumer.accept(expectedField);
if (expectedOnScalar != NO_EXPECTATION) {
consumer.accept(expectedOnScalar);
}
};
}

Expand All @@ -146,14 +150,25 @@ static <T extends IonCursorBinary> ExpectationProvider<T> scalarField(Expectatio
*/
@SuppressWarnings("unchecked")
static <T extends IonCursorBinary> ExpectationProvider<T> scalar() {
return scalarField((Expectation<T>) NO_EXPECTATION);
return scalar((Expectation<T>) NO_EXPECTATION);
}

/**
* Provides an Expectation that verifies that the value on which the cursor is currently positioned has the given
* type.
*/
static <T extends IonReaderContinuableCoreBinary> ExpectationProvider<T> type(IonType expectedType) {
return consumer -> consumer.accept(new Expectation<>(
String.format("type(%s)", expectedType),
cursor -> assertEquals(expectedType, cursor.getType()))
);
}

/**
* Provides Expectations that verify that advancing the cursor to the next value positions the cursor on a scalar
* with type int and the given expected value.
*/
static <T extends IonReaderContinuableCoreBinary> ExpectationProvider<T> intValue(int expectedValue) {
static <T extends IonReaderContinuableCoreBinary> ExpectationProvider<T> fillIntValue(int expectedValue) {
return consumer -> consumer.accept(new Expectation<>(
String.format("int(%d)", expectedValue),
reader -> {
Expand All @@ -168,7 +183,7 @@ static <T extends IonReaderContinuableCoreBinary> ExpectationProvider<T> intValu
* Provides Expectations that verify that advancing the cursor to the next value positions the cursor on a scalar
* with type string and the given expected value.
*/
static <T extends IonReaderContinuableCoreBinary> ExpectationProvider<T> stringValue(String expectedValue) {
static <T extends IonReaderContinuableCoreBinary> ExpectationProvider<T> fillStringValue(String expectedValue) {
return consumer -> consumer.accept(new Expectation<>(
String.format("string(%s)", expectedValue),
reader -> {
Expand All @@ -183,7 +198,7 @@ static <T extends IonReaderContinuableCoreBinary> ExpectationProvider<T> stringV
* Provides Expectations that verify that advancing the cursor to the next value positions the cursor on a scalar
* with type symbol and the given expected value.
*/
static <T extends IonReaderContinuableApplicationBinary> ExpectationProvider<T> symbolValue(String expectedValue) {
static <T extends IonReaderContinuableApplicationBinary> ExpectationProvider<T> fillSymbolValue(String expectedValue) {
return consumer -> consumer.accept(new Expectation<>(
String.format("symbol(%s)", expectedValue),
reader -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
import static com.amazon.ion.impl.IonCursorTestUtilities.container;
import static com.amazon.ion.impl.IonCursorTestUtilities.endContainer;
import static com.amazon.ion.impl.IonCursorTestUtilities.endStream;
import static com.amazon.ion.impl.IonCursorTestUtilities.intValue;
import static com.amazon.ion.impl.IonCursorTestUtilities.fillIntValue;
import static com.amazon.ion.impl.IonCursorTestUtilities.scalar;
import static com.amazon.ion.impl.IonCursorTestUtilities.scalarField;
import static com.amazon.ion.impl.IonCursorTestUtilities.symbolValue;
import static com.amazon.ion.impl.IonCursorTestUtilities.scalar;
import static com.amazon.ion.impl.IonCursorTestUtilities.fillSymbolValue;

public class IonReaderContinuableApplicationBinaryTest {

Expand Down Expand Up @@ -63,7 +63,7 @@ private static Expectation<IonReaderContinuableApplicationBinary> fieldName(Stri
* name, without filling the scalar.
*/
private static ExpectationProvider<IonReaderContinuableApplicationBinary> scalarFieldName(String expectedFieldName) {
return scalarField(fieldName(expectedFieldName));
return IonCursorTestUtilities.scalar(fieldName(expectedFieldName));
}

@ParameterizedTest(name = "constructFromBytes={0}")
Expand All @@ -79,7 +79,7 @@ public void basicContainer(boolean constructFromBytes) {
assertSequence(
reader,
container(
scalarFieldName("name"), intValue(1),
scalarFieldName("name"), fillIntValue(1),
endContainer()
),
endStream()
Expand All @@ -97,8 +97,8 @@ public void basicSystemSymbols(boolean constructFromBytes) {
);
assertSequence(
reader,
scalar(), symbolValue("name"),
scalar(), symbolValue("version")
scalar(), fillSymbolValue("name"),
scalar(), fillSymbolValue("version")
);
}

Expand All @@ -121,8 +121,8 @@ public void basicLocalSymbols(boolean constructFromBytes) {
);
assertSequence(
reader,
scalar(), symbolValue("A"),
scalar(), symbolValue("B")
scalar(), fillSymbolValue("A"),
scalar(), fillSymbolValue("B")
);
}

Expand Down
31 changes: 13 additions & 18 deletions test/com/amazon/ion/impl/IonReaderContinuableCoreBinaryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,22 @@
import org.junit.jupiter.params.provider.ValueSource;

import java.io.ByteArrayInputStream;
import java.util.function.Consumer;

import static com.amazon.ion.BitUtils.bytes;
import static com.amazon.ion.IonCursor.Event.START_CONTAINER;
import static com.amazon.ion.IonCursor.Event.VALUE_READY;
import static com.amazon.ion.impl.IonCursorTestUtilities.STANDARD_BUFFER_CONFIGURATION;
import static com.amazon.ion.impl.IonCursorTestUtilities.Expectation;
import static com.amazon.ion.impl.IonCursorTestUtilities.ExpectationProvider;
import static com.amazon.ion.impl.IonCursorTestUtilities.STEP_IN;
import static com.amazon.ion.impl.IonCursorTestUtilities.STEP_OUT;
import static com.amazon.ion.impl.IonCursorTestUtilities.assertSequence;
import static com.amazon.ion.impl.IonCursorTestUtilities.container;
import static com.amazon.ion.impl.IonCursorTestUtilities.containerField;
import static com.amazon.ion.impl.IonCursorTestUtilities.container;
import static com.amazon.ion.impl.IonCursorTestUtilities.endContainer;
import static com.amazon.ion.impl.IonCursorTestUtilities.endStream;
import static com.amazon.ion.impl.IonCursorTestUtilities.fillContainer;
import static com.amazon.ion.impl.IonCursorTestUtilities.intValue;
import static com.amazon.ion.impl.IonCursorTestUtilities.fillIntValue;
import static com.amazon.ion.impl.IonCursorTestUtilities.scalar;
import static com.amazon.ion.impl.IonCursorTestUtilities.scalar;
import static com.amazon.ion.impl.IonCursorTestUtilities.scalarField;
import static com.amazon.ion.impl.IonCursorTestUtilities.startContainer;
import static com.amazon.ion.impl.IonCursorTestUtilities.stringValue;
import static com.amazon.ion.impl.IonCursorTestUtilities.fillStringValue;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class IonReaderContinuableCoreBinaryTest {
Expand Down Expand Up @@ -68,7 +63,7 @@ private static Expectation<IonReaderContinuableCoreBinary> fieldSid(int expected
* SID, without filling the scalar.
*/
private static ExpectationProvider<IonReaderContinuableCoreBinary> scalarFieldSid(int expectedFieldSid) {
return scalarField(fieldSid(expectedFieldSid));
return IonCursorTestUtilities.scalar(fieldSid(expectedFieldSid));
}


Expand All @@ -79,7 +74,7 @@ private static ExpectationProvider<IonReaderContinuableCoreBinary> scalarFieldSi
*/
@SafeVarargs
private static ExpectationProvider<IonReaderContinuableCoreBinary> containerFieldSid(int expectedFieldSid, ExpectationProvider<IonReaderContinuableCoreBinary>... expectations) {
return containerField(fieldSid(expectedFieldSid), expectations);
return IonCursorTestUtilities.container(fieldSid(expectedFieldSid), expectations);
}

@ParameterizedTest(name = "constructFromBytes={0}")
Expand All @@ -95,7 +90,7 @@ public void basicContainer(boolean constructFromBytes) {
assertSequence(
reader,
container(
scalarFieldSid(4), intValue(1),
scalarFieldSid(4), fillIntValue(1),
endContainer()
),
endStream()
Expand All @@ -113,8 +108,8 @@ public void basicStrings(boolean constructFromBytes) {
);
assertSequence(
reader,
scalar(), stringValue("foo"),
scalar(), stringValue("bar"),
scalar(), fillStringValue("foo"),
scalar(), fillStringValue("bar"),
endStream()
);
}
Expand Down Expand Up @@ -190,7 +185,7 @@ public void basicTopLevelSkipThenConsume(boolean constructFromBytes) {
assertSequence(
reader,
startContainer(),
scalar(), intValue(3),
scalar(), fillIntValue(3),
endStream()
);
}
Expand All @@ -214,7 +209,7 @@ public void nestedContainers(boolean constructFromBytes) {
containerFieldSid(3,
scalar()
),
scalar(), intValue(1)
scalar(), fillIntValue(1)
),
endStream()
);
Expand All @@ -239,7 +234,7 @@ public void fillContainerAtDepth0(boolean constructFromBytes) {
containerFieldSid(3,
scalar()
),
scalar(), intValue(1)
scalar(), fillIntValue(1)
),
endStream()
);
Expand Down Expand Up @@ -289,7 +284,7 @@ public void fillContainerThenSkip(boolean constructFromBytes) {
reader,
fillContainer(IonType.STRUCT),
container(
scalar(), intValue(0),
scalar(), fillIntValue(0),
endContainer()
),
endStream()
Expand Down
Loading

0 comments on commit e01b348

Please sign in to comment.