Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple from ExUnit #96

Merged
merged 11 commits into from
Mar 21, 2018
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ def project do
end
```

## Integrating a testing library

By default, `use WhiteBread.Context` will import ExUnit.Assertions. If you're not using ExUnit, you'll probably want to override this default by calling `use WhiteBread.Context, test_library: :some_other_library_name`.

At the moment, the only library names available are `:ex_unit` (same as the default), `:espec`, and `nil` (which skips the test library setup step altogether).

# Next steps - Additional Suites and subcontexts

After following the getting started steps you may find your default context starts to get a bit large.
Expand Down
19 changes: 16 additions & 3 deletions lib/white_bread/context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ defmodule WhiteBread.Context do
@step_keywords [:given_, :when_, :then_, :and_, :but_]

@doc false
defmacro __using__(_opts) do
defmacro __using__(opts \\ []) do
opts = Keyword.merge [test_library: :ex_unit], opts
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small change request. Can this be pulled out as @default_test_library so that we have

opts = Keyword.merge [test_library: @default_test_library], opts

and further up the file:

@step_keywords [:given_, :when_, :then_, :and_, :but_]
@default_test_library :ex_unit

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds like a good idea.

[test_library: test_library] = opts

quote do
import WhiteBread.Context
import ExUnit.Assertions
unquote(import_test_library test_library)

@behaviour WhiteBread.ContextBehaviour

Expand Down Expand Up @@ -110,5 +113,15 @@ defmodule WhiteBread.Context do
end
end


defp import_test_library(test_library) do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is a function that returns a quoted block. It seemed like the right thing here, but I'm open to other ideas (considering I'm a relative beginner in Elixir...).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems a sensible approach to me here. It's relatively easy to follow what it's doing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized that I don't know the answer to this question: if macros are evaluated at compile time, will the fact that this is a function cause any problems? (It's been working well for me, but I just want to make sure that I didn't stumble into an Elixir gotcha.)

case test_library do
:ex_unit -> quote do: import ExUnit.Assertions
:espec -> quote do
require ESpec
use ESpec
end
nil -> quote do: true
_ -> raise ArgumentError, ":test_library must be one of :ex_unit, :espec, or nil."
end
end
end