Skip to content

Commit

Permalink
NumericSteps etc.: some more functionality and tests from odb (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpollmeier authored Jun 26, 2024
1 parent 38622dc commit 173993a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
3 changes: 3 additions & 0 deletions core/src/main/scala/flatgraph/traversal/Language.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ trait language {

implicit def iteratorToEdgeSteps[A <: Edge](iter: IterableOnce[A]): EdgeSteps[A] =
new EdgeSteps[A](iter.iterator)

implicit def iteratorToNumericSteps[A: Numeric](iter: IterableOnce[A]): NumericSteps[A] =
new NumericSteps[A](iter)
}

@Traversal(elementType = classOf[AnyRef])
Expand Down
31 changes: 31 additions & 0 deletions core/src/main/scala/flatgraph/traversal/NumericSteps.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package flatgraph.traversal

class NumericSteps[N](val traversal: Iterator[N]) extends AnyVal {

def greaterThan(n: N)(implicit numeric: Numeric[N]): Iterator[N] =
traversal.filter(numeric.gt(_, n))

def greaterThanEqual(n: N)(implicit numeric: Numeric[N]): Iterator[N] =
traversal.filter(numeric.gteq(_, n))

def lessThan(n: N)(implicit numeric: Numeric[N]): Iterator[N] =
traversal.filter(numeric.lt(_, n))

def lessThanEqual(n: N)(implicit numeric: Numeric[N]): Iterator[N] =
traversal.filter(numeric.lteq(_, n))

def equal(n: N)(implicit numeric: Numeric[N]): Iterator[N] =
traversal.filter(numeric.equiv(_, n))

def equiv(n: N)(implicit numeric: Numeric[N]): Iterator[N] = equal(n)

def between(startInclusive: N, endExclusive: N)(implicit numeric: Numeric[N]): Iterator[N] =
traversal.filter(n => numeric.gteq(n, startInclusive) && numeric.lt(n, endExclusive))

def inside(startExclusive: N, endExclusive: N)(implicit numeric: Numeric[N]): Iterator[N] =
traversal.filter(n => numeric.gt(n, startExclusive) && numeric.lt(n, endExclusive))

def outside(startInclusive: N, endInclusive: N)(implicit numeric: Numeric[N]): Iterator[N] =
traversal.filter(n => numeric.lt(n, startInclusive) || numeric.gt(n, endInclusive))

}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ class GratefulDeadTests extends AnyWordSpec {
gratefulDead.song.performancesGte(1).size shouldBe 483
gratefulDead.song.performancesLt(1).size shouldBe 101
gratefulDead.song.performancesLte(1).size shouldBe 243

// numeric filter steps
gratefulDead.song.where(_.performances.equal(1)).size shouldBe 142
gratefulDead.song.where(_.performances.greaterThan(1)).size shouldBe 341
gratefulDead.song.where(_.performances.greaterThanEqual(1)).size shouldBe 483
gratefulDead.song.where(_.performances.lessThan(1)).size shouldBe 101
gratefulDead.song.where(_.performances.lessThanEqual(1)).size shouldBe 243
}

"throw useful exception when passing invalid regexp" in {
Expand Down
14 changes: 11 additions & 3 deletions tests/src/test/scala/flatgraph/traversal/TraversalTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.scalatest.wordspec.AnyWordSpec
import testdomains.generic.GenericDomain
import testdomains.generic.language.*
import testdomains.generic.edges.ConnectedTo
import testdomains.generic.nodes.NodeA
import testdomains.generic.nodes.{NewNodeA, NewNodeB, NodeA, NodeB}

import scala.collection.mutable

Expand Down Expand Up @@ -94,7 +94,16 @@ class TraversalTests extends AnyWordSpec {
oneToFour.without(Set(2, 4)).l shouldBe Seq(1, 3)
}

".help step" should {
"collectAll step should collect (and cast) all elements of the given type" in {
val anyIter: Iterator[Any] = Iterator("a", "b", 1, 2, 3)
val stringIter = anyIter.collectAll[String]

// just verifying that we can assign it
val stringIter2: Iterator[String] = stringIter
stringIter2.size shouldBe 2
}

"help step" should {
// a specific domain would provide it's own DocSearchPackage implementation, to specify where we're supposed to scan for @Doc annotations
given DocSearchPackages = DocSearchPackages.default
given AvailableWidthProvider = new Table.ConstantWidth(120)
Expand Down Expand Up @@ -159,7 +168,6 @@ class TraversalTests extends AnyWordSpec {
thingTraversalHelpVerbose should include("result to a list")
thingTraversalHelpVerbose should include("flatgraph.traversal.GenericSt")
}

}

"generic graph steps" in {
Expand Down

0 comments on commit 173993a

Please sign in to comment.