Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Initial checkin

  • Loading branch information...
commit f1d7da35029ba54cd95f809e33b7764926d5a422 0 parents
Paul Butcher authored May 11, 2011
3  .gitignore
... ...
@@ -0,0 +1,3 @@
  1
+target/
  2
+*_managed/
  3
+boot/
8  project/build.properties
... ...
@@ -0,0 +1,8 @@
  1
+#Project properties
  2
+#Sat Feb 26 23:58:14 GMT 2011
  3
+project.organization=com.example
  4
+project.name=Mock Turtle
  5
+sbt.version=0.7.4
  6
+project.version=1.0
  7
+build.scala.versions=2.8.1
  8
+project.initialize=false
7  project/build/Project.scala
... ...
@@ -0,0 +1,7 @@
  1
+import sbt._ 
  2
+
  3
+class Project(info: ProjectInfo) extends DefaultProject(info) { 
  4
+  val scalatest = "org.scalatest" % "scalatest" % "1.2"
  5
+  val borachio = 
  6
+    "com.borachio" %% "borachio" % "latest.integration"
  7
+}
37  src/main/scala/Controller.scala
... ...
@@ -0,0 +1,37 @@
  1
+package com.example
  2
+
  3
+import scala.math.{atan2, sqrt}
  4
+
  5
+class Controller(turtle: Turtle) {
  6
+  
  7
+  def drawLine(start: (Double, Double), end: (Double, Double)) {
  8
+    moveTo(start)
  9
+    
  10
+    val initialAngle = turtle.getAngle
  11
+    val deltaPos = delta(start, end)
  12
+
  13
+    turtle.turn(angle(deltaPos) - initialAngle)
  14
+    turtle.penDown
  15
+    turtle.forward(distance(deltaPos))
  16
+  }
  17
+  
  18
+  def delta(pos1: (Double, Double), pos2: (Double, Double)) = 
  19
+    (pos2._1 - pos1._1, pos2._2 - pos1._2)
  20
+  
  21
+  def distance(delta: (Double, Double)) = 
  22
+    sqrt(delta._1 * delta._1 + delta._2 * delta._2)
  23
+  
  24
+  def angle(delta: (Double, Double)) = 
  25
+    atan2(delta._2, delta._1)
  26
+  
  27
+  def moveTo(pos: (Double, Double)) {
  28
+    val initialPos = turtle.getPosition
  29
+    val initialAngle = turtle.getAngle
  30
+
  31
+    val deltaPos = delta(initialPos, pos)
  32
+
  33
+    turtle.penUp
  34
+    turtle.turn(angle(deltaPos) - initialAngle)
  35
+    turtle.forward(distance(deltaPos))
  36
+  }
  37
+}
10  src/main/scala/Turtle.scala
... ...
@@ -0,0 +1,10 @@
  1
+package com.example
  2
+
  3
+trait Turtle {
  4
+  def penUp()
  5
+  def penDown()
  6
+  def forward(distance: Double): (Double, Double)
  7
+  def turn(angle: Double)
  8
+  def getAngle: Double
  9
+  def getPosition: (Double, Double)
  10
+}
27  src/test/scala/ControllerTest.scala
... ...
@@ -0,0 +1,27 @@
  1
+package com.example
  2
+
  3
+import org.scalatest.Suite
  4
+import com.borachio.MockFactory
  5
+import scala.math.{Pi, sqrt}
  6
+
  7
+class MockFunctionTest extends Suite with MockFactory {
  8
+  
  9
+  val mockTurtle = mock[Turtle]
  10
+  val controller = new Controller(mockTurtle)
  11
+
  12
+  def testDrawLine() {
  13
+    inSequence {
  14
+      mockTurtle expects 'getPosition returning (0.0, 0.0)
  15
+      mockTurtle expects 'getAngle returning 0.0
  16
+      mockTurtle expects 'penUp
  17
+      mockTurtle expects 'turn withArgs (~(Pi / 4))
  18
+      mockTurtle expects 'forward withArgs (~sqrt(4.0))
  19
+      mockTurtle expects 'getAngle returning Pi / 4
  20
+      mockTurtle expects 'turn withArgs (~(-Pi / 4))
  21
+      mockTurtle expects 'penDown
  22
+      mockTurtle expects 'forward withArgs (1.0)
  23
+    }
  24
+    
  25
+    controller.drawLine((1.0, 1.0), (2.0, 1.0))
  26
+  }
  27
+}

0 notes on commit f1d7da3

Please sign in to comment.
Something went wrong with that request. Please try again.