Unit testing: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|Perl 6}}: Add some verbiage about Perl 6)
m (→‎{{header|Perl 6}}: added zkl header)
Line 57: Line 57:


Unit testing tools are not built in to the base compiler. Basic tools are distributed with the compiler as loadable modules. Many more complex toolkits and test harnesses are available through the [https://modules.perl6.org/ Perl 6 ecosystem]. In general, it is uncommon to include testing code in with run-time code; not unheard-of and not forbidden, but uncommon. Instead Perl modules by convention and community pressure tend to have a test suite that is automatically run when the module is installed. Again, there is no rule that a module that is released for public consumption '''must''' have a test suite, but the community tends to favour modules that have them and discourage/avoid those without.
Unit testing tools are not built in to the base compiler. Basic tools are distributed with the compiler as loadable modules. Many more complex toolkits and test harnesses are available through the [https://modules.perl6.org/ Perl 6 ecosystem]. In general, it is uncommon to include testing code in with run-time code; not unheard-of and not forbidden, but uncommon. Instead Perl modules by convention and community pressure tend to have a test suite that is automatically run when the module is installed. Again, there is no rule that a module that is released for public consumption '''must''' have a test suite, but the community tends to favour modules that have them and discourage/avoid those without.

=={{header|zkl}}==

Revision as of 20:54, 27 February 2019

Unit testing is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Demonstrate any support for Unit testing built into the language implementation.

See https://en.wikipedia.org/wiki/Unit_testing and Test a function for more information.

Clearly state whether this support is internal, or external to the interpreter or compiler used.

Jsish

Jsish supports an internal -u command line option to run unit tests, as part of the base implementation. These tests can include trial inputs and expected outputs in special comment blocks within a source file. Other command line options determine the level of verbosity and logging used when running tests. Being inside comment sections, unit tests do not affect the normal operation of a script (besides the negligible time taken for the interpreter to read in and skip over the comment sections). When in -u test-mode, source lines starting and ending with semi-colons ; are echoed using a special output format that also evaluates the enclosed expression and captures the result.

<lang javascript>/* A+B in Jsish */ var line = console.input(); var nums = line.match(/^\s*([+-]?[0-9]+)\s+([+-]?[0-9]+)\s*/); if (nums) {

   var A = Number(nums[1]);
   var B = Number(nums[2]);
   if (A <= 1000 && A >= -1000 && B <= 1000 && B >= -1000) {
       printf("%d\n", A + B);
   } else {
       puts("error: A and B both need to be in range -1000 thru 1000 inclusive");
   }

} else {

   puts("error: A+B requires two numbers separated by space");

}

/*

!INPUTSTART!

a b 1234 123 -1000 +1000 123 -234

!INPUTEND!

  • /

/*

!EXPECTSTART!

error: A+B requires two numbers separated by space error: A and B both need to be in range -1000 thru 1000 inclusive 0 -111

!EXEPECTEND!

  • /</lang>
Output:
prompt$ jsish -u A+B.jsi
[PASS] A+B.jsi

prompt$ jsish A+B.jsi
123 -234
-111

Perl 6

Perl 6 does not really have a mechanism built into the compiler to do unit testing, (It does have design-by-contract capabilities built in, but that is more for run-time, not really for unit testing.) Perl 6, and Perl in general does have unit testing built into the community. The Test-Anything-Protocol was invented and developed specifically to do unit testing on the original version of Perl, is still heavily used for modern versions, and has become a major standard for unit testing in many different languages other than Perl.

Testing is such a basic value in Perl 6, that a suite of unit tests is the language specification. As decreed by Larry Wall, the original author of Perl and the lead designer of Perl 6, anything that can pass "roast", the official Perl 6 test suite, or at least majority portions of it, can call itself Perl 6.

Unit testing tools are not built in to the base compiler. Basic tools are distributed with the compiler as loadable modules. Many more complex toolkits and test harnesses are available through the Perl 6 ecosystem. In general, it is uncommon to include testing code in with run-time code; not unheard-of and not forbidden, but uncommon. Instead Perl modules by convention and community pressure tend to have a test suite that is automatically run when the module is installed. Again, there is no rule that a module that is released for public consumption must have a test suite, but the community tends to favour modules that have them and discourage/avoid those without.

zkl