in reply to Re: Being more Assert-ive with Perl
in thread Being more Assert-ive with Perl

Design By Contract does not seem to solve the problem, it only makes language translator complain a little earlier, does it?

Yes, but nothing can solve that problem really, since its "user error". I forgot where I heard it, but I have always like the quote: "Complain early and often" when speaking about error handling.

We need to test the code with both correct and incorrect input values in any case.

I agree. My point is that you can efficiently create those tests using this style and not have the overhead of full blown conditional blocks.

-stvn

Replies are listed 'Best First'.
Re^3: Being more Assert-ive with Perl
by kappa (Chaplain) on Sep 17, 2004 at 15:33 UTC

    Hm. Imagine a tool that takes all our declared contracts and composes a huge test suite that tries to test whether each contract is respected in our code, say, by feeding a series of valid and invalid data and turning off all runtime contract checks.

    E.g. I declare that my complex compute_graviton_phasor_coefficient sub wants to take a positive prime number as argument. Now I'd like to get a test that will run it against random numbers and testing that it dies in the right places.

    And, putting dreams aside, I'm going now to read about specification testing and try Test::LectroTest :)

      Hm. Imagine a tool that takes all our declared contracts and composes a huge test suite that tries to test whether each contract is respected in our code, say, by feeding a series of valid and invalid data and turning off all runtime contract checks.

      I like the idea of a tool, but why turn off all runtime contract checks? Wouldn't you want them in there to catch the errors?

      And, putting dreams aside, I'm going now to read about specification testing and try Test::LectroTest :)

      Me too, Test::LectroTest looks very cool.

      -stvn
        I like the idea of a tool, but why turn off all runtime contract checks? Wouldn't you want them in there to catch the errors?

        What's the point of testing the code below with something but positive numbers?

        is_numeric($_[0]) && $_[0] > 0 or die "Invalid input, give me a positi +ve number\n"; # is_numeric taken from perlfaq4

        This check is here for a reason and we'd like to test the reasoning. Will it die horribly trying to multiply a hashref by two or taking a square root of -100? Will it just plain lie?