Initial commit

This commit is contained in:
Uku Taht
2015-12-15 17:53:27 +00:00
commit ae6b5a7f19
10 changed files with 168 additions and 0 deletions

23
lib/equalities.ex Normal file
View File

@@ -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

22
lib/koans.ex Normal file
View File

@@ -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

7
lib/lists.ex Normal file
View File

@@ -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

71
lib/meditate.ex Normal file
View File

@@ -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

0
lib/watcher.ex Normal file
View File