commit ae6b5a7f19e51c342a4596f6e1023fcce8cd5462 Author: Uku Taht Date: Tue Dec 15 17:53:27 2015 +0000 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..755b605 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/_build +/cover +/deps +erl_crash.dump +*.ez diff --git a/README.md b/README.md new file mode 100644 index 0000000..f75bda7 --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# ElixirKoans + +**TODO: Add description** + +## Installation + +If [available in Hex](https://hex.pm/docs/publish), the package can be installed as: + + 1. Add elixir_koans to your list of dependencies in `mix.exs`: + + def deps do + [{:elixir_koans, "~> 0.0.1"}] + end + + 2. Ensure elixir_koans is started before your application: + + def application do + [applications: [:elixir_koans]] + end diff --git a/lib/equalities.ex b/lib/equalities.ex new file mode 100644 index 0000000..f636f25 --- /dev/null +++ b/lib/equalities.ex @@ -0,0 +1,23 @@ +defmodule Equalities do + use Koans + + koan "We shall contemplate truth by testing reality, via equality" do + assert true == true + end + + koan "To understand reality, we must compare our expectations against reality" do + assert 2 == 1 + 1 + end + + koan "Some things may appear different, but be the same" do + assert 1 == 2 / 2 + end + + koan "Something is not equal to nothing" do + assert !(1 == nil) + end + + koan "When things cannot be equal, they must be different" do + refute :something == 4 + end +end diff --git a/lib/koans.ex b/lib/koans.ex new file mode 100644 index 0000000..98dcee0 --- /dev/null +++ b/lib/koans.ex @@ -0,0 +1,22 @@ +defmodule Koans do + defmacro koan(name, body) do + compiled_name = :"koan: #{name}" + quote do + def unquote(compiled_name)() do + try do + unquote(body) + :ok + rescue + e in ExUnit.AssertionError -> e + end + end + end + end + + defmacro __using__(_) do + quote do + import Koans + import ExUnit.Assertions + end + end +end diff --git a/lib/lists.ex b/lib/lists.ex new file mode 100644 index 0000000..75a63b0 --- /dev/null +++ b/lib/lists.ex @@ -0,0 +1,7 @@ +defmodule Lists do + use Koans + + koan "We shall contemplate truth by testing reality, via equality" do + assert List.first([1, 2, 3]) == 2 + end +end diff --git a/lib/meditate.ex b/lib/meditate.ex new file mode 100644 index 0000000..469af27 --- /dev/null +++ b/lib/meditate.ex @@ -0,0 +1,71 @@ +defmodule Mix.Tasks.Meditate do + use Mix.Task + alias IO.ANSI + + @modules [ + Equalities, + Lists + ] + + def run(_) do + IO.puts("") + Enum.take_while(@modules, fn(mod) -> + run_module(mod) == :passed + end) + end + + def run_module(module) do + IO.puts("Considering #{display_module(module)}...") + + functions = module.__info__(:functions) + + koans = Enum.map(functions, fn({name, _arity}) -> name end) + |> Enum.filter(&koan?/1) + + passed = Enum.take_while(koans, fn(name) -> + run_koan(module, name) == :passed + end) + + + if Enum.count(koans) == Enum.count(passed) do + :passed + else + :failed + end + end + + def run_koan(module, name) do + case apply(module, name, []) do + :ok -> :passed + error -> + show_failure(error, module, name) + :failed + end + end + + def show_failure(%{expr: expr}, module, name) do + IO.puts("") + IO.puts("Now meditate upon #{display_module(module)}") + IO.puts("---------------------------------------") + IO.puts("Assertion failed!") + IO.puts(display_koan(name)) + IO.puts(format_red(Macro.to_string(expr))) + end + + def format_red(str) do + Enum.join([ANSI.red, str, ANSI.reset], "") + end + + defp koan?(fun_name) do + String.starts_with?(to_string(fun_name), "koan: ") + end + + defp display_module(module) do + Module.split(module) |> List.last + end + + defp display_koan(name) do + String.replace(to_string(name), "koan: ", "") + end +end + diff --git a/lib/watcher.ex b/lib/watcher.ex new file mode 100644 index 0000000..e69de29 diff --git a/mix.exs b/mix.exs new file mode 100644 index 0000000..af2f16e --- /dev/null +++ b/mix.exs @@ -0,0 +1,18 @@ +defmodule Koans.Mixfile do + use Mix.Project + + def project do + [app: :elixir_koans, + version: "0.0.1", + elixir: "~> 1.1", + deps: deps] + end + + def application do + [applications: [:logger]] + end + + defp deps do + [{:exfswatch, "~> 0.1.0"}] + end +end diff --git a/mix.lock b/mix.lock new file mode 100644 index 0000000..44d3806 --- /dev/null +++ b/mix.lock @@ -0,0 +1,2 @@ +%{"exfswatch": {:hex, :exfswatch, "0.1.0"}, + "fs": {:hex, :fs, "0.9.2"}} diff --git a/test/test_helper.exs b/test/test_helper.exs new file mode 100644 index 0000000..869559e --- /dev/null +++ b/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start()