Skip to content

Commit

Permalink
Use ESpec.Context.describe in ex_unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmi committed Jun 26, 2016
1 parent 9ebec9c commit 6fdd798
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 28 deletions.
13 changes: 9 additions & 4 deletions lib/espec/doc_test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@ defmodule ESpec.DocTest do
quote do
examples = ESpec.DocExample.extract(unquote(module))

examples = cond do
Keyword.get(unquote(opts), :only, :false) -> ESpec.DocTest.filter_only(examples, unquote(opts)[:only])
Keyword.get(unquote(opts), :except, false) -> ESpec.DocTest.filter_except(examples, unquote(opts)[:except])
true -> examples
examples = if Keyword.get(unquote(opts), :only, :false) do
ESpec.DocTest.filter_only(examples, unquote(opts)[:only])
else
examples
end
examples = if Keyword.get(unquote(opts), :except, false) do
ESpec.DocTest.filter_except(examples, unquote(opts)[:except])
else
examples
end

Enum.with_index(examples)
Expand Down
6 changes: 3 additions & 3 deletions test/assertions/accepted_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule AcceptedTest do
defmodule SomeSpec do
use ESpec

describe "function call in another process" do
ESpec.Context.describe "function call in another process" do
defmodule Server do
def call(a, b), do: SomeModule.func(a, b)
end
Expand All @@ -35,7 +35,7 @@ defmodule AcceptedTest do
end
end

describe "count option" do
ESpec.Context.describe "count option" do
before do
allow(SomeModule).to accept(:func, fn(a, b) -> a+b end)
SomeModule.func(1, 2)
Expand All @@ -46,7 +46,7 @@ defmodule AcceptedTest do
it do: expect(SomeModule).to_not accepted(:func, [1, 2], count: 1)
end

describe "any args" do
ESpec.Context.describe "any args" do
before do
allow(SomeModule).to accept(:func, fn(a, b) -> a+b end)
SomeModule.func(1, 2)
Expand Down
8 changes: 4 additions & 4 deletions test/assertions/eq_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ defmodule EqTest do
use ESpec

context "Success" do
describe "ESpec.Assertions.Eq" do
ESpec.Context.describe "ESpec.Assertions.Eq" do
it do: expect(1+1).to eq(2.0)
it do: expect(1+1).to_not eq(3)
end

describe "be" do
ESpec.Context.describe "be" do
it do: expect(1+1 == 2).to be true
it do: expect(1+1 == 1).to_not be true
it do: expect(1+1 == 1).to be false
Expand All @@ -20,12 +20,12 @@ defmodule EqTest do
end

context "Errors" do
describe "ESpec.Assertions.Eq" do
ESpec.Context.describe "ESpec.Assertions.Eq" do
it do: expect(1+1).to eq(3.0)
it do: expect(1+1).to_not eq(2)
end

describe "be" do
ESpec.Context.describe "be" do
it do: expect(1+1 == 1).to be true
it do: expect(1+1 == 1).to_not be false
end
Expand Down
2 changes: 1 addition & 1 deletion test/before_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule BeforeTest do
before do: {:ok, a: 10, b: 2}
it do: "#{shared[:a]} and #{shared[:b]} is defined"

describe "Describe" do
ESpec.Context.describe "Describe" do
before do: {:ok, b: fn(a) -> a*2 end}
it do: "#{shared[:b].(10)} == 20"
end
Expand Down
6 changes: 3 additions & 3 deletions test/contexts/context_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule ContextTest do
context "Context 1", a: 1, b: 2 do
it do: "is in Context 1"

describe "Describe 1" do
ESpec.Context.describe "Describe 1" do
it do: "is in Describe 1"
end
end
Expand All @@ -22,7 +22,7 @@ defmodule ContextTest do
it do: "in context without description bu with opts"
end

describe "is alias" do
ESpec.Context.describe "is alias" do
it do: "some example"
end

Expand All @@ -35,7 +35,7 @@ defmodule ContextTest do
end

context "Do nothing with empty context"
describe "Do nothing with empty context"
ESpec.Context.describe "Do nothing with empty context"
example_group "Do nothing with empty context"
end

Expand Down
2 changes: 1 addition & 1 deletion test/example_runner_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ defmodule ExampleRunnerTest do
test "run failed example with runtime error", context do
example = ESpec.ExampleRunner.run(context[:ex4])
assert example.status == :failure
assert String.match?(example.error.message, ~r/undefined function/)
assert String.match?(example.error.message, ~r/undefined/)
end

test "run example which throws term", context do
Expand Down
16 changes: 8 additions & 8 deletions test/let/leaking_let_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,44 @@ defmodule LeakingLetTest do

defmodule SomeSpec do
use ESpec
describe "first" do
ESpec.Context.describe "first" do
let :a, do: 1
it do: expect a |> to(eq 1)
end
describe "second" do
ESpec.Context.describe "second" do
it do: expect a |> to(eq 1)
end
it do: expect a |> to(eq 1)
end

defmodule SomeSpec2 do
use ESpec
describe "second" do
ESpec.Context.describe "second" do
it do: a |> should(eq 1)
end
describe "first" do
ESpec.Context.describe "first" do
let :a, do: 1
it do: a |> should(eq 1)
end
end

defmodule SomeSpec3 do
use ESpec
describe "example when local variable is the same as let" do
describe "use let val" do
ESpec.Context.describe "example when local variable is the same as let" do
ESpec.Context.describe "use let val" do
let :result, do: 42

it do: expect(result) |> to(eq 42)
end

describe "use let name in example" do
ESpec.Context.describe "use let name in example" do
it "is not okay" do
result = 23
expect(result) |> to(eq 23)
end
end

describe "use let name in pattern match" do
ESpec.Context.describe "use let name in pattern match" do
it "is not okay" do
{:ok, result} = {:ok, 123} # this still fails
expect(result) |> to(eq 123)
Expand Down
8 changes: 4 additions & 4 deletions test/let/leaking_subject_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ defmodule LeakingSubjectTest do
defmodule SomeSpec do
use ESpec

describe "first" do
ESpec.Context.describe "first" do
subject do: []

it "is empty by default", do: should be_empty
end

describe "second" do
ESpec.Context.describe "second" do
it do: should be_empty
it do: is_expected |> to(be_empty)
it do: is_expected.to be_empty
Expand All @@ -19,10 +19,10 @@ defmodule LeakingSubjectTest do

defmodule SomeSpec2 do
use ESpec
describe "second" do
ESpec.Context.describe "second" do
it do: should(eq 1)
end
describe "first" do
ESpec.Context.describe "first" do
subject do: 1
it do: should(eq 1)
end
Expand Down

0 comments on commit 6fdd798

Please sign in to comment.