obmarg / ex_unit_fixtures

A library for defining modular dependencies (fixtures) for ExUnit tests.
MIT License
13 stars 5 forks source link

Potential API changes for defining fixtures. #36

Closed obmarg closed 8 years ago

obmarg commented 8 years ago

Could be worth looking into using on_definition instead of deffixture to declare fixtures. Not sure precisely how this would work, but needs investigation.

obmarg commented 8 years ago

Docs here: http://elixir-lang.org/docs/stable/elixir/Module.html

obmarg commented 8 years ago

An idea of how this could look:

defmodule Example do
  use ExUnitFixtures.FixtureModule

  @fixture db: ~w(a_dep, b_dep)a
  def db(a_dep, b_dep) do
    # create stuff
  end

end

Using normal def w/ explicit dependency list would reduce the macro based magic and make using pattern matching and the like easier, at the possible cost of verbosity.

Could possibly provide deffixture macro that compiles down to this?

obmarg commented 8 years ago

That last example might not even need to use on_definition, since we just need to build up a list of fixtures in an accumulating variable, then we can map them to functions without problem,

This could also provide a more natural place to specify scopes. Though that needs some more thoguht.

obmarg commented 8 years ago

Another possible option might be exposing it as a function/macro

defmodule Example do
  use ExUnitFixtures.FixtureModule

  def db(a_dep, b_dep) do
  end

  fixture :db, :a_dep, :b_dep
  # Or
  fixture db: [:a_dep, :b_dep]
  # Or
  fixture db: ~w(a_dep, b_dep)a
end

Somewhat similar to how defoverridable works. This might have the advantage of mostly not requiring as much macro magic, and maybe being able to work with general functions, rather than functions defined explicitly for the purpose of being fixtures....

obmarg commented 8 years ago

Ok, so leaning towards an API like this:

register_fixture db, [:a_dep, :b_dep], scope: whatever
def db(a_dep, b_dep) do
end

Where register_fixture registers the fixture with some sort of store, rather than using module attributes. If possible, this should remove the need to use modules everywhere. We might still need some hooks on_compile or similar to run the preprocessing, but maybe that can be done elsewhere...

obmarg commented 8 years ago

Dealt with in #41