Skip to content

Commit

Permalink
Fix MatchError bug with sealed trait -> open trait refactor (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffmay authored Oct 21, 2022
1 parent 42f8f1f commit 7006e5c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
14 changes: 4 additions & 10 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 @@ -126,11 +123,8 @@ object DerivedFact {
): 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 _ => None
}
def unapply(fact: DerivedFact): Some[(FactType[fact.Value], fact.Value, Evidence)] =
Some((fact.typeInfo, fact.value, fact.evidence))
}

/**
Expand Down
43 changes: 43 additions & 0 deletions core-v1/src/test/scala/data/FactSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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)
}
}
}

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 7006e5c

Please sign in to comment.