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: CompositeAttribute, CompositeValue #18

Merged
merged 5 commits into from
Feb 6, 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
29 changes: 25 additions & 4 deletions src/main/kotlin/com/nftco/flow/sdk/cadence/json-cadence.kt
Original file line number Diff line number Diff line change
Expand Up @@ -484,14 +484,35 @@ open class CompositeField(type: String, value: CompositeValue) : Field<Composite
operator fun contains(name: String): Boolean = value?.getField<Field<*>>(name) != null
}

open class CompositeAttribute(val name: String, val value: Field<*>) : Serializable
open class CompositeAttribute(val name: String, val value: Field<*>) : Serializable {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is CompositeAttribute) return false

if (name != other.name) return false
if (value != other.value) return false

return true
}

override fun hashCode(): Int {
var result = name.hashCode()
result = 31 * result + value.hashCode()
return result
}
}
open class CompositeValue(val id: String, val fields: Array<CompositeAttribute>) : Serializable {
@Suppress("UNCHECKED_CAST")
fun <T : Field<*>> getField(name: String): T? = fields.find { it.name == name }?.value as T?
fun <T : Field<*>> getRequiredField(name: String): T = getField(name) ?: throw IllegalStateException("Value for $name not found")

@Suppress("UNCHECKED_CAST")
operator fun <T> get(name: String): T? = getField<Field<*>>(name)?.value as T?
inline operator fun <reified T : Field<*>> get(name: String): T? {
val field = fields.find { it.name == name }?.value
return if (field is T) {
field
} else {
null
}
}
operator fun contains(name: String): Boolean = fields.find { it.name == name } != null
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.nftco.flow.sdk.cadence.fields.composite

import com.nftco.flow.sdk.cadence.CompositeAttribute
import com.nftco.flow.sdk.cadence.StringField
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotEquals
import org.junit.jupiter.api.Test

class JsonCadenceBuilderCompositeAttributeTest {
@Test
fun `Test creating CompositeAttribute with valid values`() {
val stringValue = StringField("test")
val compositeAttribute = CompositeAttribute("name", stringValue)

assertEquals("name", compositeAttribute.name)
assertEquals(stringValue, compositeAttribute.value)
}

@Test
fun `Test hashCode`() {
val stringValue1 = StringField("test")
val stringValue2 = StringField("test")
val compositeAttribute1 = CompositeAttribute("name", stringValue1)
val compositeAttribute2 = CompositeAttribute("name", stringValue2)

assertEquals(compositeAttribute1.hashCode(), compositeAttribute2.hashCode())
}

@Test
fun `Test equals`() {
val stringValue1 = StringField("test")
val stringValue2 = StringField("test")
val stringValue3 = StringField("anotherTest")
val compositeAttribute1 = CompositeAttribute("name", stringValue1)
val compositeAttribute2 = CompositeAttribute("name", stringValue2)
val compositeAttribute3 = CompositeAttribute("name", stringValue3)

assertEquals(compositeAttribute1, compositeAttribute2)
assertNotEquals(compositeAttribute1, compositeAttribute3)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.nftco.flow.sdk.cadence.fields.composite

import com.nftco.flow.sdk.cadence.CompositeAttribute
import com.nftco.flow.sdk.cadence.CompositeValue
import com.nftco.flow.sdk.cadence.IntNumberField
import com.nftco.flow.sdk.cadence.StringField
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows

class JsonCadenceBuilderCompositeValueTest {
@Test
fun `Test getField`() {
val stringValue = StringField("test")
val compositeAttribute = CompositeAttribute("name", stringValue)
val compositeValue = CompositeValue("id", arrayOf(compositeAttribute))

assertEquals(stringValue, compositeValue.getField<StringField>("name"))
assertNull(compositeValue.getField<IntNumberField>("nonExistentField"))
}

@Test
fun `Test getRequiredField`() {
val stringValue = StringField("test")
val compositeAttribute = CompositeAttribute("name", stringValue)
val compositeValue = CompositeValue("id", arrayOf(compositeAttribute))

assertEquals(stringValue, compositeValue.getRequiredField<StringField>("name"))
assertThrows<IllegalStateException> {
compositeValue.getRequiredField<IntNumberField>("nonExistentField")
}
}

@Test
fun `Test operator get`() {
val stringValue = StringField("test")
val compositeAttribute = CompositeAttribute("name", stringValue)
val compositeValue = CompositeValue("id", arrayOf(compositeAttribute))

assertEquals(stringValue, compositeValue["name"])
assertNull(compositeValue[IntNumberField::class.java.simpleName])
assertNull(compositeValue["nonExistentField"])
}

@Test
fun `Test operator contains`() {
val stringValue = StringField("test")
val compositeAttribute = CompositeAttribute("name", stringValue)
val compositeValue = CompositeValue("id", arrayOf(compositeAttribute))

assertEquals(true, "name" in compositeValue)
assertEquals(false, "nonExistentField" in compositeValue)
}
}
Loading