From 6adc8efb3c7be00d5cf8b81f68bdeaac213e9134 Mon Sep 17 00:00:00 2001 From: Colin Caine Date: Mon, 28 Sep 2020 18:56:07 +0100 Subject: [PATCH] robot-simulator: ensure mutating functions return robot (#259) Closes #224. --- exercises/robot-simulator/robot-simulator.ipynb | 2 +- exercises/robot-simulator/runtests.jl | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/exercises/robot-simulator/robot-simulator.ipynb b/exercises/robot-simulator/robot-simulator.ipynb index 68964a28..a3eda669 100644 --- a/exercises/robot-simulator/robot-simulator.ipynb +++ b/exercises/robot-simulator/robot-simulator.ipynb @@ -27,7 +27,7 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": ["using Test\n", "\n", "# include(\"robot-simulator.jl\")\n", "\n", "@testset \"constructor\" begin\n", " r = Robot((0, 0), NORTH)\n", " @test position(r) == Point(0, 0)\n", " @test heading(r) == NORTH\n", "\n", " r = Robot((-1, -1), SOUTH)\n", " @test position(r) == Point{Int}(-1, -1)\n", " @test heading(r) == SOUTH\n", "end\n", "\n", "@testset \"rotate +π/2\" begin\n", " r = Robot((0, 0), NORTH)\n", " turn_right!(r)\n", " @test position(r) == Point(0, 0)\n", " @test heading(r) == EAST\n", " turn_right!(r)\n", " @test heading(r) == SOUTH\n", " turn_right!(r)\n", " @test heading(r) == WEST\n", " turn_right!(r)\n", " @test heading(r) == NORTH\n", "end\n", "\n", "@testset \"rotate -π/2\" begin\n", " r = Robot((0, 0), NORTH)\n", " turn_left!(r)\n", " @test position(r) == Point(0, 0)\n", " @test heading(r) == WEST\n", " turn_left!(r)\n", " @test heading(r) == SOUTH\n", " turn_left!(r)\n", " @test heading(r) == EAST\n", " turn_left!(r)\n", " @test heading(r) == NORTH\n", "end\n", "\n", "@testset \"advance\" begin\n", " r = Robot((0, 0), NORTH)\n", " advance!(r)\n", " @test heading(r) == NORTH\n", " @test position(r) == Point(0, 1)\n", "\n", " @test position(advance!(Robot((0, 0), SOUTH))) == Point(0, -1)\n", " @test position(advance!(Robot((0, 0), EAST))) == Point(1, 0)\n", " @test position(advance!(Robot((0, 0), WEST))) == Point(-1, 0)\n", "end\n", "\n", "@testset \"instructions\" begin\n", " @testset \"move west and north\" begin\n", " r = Robot((0, 0), NORTH)\n", " move!(r, \"LAAARALA\")\n", " @test position(r) == Point(-4, 1)\n", " @test heading(r) == WEST\n", " end\n", "\n", " @testset \"move west and south\" begin\n", " r = Robot((2, -7), EAST)\n", " move!(r, \"RRAAAAALA\")\n", " @test position(r) == Point(-3, -8)\n", " @test heading(r) == SOUTH\n", " end\n", "\n", " @testset \"move east and north\" begin\n", " r = Robot((8, 4), SOUTH)\n", " move!(r, \"LAAARRRALLLL\")\n", " @test position(r) == Point(11, 5)\n", " @test heading(r) == NORTH\n", " end\n", "end"] + "source": ["using Test\n", "\n", "# include(\"robot-simulator.jl\")\n", "\n", "@testset \"constructor\" begin\n", " r = Robot((0, 0), NORTH)\n", " @test position(r) == Point(0, 0)\n", " @test heading(r) == NORTH\n", "\n", " r = Robot((-1, -1), SOUTH)\n", " @test position(r) == Point{Int}(-1, -1)\n", " @test heading(r) == SOUTH\n", "end\n", "\n", "@testset \"mutating functions should return robot\" begin\n", " r = Robot((0, 0), NORTH)\n", " @test r == turn_right!(r)\n", " @test r == turn_left!(r)\n", " @test r == advance!(r)\n", " @test r == move!(r, \"A\")\n", "end\n", "\n", "@testset \"rotate +π/2\" begin\n", " r = Robot((0, 0), NORTH)\n", " turn_right!(r)\n", " @test position(r) == Point(0, 0)\n", " @test heading(r) == EAST\n", " turn_right!(r)\n", " @test heading(r) == SOUTH\n", " turn_right!(r)\n", " @test heading(r) == WEST\n", " turn_right!(r)\n", " @test heading(r) == NORTH\n", "end\n", "\n", "@testset \"rotate -π/2\" begin\n", " r = Robot((0, 0), NORTH)\n", " turn_left!(r)\n", " @test position(r) == Point(0, 0)\n", " @test heading(r) == WEST\n", " turn_left!(r)\n", " @test heading(r) == SOUTH\n", " turn_left!(r)\n", " @test heading(r) == EAST\n", " turn_left!(r)\n", " @test heading(r) == NORTH\n", "end\n", "\n", "@testset \"advance\" begin\n", " r = Robot((0, 0), NORTH)\n", " advance!(r)\n", " @test heading(r) == NORTH\n", " @test position(r) == Point(0, 1)\n", "\n", " @test position(advance!(Robot((0, 0), SOUTH))) == Point(0, -1)\n", " @test position(advance!(Robot((0, 0), EAST))) == Point(1, 0)\n", " @test position(advance!(Robot((0, 0), WEST))) == Point(-1, 0)\n", "end\n", "\n", "@testset \"instructions\" begin\n", " @testset \"move west and north\" begin\n", " r = Robot((0, 0), NORTH)\n", " move!(r, \"LAAARALA\")\n", " @test position(r) == Point(-4, 1)\n", " @test heading(r) == WEST\n", " end\n", "\n", " @testset \"move west and south\" begin\n", " r = Robot((2, -7), EAST)\n", " move!(r, \"RRAAAAALA\")\n", " @test position(r) == Point(-3, -8)\n", " @test heading(r) == SOUTH\n", " end\n", "\n", " @testset \"move east and north\" begin\n", " r = Robot((8, 4), SOUTH)\n", " move!(r, \"LAAARRRALLLL\")\n", " @test position(r) == Point(11, 5)\n", " @test heading(r) == NORTH\n", " end\n", "end"] }, { "cell_type": "markdown", diff --git a/exercises/robot-simulator/runtests.jl b/exercises/robot-simulator/runtests.jl index 6f88ac40..4bf27677 100644 --- a/exercises/robot-simulator/runtests.jl +++ b/exercises/robot-simulator/runtests.jl @@ -12,6 +12,14 @@ include("robot-simulator.jl") @test heading(r) == SOUTH end +@testset "mutating functions should return robot" begin + r = Robot((0, 0), NORTH) + @test r == turn_right!(r) + @test r == turn_left!(r) + @test r == advance!(r) + @test r == move!(r, "A") +end + @testset "rotate +π/2" begin r = Robot((0, 0), NORTH) turn_right!(r)