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
Merged

Decouple from ExUnit #96

merged 11 commits into from
Mar 21, 2018

Conversation

marnen
Copy link
Contributor

@marnen marnen commented Mar 9, 2018

WhiteBread.Context brings in ExUnit.Assertions, which was conflicting with the ESpec functions that I want to use in my steps. So I made the test library configurable, defaulting to ExUnit.

Really, though, I only did it this way to avoid breaking existing behavior. In the longer term, I don't actually think that WhiteBread.Context should import a testing library at all out of the box. I'd rather see something like this:

defmodule MyProject.Context
  defmacro __using__(_opts) do
    quote do
      use WhiteBread.Context # without testing library being loaded
      import ExUnit.Assertions
    end
  end
end
defmodule MyProject.StepDefinitions
  use MyProject.Context
end

What do you think?

@@ -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.)

@marnen marnen mentioned this pull request Mar 14, 2018
@meadsteve
Copy link
Collaborator

@marnen I really like this approach. I actually now regret defaulting to always importing ExUnit.Assertions. I think I took the "batteries included" approach a little too far.

@@ -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.

use ESpec
end
nil -> quote do: true
_ -> raise ArgumentError, "#{inspect test_library} is not a recognized value for :test_library. Recognized values are :ex_unit, :espec, and nil."
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 also made the error message a little clearer here by printing the bad value.

@marnen
Copy link
Contributor Author

marnen commented Mar 19, 2018

I think this is probably ready to merge now.

@meadsteve meadsteve merged commit 35103c7 into meadery:master Mar 21, 2018
@marnen marnen deleted the decouple-from-exunit branch August 9, 2018 12:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants