Skip to content

Commit

Permalink
Improvements to CLI
Browse files Browse the repository at this point in the history
- Add --size=integer argument
- Improve test coverage
- Refactoring
  • Loading branch information
simonewebdesign committed Jun 20, 2017
1 parent 98f3b96 commit 87d3005
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 32 deletions.
25 changes: 12 additions & 13 deletions lib/life/cli.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
99 changes: 80 additions & 19 deletions test/life/cli_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
╔═══╦═══╦═══╗
β•‘ β•‘ β•‘ β•‘
╠═══╬═══╬═══╣
Expand All @@ -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

0 comments on commit 87d3005

Please sign in to comment.