展开某些宏调用

时间:2017-01-01 13:33:50

标签: macros elixir

我找到了similar question,但没有解释某些宏调用的扩展。例如,如何查看此调用扩展到什么内容?

defmodule Test do
  use ExUnit.Case
  test "always pass", do: assert true
end

1 个答案:

答案 0 :(得分:2)

你几乎可以按照你所链接的问题的方法进行操作:

SELECT product_name,
  product_type,
  product_status,
  product_store,
  product_price 
FROM tbl_product 
WHERE user_id = user.Id 
 AND product_status = status 
 AND product_name LIKE 'productName%';

如果您现在使用defmodule Test do use ExUnit.Case # (other tests here) ast = quote do test "always pass", do: assert true end expanded = Macro.expand(ast, __ENV__) IO.puts "Original:" # IO.puts inspect(ast, pretty: true) IO.puts ast |> Macro.to_string IO.puts "" IO.puts "Expanded:" # IO.puts inspect(expanded, pretty: true) IO.puts expanded |> Macro.to_string end 运行测试,则会看到mix test宏的扩展版本:

test

您可以取消注释Original: test("always pass") do assert(true) end Expanded: ( var = {:_, [], ExUnit.Case} contents = {:__block__, [], [{:assert, [context: CrawlieTest, import: ExUnit.Assertions], [true]}, :ok]} message = "always pass" ( name = ExUnit.Case.register_test(__ENV__, :test, message, []) def(unquote(name)(unquote(var))) do unquote(contents) end ) ) 行,以查看原始版本和展开版本在Abstract Syntax Tree表示中的外观。