1. Philipp Dörfler
  2. swiftsteel

Commits

Philipp Dörfler  committed e916dc7

Refactored MapPoint test

  • Participants
  • Parent commits 7855a80
  • Branches default

Comments (0)

Files changed (6)

File src/main/scala/org/swiftsteel/model/playground/points/MapPoint.scala

View file
   val x: Double,
   val y: Double
 ) {
-  MapPoint.checkMapBoundsViolationOf(this)
+  MapPoint checkMapBoundsViolationOf this
   
-  def + (that: MapPoint) = {
-    require(map == that.map)
-    MapPoint(map, x + that.x, y + that.y)
+  def + (other: MapPoint) = {
+    mustBeOnSameMapAs(other)
+    MapPoint(map, x + other.x, y + other.y)
   }
   
-  def - (that: MapPoint) = {
-    require(map == that.map)
-    MapPoint(map, x - that.x, y - that.y)
+  def - (other: MapPoint) = {
+    mustBeOnSameMapAs(other)
+    MapPoint(map, x - other.x, y - other.y)
   }
   
   def * (amount: Double) = MapPoint(map, x * amount, y * amount)
   
-  def isNear (that: MapPoint) =
-    abs(that.x - this.x) < FastMath.FLT_EPSILON &&
-    abs(that.y - this.y) < FastMath.FLT_EPSILON
+  def isNear (other: MapPoint) = {
+    mustBeOnSameMapAs(other)
+    abs(other.x - this.x) < FastMath.FLT_EPSILON &&
+    abs(other.y - this.y) < FastMath.FLT_EPSILON
+  }
     
   lazy val to3D = Point(map, x, y, map.heightAt(this))
+  
+  private def mustBeOnSameMapAs(other: MapPoint) =
+    require(map == other.map, "terrain of both points differs: %s != %s".format(map, other.map))
 }
 
 object MapPoint {

File src/test/scala/org/swiftsteel/model/playground/TerrainSpec.scala

View file
   
   "the terrain" can never {
     "have a heightmap which is not dividable by two" in {
-      (new PlainTerrain with OddHeightMap) must throwA[IllegalArgumentException]
+      (new PlainTerrain with OddHeightMap) must throwAn[IllegalArgumentException]
     }
     
     "have a heightmap with too less rows" in {
-      new TestTerrain(1, 2) must throwA[IllegalArgumentException]
+      new TestTerrain(1, 2) must throwAn[IllegalArgumentException]
     }
     
     "have a heightmap with too less columns" in {
-      new TestTerrain(2, 1) must throwA[IllegalArgumentException]
+      new TestTerrain(2, 1) must throwAn[IllegalArgumentException]
     }
   }
   
   }
   
   private def given(terrain: Terrain) = this.terrain = terrain
-  
-  def never = addToSusVerb("never")
 }
 
 trait ModifiedHeightMap extends Terrain {

File src/test/scala/org/swiftsteel/model/playground/points/MapPointSpec.scala

View file
     "exceed the map boundaries" >> {
       "when being added" in {
         given(pointAt(40, 20))
-        (when(0.0001, 0.0001) isAdded()) must throwA[IllegalArgumentException]
+        (when(0.0001, 0.0001) isAdded()) must throwAn[IllegalArgumentException]
       }
       
       "when being subtracted" in {
         given(pointAt(0, 0))
-        (when(0.0001, 0.0001) isSubtracted()) must throwA[IllegalArgumentException]
+        (when(0.0001, 0.0001) isSubtracted()) must throwAn[IllegalArgumentException]
       }
       
       "at instantiation" in {
-        pointAt(0, -0.0001) must throwA[IllegalArgumentException]
+        pointAt(0, -0.0001) must throwAn[IllegalArgumentException]
+      }
+    }
+    
+    "be used with another map's points" >> {
+      "in additions" in {
+        given(foreignPointAt(4, 2))
+        (when(0, 0) isAdded()) must throwAn[IllegalArgumentException]
+      }
+      
+      "in subtractions" in {
+        given(foreignPointAt(4, 2))
+        (when(0, 0) isSubtracted()) must throwAn[IllegalArgumentException]
+      }
+      
+      "in comparisons" in {
+        given(foreignPointAt(4, 2))
+        whenCheckedForBeingCloseTo(pointAt(4, 2)) must throwAn[IllegalArgumentException]
       }
     }
   }
   
-  def never = addToSusVerb("never")
-  
   private def given(point: MapPoint) = this.point = point
   
   def when(x: Double, y: Double) = new PointExpectation((x, y))
   
-  private def pointAt(x: Double, y: Double) = terrain.pointAt(x, y)
+  def pointAt(x: Double, y: Double) = terrain.pointAt(x, y)
+  def foreignPointAt(x: Double, y: Double) = new PlainTerrain().pointAt(x, y)
     
   class PointExpectation(boring: (Double, Double)) {
     def isAdded() = point = point + pointAt(boring._1, boring._2)
     def isSubtracted() = point = point - pointAt(boring._1, boring._2)
   }
   
-  private def whenMultipliedWith(amount: Double) = point = point * amount
+  def whenMultipliedWith(amount: Double) = point = point * amount
+  def whenCheckedForBeingCloseTo(other: MapPoint) = point isNear other
 }

File src/test/scala/org/swiftsteel/model/playground/points/PointSpec.scala

View file
     "exceed the map boundaries" >> {
       "when added" in {
         given(pointAt(40, 20))
-        (when(0.0001, 0) isAdded()) must throwA[IllegalArgumentException]
+        (when(0.0001, 0) isAdded()) must throwAn[IllegalArgumentException]
       }
       
       "when subtracted" in {
         given(pointAt(0.1, 0.1))
-        (when(0.2, 0) isSubtracted()) must throwA[IllegalArgumentException]
+        (when(0.2, 0) isSubtracted()) must throwAn[IllegalArgumentException]
       }
       
       "at instantiation" in {
-        pointAt(-1, -1) must throwA[IllegalArgumentException]
+        pointAt(-1, -1) must throwAn[IllegalArgumentException]
       }
     }
   }
   
-  def never = addToSusVerb("never")
-  
   private def given(point: Point) = this.point = point
   
   def when(x: Double, y: Double) = new PointExpectation((x, y))

File src/test/scala/org/swiftsteel/model/time/MomentSpec.scala

View file
       Moment(-1) must throwAn[IllegalArgumentException]
     }
   }
-  
-  def never = addToSusVerb("never")
 }

File src/test/scala/org/swiftsteel/specs/SwiftSteelSpec.scala

View file
 import org.specs._
 import org.specs.util.DataTables
 
-class SwiftSteelSpec extends Specification with DataTables
+class SwiftSteelSpec extends Specification with DataTables {
+  def never = addToSusVerb("never")
+}