All modules have an intro.

This commit is contained in:
Felipe Sere
2016-05-11 21:44:18 +01:00
parent 94ab9826ae
commit f5477f0fb0
16 changed files with 30 additions and 12 deletions

View File

@@ -83,15 +83,12 @@ defmodule Koans do
defmacro __before_compile__(env) do defmacro __before_compile__(env) do
koans = koans(env) koans = koans(env)
intro = extract_into(env)
quote do quote do
def all_koans do def all_koans do
unquote(koans) unquote(koans)
end end
def intro do def intro, do: @intro
unquote(intro)
end
end end
end end
@@ -101,13 +98,6 @@ defmodule Koans do
|> Enum.reverse |> Enum.reverse
end end
defp extract_into(env) do
env.module
|> Module.get_attribute(:intro)
|> default("")
|> String.strip
end
defp default(nil, n), do: n defp default(nil, n), do: n
defp default(n, _), do: n defp default(n, _), do: n
end end

View File

@@ -1,6 +1,8 @@
defmodule Strings do defmodule Strings do
use Koans use Koans
@intro "Strings"
koan "Strings are there to represent text" do koan "Strings are there to represent text" do
assert "hello" == ___ assert "hello" == ___
end end

View File

@@ -1,6 +1,8 @@
defmodule Atoms do defmodule Atoms do
use Koans use Koans
@intro "Atoms"
koan "Atoms are sort of like strings" do koan "Atoms are sort of like strings" do
adam = :human adam = :human
assert adam == ___ assert adam == ___

View File

@@ -1,6 +1,8 @@
defmodule Tuples do defmodule Tuples do
use Koans use Koans
@intro "Tuples"
koan "Tuples can contain different things" do koan "Tuples can contain different things" do
assert {:a, 1, "hi"} == ___ assert {:a, 1, "hi"} == ___
end end

View File

@@ -1,6 +1,8 @@
defmodule Lists do defmodule Lists do
use Koans use Koans
@intro "Lists"
koan "We can see what is ahead" do koan "We can see what is ahead" do
assert List.first([1, 2, 3]) == ___ assert List.first([1, 2, 3]) == ___
end end

View File

@@ -1,6 +1,8 @@
defmodule KeywordLists do defmodule KeywordLists do
use Koans use Koans
@intro "KeywordLists"
koan "Like maps, keyword lists are key-value pairs" do koan "Like maps, keyword lists are key-value pairs" do
kw_list = [foo: "bar"] kw_list = [foo: "bar"]

View File

@@ -1,6 +1,8 @@
defmodule Maps do defmodule Maps do
use Koans use Koans
@intro "Maps"
@person %{ @person %{
first_name: "Jon", first_name: "Jon",
last_name: "Snow", last_name: "Snow",

View File

@@ -1,6 +1,8 @@
defmodule Structs do defmodule Structs do
use Koans use Koans
@intro "Structs"
defmodule Person do defmodule Person do
defstruct [:name, :age] defstruct [:name, :age]
end end

View File

@@ -1,6 +1,8 @@
defmodule PatternMatching do defmodule PatternMatching do
use Koans use Koans
@intro "PatternMatching"
koan "One matches one" do koan "One matches one" do
assert match?(1, ___) assert match?(1, ___)
end end

View File

@@ -1,6 +1,8 @@
defmodule Functions do defmodule Functions do
use Koans use Koans
@intro "Functions"
def greet(name) do def greet(name) do
"Hello, #{name}!" "Hello, #{name}!"
end end

View File

@@ -1,6 +1,8 @@
defmodule Enums do defmodule Enums do
use Koans use Koans
@intro "Enums"
koan "Knowing how many elements are in a list is important for book-keeping" do koan "Knowing how many elements are in a list is important for book-keeping" do
assert Enum.count([1, 2, 3]) == ___ assert Enum.count([1, 2, 3]) == ___
end end

View File

@@ -1,6 +1,8 @@
defmodule Processes do defmodule Processes do
use Koans use Koans
@intro "Processes"
koan "You are a process" do koan "You are a process" do
assert Process.alive?(self) == ___ assert Process.alive?(self) == ___
end end

View File

@@ -1,6 +1,8 @@
defmodule Tasks do defmodule Tasks do
use Koans use Koans
@intro "Tasks"
koan "Tasks can be used for asynchronous computations with results" do koan "Tasks can be used for asynchronous computations with results" do
task = Task.async(fn -> 3 * 3 end) task = Task.async(fn -> 3 * 3 end)
do_other_stuff() do_other_stuff()

View File

@@ -1,6 +1,8 @@
defmodule Agents do defmodule Agents do
use Koans use Koans
@intro "Agents"
koan "Agents maintain state, so you can ask them about it" do koan "Agents maintain state, so you can ask them about it" do
{:ok, pid} = Agent.start_link(fn -> "Hi there" end) {:ok, pid} = Agent.start_link(fn -> "Hi there" end)
assert Agent.get(pid, &(&1)) == ___ assert Agent.get(pid, &(&1)) == ___

View File

@@ -12,6 +12,6 @@ defmodule ExecuteTest do
end end
test "can access intro" do test "can access intro" do
assert SampleKoan.intro == "There is something" assert SampleKoan.intro == "There is something\n"
end end
end end

View File

@@ -1,6 +1,8 @@
defmodule PassingKoan do defmodule PassingKoan do
use Koans use Koans
@intro "something"
koan "Hi there" do koan "Hi there" do
assert 1 == 1 assert 1 == 1
end end