purescript-spec icon indicating copy to clipboard operation
purescript-spec copied to clipboard

forAll :: ∀ e a f. Foldable f => (a -> String) -> String -> f a -> (a -> Aff e Unit) -> Spec e Unit

Open safareli opened this issue 7 years ago • 6 comments

What are thoughts on adding something like this this?

forAll :: ∀ e a f. Foldable f => (a -> String) -> String -> f a -> (a -> Aff e Unit) -> Spec e Unit
forAll itTitle title arb f = describe title do
  for_ arb \a -> it (itTitle a) (f a)

...
  forAll _.str "format (unformat a) = a" arb \({ str }) -> do
    (format $ unformat str) `shouldEqual` (Right str) 
...

I have been using for_ in it block but if some test case fails then other items are not tested and you can't see them in log. using this function you can see each item in log and all of them will be executed.

safareli avatar Apr 25 '17 19:04 safareli

I would welcome utility functions like these as I have been re-inventing that wheel many times over for neodoc

felixSchl avatar Apr 25 '17 21:04 felixSchl

I was looking for something like this to write a tests that takes a record of functions that work against a database, and a record of the same type with fakes, making sure both behave the same in the test.

On Tue, Apr 25, 2017 at 10:24 PM, Felix Schlitter [email protected] wrote:

I would welcome utility functions like these as I have been re-inventing that wheel many https://github.com/felixSchl/neodoc/blob/development/test/Test/Spec/SolveSpec.purs#L240 times https://github.com/felixSchl/neodoc/blob/development/test/Test/Spec/DescParserSpec.purs#L58 over https://github.com/felixSchl/neodoc/blob/development/test/Test/Spec/ArgParserSpec.purs#L974 for neodoc

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/owickstrom/purescript-spec/issues/49#issuecomment-297169561, or mute the thread https://github.com/notifications/unsubscribe-auth/AAK7uOLsTKOtE7xPrUJuIcrwvaoq_hWDks5rzmSigaJpZM4NH69o .

mostalive avatar Apr 26 '17 09:04 mostalive

Yeah, this does seem useful. I have some questions/considerations:

  • The name forAll feels like it's too close to forall. Would prefer something closer to it, as it's basically a "multi-it". Perhaps they?
  • I'd prefer to drop the outer describe in this definition, keeping it a bit simpler, and not making an assumption that you want a separate level for the foldable. I'm not sure, but you might want to do something like:
    describe "many things" do
      they show [2, 4] \n -> (n `mod` 2) `shouldEqual` 0
      they show [4, 8] \n -> (n `mod` 4) `shouldEqual` 0
    
  • And as a last thing, is the argument order the way we want? Not saying it isn't, but you might have feedback on that.

Thanks for the suggestion!

owickstrom avatar Apr 27 '17 05:04 owickstrom

  • dropping out describe is good idea.
  • they seems good to me.

so at this point it looks like:

they :: ∀ e a f. Foldable f => (a -> String) -> f a -> (a -> Aff e Unit) -> Aff e Unit
they itTitle arb f = for_ arb \a -> it (itTitle a) (f a)

...
  describe "format (unformat a) = a" do
    they show arb \(str) -> (format $ unformat str) `shouldEqual` (Right str) 
...

About order:

I don't have strong opinion on it, but If we leave f as last argument, then we have two options:

  • NAME itTitle arb f, infix usage: itTitle `NAME` arb f
  • NAME arb itTitle f, infix usage: arb `NAME` itTitle f I can't think of a good name (english is not my native language) but there might be some name which will best fit this usage. maybe that? (show `that` validDates parseAndUnparseComutes)

safareli avatar Apr 27 '17 10:04 safareli

Yeah, I could not come with any specific order either, as it doesn't read naturally anyway. I think we should go with what you already have:

they :: ∀ e a f. Foldable f => (a -> String) -> f a -> (a -> Aff e Unit) -> Aff e Unit

Would you like to submit this as a PR?

owickstrom avatar Apr 28 '17 04:04 owickstrom

Would you like to submit this as a PR?

Yes!

safareli avatar Apr 28 '17 12:04 safareli