Improve and enforce naming of koans at compile time

This commit is contained in:
Felipe Sere
2016-04-10 19:14:35 +01:00
parent 98ab1a0e26
commit 6752ec758c
6 changed files with 23 additions and 17 deletions

View File

@@ -1,5 +1,12 @@
defmodule Koans do
defp valid_name(name) do
Regex.match?(~r/([A-Z]|\.\.\.).+/, name)
end
defmacro koan(name, body) do
if not valid_name(name) do
raise "Name does not start with a capital ltter: #{name}"
end
compiled_name = String.to_atom(name)
number_of_args = Blanks.count(body)
quote do

View File

@@ -1,52 +1,51 @@
defmodule Arithmetic do
use Koans
koan "it can add numbers" do
koan "It can add numbers" do
assert 3 + :__ == 7
end
koan "it can substract numbers" do
koan "It can substract numbers" do
assert 7 - 4 == :__
end
koan "it can multiply" do
koan "It can multiply" do
assert 3 * 4 == :__
end
koan "floats and integers can be equal..." do
koan "Floats and integers can be equal..." do
assert 3.0 == :__
end
koan "..unless they are not precisely the same" do
koan "...unless they are not precisely the same" do
assert 3.2 != :__
end
koan "it does floating point division" do
koan "It does floating point division" do
assert 5 / 2 == :__
end
koan "it does integer division" do
koan "It does integer division" do
numerator = 5
denominator = 2
assert div(numerator, denominator) == :__
end
koan "it calculates the remainder of a division" do
koan "It calculates the remainder of a division" do
numerator = 5
denominator = 2
assert rem(numerator, denominator) == :__
end
koan "it finds the maximum in a list" do
koan "It finds the maximum in a list" do
assert Enum.max([1,2,3,4]) == :__
end
koan "it finds the minimum in a list" do
koan "It finds the minimum in a list" do
assert Enum.min([1,2,3,4]) == :__
end
koan "it can compare numbers" do
koan "It can compare numbers" do
assert 5 > :__
end
end

View File

@@ -16,7 +16,7 @@ defmodule Enums do
end
def even?(n), do: rem(n, 2) == 0
koan "sometimes you you just want to know if there are any elements fullfilling a condition" do
koan "Sometimes you you just want to know if there are any elements fullfilling a condition" do
assert Enum.any?([1,2,3], &even?/1) == :__
assert Enum.any?([1,3,5], &even?/1) == :__
end
@@ -28,7 +28,7 @@ defmodule Enums do
end
def multiply_by_ten(n), do: 10 * n
koan "map converts each element of a list by running some function with it" do
koan "Map converts each element of a list by running some function with it" do
assert Enum.map([1,2,3], &multiply_by_ten/1) == :__
end

View File

@@ -28,7 +28,7 @@ defmodule Tasks do
assert Task.shutdown(handle) == :__
end
koan "shutdown will give you an answer if it has it" do
koan "Shutdown will give you an answer if it has it" do
handle = Task.async(fn -> 3 * 3 end)
:timer.sleep(10)
assert Task.shutdown(handle) == {:ok, :__}

View File

@@ -1,7 +1,7 @@
defmodule PassingKoan do
use Koans
koan "hi there" do
koan "Hi there" do
assert 1 == 1
end
end

View File

@@ -1,7 +1,7 @@
defmodule SampleKoan do
use Koans
koan "thinking more than once" do
koan "Thinking more than once" do
assert 3 == :__
assert 4 == :__
end