All modules have an intro.
This commit is contained in:
12
lib/koans.ex
12
lib/koans.ex
@@ -83,15 +83,12 @@ defmodule Koans do
|
||||
|
||||
defmacro __before_compile__(env) do
|
||||
koans = koans(env)
|
||||
intro = extract_into(env)
|
||||
quote do
|
||||
def all_koans do
|
||||
unquote(koans)
|
||||
end
|
||||
|
||||
def intro do
|
||||
unquote(intro)
|
||||
end
|
||||
def intro, do: @intro
|
||||
end
|
||||
end
|
||||
|
||||
@@ -101,13 +98,6 @@ defmodule Koans do
|
||||
|> Enum.reverse
|
||||
end
|
||||
|
||||
defp extract_into(env) do
|
||||
env.module
|
||||
|> Module.get_attribute(:intro)
|
||||
|> default("")
|
||||
|> String.strip
|
||||
end
|
||||
|
||||
defp default(nil, n), do: n
|
||||
defp default(n, _), do: n
|
||||
end
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
defmodule Strings do
|
||||
use Koans
|
||||
|
||||
@intro "Strings"
|
||||
|
||||
koan "Strings are there to represent text" do
|
||||
assert "hello" == ___
|
||||
end
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
defmodule Atoms do
|
||||
use Koans
|
||||
|
||||
@intro "Atoms"
|
||||
|
||||
koan "Atoms are sort of like strings" do
|
||||
adam = :human
|
||||
assert adam == ___
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
defmodule Tuples do
|
||||
use Koans
|
||||
|
||||
@intro "Tuples"
|
||||
|
||||
koan "Tuples can contain different things" do
|
||||
assert {:a, 1, "hi"} == ___
|
||||
end
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
defmodule Lists do
|
||||
use Koans
|
||||
|
||||
@intro "Lists"
|
||||
|
||||
koan "We can see what is ahead" do
|
||||
assert List.first([1, 2, 3]) == ___
|
||||
end
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
defmodule KeywordLists do
|
||||
use Koans
|
||||
|
||||
@intro "KeywordLists"
|
||||
|
||||
koan "Like maps, keyword lists are key-value pairs" do
|
||||
kw_list = [foo: "bar"]
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
defmodule Maps do
|
||||
use Koans
|
||||
|
||||
@intro "Maps"
|
||||
|
||||
@person %{
|
||||
first_name: "Jon",
|
||||
last_name: "Snow",
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
defmodule Structs do
|
||||
use Koans
|
||||
|
||||
@intro "Structs"
|
||||
|
||||
defmodule Person do
|
||||
defstruct [:name, :age]
|
||||
end
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
defmodule PatternMatching do
|
||||
use Koans
|
||||
|
||||
@intro "PatternMatching"
|
||||
|
||||
koan "One matches one" do
|
||||
assert match?(1, ___)
|
||||
end
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
defmodule Functions do
|
||||
use Koans
|
||||
|
||||
@intro "Functions"
|
||||
|
||||
def greet(name) do
|
||||
"Hello, #{name}!"
|
||||
end
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
defmodule Enums do
|
||||
use Koans
|
||||
|
||||
@intro "Enums"
|
||||
|
||||
koan "Knowing how many elements are in a list is important for book-keeping" do
|
||||
assert Enum.count([1, 2, 3]) == ___
|
||||
end
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
defmodule Processes do
|
||||
use Koans
|
||||
|
||||
@intro "Processes"
|
||||
|
||||
koan "You are a process" do
|
||||
assert Process.alive?(self) == ___
|
||||
end
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
defmodule Tasks do
|
||||
use Koans
|
||||
|
||||
@intro "Tasks"
|
||||
|
||||
koan "Tasks can be used for asynchronous computations with results" do
|
||||
task = Task.async(fn -> 3 * 3 end)
|
||||
do_other_stuff()
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
defmodule Agents do
|
||||
use Koans
|
||||
|
||||
@intro "Agents"
|
||||
|
||||
koan "Agents maintain state, so you can ask them about it" do
|
||||
{:ok, pid} = Agent.start_link(fn -> "Hi there" end)
|
||||
assert Agent.get(pid, &(&1)) == ___
|
||||
|
||||
@@ -12,6 +12,6 @@ defmodule ExecuteTest do
|
||||
end
|
||||
|
||||
test "can access intro" do
|
||||
assert SampleKoan.intro == "There is something"
|
||||
assert SampleKoan.intro == "There is something\n"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
defmodule PassingKoan do
|
||||
use Koans
|
||||
|
||||
@intro "something"
|
||||
|
||||
koan "Hi there" do
|
||||
assert 1 == 1
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user