From 6752ec758c8e8c4c1b22fae43a5da672c134fe8c Mon Sep 17 00:00:00 2001 From: Felipe Sere Date: Sun, 10 Apr 2016 19:14:35 +0100 Subject: [PATCH] Improve and enforce naming of koans at compile time --- lib/koans.ex | 7 +++++++ lib/koans/02_arithmetic.ex | 23 +++++++++++------------ lib/koans/10_enums.ex | 4 ++-- lib/koans/12_task.ex | 2 +- test/support/passing_koan.ex | 2 +- test/support/sample_koan.ex | 2 +- 6 files changed, 23 insertions(+), 17 deletions(-) diff --git a/lib/koans.ex b/lib/koans.ex index e4c26c6..aba4c1a 100644 --- a/lib/koans.ex +++ b/lib/koans.ex @@ -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 diff --git a/lib/koans/02_arithmetic.ex b/lib/koans/02_arithmetic.ex index e2f85c8..4ea629c 100644 --- a/lib/koans/02_arithmetic.ex +++ b/lib/koans/02_arithmetic.ex @@ -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 diff --git a/lib/koans/10_enums.ex b/lib/koans/10_enums.ex index fafb906..c345e29 100644 --- a/lib/koans/10_enums.ex +++ b/lib/koans/10_enums.ex @@ -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 diff --git a/lib/koans/12_task.ex b/lib/koans/12_task.ex index 4c6873d..4d6077f 100644 --- a/lib/koans/12_task.ex +++ b/lib/koans/12_task.ex @@ -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, :__} diff --git a/test/support/passing_koan.ex b/test/support/passing_koan.ex index 24120d1..e40451c 100644 --- a/test/support/passing_koan.ex +++ b/test/support/passing_koan.ex @@ -1,7 +1,7 @@ defmodule PassingKoan do use Koans - koan "hi there" do + koan "Hi there" do assert 1 == 1 end end diff --git a/test/support/sample_koan.ex b/test/support/sample_koan.ex index d48e340..d84cda5 100644 --- a/test/support/sample_koan.ex +++ b/test/support/sample_koan.ex @@ -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