How to unit-test different typeclass instances for Numeric?
Let's say I have two instances of the typeclass Numeric
.
class Money(c: String, x: Long, y: Int)
class Quantity(c: String, x: Long, y: Int)
implicit val numericMoney: Numeric[Money] = new Numeric[Money]
implicit val numericQuantity: Numeric[Quantity] = new Numeric[Quantity]
Money and Quantity should behave the same in the Numeric instance.
I have scalaTest tests which check that Money behaves correctly.
e.g.
import implicits.NumericMoney.numericMoney._
class MoneyOpsSpec extends WordSpec with Matchers {
val max = Money("", Long.MaxValue, 999999999)
val min = Money("", Long.MinValue, -999999999)
"A Money object" when {
"zero" should {
"be neutral element under addition" in {
zero + Money("", 15, 50) should ===(Money("", 15, 50))
Money("", 15, 50) + zero should ===(Money("", 15, 50))
}
"be neutral element under subtraction" in {
zero - Money("", 15, 50) should ===(Money("", -15, -50))
Money("", 15, 50) - zero should ===(Money("", 15, 50))
}
"be invariant under negation" in {
-zero should ===(zero)
}
}
}
}
Quantity
spec should be executed in the same way. Can I implement a generic spec and use Money
and Quantity
as an input for that spec? Or do scalaTest or specs2 have someting to make sure that a Numeric typeclass instance behaves correctly? I can switch testing frameworks easily.
scala typeclass scalatest specs2
add a comment |
Let's say I have two instances of the typeclass Numeric
.
class Money(c: String, x: Long, y: Int)
class Quantity(c: String, x: Long, y: Int)
implicit val numericMoney: Numeric[Money] = new Numeric[Money]
implicit val numericQuantity: Numeric[Quantity] = new Numeric[Quantity]
Money and Quantity should behave the same in the Numeric instance.
I have scalaTest tests which check that Money behaves correctly.
e.g.
import implicits.NumericMoney.numericMoney._
class MoneyOpsSpec extends WordSpec with Matchers {
val max = Money("", Long.MaxValue, 999999999)
val min = Money("", Long.MinValue, -999999999)
"A Money object" when {
"zero" should {
"be neutral element under addition" in {
zero + Money("", 15, 50) should ===(Money("", 15, 50))
Money("", 15, 50) + zero should ===(Money("", 15, 50))
}
"be neutral element under subtraction" in {
zero - Money("", 15, 50) should ===(Money("", -15, -50))
Money("", 15, 50) - zero should ===(Money("", 15, 50))
}
"be invariant under negation" in {
-zero should ===(zero)
}
}
}
}
Quantity
spec should be executed in the same way. Can I implement a generic spec and use Money
and Quantity
as an input for that spec? Or do scalaTest or specs2 have someting to make sure that a Numeric typeclass instance behaves correctly? I can switch testing frameworks easily.
scala typeclass scalatest specs2
add a comment |
Let's say I have two instances of the typeclass Numeric
.
class Money(c: String, x: Long, y: Int)
class Quantity(c: String, x: Long, y: Int)
implicit val numericMoney: Numeric[Money] = new Numeric[Money]
implicit val numericQuantity: Numeric[Quantity] = new Numeric[Quantity]
Money and Quantity should behave the same in the Numeric instance.
I have scalaTest tests which check that Money behaves correctly.
e.g.
import implicits.NumericMoney.numericMoney._
class MoneyOpsSpec extends WordSpec with Matchers {
val max = Money("", Long.MaxValue, 999999999)
val min = Money("", Long.MinValue, -999999999)
"A Money object" when {
"zero" should {
"be neutral element under addition" in {
zero + Money("", 15, 50) should ===(Money("", 15, 50))
Money("", 15, 50) + zero should ===(Money("", 15, 50))
}
"be neutral element under subtraction" in {
zero - Money("", 15, 50) should ===(Money("", -15, -50))
Money("", 15, 50) - zero should ===(Money("", 15, 50))
}
"be invariant under negation" in {
-zero should ===(zero)
}
}
}
}
Quantity
spec should be executed in the same way. Can I implement a generic spec and use Money
and Quantity
as an input for that spec? Or do scalaTest or specs2 have someting to make sure that a Numeric typeclass instance behaves correctly? I can switch testing frameworks easily.
scala typeclass scalatest specs2
Let's say I have two instances of the typeclass Numeric
.
class Money(c: String, x: Long, y: Int)
class Quantity(c: String, x: Long, y: Int)
implicit val numericMoney: Numeric[Money] = new Numeric[Money]
implicit val numericQuantity: Numeric[Quantity] = new Numeric[Quantity]
Money and Quantity should behave the same in the Numeric instance.
I have scalaTest tests which check that Money behaves correctly.
e.g.
import implicits.NumericMoney.numericMoney._
class MoneyOpsSpec extends WordSpec with Matchers {
val max = Money("", Long.MaxValue, 999999999)
val min = Money("", Long.MinValue, -999999999)
"A Money object" when {
"zero" should {
"be neutral element under addition" in {
zero + Money("", 15, 50) should ===(Money("", 15, 50))
Money("", 15, 50) + zero should ===(Money("", 15, 50))
}
"be neutral element under subtraction" in {
zero - Money("", 15, 50) should ===(Money("", -15, -50))
Money("", 15, 50) - zero should ===(Money("", 15, 50))
}
"be invariant under negation" in {
-zero should ===(zero)
}
}
}
}
Quantity
spec should be executed in the same way. Can I implement a generic spec and use Money
and Quantity
as an input for that spec? Or do scalaTest or specs2 have someting to make sure that a Numeric typeclass instance behaves correctly? I can switch testing frameworks easily.
scala typeclass scalatest specs2
scala typeclass scalatest specs2
asked Nov 23 '18 at 7:00
mrt181mrt181
2,58165078
2,58165078
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Can I implement a generic spec and use Money and Quantity as an input for that spec?
Sure. Just take the implicit as a constructor argument. Not tested, but should be approximately (with minimal changes):
abstract class NumOpsSpec[T](implicit num: Numeric[T], tag: ClassTag[T]) extends WordSpec with Matchers {
import num._
val max: T
val min: T
val someElement: T
s"A ${tag.runtimeClass.simpleName} object" when {
"zero" should {
"be neutral element under addition" in {
zero + someElement should ===(someElement)
someElement + zero should ===(someElement)
}
"be neutral element under subtraction" in {
zero - someElement should ===(- someElement)
someElement - zero should ===(someElement)
}
"be invariant under negation" in {
-zero should ===(zero)
}
}
}
}
class MoneyOpsSpec extends NumOpsSpec[Money] {
override val max = Money("", Long.MaxValue, 999999999)
override val min = Money("", Long.MinValue, -999999999)
override val someElement = Money("", 15, 50)
}
class QuantityOpsSpec extends NumOpsSpec[Quantity] {
override val max = ???
override val min = ???
override val someElement = ???
}
You could also look into https://github.com/typelevel/discipline for testing typeclass laws in general.
add a comment |
I think what you need is to create abstract methods for testing different operations, and use it with both objects. for ex. to test addition
def testAddition[T](a: T, b: T, expectedResult: T)(implicit n: Numeric[T]) = {
n.plus(a, b) ==== expectedResult
}
then you can call this method with Money
or Quantity
testAddition(Money(1, 1), Money(2, 2), Money(3, 3))
testAddition(Quantity(1, 1), Quantity(2, 2), Quantity(3, 3))
I would like to avoid this approach and instead use the WordMatchers syntax but with different instances injected into the spec
– mrt181
Nov 23 '18 at 9:07
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441994%2fhow-to-unit-test-different-typeclass-instances-for-numeric%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Can I implement a generic spec and use Money and Quantity as an input for that spec?
Sure. Just take the implicit as a constructor argument. Not tested, but should be approximately (with minimal changes):
abstract class NumOpsSpec[T](implicit num: Numeric[T], tag: ClassTag[T]) extends WordSpec with Matchers {
import num._
val max: T
val min: T
val someElement: T
s"A ${tag.runtimeClass.simpleName} object" when {
"zero" should {
"be neutral element under addition" in {
zero + someElement should ===(someElement)
someElement + zero should ===(someElement)
}
"be neutral element under subtraction" in {
zero - someElement should ===(- someElement)
someElement - zero should ===(someElement)
}
"be invariant under negation" in {
-zero should ===(zero)
}
}
}
}
class MoneyOpsSpec extends NumOpsSpec[Money] {
override val max = Money("", Long.MaxValue, 999999999)
override val min = Money("", Long.MinValue, -999999999)
override val someElement = Money("", 15, 50)
}
class QuantityOpsSpec extends NumOpsSpec[Quantity] {
override val max = ???
override val min = ???
override val someElement = ???
}
You could also look into https://github.com/typelevel/discipline for testing typeclass laws in general.
add a comment |
Can I implement a generic spec and use Money and Quantity as an input for that spec?
Sure. Just take the implicit as a constructor argument. Not tested, but should be approximately (with minimal changes):
abstract class NumOpsSpec[T](implicit num: Numeric[T], tag: ClassTag[T]) extends WordSpec with Matchers {
import num._
val max: T
val min: T
val someElement: T
s"A ${tag.runtimeClass.simpleName} object" when {
"zero" should {
"be neutral element under addition" in {
zero + someElement should ===(someElement)
someElement + zero should ===(someElement)
}
"be neutral element under subtraction" in {
zero - someElement should ===(- someElement)
someElement - zero should ===(someElement)
}
"be invariant under negation" in {
-zero should ===(zero)
}
}
}
}
class MoneyOpsSpec extends NumOpsSpec[Money] {
override val max = Money("", Long.MaxValue, 999999999)
override val min = Money("", Long.MinValue, -999999999)
override val someElement = Money("", 15, 50)
}
class QuantityOpsSpec extends NumOpsSpec[Quantity] {
override val max = ???
override val min = ???
override val someElement = ???
}
You could also look into https://github.com/typelevel/discipline for testing typeclass laws in general.
add a comment |
Can I implement a generic spec and use Money and Quantity as an input for that spec?
Sure. Just take the implicit as a constructor argument. Not tested, but should be approximately (with minimal changes):
abstract class NumOpsSpec[T](implicit num: Numeric[T], tag: ClassTag[T]) extends WordSpec with Matchers {
import num._
val max: T
val min: T
val someElement: T
s"A ${tag.runtimeClass.simpleName} object" when {
"zero" should {
"be neutral element under addition" in {
zero + someElement should ===(someElement)
someElement + zero should ===(someElement)
}
"be neutral element under subtraction" in {
zero - someElement should ===(- someElement)
someElement - zero should ===(someElement)
}
"be invariant under negation" in {
-zero should ===(zero)
}
}
}
}
class MoneyOpsSpec extends NumOpsSpec[Money] {
override val max = Money("", Long.MaxValue, 999999999)
override val min = Money("", Long.MinValue, -999999999)
override val someElement = Money("", 15, 50)
}
class QuantityOpsSpec extends NumOpsSpec[Quantity] {
override val max = ???
override val min = ???
override val someElement = ???
}
You could also look into https://github.com/typelevel/discipline for testing typeclass laws in general.
Can I implement a generic spec and use Money and Quantity as an input for that spec?
Sure. Just take the implicit as a constructor argument. Not tested, but should be approximately (with minimal changes):
abstract class NumOpsSpec[T](implicit num: Numeric[T], tag: ClassTag[T]) extends WordSpec with Matchers {
import num._
val max: T
val min: T
val someElement: T
s"A ${tag.runtimeClass.simpleName} object" when {
"zero" should {
"be neutral element under addition" in {
zero + someElement should ===(someElement)
someElement + zero should ===(someElement)
}
"be neutral element under subtraction" in {
zero - someElement should ===(- someElement)
someElement - zero should ===(someElement)
}
"be invariant under negation" in {
-zero should ===(zero)
}
}
}
}
class MoneyOpsSpec extends NumOpsSpec[Money] {
override val max = Money("", Long.MaxValue, 999999999)
override val min = Money("", Long.MinValue, -999999999)
override val someElement = Money("", 15, 50)
}
class QuantityOpsSpec extends NumOpsSpec[Quantity] {
override val max = ???
override val min = ???
override val someElement = ???
}
You could also look into https://github.com/typelevel/discipline for testing typeclass laws in general.
answered Nov 23 '18 at 11:41
Alexey RomanovAlexey Romanov
107k25212353
107k25212353
add a comment |
add a comment |
I think what you need is to create abstract methods for testing different operations, and use it with both objects. for ex. to test addition
def testAddition[T](a: T, b: T, expectedResult: T)(implicit n: Numeric[T]) = {
n.plus(a, b) ==== expectedResult
}
then you can call this method with Money
or Quantity
testAddition(Money(1, 1), Money(2, 2), Money(3, 3))
testAddition(Quantity(1, 1), Quantity(2, 2), Quantity(3, 3))
I would like to avoid this approach and instead use the WordMatchers syntax but with different instances injected into the spec
– mrt181
Nov 23 '18 at 9:07
add a comment |
I think what you need is to create abstract methods for testing different operations, and use it with both objects. for ex. to test addition
def testAddition[T](a: T, b: T, expectedResult: T)(implicit n: Numeric[T]) = {
n.plus(a, b) ==== expectedResult
}
then you can call this method with Money
or Quantity
testAddition(Money(1, 1), Money(2, 2), Money(3, 3))
testAddition(Quantity(1, 1), Quantity(2, 2), Quantity(3, 3))
I would like to avoid this approach and instead use the WordMatchers syntax but with different instances injected into the spec
– mrt181
Nov 23 '18 at 9:07
add a comment |
I think what you need is to create abstract methods for testing different operations, and use it with both objects. for ex. to test addition
def testAddition[T](a: T, b: T, expectedResult: T)(implicit n: Numeric[T]) = {
n.plus(a, b) ==== expectedResult
}
then you can call this method with Money
or Quantity
testAddition(Money(1, 1), Money(2, 2), Money(3, 3))
testAddition(Quantity(1, 1), Quantity(2, 2), Quantity(3, 3))
I think what you need is to create abstract methods for testing different operations, and use it with both objects. for ex. to test addition
def testAddition[T](a: T, b: T, expectedResult: T)(implicit n: Numeric[T]) = {
n.plus(a, b) ==== expectedResult
}
then you can call this method with Money
or Quantity
testAddition(Money(1, 1), Money(2, 2), Money(3, 3))
testAddition(Quantity(1, 1), Quantity(2, 2), Quantity(3, 3))
answered Nov 23 '18 at 7:36
Mahmoud HanafyMahmoud Hanafy
96511528
96511528
I would like to avoid this approach and instead use the WordMatchers syntax but with different instances injected into the spec
– mrt181
Nov 23 '18 at 9:07
add a comment |
I would like to avoid this approach and instead use the WordMatchers syntax but with different instances injected into the spec
– mrt181
Nov 23 '18 at 9:07
I would like to avoid this approach and instead use the WordMatchers syntax but with different instances injected into the spec
– mrt181
Nov 23 '18 at 9:07
I would like to avoid this approach and instead use the WordMatchers syntax but with different instances injected into the spec
– mrt181
Nov 23 '18 at 9:07
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441994%2fhow-to-unit-test-different-typeclass-instances-for-numeric%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown