forked from the-nft-company/flow-jvm-sdk
-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unit tests: Field, AddressField, VoidField #9
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fb3c632
Working on unit tests for Field, Address Field, Void Field
lealobanov ddaaef6
Merge branch 'main' of https://github.com/onflow/flow-jvm-sdk into fi…
lealobanov 43913ef
Additional tests on Field and VoidField
lealobanov 6d23847
Additional tests on AddressField
lealobanov d9a0f36
Fix test linting
lealobanov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
src/test/kotlin/com/nftco/flow/sdk/cadence/fields/JsonCadenceBuilderAddressFieldTest.kt
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,81 @@ | ||
package com.nftco.flow.sdk.cadence.fields | ||
|
||
import com.nftco.flow.sdk.FlowAddress | ||
import com.nftco.flow.sdk.cadence.AddressField | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Assertions.* | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertThrows | ||
|
||
class JsonCadenceBuilderAddressFieldTest { | ||
|
||
@Test | ||
fun `test decoding AddressField with decodeToAny`() { | ||
val addressValue = "0x0f7531409b1719ee" | ||
val addressField = AddressField(addressValue) | ||
|
||
assertDoesNotThrow { addressField.decodeToAny() } | ||
} | ||
|
||
@Test | ||
fun `test decoding AddressField to FlowAddress`() { | ||
val addressValue = "0x0f7531409b1719ee" | ||
val addressField = AddressField(addressValue) | ||
|
||
val result = addressField.decodeToAny() | ||
val expectedBytes = "15, 117, 49, 64, -101, 23, 25, -18".split(", ").map { it.toByte() }.toByteArray() | ||
|
||
assertThat(result).isInstanceOf(FlowAddress::class.java) | ||
assertThat((result as FlowAddress).bytes).isEqualTo(expectedBytes) | ||
} | ||
|
||
@Test | ||
fun `test decoding invalid AddressField with decodeToAny`() { | ||
val invalidAddressValue = "invalid_address" | ||
val addressField = AddressField(invalidAddressValue) | ||
|
||
assertThrows<Exception> { addressField.decodeToAny() } | ||
} | ||
|
||
@Test | ||
fun `Test equality for AddressField with the same value`() { | ||
val address1 = AddressField("0x123") | ||
val address2 = AddressField("0x123") | ||
assertEquals(address1, address2) | ||
assertEquals(address1.hashCode(), address2.hashCode()) | ||
} | ||
|
||
@Test | ||
fun `Test equality for AddressField with different values`() { | ||
val address1 = AddressField("0x123") | ||
val address2 = AddressField("0x456") | ||
assertNotEquals(address1, address2) | ||
assertNotEquals(address1.hashCode(), address2.hashCode()) | ||
} | ||
|
||
@Test | ||
fun `Test equality for AddressField with lowercase value`() { // need to investigate this case | ||
val address1 = AddressField("0xABC") | ||
val address2 = AddressField("0xabc") | ||
assertEquals(address1, address2) | ||
assertEquals(address1.hashCode(), address2.hashCode()) | ||
} | ||
|
||
@Test | ||
fun `Test equality for AddressField with bytes`() { | ||
val bytes = byteArrayOf(1, 2, 3) | ||
val address1 = AddressField(bytes) | ||
val address2 = AddressField("0x010203") | ||
assertEquals(address1, address2) | ||
assertEquals(address1.hashCode(), address2.hashCode()) | ||
} | ||
|
||
@Test | ||
fun `Test equality for AddressField with bytes and different values`() { | ||
val bytes = byteArrayOf(1, 2, 3) | ||
val address1 = AddressField(bytes) | ||
val address2 = AddressField("0x040506") | ||
assertNotEquals(address1, address2) | ||
assertNotEquals(address1.hashCode(), address2.hashCode()) | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/test/kotlin/com/nftco/flow/sdk/cadence/fields/JsonCadenceBuilderFieldTest.kt
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,70 @@ | ||
package com.nftco.flow.sdk.cadence.fields | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.nftco.flow.sdk.cadence.* | ||
import org.assertj.core.api.Assertions.assertThatThrownBy | ||
import org.junit.jupiter.api.Assertions.* | ||
import org.junit.jupiter.api.Test | ||
|
||
class JsonCadenceBuilderFieldTest { | ||
|
||
private val objectMapper = ObjectMapper() | ||
@Test | ||
fun `Test equality for Field with the same type and value`() { | ||
val field1 = SampleField(TYPE_STRING, "value") | ||
val field2 = SampleField(TYPE_STRING, "value") | ||
assertTrue(field1 == field2) | ||
assertTrue(field1.hashCode() == field2.hashCode()) | ||
} | ||
|
||
@Test | ||
fun `Test equality for Field with different values`() { | ||
val field1 = SampleField(TYPE_STRING, "value1") | ||
val field2 = SampleField(TYPE_STRING, "value2") | ||
assertFalse(field1 == field2) | ||
assertFalse(field1.hashCode() == field2.hashCode()) | ||
} | ||
|
||
@Test | ||
fun `Test equality for Field with different types`() { | ||
val field1 = SampleField(TYPE_STRING, "value") | ||
val field2 = SampleField(TYPE_INT, 42) | ||
assertFalse(field1 == field2) | ||
assertFalse(field1.hashCode() == field2.hashCode()) | ||
} | ||
|
||
@Test | ||
fun `Test equality for Field with null values`() { | ||
val field1 = SampleField(TYPE_STRING, null) | ||
val field2 = SampleField(TYPE_STRING, null) | ||
assertTrue(field1 == field2) | ||
assertTrue(field1.hashCode() == field2.hashCode()) | ||
} | ||
|
||
@Test | ||
fun `Test equality for Field with null value and non-null value`() { | ||
val field1 = SampleField(TYPE_STRING, null) | ||
val field2 = SampleField(TYPE_STRING, "value") | ||
assertFalse(field1 == field2) | ||
assertFalse(field1.hashCode() == field2.hashCode()) | ||
} | ||
|
||
@Test | ||
fun `Test equality for Field with different types and values`() { | ||
val field1 = SampleField(TYPE_STRING, "value") | ||
val field2 = SampleField(TYPE_INT, 42) | ||
assertFalse(field1 == field2) | ||
assertFalse(field1.hashCode() == field2.hashCode()) | ||
} | ||
|
||
@Test | ||
fun `Test decoding unsupported field throws exception`() { | ||
val unsupportedField = object : Field<Unit>("Unsupported", null) {} | ||
val expectedMessage = " Can't find right class " | ||
|
||
assertThatThrownBy { unsupportedField.decodeToAny() } | ||
.isInstanceOf(Exception::class.java) | ||
.hasMessage(expectedMessage) | ||
} | ||
private class SampleField(type: String, value: Any?) : Field<Any>(type, value) | ||
} |
42 changes: 42 additions & 0 deletions
42
src/test/kotlin/com/nftco/flow/sdk/cadence/fields/JsonCadenceBuilderVoidFieldTest.kt
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,42 @@ | ||
package com.nftco.flow.sdk.cadence.fields | ||
|
||
import com.nftco.flow.sdk.cadence.* | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Assertions.* | ||
import org.junit.jupiter.api.Test | ||
|
||
class JsonCadenceBuilderVoidFieldTest { | ||
|
||
@Test | ||
fun `Test equality of VoidField instances`() { | ||
val voidField1 = VoidField() | ||
val voidField2 = VoidField() | ||
|
||
assertEquals(voidField1, voidField2) | ||
} | ||
|
||
@Test | ||
fun `Test hashing of VoidField instances`() { | ||
val voidField1 = VoidField() | ||
val voidField2 = VoidField() | ||
|
||
assertEquals(voidField1.hashCode(), voidField2.hashCode()) | ||
} | ||
|
||
@Test | ||
fun `Test equality with other Field types`() { | ||
val voidField = VoidField() | ||
val stringField = StringField("test") | ||
|
||
assertNotEquals(voidField, stringField) | ||
} | ||
|
||
@Test | ||
fun `Test decoding VoidField returns null`() { | ||
val voidField = VoidField() | ||
|
||
val decodedValue: Any? = voidField.decodeToAny() | ||
|
||
assertThat(decodedValue).isNull() | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to investigate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@franklywatson do you by chance know if the SDK should handle case-insensitive wallet addresses? Currently both the address value and hashCode() are case-sensitive, so this case fails.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should thanks