Skip to content

Commit

Permalink
Add FizzBadd implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sambostock committed Apr 10, 2017
1 parent 9dc6617 commit 1228c4c
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 0 deletions.
52 changes: 52 additions & 0 deletions lib/fizz_badd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,56 @@ defmodule FizzBadd do
@moduledoc """
Documentation for FizzBadd.
"""

alias __MODULE__.ShittyFizzBuzz, as: ShittyFizzBuzz
alias __MODULE__.StatementWrapper, as: StatementWrapper

def generate(n) when not is_integer(n) do
raise ArgumentError, "expects Integer, not `#{n}`"
end
def generate(n) when n < 1 do
raise ArgumentError, "can't generate negative lines (#{n})"
end
def generate(n) do """
defmodule FizzBuzz do
#{moduledoc()}
#{integer_guard()}
#{positive_guard()}
#{in_range_guard(n)}
def fizz_buzz(n) do
#{statements(n)}
end
end
"""
end

defp moduledoc, do: """
@moduledoc \"""
A Fizz Buzz implementation.
\"""
"""

defp integer_guard, do: """
def fizz_buzz(n) when not is_integer(n) do
raise ArgumentError, "expects Integer, not `\#{n}`"
end
"""

defp positive_guard, do: """
def fizz_buzz(n) when n < 1 do
raise ArgumentError, "can't Fizz Buzz negative numbers (\#{n})"
end
"""

defp in_range_guard(n), do: """
def fizz_buzz(n) when n > #{n} do
raise ArgumentError, "Fizz Buzz not implemented beyond #{n} (\#{n})"
end
"""

def statements(n) do
n
|> ShittyFizzBuzz.statements
|> StatementWrapper.wrap(indent: " ")
end
end
99 changes: 99 additions & 0 deletions test/fizz_badd_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,103 @@
defmodule FizzBaddTest do
use ExUnit.Case, async: true
doctest FizzBadd

test "won't generate empty implementations" do
assert_raise ArgumentError, fn ->
FizzBadd.generate(0)
end
end

test "won't accept arguments out of range" do
assert_raise ArgumentError, fn ->
FizzBadd.generate(-1)
end
end

test "generates the correct implementation given 1" do
assert FizzBadd.generate(1) == """
defmodule FizzBuzz do
@moduledoc \"""
A Fizz Buzz implementation.
\"""
def fizz_buzz(n) when not is_integer(n) do
raise ArgumentError, "expects Integer, not `\#{n}`"
end
def fizz_buzz(n) when n < 1 do
raise ArgumentError, "can't Fizz Buzz negative numbers (\#{n})"
end
def fizz_buzz(n) when n > 1 do
raise ArgumentError, "Fizz Buzz not implemented beyond 1 (\#{n})"
end
def fizz_buzz(n) do
if n == 1, do: IO.puts "1"
end
end
"""
end

test "generates the correct implementation given 2" do
assert FizzBadd.generate(2) === """
defmodule FizzBuzz do
@moduledoc \"""
A Fizz Buzz implementation.
\"""
def fizz_buzz(n) when not is_integer(n) do
raise ArgumentError, "expects Integer, not `\#{n}`"
end
def fizz_buzz(n) when n < 1 do
raise ArgumentError, "can't Fizz Buzz negative numbers (\#{n})"
end
def fizz_buzz(n) when n > 2 do
raise ArgumentError, "Fizz Buzz not implemented beyond 2 (\#{n})"
end
def fizz_buzz(n) do
if n >= 1, do: IO.write "1"; if n > 1, do: IO.write ", "; if n == 2, do: IO.puts "2"
end
end
"""
end

test "generates the correct implementation given 15" do
assert FizzBadd.generate(15) === """
defmodule FizzBuzz do
@moduledoc \"""
A Fizz Buzz implementation.
\"""
def fizz_buzz(n) when not is_integer(n) do
raise ArgumentError, "expects Integer, not `\#{n}`"
end
def fizz_buzz(n) when n < 1 do
raise ArgumentError, "can't Fizz Buzz negative numbers (\#{n})"
end
def fizz_buzz(n) when n > 15 do
raise ArgumentError, "Fizz Buzz not implemented beyond 15 (\#{n})"
end
def fizz_buzz(n) do
if n >= 1, do: IO.write "1"; if n > 1, do: IO.write ", "; if n >= 2, do: IO.write "2"; if n > 2, do: IO.write ", "
if n >= 3, do: IO.write "Fizz"; if n > 3, do: IO.write ", "; if n >= 4, do: IO.write "4"
if n > 4, do: IO.write ", "; if n >= 5, do: IO.write "Buzz"; if n > 5, do: IO.write ", "
if n >= 6, do: IO.write "Fizz"; if n > 6, do: IO.write ", "; if n >= 7, do: IO.write "7"
if n > 7, do: IO.write ", "; if n >= 8, do: IO.write "8"; if n > 8, do: IO.write ", "
if n >= 9, do: IO.write "Fizz"; if n > 9, do: IO.write ", "; if n >= 10, do: IO.write "Buzz"
if n > 10, do: IO.write ", "; if n >= 11, do: IO.write "11"; if n > 11, do: IO.write ", "
if n >= 12, do: IO.write "Fizz"; if n > 12, do: IO.write ", "; if n >= 13, do: IO.write "13"
if n > 13, do: IO.write ", "; if n >= 14, do: IO.write "14"; if n > 14, do: IO.write ", "
if n == 15, do: IO.puts "Fizz Buzz"
end
end
"""
end
end

0 comments on commit 1228c4c

Please sign in to comment.