Skip to content

Commit

Permalink
restrict multiple expectations on same method
Browse files Browse the repository at this point in the history
  • Loading branch information
goshacodes committed Sep 28, 2024
1 parent d0787f8 commit 52f6994
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/main/scala/backstub/internal/StubCreation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ class StubCreation(using override val quotes: Quotes) extends StubUtils:
} =>
val method = methods.searchMethod(select.asTerm, None, TypeRepr.of[res])

if (expectations.exists(_._1.symbol == method.symbol))
report.errorAndAbort(s"Expectation for ${select.asTerm.show(using Printer.TreeShortCode)} is already set")

val expectation = Expectation(value.asTerm)
loop(rest, (method -> expectation) :: expectations)

Expand All @@ -173,6 +176,9 @@ class StubCreation(using override val quotes: Quotes) extends StubUtils:
} =>
val method = methods.searchMethod(select.asTerm, Some(TypeRepr.of[Tuple1[arg]]), TypeRepr.of[res])

if (expectations.exists(_._1.symbol == method.symbol))
report.errorAndAbort(s"Expectation for ${select.asTerm.show(using Printer.TreeShortCode)} is already set")

val expectation = Expectation(apply.asTerm)
loop(rest, (method -> expectation) :: expectations)

Expand All @@ -184,6 +190,10 @@ class StubCreation(using override val quotes: Quotes) extends StubUtils:
.returns((${apply}: args => res))
} =>
val method = methods.searchMethod(select.asTerm, Some(TypeRepr.of[args]), TypeRepr.of[res])

if (expectations.exists(_._1.symbol == method.symbol))
report.errorAndAbort(s"Expectation for ${select.asTerm.show(using Printer.TreeShortCode)} is already set")

val expectation = Expectation(apply.asTerm)
loop(rest, (method -> expectation) :: expectations)

Expand Down
5 changes: 2 additions & 3 deletions src/test/scala/backstub/ExpectationsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package backstub

import backstub.*

class ExpectationsSpec extends munit.FunSuite with Stubs {
class ExpectationsSpec extends munit.FunSuite with Stubs:

override def afterEach(context: AfterEach) =
resetStubs()
Expand All @@ -20,7 +20,7 @@ class ExpectationsSpec extends munit.FunSuite with Stubs {
test("zero args"):
val foo = stub[Foo]:
Expect[Foo]
.method[String](_.zeroArgs).returnsOnly("5")
.method(_.zeroArgs).returnsOnly("5")

val res = foo.zeroArgs
foo.zeroArgs
Expand Down Expand Up @@ -83,4 +83,3 @@ class ExpectationsSpec extends munit.FunSuite with Stubs {
assert(secondCalls == List((1, true)))
assert(thirdCallsTimes == 1)

}

0 comments on commit 52f6994

Please sign in to comment.