Permalink
Browse files

Replace some tests with doctests

  • Loading branch information...
ck3g committed Nov 24, 2017
1 parent 0e06388 commit 92b4e23c881f9f8341882a8378f5e2b03faeb17a
Showing with 55 additions and 24 deletions.
  1. +55 −3 lib/toy_robot.ex
  2. +0 −21 test/toy_robot_test.exs
View
@@ -2,13 +2,19 @@ defmodule ToyRobot do
@table_top_x 4
@table_top_y 4
@doc """
Places the robot to the default position
Examples:
iex> ToyRobot.place
{:ok, %ToyRobot.Position{facing: :north, x: 0, y: 0}}
"""
def place do
{:ok, %ToyRobot.Position{}}
end
def place(x, y, _facing)
when x < 0 or y < 0 or x > @table_top_y or y > @table_top_y
do
def place(x, y, _facing) when x < 0 or y < 0 or x > @table_top_y or y > @table_top_y do
{:failure, "Invalid position"}
end
@@ -18,39 +24,85 @@ defmodule ToyRobot do
{:failure, "Invalid facing direction"}
end
@doc """
Places the robot to the provided position,
but prevents it to be placed outside of the table and facing invalid direction.
Examples:
iex> ToyRobot.place(1, 2, :south)
{:ok, %ToyRobot.Position{facing: :south, x: 1, y: 2}}
iex> ToyRobot.place(-1, -1, :north)
{:failure, "Invalid position"}
iex> ToyRobot.place(0, 0, :north_east)
{:failure, "Invalid facing direction"}
"""
def place(x, y, facing) do
{:ok, %ToyRobot.Position{x: x, y: y, facing: facing}}
end
@doc """
Provides the report of the robot's current position
Examples:
iex> {:ok, robot} = ToyRobot.place(2, 3, :west)
iex> ToyRobot.report(robot)
{2, 3, :west}
"""
def report(%ToyRobot.Position{x: x, y: y, facing: facing} = _robot) do
{x, y, facing}
end
@directions_to_the_right %{north: :east, east: :south, south: :west, west: :north}
@doc """
Rotates the robot to the right
"""
def right(%ToyRobot.Position{facing: facing} = robot) do
%ToyRobot.Position{robot | facing: @directions_to_the_right[facing]}
end
@directions_to_the_left Enum.map(@directions_to_the_right, fn {from, to} -> {to, from} end)
@doc """
Rotates the robot to the left
"""
def left(%ToyRobot.Position{facing: facing} = robot) do
%ToyRobot.Position{robot | facing: @directions_to_the_left[facing]}
end
@doc """
Moves the robot to the north, but prevents it to fall
"""
def move(%ToyRobot.Position{x: _, y: y, facing: :north} = robot) when y < @table_top_y do
%ToyRobot.Position{robot | y: y + 1}
end
@doc """
Moves the robot to the east, but prevents it to fall
"""
def move(%ToyRobot.Position{x: x, y: _, facing: :east} = robot) when x < @table_top_x do
%ToyRobot.Position{robot | x: x + 1}
end
@doc """
Moves the robot to the south, but prevents it to fall
"""
def move(%ToyRobot.Position{x: _, y: y, facing: :south} = robot) when y > 0 do
%ToyRobot.Position{robot | y: y - 1}
end
@doc """
Moves the robot to the west, but prevents it to fall
"""
def move(%ToyRobot.Position{x: x, y: _, facing: :west} = robot) when x > 0 do
%ToyRobot.Position{robot | x: x - 1}
end
@doc """
Does not change the position of the robot.
This function used as fallback if the robot cannot move outside the table
"""
def move(robot), do: robot
end
View
@@ -2,27 +2,6 @@ defmodule ToyRobotTest do
use ExUnit.Case
doctest ToyRobot
test "places the Toy Robot on the table in the default position" do
assert ToyRobot.place == {:ok, %ToyRobot.Position{x: 0, y: 0, facing: :north}}
end
test "places the Toy Robot on the table in the specified position" do
assert ToyRobot.place(1, 2, :south) == {:ok, %ToyRobot.Position{x: 1, y: 2, facing: :south}}
end
test "does not place the robot outside of the table" do
assert ToyRobot.place(-1, -1, :north) == {:failure, "Invalid position"}
end
test "does not place the robot with invalid facing direction" do
assert ToyRobot.place(0, 0, :north_west) == {:failure, "Invalid facing direction"}
end
test "provides the report of the robot's position" do
{:ok, robot} = ToyRobot.place(2, 3, :west)
assert ToyRobot.report(robot) == {2, 3, :west}
end
test "rotates the robot to the right" do
{:ok, robot} = ToyRobot.place(0, 0, :north)
position = robot |> ToyRobot.right |> ToyRobot.report

0 comments on commit 92b4e23

Please sign in to comment.