Skip to content

Commit

Permalink
restricted vars support
Browse files Browse the repository at this point in the history
  • Loading branch information
goshacodes committed Oct 17, 2023
1 parent c32eedd commit b40375e
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ class ProxyMockTest extends AnyFreeSpec with MockFactory {
}

/*
//! TODO - in scala 3 we can't distinguish abstract var setters with non abstract vars setters
// TODO and override modifier is not allowed when overriding abstract vars
"cope with a var" in {
withExpectations {
val m = mock[TestTrait]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
package org.scalamock.clazz

import org.scalamock.context.MockContext
import org.scalamock.util.Defaultable

import scala.quoted.*
import scala.reflect.Selectable
Expand Down
2 changes: 1 addition & 1 deletion shared/src/main/scala-3/org/scalamock/clazz/Utils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private[clazz] class Utils(using val quotes: Quotes):

def apply(tpe: TypeRepr): List[MockableDefinition] =
val methods = (tpe.typeSymbol.methodMembers.toSet -- TypeRepr.of[Object].typeSymbol.methodMembers).toList
.filter(sym => !sym.flags.is(Flags.Private) && !sym.flags.is(Flags.Final))
.filter(sym => !sym.flags.is(Flags.Private) && !sym.flags.is(Flags.Final) && !sym.flags.is(Flags.Mutable))
.filterNot(sym => tpe.memberType(sym) match
case defaultParam @ ByNameType(AnnotatedType(_, Apply(Select(New(Inferred()), "<init>"), Nil))) => true
case _ => false
Expand Down
26 changes: 26 additions & 0 deletions shared/src/test/scala-2/VarSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.paulbutcher.test

import org.scalamock.scalatest.MockFactory
import org.scalatest.funspec.AnyFunSpec

class VarSpec extends AnyFunSpec with MockFactory {

autoVerify = false

trait Vars {
var aVar: String
var concreteVar = "foo"
}

it("mock traits with vars") {
withExpectations {
val m = mock[Vars]
(m.aVar_= _).expects("foo")
(() => m.aVar).expects().returning("bar")
m.aVar = "foo"
assertResult("bar") {
m.aVar
}
}
}
}
19 changes: 19 additions & 0 deletions shared/src/test/scala-3/com/paulbutcher/test/VarSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.paulbutcher.test

import org.scalamock.scalatest.MockFactory
import org.scalatest.funspec.AnyFunSpec

class VarSpec extends AnyFunSpec with MockFactory {

trait Vars {
var aVar: Int = scala.compiletime.uninitialized
var concreteVar = "foo"
}

it("mock traits with vars") {
val m = mock[Vars]
m.aVar = 6
m.concreteVar = "bar"
}

}
3 changes: 0 additions & 3 deletions shared/src/test/scala/com/paulbutcher/test/TestTrait.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ trait TestTrait {
def otherPackageUpperBound[T <: SomeClass](x: T): T
def explicitPackageReference(x: yet.another.pkg.YetAnotherClass): yet.another.pkg.YetAnotherClass
def explicitPackageUpperBound[T <: yet.another.pkg.YetAnotherClass](x: T): T

//var aVar: String
//var concreteVar = "foo"

val aVal: String
val concreteVal = "foo"
Expand Down
13 changes: 8 additions & 5 deletions shared/src/test/scala/com/paulbutcher/test/mock/MockTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,20 @@ class MockTest extends AnyFreeSpec with MockFactory with Matchers {

autoVerify = false

"Mocks should" - {/*
"cope with a var" in {
"Mocks should" - {
//! TODO - in scala 3 we can't distinguish abstract var setters with non abstract vars setters
// TODO and override modifier is not allowed when overriding abstract vars
/*
"cope with a var" in {
withExpectations {
val m = mock[TestTrait]
(m.aVar_= _).expects("foo")
(() => m.aVar).expects().returning("bar")
m.aVar = "foo"
assertResult("bar") { m.aVar }
assertResult(null) { m.aVar }
}
}
*/
}*/

"fail if an unexpected method call is made" in {
withExpectations {
val m = mock[TestTrait]
Expand Down

0 comments on commit b40375e

Please sign in to comment.