-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
159 additions
and
0 deletions.
There are no files selected for viewing
159 changes: 159 additions & 0 deletions
159
src/test/java/com/casper/sdk/e2e/steps/NestedTupleStepDefinitions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package com.casper.sdk.e2e.steps; | ||
|
||
import com.casper.sdk.e2e.exception.NotImplementedException; | ||
import com.casper.sdk.model.clvalue.*; | ||
import dev.oak3.sbs4j.exception.ValueSerializationException; | ||
import io.cucumber.java.en.And; | ||
import io.cucumber.java.en.Given; | ||
import io.cucumber.java.en.Then; | ||
import org.javatuples.Pair; | ||
import org.javatuples.Triplet; | ||
import org.javatuples.Unit; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.core.Is.is; | ||
|
||
/** | ||
* Step definitions for nested Tuple values. | ||
* | ||
* @author [email protected] | ||
*/ | ||
public class NestedTupleStepDefinitions { | ||
|
||
public static final String FIRST = "first"; | ||
public static final String SECOND = "second"; | ||
public static final String THIRD = "third"; | ||
private CLValueTuple2 tuple2Root; | ||
private CLValueTuple1 tuple1Root; | ||
private CLValueTuple3 tuple3Root; | ||
|
||
@Given("that a nested Tuple1 is defined as \\(\\({long})) using U32 numeric values") | ||
public void thatANestedTupleIsDefinedAsUsingUNumericValue(long arg0) throws ValueSerializationException { | ||
final CLValueTuple1 innerTuple = new CLValueTuple1(Unit.with(new CLValueU32(arg0))); | ||
tuple1Root = new CLValueTuple1(Unit.with(innerTuple)); | ||
} | ||
|
||
@Then("the {string} element of the Tuple{int} is {string}") | ||
public void theNthElementOfTheNestedTupleXIs(final String elementIndex, final int tuple, final String tupleStr) { | ||
final AbstractCLValue<?, ?> clValue = getTupleValue(tuple, elementIndex.toLowerCase()); | ||
final List<Long> actualTupleValues = getTupleValues(clValue); | ||
final List<Long> expectedTupleValues = getTupleValues(tupleStr); | ||
assertThat(actualTupleValues, is(expectedTupleValues)); | ||
} | ||
|
||
@Then("the {string} element of the Tuple{int} is {long}") | ||
public void theNthElementOfTheTupleXIs(final String elementIndex, final int tuple, final long expected) { | ||
final AbstractCLValue<?, ?> clValue = getTupleValue(tuple, elementIndex.toLowerCase()); | ||
final long actual = (Long) clValue.getValue(); | ||
assertThat(actual, is(expected)); | ||
} | ||
|
||
@Given("that a nested Tuple2 is defined as \\({long}, \\({long}, \\({long}, {long}))) using U32 numeric values") | ||
public void thatATupleIsDefinedAs(final long arg0, final long arg1, final long arg2, final long arg3) throws ValueSerializationException { | ||
final CLValueTuple2 innerTuple2 = new CLValueTuple2(Pair.with(new CLValueU32(arg2), new CLValueU32(arg3))); | ||
final CLValueTuple2 innerTuple1 = new CLValueTuple2(Pair.with(new CLValueU32(arg1), innerTuple2)); | ||
tuple2Root = new CLValueTuple2(Pair.with(new CLValueU32(arg0), innerTuple1)); | ||
} | ||
|
||
@And("the Tuple{long} bytes are {string}") | ||
public void theTuplesBytesAre(final long tuple, final String hexBytes) { | ||
assertThat(getTuple(tuple).getBytes(), is(hexBytes)); | ||
} | ||
|
||
@Given("that a nested Tuple3 is defined as \\({long}, {long}, \\({long}, {long}, \\({long}, {long}, {long}))) using U32 numeric values") | ||
public void thatANestedTupleIsDefinedAsUsingUNumericValues(long arg0, long arg1, long arg2, long arg3, long arg4, long arg5, long arg6) throws ValueSerializationException { | ||
|
||
tuple3Root = new CLValueTuple3(Triplet.with( | ||
new CLValueU32(arg0), | ||
new CLValueU32(arg1), | ||
new CLValueTuple3(Triplet.with( | ||
new CLValueU32(arg2), | ||
new CLValueU32(arg3), | ||
new CLValueTuple3(Triplet.with( | ||
new CLValueU32(arg4), | ||
new CLValueU32(arg5), | ||
new CLValueU32(arg6) | ||
)) | ||
)) | ||
)); | ||
} | ||
|
||
private AbstractCLValue<?, ?> getTuple(final long tuple) { | ||
if (tuple == 1) { | ||
return tuple1Root; | ||
} else if (tuple == 2) { | ||
return tuple2Root; | ||
} else if (tuple == 3) { | ||
return tuple3Root; | ||
} else { | ||
throw new NotImplementedException("Tuple " + tuple + " not implemented"); | ||
} | ||
} | ||
|
||
private AbstractCLValue<?, ?> getTupleValue(final long tuple, final String valueIndex) { | ||
if (tuple == 1) { | ||
if (FIRST.equals(valueIndex)) { | ||
return tuple1Root.getValue().getValue0(); | ||
} | ||
|
||
} else if (tuple == 2) { | ||
if (FIRST.equals(valueIndex)) { | ||
return tuple2Root.getValue().getValue0(); | ||
} else if (SECOND.equals(valueIndex)) { | ||
return tuple2Root.getValue().getValue1(); | ||
} | ||
} else if (tuple == 3) { | ||
if (FIRST.equals(valueIndex)) { | ||
return tuple3Root.getValue().getValue0(); | ||
} else if (SECOND.equals(valueIndex)) { | ||
return tuple3Root.getValue().getValue1(); | ||
} else if (THIRD.equals(valueIndex)) { | ||
return tuple3Root.getValue().getValue2(); | ||
} | ||
} | ||
|
||
throw new NotImplementedException("Tuple " + tuple + " " + valueIndex + " not implemented"); | ||
} | ||
|
||
private List<Long> getTupleValues(final AbstractCLValue<?,?> tuple) { | ||
return getTupleValues(tuple, new ArrayList<>()); | ||
} | ||
|
||
private List<Long> getTupleValues(String str) { | ||
str = str.replaceAll("\\(", ""); | ||
str = str.replaceAll("\\)", ""); | ||
|
||
final List<Long> tupleValues = new ArrayList<>(); | ||
for (String s : str.split(",")) { | ||
tupleValues.add(Long.valueOf(s.trim())); | ||
} | ||
return tupleValues; | ||
} | ||
|
||
private List<Long> getTupleValues(final AbstractCLValue<?, ?> tuple, final ArrayList<Long> tupleValues) { | ||
if (tuple instanceof CLValueTuple1) { | ||
processTupleValue(tupleValues, ((CLValueTuple1) tuple).getValue().getValue0()); | ||
} else if (tuple instanceof CLValueTuple2) { | ||
processTupleValue(tupleValues, ((CLValueTuple2) tuple).getValue().getValue0()); | ||
processTupleValue(tupleValues, ((CLValueTuple2) tuple).getValue().getValue1()); | ||
} else if (tuple instanceof CLValueTuple3) { | ||
processTupleValue(tupleValues, ((CLValueTuple3) tuple).getValue().getValue0()); | ||
processTupleValue(tupleValues, ((CLValueTuple3) tuple).getValue().getValue1()); | ||
processTupleValue(tupleValues, ((CLValueTuple3) tuple).getValue().getValue2()); | ||
} | ||
return tupleValues; | ||
} | ||
|
||
private void processTupleValue(final ArrayList<Long> tupleValues, final Object value) { | ||
if (value instanceof CLValueU32) { | ||
tupleValues.add(((CLValueU32) value).getValue()); | ||
} else { | ||
getTupleValues((AbstractCLValue<?, ?>) value, tupleValues); | ||
} | ||
} | ||
} | ||
|
||
|