Skip to content

Commit

Permalink
Override equals function for Dynamic Value, Add comprehensive tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BijenderKumar1 committed Jun 30, 2024
1 parent ba471e6 commit 02f9a7d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
33 changes: 32 additions & 1 deletion tests/shared/src/test/scala-2/zio/schema/DynamicValueSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ import zio.test.{ Sized, TestConfig, _ }

object DynamicValueSpec extends ZIOSpecDefault {

case class Person(name: String, age: Int)

object Person {
implicit val schema: Schema[Person] = DeriveSchema.gen[Person]
}

case class User(name: String, age: Int)

object User {
implicit val schema: Schema[User] = DeriveSchema.gen[User]
}

def spec: Spec[Environment, Any] =
suite("DynamicValueSpec")(
suite("round-trip")(
Expand Down Expand Up @@ -101,12 +113,31 @@ object DynamicValueSpec extends ZIOSpecDefault {
}
} @@ TestAspect.size(250) @@ TestAspect.ignore
),
suite("hashCode consistency")(
suite("hashCode and equality consistency")(
test("hashCode does not change") {
val primitive1 = DynamicValue.Primitive(123, StandardType.IntType)
val hash1 = primitive1.hashCode()
val hash2 = primitive1.hashCode()
assert(hash1)(equalTo(hash2))
},
test("equivalent DynamicValues have same hashCode and equality") {
val person1 = Person("John Doe", 42)
val person2 = Person("John Doe", 42)

val dynamicPerson1 = DynamicValue.fromSchemaAndValue(Person.schema, person1)
val dynamicPerson2 = DynamicValue.fromSchemaAndValue(Person.schema, person2)

assert(dynamicPerson1.hashCode())(equalTo(dynamicPerson2.hashCode())) &&
assert(dynamicPerson1)(equalTo(dynamicPerson2))
},
test("different DynamicValues are not equal") {
val person1 = Person("John Doe", 42)
val person2 = Person("Jane Doe", 42)

val dynamicPerson1 = DynamicValue.fromSchemaAndValue(Person.schema, person1)
val dynamicPerson2 = DynamicValue.fromSchemaAndValue(Person.schema, person2)

assert(dynamicPerson1)(not(equalTo(dynamicPerson2)))
}
)
)
Expand Down
37 changes: 37 additions & 0 deletions zio-schema/shared/src/main/scala/zio/schema/DynamicValue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,43 @@ sealed trait DynamicValue {
case DynamicValue.Error(message) => message.hashCode()
case DynamicValue.Singleton(instance) => instance.hashCode()
}
override def equals(obj: Any): Boolean = obj match {
case that: DynamicValue =>
(this, that) match {
case (DynamicValue.Primitive(value1, standardType1), DynamicValue.Primitive(value2, standardType2)) =>
value1 == value2 && standardType1 == standardType2
case (DynamicValue.Record(id1, values1), DynamicValue.Record(id2, values2)) =>
id1 == id2 && values1 == values2
case (DynamicValue.Enumeration(id1, value1), DynamicValue.Enumeration(id2, value2)) =>
id1 == id2 && value1 == value2
case (DynamicValue.Sequence(values1), DynamicValue.Sequence(values2)) =>
values1 == values2
case (DynamicValue.Dictionary(entries1), DynamicValue.Dictionary(entries2)) =>
entries1 == entries2
case (DynamicValue.SetValue(values1), DynamicValue.SetValue(values2)) =>
values1 == values2
case (DynamicValue.SomeValue(value1), DynamicValue.SomeValue(value2)) =>
value1 == value2
case (DynamicValue.NoneValue, DynamicValue.NoneValue) =>
true
case (DynamicValue.Tuple(left1, right1), DynamicValue.Tuple(left2, right2)) =>
left1 == left2 && right1 == right2
case (DynamicValue.LeftValue(value1), DynamicValue.LeftValue(value2)) =>
value1 == value2
case (DynamicValue.RightValue(value1), DynamicValue.RightValue(value2)) =>
value1 == value2
case (DynamicValue.BothValue(left1, right1), DynamicValue.BothValue(left2, right2)) =>
left1 == left2 && right1 == right2
case (DynamicValue.DynamicAst(ast1), DynamicValue.DynamicAst(ast2)) =>
ast1 == ast2
case (DynamicValue.Error(message1), DynamicValue.Error(message2)) =>
message1 == message2
case (DynamicValue.Singleton(instance1), DynamicValue.Singleton(instance2)) =>
instance1 == instance2
case _ => false
}
case _ => false
}

}

Expand Down

0 comments on commit 02f9a7d

Please sign in to comment.