-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Invariant[Fractional] #4032
Merged
Merged
Invariant[Fractional] #4032
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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 |
---|---|---|
|
@@ -4,6 +4,7 @@ import cats.{Eval, Foldable, Id, Now} | |
import cats.data.NonEmptyLazyList | ||
import cats.laws.discipline.{ExhaustiveCheck, MiniInt, NonEmptyParallelTests, ParallelTests} | ||
import cats.laws.discipline.arbitrary._ | ||
import cats.laws.discipline.DeprecatedEqInstances | ||
import cats.syntax.either._ | ||
import cats.syntax.foldable._ | ||
import cats.syntax.parallel._ | ||
|
@@ -12,6 +13,7 @@ import cats.syntax.eq._ | |
import org.scalacheck.Prop._ | ||
import cats.kernel.{Eq, Order} | ||
import cats.laws.discipline.eq._ | ||
import org.scalacheck.Arbitrary | ||
|
||
trait ScalaVersionSpecificFoldableSuite { self: FoldableSuiteAdditional => | ||
test("Foldable[LazyList].foldM stack safety") { | ||
|
@@ -166,6 +168,7 @@ trait ScalaVersionSpecificTraverseSuite { self: TraverseSuiteAdditional => | |
|
||
trait ScalaVersionSpecificAlgebraInvariantSuite { | ||
|
||
// This version-specific instance is required since 2.12 and below do not have parseString on the Numeric class | ||
protected val integralForMiniInt: Integral[MiniInt] = new Integral[MiniInt] { | ||
def compare(x: MiniInt, y: MiniInt): Int = Order[MiniInt].compare(x, y) | ||
def plus(x: MiniInt, y: MiniInt): MiniInt = x + y | ||
|
@@ -182,6 +185,7 @@ trait ScalaVersionSpecificAlgebraInvariantSuite { | |
def parseString(str: String): Option[MiniInt] = Integral[Int].parseString(str).flatMap(MiniInt.fromInt) | ||
} | ||
|
||
// This version-specific instance is required since 2.12 and below do not have parseString on the Numeric class | ||
implicit protected def eqNumeric[A: Eq: ExhaustiveCheck]: Eq[Numeric[A]] = Eq.by { numeric => | ||
// This allows us to catch the case where the fromInt overflows. We use the None to compare two Numeric instances, | ||
// verifying that when fromInt throws for one, it throws for the other. | ||
|
@@ -212,6 +216,34 @@ trait ScalaVersionSpecificAlgebraInvariantSuite { | |
) | ||
} | ||
|
||
// This version-specific instance is required since 2.12 and below do not have parseString on the Numeric class | ||
implicit protected def eqFractional[A: Eq: Arbitrary]: Eq[Fractional[A]] = { | ||
// This deprecated instance is required since there is not `ExhaustiveCheck` for any types for which a `Fractional` | ||
// can easily be defined | ||
import DeprecatedEqInstances.catsLawsEqForFn1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that we need to use this instance since there is no type for which both |
||
|
||
Eq.by { fractional => | ||
val parseFloatStrings: Option[Double] => Option[A] = { | ||
case Some(f) => fractional.parseString(f.toString) | ||
case None => fractional.parseString("invalid") // Use this to test parsing of non-numeric strings | ||
} | ||
|
||
( | ||
fractional.compare _, | ||
fractional.plus _, | ||
fractional.minus _, | ||
fractional.times _, | ||
fractional.negate _, | ||
fractional.fromInt _, | ||
fractional.toInt _, | ||
fractional.toLong _, | ||
fractional.toFloat _, | ||
fractional.toDouble _, | ||
parseFloatStrings | ||
) | ||
} | ||
} | ||
|
||
} | ||
|
||
class TraverseLazyListSuite extends TraverseSuite[LazyList]("LazyList") | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that in 2.12 we were missing
InvariantInstances
as part ofcats.instances.all
prior to this PR.