Skip to content
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 5 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to investigate

Copy link
Collaborator Author

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.

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

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())
}
}
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)
}
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()
}
}
Loading