parroty / excheck

Property-based testing library for Elixir (QuickCheck style).
MIT License
316 stars 26 forks source link

Could this library use normal `for` comprehension syntax for generators instead of something custom? #28

Open myronmarston opened 8 years ago

myronmarston commented 8 years ago

The current generator syntax--e.g. for_all x in int--requires that I learn a new syntax, but it sure looks similar to Elixir's existing for comprehension syntax. Is there any reason that can't be used instead?

For example, looking over the examples in the readme:

property :square do
  for_all x in int, do: x * x >= 0
end

Could be:

property :squre do
  for x <- int, do: x * x >= 0
end

For a more involved example involving a such_that like:

for_all {x, y} in such_that({xx, yy} in {int, int} when xx < yy) do
  x < y
end

Could perhaps be:

for x <- int, y <- int, x < y do
  x < y
end

I just tried ExCheck for the first time today so I'm sure there are aspects of it's API I'm not understanding, but it would greatly reduce the learning curve if my existing knowledge of Elixir's for comprehensions could be used instead of learning a new API.

luc-tielen commented 8 years ago

It should be possible since all those constructs are based upon macros which basically expand the language. I think parroty made them the way they are so the API looks similar to triq syntax, but this syntax does indeed look really nice and intuitive.

A quick experiment using macros:

Interactive Elixir (1.3.1) - press Ctrl+C to exit (type h() ENTER for help) iex(1)> quote do: for x <- [1,2,3], do: x {:for, [], [{:<-, [], [{:x, [], Elixir}, [1, 2, 3]]}, [do: {:x, [], Elixir}]]} iex(2)> quote do: for x <- [1,2,3], x < 2, do: x {:for, [], [{:<-, [], [{:x, [], Elixir}, [1, 2, 3]]}, {:<, [context: Elixir, import: Kernel], [{:x, [], Elixir}, 2]}, [do: {:x, [], Elixir}]]}

It should be possible with the power of macros. Maybe we could even keep the constructs we have now and just build these macros on top of them in order to reduce the amount of work..