Created
January 5, 2015 11:28
-
-
Save radarek/06e4f5ee613a58c91a32 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule ExUnit.Lets do | |
defmacro __using__([]) do | |
quote do | |
import ExUnit.Lets | |
end | |
end | |
defmacro let(name, expr) do | |
if match?([do: _], expr) do | |
expr = expr[:do] | |
end | |
quote do | |
def unquote(name)() do | |
dict = Process.get(:_ex_unit_lets, HashDict.new) | |
unless HashDict.has_key?(dict, unquote(name)) do | |
dict = HashDict.put(dict, unquote(name), unquote(expr)) | |
end | |
Process.put(:_ex_unit_lets, dict) | |
HashDict.get(dict, unquote(name)) | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Code.require_file "../test_helper.exs", __DIR__ | |
defmodule ExUnit.LetsTest do | |
import ExUnit.CaptureIO | |
use ExUnit.Case, async: true | |
use ExUnit.Lets | |
let :foo, "foo" | |
let :bar do | |
IO.puts("let :bar evaluated") | |
"bar" | |
end | |
let :baz, do: "baz" | |
test "getting let value" do | |
assert foo == "foo" | |
assert bar == "bar" | |
assert baz == "baz" | |
end | |
test "evaluates each let at most once" do | |
assert capture_io(fn -> | |
bar | |
bar | |
end) == "let :bar evaluated\n" | |
end | |
test "new test evaluates let again" do | |
assert capture_io(fn -> | |
bar | |
bar | |
end) == "let :bar evaluated\n" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment