Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add given instances for RefinedTypeOps #143

Merged
merged 5 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
test: Cleanup RefinedOpsSuite
  • Loading branch information
Iltotore committed Jun 23, 2023
commit ea3e660b9ba6985ebbb752ad125c420fcefd2c63
67 changes: 20 additions & 47 deletions main/test/src/io/github/iltotore/iron/testing/RefinedOpsSuite.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package io.github.iltotore.iron.testing

import io.github.iltotore.iron.{:|, IronType, autoRefine}
import io.github.iltotore.iron.constraint.numeric.{Positive, *}
import utest.{TestSuite, Tests, compileError, test}
import io.github.iltotore.iron.*
import io.github.iltotore.iron.constraint.all.*
import io.github.iltotore.iron.constraint.numeric.*
import utest.*

import scala.util.{Failure, Try}

opaque type Temperature = Double :| Positive
object Temperature extends RefinedTypeOps[Temperature]
import scala.util.{Failure, Success, Try}

object RefinedTypeOpsSuite extends TestSuite:
val tests: Tests = Tests {
Expand All @@ -32,65 +27,43 @@ object RefinedTypeOpsSuite extends TestSuite:
val t1 = Temperature(x)
val t2 = Temperature(y)

assert(t1 == Temperature(5.0), "should be result of 'apply'")
assert(t2 == Temperature(15.0), "should be result of 'apply'")
assert(t1 == Temperature(5.0))
assert(t2 == Temperature(15.0))
}

test("either") {
val eitherWithFailingPredicate = Temperature.either(-5.0)
assert(eitherWithFailingPredicate == Left("Should be strictly positive"), "'either' returns left if predicate fails")
assert(eitherWithFailingPredicate == Left("Should be strictly positive"))
val eitherWithSucceedingPredicate = Temperature.either(100)
assert(eitherWithSucceedingPredicate == Right(Temperature(100)), "right should contain result of 'apply'")
assert(eitherWithSucceedingPredicate == Right(Temperature(100)))
}

test("option") {
val fromWithFailingPredicate = Temperature.option(-5.0)
assert(fromWithFailingPredicate == None, "'option' returns None if predicate fails")
assert(fromWithFailingPredicate == None)
val fromWithSucceedingPredicate = Temperature.option(100)
assert(fromWithSucceedingPredicate == Some(Temperature(100)), "Some should contain result of 'apply'")
assert(fromWithSucceedingPredicate == Some(Temperature(100)))
}

test("applyUnsafe") {
val appliedUnsafeWithFailingPredicate = Try(Temperature.applyUnsafe(-1))

assert(appliedUnsafeWithFailingPredicate.isFailure, "appliedUnsafe should fail when predicate fails")
assert(
appliedUnsafeWithFailingPredicate.toEither.swap.toOption.get.getMessage == "Should be strictly positive",
"exception should duplicate message of failed predicate"
)

val appliedUnsafeWithSucceedingPredicate = Try(Temperature.applyUnsafe(100))
assert(
appliedUnsafeWithSucceedingPredicate.toOption.get == Temperature(100),
"should be wrapped result of apply"
)
test - assertMatch(Try(Temperature.applyUnsafe(-100))) { case Failure(e) if e.getMessage == "Should be strictly positive" => }
test - assert(Temperature.applyUnsafe(100) == Temperature(100))
}

test("assume") {
val x: Double = -15.0
val t1 = Temperature.assume(x)

assert(t1 == -15.0.asInstanceOf[Temperature])
}
test("assume") - assert(Temperature.assume(-15) == -15.0.asInstanceOf[Temperature])

test("ops are being applied to non-opaque types and don't change behaviour") {
type Moisture = Double :| Positive
object Moisture extends RefinedTypeOps[Moisture]
test("nonOpaque") {
val moisture = Moisture(11)
val positive: Double :| Positive = 11
val greaterThan10: Double :| Greater[10] = 11

assert(Moisture(positive) == moisture)
assert(Moisture(greaterThan10) == moisture)
assert(Moisture(positive) == moisture)
val eitherWithFailingPredicate = Moisture.either(-5.0)
assert(eitherWithFailingPredicate == Left("Should be strictly positive"), "'either' returns left if predicate fails")
val eitherWithSucceedingPredicate = Moisture.either(100)
assert(eitherWithSucceedingPredicate == Right(Moisture(100)), "right should contain result of 'apply'")
val fromWithFailingPredicate = Moisture.option(-5.0)
assert(fromWithFailingPredicate == None, "'option' returns None if predicate fails")
val fromWithSucceedingPredicate = Moisture.option(100)
assert(fromWithSucceedingPredicate == Some(Moisture(100)), "Some should contain result of 'apply'")
test - assert(Moisture(positive) == moisture)
test - assert(Moisture(greaterThan10) == moisture)
test - assert(Moisture(positive) == moisture)
test - assert(Moisture.either(-5.0) == Left("Should be strictly positive"))
test - assert(Moisture.either(100) == Right(Moisture(100)))
test - assert(Moisture.option(-5.0) == None)
test - assert(Moisture.option(100) == Some(Moisture(100)))
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.iltotore.iron.testing

import io.github.iltotore.iron.*
import io.github.iltotore.iron.constraint.numeric.Positive

//Opaque types are truly opaque when used in another file than the one where they're defined. See Scala documentation.
opaque type Temperature = Double :| Positive
object Temperature extends RefinedTypeOps[Temperature]

type Moisture = Double :| Positive
object Moisture extends RefinedTypeOps[Moisture]