-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Close #196 - [refined4s-core] Add CanBeOrdered for providing Ordering…
… and Conversion[Type, Ordered[Type]]
- Loading branch information
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
modules/refined4s-core/shared/src/main/scala/refined4s/CanBeOrdered.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package refined4s | ||
|
||
/** @author Kevin Lee | ||
* @since 2023-12-29 | ||
*/ | ||
trait CanBeOrdered[A: Ordering] { | ||
self: NewtypeBase[A] => | ||
|
||
given derivedOrdering: Ordering[Type] = deriving[Ordering] | ||
|
||
given derivedToOrdered: Conversion[Type, Ordered[Type]] with { | ||
def apply(a: Type): Ordered[Type] = | ||
Ordered.orderingToOrdered[Type](a)(using derivedOrdering) | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
modules/refined4s-core/shared/src/test/scala/refined4s/CanBeOrderedSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package refined4s | ||
|
||
import hedgehog.* | ||
import hedgehog.runner.* | ||
|
||
/** @author Kevin Lee | ||
* @since 2023-12-29 | ||
*/ | ||
object CanBeOrderedSpec extends Properties { | ||
override def tests: List[Test] = List( | ||
property("test Ordering from CanBeOrdered", testOrdering), | ||
property("test Ordered from CanBeOrdered", testOrdered), | ||
) | ||
|
||
def testOrdering: Property = | ||
for { | ||
n1 <- Gen.int(Range.linear(Int.MinValue, Int.MaxValue)).log("n1") | ||
n2 <- Gen.int(Range.linear(Int.MinValue, Int.MaxValue)).log("n2") | ||
} yield { | ||
val input1 = MyNum(n1) | ||
val input2 = MyNum(n2) | ||
|
||
val expected = n1.compare(n2) | ||
Result.diff(input1, input2)(Ordering[MyNum].compare(_, _) == expected) | ||
} | ||
|
||
def testOrdered: Property = | ||
for { | ||
n1 <- Gen.int(Range.linear(Int.MinValue, Int.MaxValue)).log("n1") | ||
n2 <- Gen.int(Range.linear(Int.MinValue, Int.MaxValue)).log("n2") | ||
} yield { | ||
val input1 = MyNum(n1) | ||
val input2 = MyNum(n2) | ||
|
||
val expected = n1.compare(n2) | ||
Result.diff(input1, input2: MyNum)(_.compare(_) == expected) | ||
} | ||
|
||
type MyNum = MyNum.Type | ||
object MyNum extends Newtype[Int], CanBeOrdered[Int] | ||
|
||
} |