Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Seems to me that VerificationTests are a bit verbose:

TestReport[{VerificationTest[StringFreeQ["apple", "lp"], True], 
  VerificationTest[StringFreeQ["apple", "a" ~~ __ ~~ "e"], False], 
  VerificationTest[StringFreeQ["apple", "ap"], False], 
  VerificationTest[StringFreeQ["apple", "x"], True], 
  VerificationTest[StringFreeQ["", "asd"], True]}]

This was the first obvious thing that I tried:

args := {{"apple", "lp"}, {"apple", "a" ~~ __ ~~ "e"}, {"apple", "ap"}, {"apple", "x"}, {"", "asd"}};
results := {True, False, (Pause[2]; False), True, True};

tr = TestReport[
  VerificationTest @@@ Thread[{StringFreeQ @@@ args, results}], 
  TimeConstraint -> Quantity[1, "Seconds"]]

But this solution has many problems:

  • Timing specs and information aren't enforced and preserved
  • Evaluations may leak

What are some elegant ways to cut down the verbosity here without messing with evaluation mechanics of the arguments to VerificationTest?

share|improve this question

You can make it look somewhat more pleasant by using simple expression parsers. For example, define an auxiliary head Tests, as follows:

ClearAll[transformTest];
SetAttributes[transformTest, {HoldAll, Listable}];
transformTest[lhs_ -> rhs_] := Hold[VerificationTest[lhs, rhs]];
transformTest[Tests[tests___]] := Thread[transformTest[{tests}], Hold];


ClearAll[Tests];
SetAttributes[Tests, HoldAll];
Tests /: TestReport[t_Tests, args___] :=
  Replace[
    transformTest[t],
    Hold[tests___] :> TestReport[tests, args]
  ];

And now you can use the following syntax:

TestReport @ Tests[
  StringFreeQ["apple", "lp"] -> True, 
  StringFreeQ["apple", "a" ~~ __ ~~ "e"] -> False,
  StringFreeQ["apple", "ap"] -> False, 
  StringFreeQ["apple", "x"] -> True,
  StringFreeQ["", "asd"] -> True
]

which looks a little nicer to me.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.