Skip to content

Commit

Permalink
Extend IterableOnce in FactTable and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff May authored and jeffmay committed Sep 27, 2022
1 parent 3b4be66 commit 97523fb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
4 changes: 3 additions & 1 deletion core-v1/src/main/scala/data/FactTable.scala
Expand Up @@ -14,7 +14,7 @@ import scala.collection.immutable.SortedMap
*
* @note some expressions can update the fact table for sub-expressions.
*/
trait FactTable extends Any {
trait FactTable extends Any with IterableOnce[Fact] {
protected type Self <: FactTable

protected def build(factsByName: SortedMap[String, FactSet]): Self
Expand Down Expand Up @@ -44,6 +44,8 @@ trait FactTable extends Any {
matchingFacts.reduceOption(_ | _).map(TypedFactSet.from).getOrElse(Set.empty)
}

final override def iterator: Iterator[Fact] = factsByName.valuesIterator.flatMap(_.iterator)

// TODO: Better support for formatting here
override def toString: String = if (factsByName.isEmpty) "FactTable.empty" else {
val factMap = factsByName.iterator.map {
Expand Down
30 changes: 30 additions & 0 deletions core-v1/src/test/scala/data/FactTableSpec.scala
@@ -0,0 +1,30 @@
package com.rallyhealth.vapors.v1

package data

import munit.FunSuite

import java.time.LocalDate

class FactTableSpec extends FunSuite {

import example.FactTypes._

test("FactTable.iterator removes duplicates") {
val allFacts = Seq[Fact](Age(23), Age(23), DateOfBirth(LocalDate.now()))
val factTable = FactTable(allFacts)
assertEquals(factTable.iterator.toSeq.sorted, allFacts.distinct.sorted)
}

test("FactTable.toSet") {
val allFacts = Seq[Fact](Age(23), DateOfBirth(LocalDate.now()), Age(24))
val factTable = FactTable(allFacts)
assertEquals(factTable.iterator.toSet, allFacts.toSet)
}

test("FactTable.toSeq") {
val allFacts = Seq[Fact](Age(23), DateOfBirth(LocalDate.now()), Age(24))
val factTable = FactTable(allFacts)
assertEquals(factTable.iterator.toSeq.sorted, allFacts.sorted)
}
}

0 comments on commit 97523fb

Please sign in to comment.