chgeuer

chgeuer

Problem with data-driven test case generation

Hi,

I’m trying to generate test cases from a JSON file. However, when I try the quote/unquote mechanism, it compiles but doesn’t find tests.

test/input.json

{
    "[1,2,3]": [ 1, 2, 3 ],
    "true": true,
    "{\"s\":1}": { "s": 1 }
}

tests/my_test.exs (working, finding three tests)

This is based on the @ AndyL discussion on ExUnit Data-driven Tests. I would like to get rid of the @expression stuff.

defmodule MyTest do
  import MyTestHelper, only: [parses_to: 2]
  use ExUnit.Case, async: true

  with {:ok, body} <- File.read(Path.join([__DIR__, "input.json"])),
       {:ok, json} <- Poison.decode(body) do
    Enum.each(json, fn {expression, result} ->
      @expression expression
      @result result

      @expression |> parses_to(@result)
    end)
  end
end

tests/my_test.exs (not working, not finding any test)

defmodule MyTest do
  import MyTestHelper, only: [parses_to: 2]
  use ExUnit.Case, async: true

  with {:ok, body} <- File.read(Path.join([__DIR__, "input.json"])),
       {:ok, json} <- Poison.decode(body) do
    Enum.each(json, fn {expression, result} ->
      quote do
        unquote(expression) |> parses_to(unquote(result))
      end
    end)
  end
end

tests/test_helper.exs

ExUnit.start()

defmodule MyTestHelper do
  use ExUnit.Case

  defmacro parses_to(expression, expected) do
    quote do
      test "Expression \"#{unquote(expression)}\"" do
        assert unquote(expected) === unquote(expression) |> Poison.decode!()
      end
    end
  end
end

Most Liked Responses

chgeuer

chgeuer

Thanks @hauleth, unfortunately that doesn’t solve the problem. I uploaded the code to chgeuer/ex_problem_repro1 for easier testing. When I replace

@expression expression
@result result
@expression |> parses_to(@result)

with

parses_to(expression, result)

it says

  • variable "result" is unused and
  • variable "result" does not exist and is being expanded to "result()"
  • (CompileError) test/my_test.exs:15: undefined function expression/0

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New

We're in Beta

About us Mission Statement