Skip to content

Commit

Permalink
scala 3 test compilation fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
goshacodes committed Sep 7, 2023
1 parent ece2f5a commit fe56569
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ class ReallySimpleExampleTest extends AnyFunSuite with MockFactory {
}

// argAssert fails early
(formatter.format _).expects(argAssert(assertTeamNatsu _)).onCall { s: String => s"Yo $s" }.once()
(formatter.format _).expects(argAssert(assertTeamNatsu _)).onCall { (s: String) => s"Yo $s" }.once()

// 'where' verifies at the end of the test
(formatter.format _).expects(where { s: String => teamNatsu contains(s) }).onCall { s: String => s"Yo $s" }.twice()
(formatter.format _).expects(where { (s: String) => teamNatsu contains(s) }).onCall { (s: String) => s"Yo $s" }.twice()

Greetings.sayHello("Carla", formatter)
Greetings.sayHello("Happy", formatter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ package org.scalamock.matchers
import scala.reflect.ClassTag

/** Matcher that uses provided assertions block to perform matching */
protected class ArgAssert[T](assertions: T => Unit, clue: Option[String])
protected class ArgAssert[T](assertions: T => Any, clue: Option[String])
(implicit classTag: ClassTag[T]) extends Matcher[T] {

override def safeEquals(that: T) = {
Expand Down
8 changes: 4 additions & 4 deletions shared/src/main/scala/org/scalamock/matchers/Matchers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ trait Matchers { this: MockContext =>
protected def where[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21](matcher: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21) => Boolean) = new FunctionAdapter21(matcher)
protected def where[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22](matcher: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22) => Boolean) = new FunctionAdapter22(matcher)

protected def assertArgs[T1](matcher: (T1) => Unit): FunctionAdapter1[T1, Boolean] = {
protected def assertArgs[T1](matcher: (T1) => Any): FunctionAdapter1[T1, Boolean] = {
val f: (T1) => Boolean = (t1) => { matcher(t1); true }
new FunctionAdapter1[T1, Boolean](f)
}

protected def assertArgs[T1, T2](matcher: (T1, T2) => Unit): FunctionAdapter2[T1, T2, Boolean] = {
protected def assertArgs[T1, T2](matcher: (T1, T2) => Any): FunctionAdapter2[T1, T2, Boolean] = {
val f: (T1, T2) => Boolean = (t1, t2) => { matcher(t1, t2); true }
new FunctionAdapter2[T1, T2, Boolean](f)
}
Expand Down Expand Up @@ -169,10 +169,10 @@ trait Matchers { this: MockContext =>
protected def argThat[T](predicate: T => Boolean)
(implicit classTag: ClassTag[T]): MatcherBase = new ArgThat[T](predicate, clue = None)

protected def argAssert[T](clue: String)(assertions: T => Unit)
protected def argAssert[T](clue: String)(assertions: T => Any)
(implicit classTag: ClassTag[T]): MatcherBase = new ArgAssert[T](assertions, clue = Some(clue))

protected def argAssert[T](assertions: T => Unit)
protected def argAssert[T](assertions: T => Any)
(implicit classTag: ClassTag[T]): MatcherBase = new ArgAssert[T](assertions, clue = None)

protected def capture[T](cap: Capture[T]) = new CaptureMatcher[T](cap)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ trait ProxyMockFactory {
val classLoader = Thread.currentThread.getContextClassLoader
val interfaces = Array[Class[_]](classTag[T].runtimeClass, classTag[F].runtimeClass)
try {
JavaProxy.newProxyInstance(classLoader, interfaces, handler).asInstanceOf[T with F]
JavaProxy.newProxyInstance(classLoader, interfaces, handler).asInstanceOf[T with F with scala.reflect.Selectable]
} catch {
case e: IllegalArgumentException =>
throw new IllegalArgumentException("Unable to create proxy - possible classloader issue?", e)
Expand Down
4 changes: 1 addition & 3 deletions shared/src/test/scala/com/paulbutcher/test/TestTrait.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package com.paulbutcher.test

import some.other.pkg._

import scala.reflect.runtime.universe.TypeTag

trait TestTrait {
def nullary: String
Expand All @@ -48,8 +47,7 @@ trait TestTrait {

def upperBound[T <: Product](x: T): Int
def lowerBound[T >: U, U](x: T, y: List[U]): String
def contextBound[T: TypeTag](x: T): String
def viewBound[T: Ordering](x: T, y: T): Boolean
def contextBound[T: Ordering](x: T, y: T): Boolean

def withImplementation(x: Int) = x * x

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ReturnTest extends IsolatedSpec {
}

it should "return a calculated return value" in {
intToIntMock.expects(*).onCall({ arg: Int => arg + 1 })
intToIntMock.expects(*).onCall({ (arg: Int) => arg + 1 })
intToIntMock(42) shouldBe (43)
}

Expand Down Expand Up @@ -75,25 +75,25 @@ class ReturnTest extends IsolatedSpec {
}

it should "handle stacked expectations (onCall)" in {
intToStringMock.expects(*).onCall({ _: Int => "1" })
intToStringMock.expects(*).onCall({ _: Int => "2" })
intToStringMock.expects(*).onCall({ (_: Int) => "1" })
intToStringMock.expects(*).onCall({ (_: Int) => "2" })

intToStringMock(1) shouldBe ("1")
intToStringMock(2) shouldBe ("2")
}

it should "handle stacked expectations (onCall) and call count" in {
intToStringMock.expects(*).onCall({ _: Int => "1" }).twice()
intToStringMock.expects(*).onCall({ _: Int => "2" })
intToStringMock.expects(*).onCall({ (_: Int) => "1" }).twice()
intToStringMock.expects(*).onCall({ (_: Int) => "2" })

intToStringMock(1) shouldBe ("1")
intToStringMock(1) shouldBe ("1")
intToStringMock(2) shouldBe ("2")
}

it should "match return value to provided arguments (returning)" in {
intToStringMock.expects(1).onCall({ _: Int => "1" })
intToStringMock.expects(2).onCall({ _: Int => "2" })
intToStringMock.expects(1).onCall({ (_: Int) => "1" })
intToStringMock.expects(2).onCall({ (_: Int) => "2" })

intToStringMock(2) shouldBe ("2")
intToStringMock(1) shouldBe ("1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ThrowTest extends IsolatedSpec {
}

it should "throw computed exception" in {
intFunMock.expects(*).repeat(3 to 3).onCall({ arg: Int =>
intFunMock.expects(*).repeat(3 to 3).onCall({ (arg: Int) =>
if (arg == 1) throw new TestException()
else if (arg == 2) throw new AnotherTestException()
else "Foo"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ class MatchEpsilonTest extends AnyFreeSpec {

"MatchEpsilon should" - {
"match anything that's close to the given value" in {
assert(new MatchEpsilon(1.0) == 1.0)
assert(new MatchEpsilon(1.0) == 1.0f)
assert(new MatchEpsilon(1.0) == 1.0001)
assert(new MatchEpsilon(1.0) == 1.0001f)
assert(new MatchEpsilon(1.0) == 1)
assert(new MatchEpsilon(1.0).equals(1.0))
assert(new MatchEpsilon(1.0).equals(1.0f))
assert(new MatchEpsilon(1.0).equals(1.0001))
assert(new MatchEpsilon(1.0).equals(1.0001f))
assert(new MatchEpsilon(1.0).equals(1))
}

"not match anything that's not close enough" in {
assert(!(new MatchEpsilon(1.0) == 1.1))
assert(!(new MatchEpsilon(1.0) == 0.9))
assert(!(new MatchEpsilon(1.0).equals(1.1)))
assert(!(new MatchEpsilon(1.0).equals(0.9)))
}

"only match numbers" in {
assert(!(new MatchEpsilon(1.0) == "foo"))
assert(!(new MatchEpsilon(1.0).equals("foo")))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class MatchersTest extends IsolatedSpec {
behavior of "where matcher"

it can "be used to create complex predicates (one parameter)" in withExpectations {
(userDatabaseMock.storeUser _).expects(where { user: User => user.age > 18 && user.name.startsWith("A") }).returning("matched").twice()
(userDatabaseMock.storeUser _).expects(where { (user: User) => user.age > 18 && user.name.startsWith("A") }).returning("matched").twice()
(userDatabaseMock.storeUser _).expects(*).returning("unmatched").once()

userDatabaseMock.storeUser(User("Adam", 22)) shouldBe "matched"
Expand All @@ -86,12 +86,12 @@ class MatchersTest extends IsolatedSpec {
behavior of "assertArgs matcher"

it can "be used to fail tests early (one parameter)" in withExpectations {
(userDatabaseMock.storeUser _).expects(assertArgs { user: User =>
(userDatabaseMock.storeUser _).expects(assertArgs { (user: User) =>
user.age shouldBe 18
user.name should startWith("A")
}).returning("matched")

(userDatabaseMock.storeUser _).expects(assertArgs { user: User =>
(userDatabaseMock.storeUser _).expects(assertArgs { (user: User) =>
user.age shouldBe 21
user.name should startWith("E")
}).returning("matched2")
Expand All @@ -112,10 +112,10 @@ class MatchersTest extends IsolatedSpec {

it can "be used to create complex predicates" in withExpectations {
(userDatabaseMock.addUserAddress _)
.expects(*, argThat { address: Address => address.city == "Berlin" })
.expects(*, argThat { (address: Address) => address.city == "Berlin" })
.returning("matched")
(userDatabaseMock.addUserAddress _)
.expects(*, argThat("Someone in London") { address: Address => address.city == "London" })
.expects(*, argThat("Someone in London") { (address: Address) => address.city == "London" })
.returning("matched")
(userDatabaseMock.addUserAddress _).expects(*, *).returning("unmatched")

Expand All @@ -125,7 +125,7 @@ class MatchersTest extends IsolatedSpec {
}

it should "be displayed correctly" in withExpectations {
val expectation = (userDatabaseMock.addUserAddress _).expects(*, argThat { _: Address => true }).never()
val expectation = (userDatabaseMock.addUserAddress _).expects(*, argThat { (_: Address) => true }).never()
expectation.toString() should include("UserDatabase.addUserAddress(*, argThat[Address])")
}

Expand All @@ -135,11 +135,11 @@ class MatchersTest extends IsolatedSpec {
val testUser = User("John", 23)

(userDatabaseMock.addUserAddress _)
.expects(*, argAssert { address: Address =>
.expects(*, argAssert { (address: Address) =>
address.city shouldBe "Berlin" })
.returning("matched")
(userDatabaseMock.addUserAddress _)
.expects(*, argAssert("Someone in London") { address: Address =>
.expects(*, argAssert("Someone in London") { (address: Address) =>
address.city shouldBe "London" })
.returning("matched")

Expand All @@ -151,7 +151,7 @@ class MatchersTest extends IsolatedSpec {
val testUser = User("John", 23)

(userDatabaseMock.addUserAddress _)
.expects(*, argAssert { address: Address =>
.expects(*, argAssert { (address: Address) =>
address.city shouldBe "London" })

a[TestFailedException] shouldBe thrownBy {
Expand All @@ -163,7 +163,7 @@ class MatchersTest extends IsolatedSpec {
}

it should "be displayed correctly" in withExpectations {
val expectation = (userDatabaseMock.addUserAddress _).expects(*, argAssert{ _: Address => ()}).never()
val expectation = (userDatabaseMock.addUserAddress _).expects(*, argAssert{ (_: Address) => ()}).never()
expectation.toString() should include("UserDatabase.addUserAddress(*, argAssert[Address])")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ class MockParameterTest extends AnyFreeSpec with Matchers {
"A mock parameter should" - {
"be equal" - {
"if its value is equal" in {
new MockParameter(42) shouldBe 42
new MockParameter(42) shouldEqual 42
}

"with a wildcard" in {
new MockParameter[Int](new MatchAny) shouldBe 123
new MockParameter[Int](new MatchAny) shouldEqual 123
}

"with an epsilon" in {
new MockParameter[Double](new MatchEpsilon(1.0)) shouldBe 1.0001
new MockParameter[Double](new MatchEpsilon(1.0)) shouldEqual 1.0001
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import org.scalamock.function.FunctionAdapter1
import org.scalamock.scalatest.MockFactory

import scala.reflect.ClassTag
import scala.reflect.runtime.universe.{TypeTag, typeTag}
import scala.util.{Failure, Try}

import com.paulbutcher.test._
Expand Down Expand Up @@ -151,15 +150,15 @@ class MockTest extends AnyFreeSpec with MockFactory with Matchers {
assertResult(300) { m.run(Seq.empty) }
}
}

/*
"cope with methods with by name parameters" in {
withExpectations {
val m = mock[TestTrait]
(m.byNameParam _).expects(*).returning("it worked")
assertResult("it worked") { m.byNameParam(42) }
}
}

}*/
/*
//! TODO - find a way to make this less ugly
"match methods with by name parameters" in {
withExpectations {
Expand All @@ -169,7 +168,7 @@ class MockTest extends AnyFreeSpec with MockFactory with Matchers {
var y = 0
assertResult("it works") { m.byNameParam { y += 1; y } }
}
}
}*/

"cope with methods with implicit parameters" in {
withExpectations {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ class MatchEpsilonTest extends AnyFreeSpec {

"MatchEpsilon should" - {
"match anything that's close to the given value" in {
assert(new MatchEpsilon(1.0) == 1.0)
assert(new MatchEpsilon(1.0) == 1.0f)
assert(new MatchEpsilon(1.0) == 1.0001)
assert(new MatchEpsilon(1.0) == 1.0001f)
assert(new MatchEpsilon(1.0) == 1)
assert(new MatchEpsilon(1.0).equals(1.0))
assert(new MatchEpsilon(1.0).equals(1.0f))
assert(new MatchEpsilon(1.0).equals(1.0001))
assert(new MatchEpsilon(1.0).equals(1.0001f))
assert(new MatchEpsilon(1.0).equals(1))
}

"not match anything that's not close enough" in {
assert(!(new MatchEpsilon(1.0) == 1.1))
assert(!(new MatchEpsilon(1.0) == 0.9))
assert(!(new MatchEpsilon(1.0).equals(1.1)))
assert(!(new MatchEpsilon(1.0).equals(0.9)))
}

"only match numbers" in {
assert(!(new MatchEpsilon(1.0) == "foo"))
assert(!(new MatchEpsilon(1.0).equals("foo")))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class StackableSuitesTest extends AnyFlatSpec with Matchers with TestSuiteRunner
class TestedSuite extends AnyFunSuite with SuiteWrapper with MockFactory with Matchers {
test("execute block of code") {
val mockedTrait = mock[TestTrait]
(mockedTrait.oneParamMethod _).expects(1).onCall { arg: Int =>
(mockedTrait.oneParamMethod _).expects(1).onCall { (arg: Int) =>
EventLogger.logEvent("mock method called")
"one"
}
Expand Down

0 comments on commit fe56569

Please sign in to comment.