Port sigil koans from iamvery/elixir-koans

7705d743ac/about_sigils.exs
h/t @sebastiangeiger
This commit is contained in:
Jay Hayes
2016-05-16 18:43:45 -05:00
parent 5f149826e1
commit 649c649084
2 changed files with 58 additions and 0 deletions

39
lib/koans/03_sigils.ex Normal file
View File

@@ -0,0 +1,39 @@
defmodule Sigils do
use Koans
@intro "Sigils"
koan "The ~s sigil is a different way of expressing string literals" do
assert ~s{This is a string} == ___
end
koan "Sigils are useful to avoid escaping quotes in strings" do
assert "\"Welcome to the jungle\", they said." == ___
end
koan "Sigils can use different delimiters" do
matches? = ~s{This works!} == ~s[This works!]
assert matches? == ___
end
koan "The lowercase ~s sigil supports string interpolation" do
assert ~s[1 + 1 = #{1+1}] == ___
end
koan "The ~S sigil is similar to ~s but doesn't do interpolation" do
assert ~S[1 + 1 = #{1+1}] == ___
end
koan "The ~w sigil creates word lists" do
assert ~w(Hello world) == [___, ___]
end
koan "The ~w sigil also allows interpolation" do
assert ~w(Hello 1#{1+1}3) == [___, ___]
end
koan "The ~W sigil behaves to ~w as ~S behaves to ~s" do
assert ~W(Hello #{1+1}) == ["Hello", ___]
end
end

View File

@@ -0,0 +1,19 @@
defmodule SigilsTests do
use ExUnit.Case
import TestHarness
test "Sigils" do
answers = [
"This is a string",
~S("Welcome to the jungle", they said.),
true,
"1 + 1 = 2",
~S(1 + 1 = #{1+1}),
{:multiple, ["Hello", "world"]},
{:multiple, ["Hello", "123"]},
~S(#{1+1}),
]
test_all(Sigils, answers)
end
end