From 87d30051eca9490d32708d7b4dfb01659516cd2a Mon Sep 17 00:00:00 2001 From: Simone Vittori Date: Tue, 20 Jun 2017 10:50:30 +0100 Subject: [PATCH] Improvements to CLI - Add --size=integer argument - Improve test coverage - Refactoring --- lib/life/cli.ex | 25 +++++------ test/life/cli_test.exs | 99 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 92 insertions(+), 32 deletions(-) diff --git a/lib/life/cli.ex b/lib/life/cli.ex index 89ef3f9..9a625bb 100644 --- a/lib/life/cli.ex +++ b/lib/life/cli.ex @@ -2,37 +2,36 @@ defmodule Life.CLI do @grid_size 10 @sleep_interval_ms 300 - + @glider [{1, 0}, {2, 1}, {0, 2}, {1, 2}, {2, 2}] def main([]) do - "no life" + loop(MapSet.new([]), @grid_size) end def main(["glider"]) do - [{3, 0}, {4, 1}, {2, 2}, {3, 2}, {4, 2}] - |> MapSet.new() - |> loop() + loop(MapSet.new(@glider), @grid_size) + end + def main(["--size=" <> grid_size]) do + loop(MapSet.new([]), String.to_integer(grid_size)) end - def loop(alive_cells, interval \\ @sleep_interval_ms) do + defp loop(alive_cells, grid_size, interval \\ @sleep_interval_ms) do alive_cells - |> calc_grid() + |> calc_grid(grid_size) |> Table.print() :timer.sleep(interval) alive_cells |> Life.tick() - |> loop() + |> loop(grid_size) end - - def calc_grid(alive_cells, size \\ @grid_size) do + # returns a 2d list. true for alive cells, false for dead + defp calc_grid(alive_cells, size) do for i <- 0..(size - 1) do for j <- 0..(size - 1) do - a = for {^j, ^i} <- alive_cells do - true - end + a = for {^j, ^i} <- alive_cells, do: true case a do [] -> false [_] -> true diff --git a/test/life/cli_test.exs b/test/life/cli_test.exs index c952874..9417257 100644 --- a/test/life/cli_test.exs +++ b/test/life/cli_test.exs @@ -2,8 +2,33 @@ defmodule CLITest do use ExUnit.Case import ExUnit.CaptureIO - # credits to this website for the ASCII table: http://www.tablesgenerator.com/text_tables - @empty_grid """ + # Credits for the ASCII tables: http://www.tablesgenerator.com/text_tables + + @empty_grid_10x10 """ + ╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗ + ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ + ╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣ + ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ + ╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣ + ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ + ╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣ + ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ + ╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣ + ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ + ╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣ + ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ + ╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣ + ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ + ╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣ + ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ + ╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣ + ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ + ╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣ + ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ + ╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝ + """ + + @empty_grid_3x3 """ ╔═══╦═══╦═══╗ ║ ║ ║ ║ ╠═══╬═══╬═══╣ @@ -13,31 +38,67 @@ defmodule CLITest do ╚═══╩═══╩═══╝ """ - @grid_with_glider """ - ╔═══╦═══╦═══╗ - ║ ║ █ ║ ║ - ╠═══╬═══╬═══╣ - ║ ║ ║ █ ║ - ╠═══╬═══╬═══╣ - ║ █ ║ █ ║ █ ║ - ╚═══╩═══╩═══╝ + @grid_10x10_with_glider """ + ╔═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╦═══╗ + ║ ║ █ ║ ║ ║ ║ ║ ║ ║ ║ ║ + ╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣ + ║ ║ ║ █ ║ ║ ║ ║ ║ ║ ║ ║ + ╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣ + ║ █ ║ █ ║ █ ║ ║ ║ ║ ║ ║ ║ ║ + ╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣ + ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ + ╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣ + ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ + ╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣ + ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ + ╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣ + ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ + ╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣ + ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ + ╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣ + ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ + ╠═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╬═══╣ + ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ ║ + ╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝ """ - @tag :skip - test "prints an empty 3x3 grid" do - assert @empty_grid == + test "with no args, prints an empty 10x10 grid" do + assert @empty_grid_10x10 == capture_io(fn -> - assert :ok == Life.CLI.main([]) + {:ok, pid} = Task.Supervisor.start_link() + task = Task.Supervisor.async(pid, fn -> + args = [] + Task.start_link(Life.CLI.main(args)) + end) + :timer.sleep(1) + Task.shutdown(task) end) end - @tag :skip - test "prints a 3x3 grid with a glider" do - grid = [{1, 0}, {2, 1}, {0, 2}, {1, 2}, {2, 2}] + test "with --size=3 arg, prints an empty 3x3 grid" do + assert @empty_grid_3x3 == + capture_io(fn -> + {:ok, pid} = Task.Supervisor.start_link() + task = Task.Supervisor.async(pid, fn -> + args = ["--size=3"] + Task.start_link(Life.CLI.main(args)) + end) + :timer.sleep(1) + Task.shutdown(task) + end) + end - assert @grid_with_glider == + test "with glider arg, prints a 10x10 with a glider in the top-left corner" do + assert @grid_10x10_with_glider == capture_io(fn -> - assert :ok == Life.CLI.print(grid) + {:ok, pid} = Task.Supervisor.start_link() + task = Task.Supervisor.async(pid, fn -> + args = ["glider"] + Task.start_link(Life.CLI.main(args)) + end) + :timer.sleep(1) + Task.shutdown(task) end) end + end