Initial commit
This commit is contained in:
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
/_build
|
||||
/cover
|
||||
/deps
|
||||
erl_crash.dump
|
||||
*.ez
|
19
README.md
Normal file
19
README.md
Normal file
@@ -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
|
23
lib/equalities.ex
Normal file
23
lib/equalities.ex
Normal 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
22
lib/koans.ex
Normal 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
7
lib/lists.ex
Normal 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
71
lib/meditate.ex
Normal 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
0
lib/watcher.ex
Normal file
18
mix.exs
Normal file
18
mix.exs
Normal file
@@ -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
|
2
mix.lock
Normal file
2
mix.lock
Normal file
@@ -0,0 +1,2 @@
|
||||
%{"exfswatch": {:hex, :exfswatch, "0.1.0"},
|
||||
"fs": {:hex, :fs, "0.9.2"}}
|
1
test/test_helper.exs
Normal file
1
test/test_helper.exs
Normal file
@@ -0,0 +1 @@
|
||||
ExUnit.start()
|
Reference in New Issue
Block a user