Skip to content

Commit

Permalink
Merge pull request #553 from goshacodes/fix_deprecations
Browse files Browse the repository at this point in the history
fix 3.4 deprecations
  • Loading branch information
goshacodes authored Jan 2, 2025
2 parents c7ccec8 + 6495a61 commit a35b864
Show file tree
Hide file tree
Showing 60 changed files with 329 additions and 347 deletions.
9 changes: 8 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ lazy val specs2 = Def.setting("org.specs2" %%% "specs2-core" % "4.20.8")

val commonSettings = Defaults.coreDefaultSettings ++ Seq(
scalaVersion := "3.4.2",
scalacOptions ++= Seq("-deprecation", "-unchecked", "-feature", "-release:8", "-experimental")
scalacOptions ++= Seq(
"-deprecation",
"-unchecked",
"-feature",
"-release:8",
"-experimental",
"-rewrite"
)
)

lazy val scalamock = crossProject(JSPlatform, JVMPlatform) in file(".") settings(
Expand Down
9 changes: 3 additions & 6 deletions examples/src/main/scala/com/example/Controller.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ package com.example

import scala.math.{atan2, sqrt}

class Controller(turtle: Turtle) {
class Controller(turtle: Turtle):

def drawLine(start: (Double, Double), end: (Double, Double)): Unit = {
def drawLine(start: (Double, Double), end: (Double, Double)): Unit =
moveTo(start)

val initialAngle = turtle.getAngle
Expand All @@ -33,7 +33,6 @@ class Controller(turtle: Turtle) {
turtle.turn(angle(deltaPos) - initialAngle)
turtle.penDown()
turtle.forward(distance(deltaPos))
}

def delta(pos1: (Double, Double), pos2: (Double, Double)) =
(pos2._1 - pos1._1, pos2._2 - pos1._2)
Expand All @@ -44,7 +43,7 @@ class Controller(turtle: Turtle) {
def angle(delta: (Double, Double)) =
atan2(delta._2, delta._1)

def moveTo(pos: (Double, Double)): Unit = {
def moveTo(pos: (Double, Double)): Unit =
val initialPos = turtle.getPosition
val initialAngle = turtle.getAngle

Expand All @@ -53,5 +52,3 @@ class Controller(turtle: Turtle) {
turtle.penUp()
turtle.turn(angle(deltaPos) - initialAngle)
turtle.forward(distance(deltaPos))
}
}
9 changes: 3 additions & 6 deletions examples/src/main/scala/com/example/Order.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@

package com.example

class Order(product: String, quantity: Int) {
class Order(product: String, quantity: Int):

def fill(warehouse: Warehouse): Unit = {
if (warehouse.hasInventory(product, quantity)) {
def fill(warehouse: Warehouse): Unit =
if warehouse.hasInventory(product, quantity) then
warehouse.remove(product, quantity)
filled = true
}
}

def isFilled = filled

private var filled = false
}
3 changes: 1 addition & 2 deletions examples/src/main/scala/com/example/Turtle.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@

package com.example

trait Turtle {
trait Turtle:
def penUp(): Unit
def penDown(): Unit
def forward(distance: Double): Unit
def turn(angle: Double): Unit
def getAngle: Double
def getPosition: (Double, Double)
def setPosition(x: Double, y: Double): (Double, Double)
}
3 changes: 1 addition & 2 deletions examples/src/main/scala/com/example/Warehouse.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

package com.example

trait Warehouse {
trait Warehouse:
def hasInventory(product: String, quantity: Int): Boolean
def remove(product: String, quantity: Int): Unit
}
8 changes: 4 additions & 4 deletions examples/src/test/scala/com/example/ControllerTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ class ControllerTest extends AnyFunSuite with MockFactory {
(() => mockTurtle.getPosition).expects().returning(0.0, 0.0)
(() => mockTurtle.getAngle).expects().returning(0.0)
}
(mockTurtle.turn _).expects(~(Pi / 4))
(mockTurtle.forward _).expects(~sqrt(2.0))
(mockTurtle.turn).expects(~(Pi / 4))
(mockTurtle.forward).expects(~sqrt(2.0))
(() => mockTurtle.getAngle).expects().returning(Pi / 4)
(mockTurtle.turn _).expects(~(-Pi / 4))
(mockTurtle.turn).expects(~(-Pi / 4))
(() => mockTurtle.penDown()).expects()
(mockTurtle.forward _).expects(1.0)
(mockTurtle.forward).expects(1.0)
}

controller.drawLine((1.0, 1.0), (2.0, 1.0))
Expand Down
6 changes: 3 additions & 3 deletions examples/src/test/scala/com/example/OrderSpecification.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class OrderSpecification extends Specification {
"remove inventory when in stock" in new MockContext {
val mockWarehouse = mock[Warehouse]
inSequence {
(mockWarehouse.hasInventory _).expects("Talisker", 50).returning(true).once()
(mockWarehouse.remove _).expects("Talisker", 50).once()
(mockWarehouse.hasInventory).expects("Talisker", 50).returning(true).once()
(mockWarehouse.remove).expects("Talisker", 50).once()
}
val order = new Order("Talisker", 50)
order.fill(mockWarehouse)
Expand All @@ -43,7 +43,7 @@ class OrderSpecification extends Specification {

"remove nothing when out of stock" in new MockContext {
val mockWarehouse = mock[Warehouse]
(mockWarehouse.hasInventory _).expects(*, *).returns(false).once()
(mockWarehouse.hasInventory).expects(*, *).returns(false).once()
val order = new Order("Talisker", 50)
order.fill(mockWarehouse)
order.isFilled must beFalse
Expand Down
6 changes: 3 additions & 3 deletions examples/src/test/scala/com/example/OrderTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class OrderTest extends AnyWordSpec with MockFactory {
"remove inventory" in {
val mockWarehouse = mock[Warehouse]
inSequence {
(mockWarehouse.hasInventory _).expects ("Talisker", 50).returning(true)
(mockWarehouse.remove _).expects("Talisker", 50).once()
(mockWarehouse.hasInventory).expects ("Talisker", 50).returning(true)
(mockWarehouse.remove).expects("Talisker", 50).once()
}

val order = new Order("Talisker", 50)
Expand All @@ -47,7 +47,7 @@ class OrderTest extends AnyWordSpec with MockFactory {
"out of stock" should {
"remove nothing" in {
val mockWarehouse = mock[Warehouse]
(mockWarehouse.hasInventory _) stubs (*, *) returning false
(mockWarehouse.hasInventory) `stubs` (*, *) `returning` false

val order = new Order("Talisker", 50)
order.fill(mockWarehouse)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ReallySimpleExampleTest extends AnyFunSuite with MockFactory {
test("Hello") {
val mockFormatter = mock[Formatter]

(mockFormatter.format _).expects("Mr Bond").returning("Ah, Mr Bond. I've been expecting you").once()
(mockFormatter.format).expects("Mr Bond").returning("Ah, Mr Bond. I've been expecting you").once()

Greetings.sayHello("Mr Bond", mockFormatter)
}
Expand All @@ -37,17 +37,17 @@ class ReallySimpleExampleTest extends AnyFunSuite with MockFactory {
val mockFormatter = stub[Formatter]
val bond = "Mr Bond"

(mockFormatter.format _).when(bond).returns("Ah, Mr Bond. I've been expecting you")
(mockFormatter.format).when(bond).returns("Ah, Mr Bond. I've been expecting you")

Greetings.sayHello(bond, mockFormatter)

(mockFormatter.format _).verify(bond).once()
(mockFormatter.format).verify(bond).once()
}

test("WithVariableParameters") {
val australianFormat = mock[Formatter]

(australianFormat.format _).expects(*).onCall { (s: String) => s"G'day $s" }.twice()
(australianFormat.format).expects(*).onCall { (s: String) => s"G'day $s" }.twice()

Greetings.sayHello("Wendy", australianFormat)
Greetings.sayHello("Gray", australianFormat)
Expand All @@ -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 All @@ -75,7 +75,7 @@ class ReallySimpleExampleTest extends AnyFunSuite with MockFactory {
test("WithBrokenGreeter") {
val brokenFormatter = mock[Formatter]

(brokenFormatter.format _).expects(*).throwing(new NullPointerException).anyNumberOfTimes()
(brokenFormatter.format).expects(*).throwing(new NullPointerException).anyNumberOfTimes()

intercept[NullPointerException] {
Greetings.sayHello("Erza", brokenFormatter)
Expand All @@ -86,8 +86,8 @@ class ReallySimpleExampleTest extends AnyFunSuite with MockFactory {
val mockFormatter = mock[Formatter]

inAnyOrder {
(mockFormatter.format _).expects("Mr Bond").returns("Ah, Mr Bond. I've been expecting you")
(mockFormatter.format _).expects("Natsu").returns("Not now Natsu!").atLeastTwice()
(mockFormatter.format).expects("Mr Bond").returns("Ah, Mr Bond. I've been expecting you")
(mockFormatter.format).expects("Natsu").returns("Not now Natsu!").atLeastTwice()
}

Greetings.sayHello("Natsu", mockFormatter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ class ControllerTest extends AnyFunSuite with MockFactory {
controller.drawLine((1.0, 1.0), (2.0, 1.0))

inSequence {
(mockTurtle.turn _).verify(~(Pi / 4))
(mockTurtle.forward _).verify(~sqrt(2.0))
(mockTurtle.turn _).verify(~(-Pi / 4))
(mockTurtle.turn).verify(~(Pi / 4))
(mockTurtle.forward).verify(~sqrt(2.0))
(mockTurtle.turn).verify(~(-Pi / 4))
(() => mockTurtle.penDown()).verify()
(mockTurtle.forward _).verify(1.0)
(mockTurtle.forward).verify(1.0)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ class HigherOrderFunctionsTest extends AnyFreeSpec with MockFactory {
"testFoldLeft" in {
val f = stubFunction[String, Int, String]

f when("initial", 0) returns "intermediate one"
f when("intermediate one", 1) returns "intermediate two"
f when("intermediate two", 2) returns "intermediate three"
f when("intermediate three", 3) returns "final"
f `when`("initial", 0) `returns` "intermediate one"
f `when`("intermediate one", 1) `returns` "intermediate two"
f `when`("intermediate two", 2) `returns` "intermediate three"
f `when`("intermediate three", 3) `returns` "final"

assertResult("final") {
Seq(0, 1, 2, 3).foldLeft("initial")(f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ class OrderTest extends AnyWordSpec with MockFactory {
"remove inventory" in {
val mockWarehouse = stub[Warehouse]

(mockWarehouse.hasInventory _) when ("Talisker", 50) returns true
(mockWarehouse.hasInventory) `when` ("Talisker", 50) `returns` true

val order = new Order("Talisker", 50)
order.fill(mockWarehouse)

assert(order.isFilled)
(mockWarehouse.remove _).verify("Talisker", 50).once()
(mockWarehouse.remove).verify("Talisker", 50).once()
}
}

Expand Down
6 changes: 2 additions & 4 deletions js/src/test/scala/org/scalamock/jstests/JSNativeTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ import org.scalatest.matchers.should.Matchers

@js.native
@JSGlobal
class FakeJSNativeClass extends js.Object {
class FakeJSNativeClass extends js.Object:
def fillText(text: String, x: Double, y: Double, maxWidth: Double = js.native): Unit = js.native
}

class JSNativeTest extends AnyFlatSpec with MockFactory with Matchers {
class JSNativeTest extends AnyFlatSpec with MockFactory with Matchers:
ignore should "create a mock for method with 'js.native' default args" in {
// val _ = mock[FakeJSNativeClass]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ import org.scalamock.scalatest.MockFactory
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class ArgThatTest extends AnyFlatSpec with Matchers with MockFactory {
class ArgThatTest extends AnyFlatSpec with Matchers with MockFactory:

behavior of "ArgThat"

it should "check predicate while matching arguments" in {
it should "check predicate while matching arguments" in:
val startsWithPredicate = argThat((x: String) => x.startsWith("A"))
startsWithPredicate.equals("Alice") shouldBe true
startsWithPredicate.equals("Anna") shouldBe true
startsWithPredicate.equals("Bob") shouldBe false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ import org.scalamock.scalatest.MockFactory
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class ArgThatTest extends AnyFlatSpec with Matchers with MockFactory {
class ArgThatTest extends AnyFlatSpec with Matchers with MockFactory:

behavior of "ArgThat"

it should "check predicate while matching arguments" in {
it should "check predicate while matching arguments" in:
val startsWithPredicate = argThat((x: String) => x.startsWith("A"))
startsWithPredicate.equals("Alice") shouldBe true
startsWithPredicate.equals("Anna") shouldBe true
startsWithPredicate.equals("Bob") shouldBe false
intercept[ClassCastException] { startsWithPredicate.equals(55) } // 55 is not a String
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ package com.paulbutcher.test.matchers
import org.scalamock.matchers.MatchAny
import org.scalatest.flatspec.AnyFlatSpec

class MatchAnyTest extends AnyFlatSpec {
class MatchAnyTest extends AnyFlatSpec:

"MatchAny" should "match anything" in {
"MatchAny" should "match anything" in:
assert(new MatchAny === 1.0)
assert(new MatchAny === "")
assert(new MatchAny === (0, 42))
assert(new MatchAny === List(1, 2, 3))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class JavaMockNamingTest extends IsolatedSpec {

it should "have a sensible method name when mocking Java polymorphic interface" in {
val myMock = mock[JavaGenericInterface[List[Int]]]
getMockMethodName(myMock.compare _) shouldBe "<mock-1> JavaGenericInterface[List[Int]].compare"
getMockMethodName(myMock.compare) shouldBe "<mock-1> JavaGenericInterface[List[Int]].compare"
}

override def newInstance = new JavaMockNamingTest
Expand Down
12 changes: 6 additions & 6 deletions jvm/src/test/scala/com.paulbutcher.test/mock/JavaMocksTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class JavaMocksTest extends IsolatedSpec {

it should "mock Java generics" in {
val m = mock[JavaGenericInterface[Int]]
(m.simpleMethod _) expects ("two") returning 42
(m.simpleMethod) `expects` ("two") `returning` 42

m.simpleMethod("two") shouldBe 42
}
Expand All @@ -36,8 +36,8 @@ class JavaMocksTest extends IsolatedSpec {
class JavaClassWithBridgeMethodExtended extends JavaClassWithBridgeMethod
val m = mock[JavaClassWithBridgeMethodExtended]

(m.compare _).expects(Integer.valueOf(5)).returning(1)
(m.compare _).expects(Integer.valueOf(6)).returning(2)
(m.compare).expects(Integer.valueOf(5)).returning(1)
(m.compare).expects(Integer.valueOf(6)).returning(2)

def useBridgeMethod[T](gen: JavaGenericInterface[T], x: T) = {
gen.compare(x)
Expand All @@ -58,20 +58,20 @@ class JavaMocksTest extends IsolatedSpec {

it should "mock a Java interface" in {
val m = mock[JavaInterface]
(m.m _).expects(42, "foo").returning("a return value")
(m.m).expects(42, "foo").returning("a return value")
assertResult("a return value") { m.m(42, "foo") }
}

it should "mock a Polymorhpic Java interface" in { // test for issue #24
val m = mock[PolymorphicJavaInterface]
(m.simplePolymorphicMethod _).expects("foo").returning(44)
(m.simplePolymorphicMethod).expects("foo").returning(44)
assertResult(44) { m.simplePolymorphicMethod[Int]("foo") }
}

it should "mock a Polymorhpic Java interface (type parametrized method parameter)" in {
val m = mock[PolymorphicJavaInterface]
val arg = new java.util.ArrayList[String]
(m.polymorphicMethod[String] _).expects(arg).returning("foo")
(m.polymorphicMethod[String]).expects(arg).returning("foo")

m.polymorphicMethod(arg) shouldBe "foo"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class ProxyMockTest extends AnyFreeSpec with MockFactory {
withExpectations {
val m = mock[TestTrait]
m.expects(Symbol("curried"))(10, 1.23).returning("curried method called")
val partial = m.curried(10) _
val partial = m.curried(10)
assertResult("curried method called") { partial(1.23) }
}
}
Expand Down
Loading

0 comments on commit a35b864

Please sign in to comment.