Skip to content

Commit

Permalink
Add another test case
Browse files Browse the repository at this point in the history
  • Loading branch information
simonewebdesign committed Jun 20, 2017
1 parent d6b2e61 commit 19f567c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/life/cli.ex
Expand Up @@ -7,14 +7,16 @@ defmodule Life.CLI do
def main([]) do
loop(MapSet.new([]), @grid_size)
end
def main(["glider", "--size=" <> grid_size]) do
loop(MapSet.new(@glider), String.to_integer(grid_size))
end
def main(["glider"]) do
loop(MapSet.new(@glider), @grid_size)
end
def main(["--size=" <> grid_size]) do
loop(MapSet.new([]), String.to_integer(grid_size))
end


defp loop(alive_cells, grid_size, interval \\ @sleep_interval_ms) do
alive_cells
|> calc_grid(grid_size)
Expand Down
23 changes: 23 additions & 0 deletions test/life/cli_test.exs
Expand Up @@ -62,6 +62,16 @@ defmodule CLITest do
╚═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╩═══╝
"""

@grid_3x3_with_glider """
╔═══╦═══╦═══╗
║ ║ █ ║ ║
╠═══╬═══╬═══╣
║ ║ ║ █ ║
╠═══╬═══╬═══╣
║ █ ║ █ ║ █ ║
╚═══╩═══╩═══╝
"""

test "with no args, prints an empty 10x10 grid" do
assert @empty_grid_10x10 ==
capture_io(fn ->
Expand Down Expand Up @@ -101,4 +111,17 @@ defmodule CLITest do
end)
end

test "with glider and --size=3 args, prints a 3x3 grid with a glider" do
assert @grid_3x3_with_glider ==
capture_io(fn ->
{:ok, pid} = Task.Supervisor.start_link()
task = Task.Supervisor.async(pid, fn ->
args = ["glider", "--size=3"]
Task.start_link(Life.CLI.main(args))
end)
:timer.sleep(1)
Task.shutdown(task)
end)
end

end

0 comments on commit 19f567c

Please sign in to comment.