Skip to content
Open
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
3 changes: 2 additions & 1 deletion scala/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@

addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.0.0")

addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.2.0")
2 changes: 1 addition & 1 deletion scala/project_sbt0.7/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ project.organization=kiel
project.name=replicant
sbt.version=0.7.3
project.version=0.1
build.scala.versions=2.9.0 2.8.1 2.8.0
build.scala.versions=2.9.1 2.9.0 2.8.1 2.8.0
project.initialize=false
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package replicant

trait MockMinder[Mock] {
object Mock {
implicit def toMock[Subject](minder: Mock[Subject]) : Subject = minder.mock
}

val mock: Mock
trait Mock[Subject] {

val mock: Subject

protected def method[Result: ResponseFallback](methodName: String, target: () => Result) =
Mocker0[Result](mock, methodName)
Expand All @@ -13,4 +17,5 @@ trait MockMinder[Mock] {
protected def method[Arg1, Arg2, Result: ResponseFallback](methodName: String, target: (Arg1, Arg2) => Result) =
Mocker[(Arg1, Arg2), Result](mock, methodName)


}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import org.junit.runner._
import testing._

@RunWith(classOf[JUnitRunner])
class MockMinderTest extends junit.JUnit3Suite with ShouldMatchers {
class MockTest extends junit.JUnit3Suite with ShouldMatchers {

trait Foo {
def noArgMethod: A
def oneArgMethod(arg: A): B
def twoArgMethod(arg1: A, arg2: B): C
}

class FooMinder extends MockMinder[Foo] { minder =>
class MockFoo extends Mock[Foo] { minder =>

val mock: Foo = new Foo {
def noArgMethod = minder.noArgMethod()
Expand All @@ -31,36 +31,39 @@ class MockMinderTest extends junit.JUnit3Suite with ShouldMatchers {
}

@Test def testControllerWithNoArgMethod {
val minder = new FooMinder

val minder = new MockFoo
val mock: Foo = minder

val noArgMethod: Mocker0[A] = minder.noArgMethod

minder.noArgMethod should equal(Mocker0[A](minder.mock, "noArgMethod"))

minder.noArgMethod.expect { A(7) }
minder.mock.noArgMethod should equal(A(7))
}

mock.noArgMethod should equal(A(7))
}


@Test def testControllerWithOneArgMethod {
val minder = new FooMinder

val minder = new MockFoo
val mock: Foo = minder

val oneArgMethod: Mocker[A, B] = minder.oneArgMethod

minder.oneArgMethod should equal(Mocker[A, B](minder.mock, "oneArgMethod"))

minder.oneArgMethod.expect(A(3)) { B(7) }
minder.mock.oneArgMethod(A(3)) should equal(B(7))
}
mock.oneArgMethod(A(3)) should equal(B(7))
}

@Test def testControllerWithTwoArgMethod {
val minder = new FooMinder

val minder = new MockFoo
val mock: Foo = minder

val twoArgMethod: Mocker[(A, B), C] = minder.twoArgMethod

minder.twoArgMethod should equal(Mocker[(A, B), C](minder.mock, "twoArgMethod"))

minder.twoArgMethod.expect(A(3), B(7)) { C(11) }
minder.mock.twoArgMethod(A(3), B(7)) should equal(C(11))
}

mock.twoArgMethod(A(3), B(7)) should equal(C(11))
}
}
2 changes: 1 addition & 1 deletion scala/src/test/scala/sample/MockGenericRepository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package sample

import replicant._

class MockGenericRepository[Subject: ResponseFallback] extends MockMinder[GenericRepository[Subject]] { minder =>
class MockGenericRepository[Subject: ResponseFallback] extends Mock[GenericRepository[Subject]] { minder =>

protected class BaseSubject {
def store(subject: Subject) = minder.store(subject)
Expand Down
2 changes: 1 addition & 1 deletion scala/src/test/scala/sample/MockRequestQueue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package sample

import replicant._

class MockRequestQueue extends MockMinder[RequestQueue] { minder =>
class MockRequestQueue extends Mock[RequestQueue] { minder =>

val mock: RequestQueue = new RequestQueue {
def nextRequest: Option[Request] = minder.nextRequest()
Expand Down
2 changes: 1 addition & 1 deletion scala/src/test/scala/sample/MockWidgetPainter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package sample

import replicant._

class MockWidgetPainter extends MockMinder[WidgetPainter] { minder =>
class MockWidgetPainter extends Mock[WidgetPainter] { minder =>

val mock: WidgetPainter = new WidgetPainter {
def paintWidget(widget: Widget) { minder.paintWidget(widget) }
Expand Down
13 changes: 7 additions & 6 deletions scala/src/test/scala/sample/PaintingTaskTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class PaintingTaskTest extends junit.JUnit3Suite with ShouldMatchers {
val requestQueue = new MockRequestQueue
val painter = new MockWidgetPainter
val widgetRepository = new MockGenericRepository[Widget]
val paintingTask = new PaintingTask(requestQueue.mock, painter.mock, widgetRepository.mock)
val paintingTask = new PaintingTask(requestQueue, painter, widgetRepository)

val requests = mutable.Queue(Some(Request(17)), Some(Request(42)), Some(Request(37)), None)
requestQueue.nextRequest.expect {
Expand All @@ -42,15 +42,16 @@ class PaintingTaskTest extends junit.JUnit3Suite with ShouldMatchers {

@Test def testOrderingResponses {
val queue = new MockRequestQueue
val mockedQueue: RequestQueue = queue
val requests = mutable.Queue(Some(Request(1)), Some(Request(2)), Some(Request(3)), None)
queue.nextRequest.expect {
requests.dequeue
}
queue.mock.nextRequest should equal(Some(Request(1)))
queue.mock.nextRequest should equal(Some(Request(2)))
queue.mock.nextRequest should equal(Some(Request(3)))
queue.mock.nextRequest should equal(None)
intercept[java.util.NoSuchElementException] { queue.mock.nextRequest }
mockedQueue.nextRequest should equal(Some(Request(1)))
mockedQueue.nextRequest should equal(Some(Request(2)))
mockedQueue.nextRequest should equal(Some(Request(3)))
mockedQueue.nextRequest should equal(None)
intercept[java.util.NoSuchElementException] { mockedQueue.nextRequest }
}

class Mocker2[Args1, Args2, Result](mock: Any, methodName: String) {
Expand Down