2016-08-17 11:14:17 +00:00
|
|
|
port module Main exposing (..)
|
2016-03-28 10:23:01 +00:00
|
|
|
|
2016-12-17 19:28:59 +00:00
|
|
|
import Test.Runner.Node exposing (run, TestProgram)
|
2016-08-17 11:14:17 +00:00
|
|
|
import Json.Encode exposing (Value)
|
|
|
|
import Test exposing (..)
|
|
|
|
import Expect
|
2016-03-28 10:23:01 +00:00
|
|
|
import RobotSimulator exposing (defaultRobot, Robot, Bearing(North, East, West, South), turnRight, turnLeft, advance, simulate)
|
|
|
|
|
|
|
|
|
|
|
|
tests : Test
|
|
|
|
tests =
|
2016-08-17 11:14:17 +00:00
|
|
|
describe "RobotSimulator"
|
|
|
|
[ describe "init"
|
2016-06-19 21:46:13 +00:00
|
|
|
(let
|
|
|
|
robot =
|
|
|
|
defaultRobot
|
|
|
|
in
|
2016-08-17 11:14:17 +00:00
|
|
|
[ test "coordinates" <|
|
|
|
|
\() -> Expect.equal { x = 0, y = 0 } robot.coordinates
|
|
|
|
, test "bearing" <|
|
|
|
|
\() -> Expect.equal North robot.bearing
|
2016-06-19 21:46:13 +00:00
|
|
|
]
|
|
|
|
)
|
2016-08-17 11:14:17 +00:00
|
|
|
, describe "setup"
|
2016-06-19 21:46:13 +00:00
|
|
|
(let
|
|
|
|
robot =
|
|
|
|
Robot South { x = -1, y = 1 }
|
|
|
|
in
|
2016-08-17 11:14:17 +00:00
|
|
|
[ test "coordinates" <|
|
|
|
|
\() -> Expect.equal { x = -1, y = 1 } robot.coordinates
|
|
|
|
, test "bearing" <|
|
|
|
|
\() -> Expect.equal South robot.bearing
|
2016-06-19 21:46:13 +00:00
|
|
|
]
|
|
|
|
)
|
2016-08-17 11:14:17 +00:00
|
|
|
, describe "turn right"
|
2016-12-17 22:15:34 +00:00
|
|
|
((List.range 1 3)
|
2016-06-19 21:46:13 +00:00
|
|
|
|> List.scanl (\_ r -> turnRight r) defaultRobot
|
|
|
|
|> List.map .bearing
|
|
|
|
|> assertionList [ North, East, South, West ]
|
2016-08-17 11:14:17 +00:00
|
|
|
|> List.indexedMap (\i e -> test ("step " ++ toString i) (\() -> e))
|
2016-06-19 21:46:13 +00:00
|
|
|
)
|
2016-08-17 11:14:17 +00:00
|
|
|
, describe
|
|
|
|
"turn left"
|
2016-12-17 22:15:34 +00:00
|
|
|
((List.range 1 3)
|
2016-06-19 21:46:13 +00:00
|
|
|
|> List.scanl (\_ r -> turnLeft r) defaultRobot
|
|
|
|
|> List.map .bearing
|
|
|
|
|> assertionList [ North, West, South, East ]
|
2016-08-17 11:14:17 +00:00
|
|
|
|> List.indexedMap (\i e -> test ("step " ++ toString i) (\() -> e))
|
2016-06-19 21:46:13 +00:00
|
|
|
)
|
2016-08-17 11:14:17 +00:00
|
|
|
, describe "advance positive north"
|
2016-06-19 21:46:13 +00:00
|
|
|
(let
|
|
|
|
robot =
|
|
|
|
Robot North { x = 0, y = 0 }
|
|
|
|
|> advance
|
|
|
|
in
|
2016-08-17 11:14:17 +00:00
|
|
|
[ test "coordinates" <|
|
|
|
|
\() -> Expect.equal { x = 0, y = 1 } robot.coordinates
|
|
|
|
, test "bearing" <|
|
|
|
|
\() -> Expect.equal North robot.bearing
|
2016-06-19 21:46:13 +00:00
|
|
|
]
|
|
|
|
)
|
2016-08-17 11:14:17 +00:00
|
|
|
, describe "advance positive east"
|
2016-06-19 21:46:13 +00:00
|
|
|
(let
|
|
|
|
robot =
|
|
|
|
Robot East { x = 0, y = 0 }
|
|
|
|
|> advance
|
|
|
|
in
|
2016-08-17 11:14:17 +00:00
|
|
|
[ test "coordinates" <|
|
|
|
|
\() -> Expect.equal { x = 1, y = 0 } robot.coordinates
|
|
|
|
, test "bearing" <|
|
|
|
|
\() -> Expect.equal East robot.bearing
|
2016-06-19 21:46:13 +00:00
|
|
|
]
|
|
|
|
)
|
2016-08-17 11:14:17 +00:00
|
|
|
, describe "advance negative south"
|
2016-06-19 21:46:13 +00:00
|
|
|
(let
|
|
|
|
robot =
|
|
|
|
Robot South { x = 0, y = 0 }
|
|
|
|
|> advance
|
|
|
|
in
|
2016-08-17 11:14:17 +00:00
|
|
|
[ test "coordinates" <|
|
|
|
|
\() -> Expect.equal { x = 0, y = -1 } robot.coordinates
|
|
|
|
, test "bearing" <|
|
|
|
|
\() -> Expect.equal South robot.bearing
|
2016-06-19 21:46:13 +00:00
|
|
|
]
|
|
|
|
)
|
2016-08-17 11:14:17 +00:00
|
|
|
, describe "advance positive west"
|
2016-06-19 21:46:13 +00:00
|
|
|
(let
|
|
|
|
robot =
|
|
|
|
Robot West { x = 0, y = 0 }
|
|
|
|
|> advance
|
|
|
|
in
|
2016-08-17 11:14:17 +00:00
|
|
|
[ test "coordinates" <|
|
|
|
|
\() -> Expect.equal { x = -1, y = 0 } robot.coordinates
|
|
|
|
, test "bearing" <|
|
|
|
|
\() -> Expect.equal West robot.bearing
|
2016-06-19 21:46:13 +00:00
|
|
|
]
|
|
|
|
)
|
2016-08-17 11:14:17 +00:00
|
|
|
, describe "simulate prog 1"
|
2016-06-19 21:46:13 +00:00
|
|
|
(let
|
|
|
|
robot =
|
|
|
|
Robot North { x = 0, y = 0 }
|
|
|
|
|> simulate "LAAARALA"
|
|
|
|
in
|
2016-08-17 11:14:17 +00:00
|
|
|
[ test "coordinates" <|
|
|
|
|
\() -> Expect.equal { x = -4, y = 1 } robot.coordinates
|
|
|
|
, test "bearing" <|
|
|
|
|
\() -> Expect.equal West robot.bearing
|
2016-06-19 21:46:13 +00:00
|
|
|
]
|
|
|
|
)
|
2016-08-17 11:14:17 +00:00
|
|
|
, describe "simulate prog 2"
|
2016-06-19 21:46:13 +00:00
|
|
|
(let
|
|
|
|
robot =
|
|
|
|
Robot East { x = 2, y = -7 }
|
|
|
|
|> simulate "RRAAAAALA"
|
|
|
|
in
|
2016-08-17 11:14:17 +00:00
|
|
|
[ test "coordinates" <|
|
|
|
|
\() -> Expect.equal { x = -3, y = -8 } robot.coordinates
|
|
|
|
, test "bearing" <|
|
|
|
|
\() -> Expect.equal South robot.bearing
|
2016-06-19 21:46:13 +00:00
|
|
|
]
|
|
|
|
)
|
2016-08-17 11:14:17 +00:00
|
|
|
, describe "simulate prog 3"
|
2016-06-19 21:46:13 +00:00
|
|
|
(let
|
|
|
|
robot =
|
|
|
|
Robot South { x = 8, y = 4 }
|
|
|
|
|> simulate "LAAARRRALLLL"
|
|
|
|
in
|
2016-08-17 11:14:17 +00:00
|
|
|
[ test "coordinates" <|
|
|
|
|
\() -> Expect.equal { x = 11, y = 5 } robot.coordinates
|
|
|
|
, test "bearing" <|
|
|
|
|
\() -> Expect.equal North robot.bearing
|
2016-06-19 21:46:13 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
]
|
2016-03-28 10:23:01 +00:00
|
|
|
|
|
|
|
|
2016-08-17 11:14:17 +00:00
|
|
|
{-| Given a list of values and another list of expected values,
|
|
|
|
generate a list of Assert Equal assertions.
|
|
|
|
-}
|
|
|
|
assertionList : List a -> List a -> List Expect.Expectation
|
|
|
|
assertionList xs ys =
|
|
|
|
List.map2 Expect.equal xs ys
|
|
|
|
|
|
|
|
|
2016-12-17 19:28:59 +00:00
|
|
|
main : TestProgram
|
2016-05-13 02:26:52 +00:00
|
|
|
main =
|
2016-08-17 11:14:17 +00:00
|
|
|
run emit tests
|
|
|
|
|
|
|
|
|
|
|
|
port emit : ( String, Value ) -> Cmd msg
|