8000 Move context functions to MockContext by steinybot · Pull Request #632 · scalamock/scalamock · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Move context functions to MockContext #632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,4 @@ import org.scalamock.matchers.Matchers
trait AbstractMockFactoryBase extends Mock with MockFunctions with Matchers { this: MockContext =>

protected def withExpectations[T](what: => T): T

protected def inAnyOrder[T](what: => T): T
protected def inSequence[T](what: => T): T
protected def inAnyOrderWithLogging[T](what: => T): T
protected def inSequenceWithLogging[T](what: => T): T
}
23 changes: 0 additions & 23 deletions core/shared/src/main/scala/org/scalamock/MockFactoryBase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,6 @@ trait MockFactoryBase extends AbstractMockFactoryBase with MockContext {
}
}

override protected def inAnyOrder[T](what: => T): T = {
inContext(new UnorderedHandlers)(what)
}

override protected def inSequence[T](what: => T): T = {
inContext(new OrderedHandlers)(what)
}

override protected def inAnyOrderWithLogging[T](what: => T) =
inContext(new UnorderedHandlers(logging = true))(what)

override protected def inSequenceWithLogging[T](what: => T) =
inContext(new OrderedHandlers(logging = true))(what)

// https://issues.scala-lang.org/browse/SI-5831
implicit val _factory: MockFactoryBase = this

Expand Down Expand Up @@ -94,13 +80,4 @@ trait MockFactoryBase extends AbstractMockFactoryBase with MockContext {
reportUnsatisfiedExpectation(oldCallLog, oldExpectationContext)
}

private def inContext[T](context: Handlers)(what: => T): T = {
currentExpectationContext.add(context)
val prevContext = currentExpectationContext
currentExpectationContext = context
val r = what
currentExpectationContext = prevContext
r
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

package org.scalamock.context

import org.scalamock.handlers.{CallHandler, Handlers}
import org.scalamock.handlers.{CallHandler, Handlers, OrderedHandlers, UnorderedHandlers}

private[scalamock] trait MockContext {
type ExpectationException <: Throwable
Expand Down Expand Up @@ -49,4 +49,27 @@ private[scalamock] trait MockContext {

/** Generates unique names for mocks, stubs, and mock functions */
def generateMockDefaultName(prefix: String): Symbol = mockNameGenerator.generateMockName(prefix)

protected def inAnyOrder[T](what: => T): T = {
inContext(new UnorderedHandlers)(what)
}

protected def inSequence[T](what: => T): T = {
inContext(new OrderedHandlers)(what)
}

protected def inAnyOrderWithLogging[T](what: => T) =
inContext(new UnorderedHandlers(logging = true))(what)

protected def inSequenceWithLogging[T](what: => T) =
inContext(new OrderedHandlers(logging = true))(what)

private def inContext[T](context: Handlers)(what: => T): T = {
currentExpectationContext.add(context)
val prevContext = currentExpectationContext
currentExpectationContext = context
val r = what
currentExpectationContext = prevContext
r
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2011-2025 ScalaMock Contributors (https://github.com/paulbutcher/ScalaMock/graphs/contributors)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package com.paulbutcher.test

import org.scalamock.scalatest.AsyncMockFactory
import org.scalatest.flatspec.{AnyFlatSpec, AsyncFlatSpec}
import org.scalatest.matchers.should.Matchers
import org.scalatest.{Assertion, OneInstancePerTest, Suite}

import scala.concurrent.Future

abstract class AsyncIsolatedSpec extends AsyncFlatSpec with AsyncMockFactory with Matchers with OneInstancePerTest {

def repeat(n: Int)(what: => Unit): Unit = {
for (i <- 0 until n)
what
}

def demandExpectationException(block: => Future[Any]): Future[Assertion] = {
recoverToSucceededIf[ExpectationException](withExpectations {
block
})
}

// made this method mandatory to override due to https://github.com/scalatest/scalatest/issues/965
override def newInstance: Suite with OneInstancePerTest
}
Loading
0