Testing macro for create

I wrote a macro which takes a list and matches the required_fields present in that list or not. Here is the code:

defmodule Some do
  defmacro __using__(_options) do
    quote location: :keep do
      defmacro create(list) do
        Enum.map(@required, fn(field) ->
          quote do
            test "#{unquote(@required)} is necessary to create (#{unquote(list)})" do
              member = Enum.member?(unquote(list), unquote(field))
              assert true == member
            end
          end
        end)
      end  
    end
  end

I can’t figure out how can I inspect the code running inside the macro because IO.inspect isn’t working inside it. and also how can I load it inside iex to pass the value to this macro because that doesn’t seem to work either. if i do some.create([list]).it gives me undefined function error in iex

some is not a valid name for a module, modulenames have to be either atoms (starting with a colon) or aliases (starting with a capital letter).

Also when you want to use macros inside of iex (or anywhere else) you have to require the implementing module first.


edit

Also after re-indenting your code, I see some more issues.

  1. There is an end missing, I blame copy paste for this and assume that in the real code that end is actually there.
  2. You are using quote inside of a quote, which is known to cause some problems but can sometime be workaround by using :bind_quoted option to the outer one.
  3. Assuming that this would work at all, it does not create the macrp some.create/1 but after you used some, you’d have the macro create available in your current module.
1 Like

Also I test macro’s by testing from tiny bits, then outer ones of that, and proceed out until I see the whole one (you can see the remnants of many IO.inspect’s in some of my libraries commented out… >.>).