-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TransactionInfo.transactionType(), JavaDoc comments and tests
TransactionInfo.transactionType() returns Optional<TransactionType> Optional<TransactionType> is the recommended type for handling a TransactionType enum. With the forthcoming "Pattern Matching for Switch" it will be possible to use it in switch statements very elegantly. Tests have been added as well as extensive JavaDoc with code examples for how to use this variable type in earlier (and admittedly more verbose) versions of Java.
- Loading branch information
1 parent
31f1e22
commit 4d3f432
Showing
4 changed files
with
136 additions
and
1 deletion.
There are no files selected for viewing
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
40 changes: 40 additions & 0 deletions
40
omnij-core/src/test/groovy/foundation/omni/tx/TransactionTypeSpec.groovy
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,40 @@ | ||
package foundation.omni.tx | ||
|
||
import spock.lang.Specification | ||
|
||
import foundation.omni.tx.Transactions.TransactionType | ||
|
||
/** | ||
* Tests for {@link TransactionType} | ||
*/ | ||
class TransactionTypeSpec extends Specification { | ||
|
||
void "switch test"(int code, boolean expectedResult) { | ||
given: "An Optional-wrapped Transaction Type enum" | ||
Optional<TransactionType> optionalType = TransactionType.find(code) | ||
|
||
when: "We use a switch expression to calculate a boolean value " | ||
boolean isSend = switch(optionalType.orElse(null)) { | ||
case TransactionType.SIMPLE_SEND, | ||
TransactionType.SEND_TO_OWNERS, | ||
TransactionType.SEND_ALL -> true | ||
case null -> false | ||
default -> false | ||
} | ||
|
||
then: "We get the correct result" | ||
isSend == expectedResult | ||
|
||
where: | ||
code | expectedResult | ||
// Transaction codes that are "sends" | ||
0 | true | ||
3 | true | ||
4 | true | ||
// Defined Transactions that are not "sends" | ||
20 | false | ||
25 | false | ||
// Undefined transaction codes | ||
300 | false | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
omnij-core/src/test/java/foundation/omni/tx/TransactionTypeTest.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,38 @@ | ||
package foundation.omni.tx; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.Optional; | ||
|
||
import foundation.omni.tx.Transactions.TransactionType; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
/** | ||
* Tests for {@link Transactions.TransactionType} | ||
*/ | ||
public class TransactionTypeTest { | ||
|
||
// TODO: Upgrade to JUnit 5 and make this a parameterized test, like its Spock equivalent | ||
@Test | ||
public void switchTest() { | ||
Optional<TransactionType> optionalType = TransactionType.find(0); | ||
|
||
boolean isSend; | ||
if (optionalType.isPresent()) { | ||
switch (optionalType.get()) { | ||
case SIMPLE_SEND: | ||
case SEND_TO_OWNERS: | ||
case SEND_ALL: | ||
isSend = true; | ||
break; | ||
default: | ||
isSend = false; | ||
break; | ||
} | ||
} else { | ||
isSend = false; | ||
} | ||
|
||
assertTrue(isSend); | ||
} | ||
} |
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