Skip to content

Commit

Permalink
Fix MatchError bug with sealed trait -> open trait refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff May committed Oct 21, 2022
1 parent 42f8f1f commit bd1a3c8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
11 changes: 4 additions & 7 deletions core-v1/src/main/scala/data/Fact.scala
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ object TypedFact {
evidence: Evidence,
): TypedFact[A] with DerivedFact = DerivedFactOfType(typeInfo, value, evidence)

def unapply[A](fact: TypedFact[A]): Some[(FactType[A], A)] = fact match {
case SourceFactOfType(typeInfo, value) => Some((typeInfo, value))
case DerivedFactOfType(typeInfo, value, _) => Some((typeInfo, value))
}
def unapply[A](fact: TypedFact[A]): Some[(FactType[A], A)] = Some((fact.typeInfo, fact.value))

def orderTypedFactByValue[T]: Order[TypedFact[T]] = { (x, y) =>
x.typeInfo.order.compare(x.value, y.value)
Expand All @@ -113,7 +110,7 @@ object TypedFact {
*
* The [[Evidence]] is tracked from the original set of [[Fact]]s based on the expression.
*/
sealed trait DerivedFact extends Fact {
trait DerivedFact extends Fact {
def evidence: Evidence
}

Expand All @@ -127,8 +124,8 @@ object DerivedFact {
DerivedFactOfType(typeInfo, value, evidence)

def unapply(fact: Fact): Option[(FactType[fact.Value], fact.Value, Evidence)] = fact match {
case DerivedFactOfType(_, _, evidence) =>
Some((fact.typeInfo, fact.value, evidence))
case derived: DerivedFact =>
Some((fact.typeInfo, fact.value, derived.evidence))
case _ => None
}
}
Expand Down
45 changes: 45 additions & 0 deletions core-v1/src/test/scala/data/FactSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.rallyhealth.vapors.v1

package data

import munit.FunSuite
import example.FactTypes

import java.time.LocalDate

class FactSpec extends FunSuite {

test("Fact extractor matches CustomFact") {
val f = CustomFact(FactTypes.Age, 23)
f match {
case Fact(tpe, value) =>
assert(tpe == f.typeInfo && value == f.value)
}
}

test("DerivedFact extractor matches CustomDerivedFact") {
val yearsAgo23 = FactTypes.DateOfBirth(LocalDate.now().minusYears(23).withDayOfYear(1))
val f = CustomDerivedFact(FactTypes.Age, 23, Evidence(yearsAgo23))
f match {
case DerivedFact(tpe, value, evidence) =>
assert(tpe == f.typeInfo && value == f.value && evidence == f.evidence)
case _ =>
fail(s"Expected $f to match a DerivedFact")
}
}
}

final case class CustomFact[T](
typeInfo: FactType[T],
value: T,
) extends Fact {
override type Value = T
}

final case class CustomDerivedFact[T](
typeInfo: FactType[T],
value: T,
evidence: Evidence,
) extends DerivedFact {
override type Value = T
}

0 comments on commit bd1a3c8

Please sign in to comment.