By the end of part 8 we finally managed to see an actual implementation of our new feature. We tested a couple of things on the commandline directly to see that it seems to be doing the right thing. For a real core feature though it would be better to have it tested in a more automated, repeatable fashion. This is what the core unit tests are for.
The core perl source distribution contains a t/ directory with unit test files, very similar to the structure used by regular CPAN modules. The process for running these is a little different; as we already saw back in part 3 they need to be invoked by t/harness. The files themselves are somewhat more limited in what other modules they can use, so the full suite of Test:: modules are unavailable. But still they are expected to emit the regular TAP output we've come to expect from Perl unit tests, and tend to be structured quite similarly inside.
For example, the isa feature added an entire new file for its unit tests. As they all relate to the new syntax and semantics around a new opcode, they go in a file under the t/op directory. I won't paste the entire t/op/isa.t file, but consider this small section: (github.com/Perl/perl5):
#!./perl BEGIN { chdir 't' if -d 't'; require './test.pl'; set_up_inc('../lib'); require Config; } use strict; use feature 'isa'; no warnings 'experimental::isa'; ... my $baseobj = bless {}, "BaseClass"; # Bareword package name ok($baseobj isa BaseClass, '$baseobj isa BaseClass'); ok(not($baseobj isa Another::Class), '$baseobj is not Another::Class');
While it doesn't use Test::More, it does still have access to some similar testing functions such as the ok test. The initial lines of boilerplate in the BEGIN block set up the testing functions from the test.pl script, so we can use them in the actual tests.
Lets now have a go at writing some tests for our new banana feature. As it works like a text transformation function we can imagine a few different test strings to throw at it.
leo@shy:~/src/bleadperl/perl [git] $ nvim t/op/banana.t leo@shy:~/src/bleadperl/perl [git] $ cat t/op/banana.t #!./perl BEGIN { chdir 't' if -d 't'; require './test.pl'; set_up_inc('../lib'); require Config; } use strict; use feature 'banana'; no warnings 'experimental::banana'; plan 7; is(ban "ABCD" ana, "NOPQ", 'Uppercase ROT13'); is(ban "abcd" ana, "nopq", 'Lowercase ROT13'); is(ban "1234" ana, "1234", 'Numbers unaffected'); is(ban "a! b! c!" ana, "n! o! p!", 'Whitespace and symbols intermingled'); my $str = "efgh"; is(ban $str ana, "rstu", 'Lexical variable'); is(ban $str . "IJK" ana, "rstuVWX", 'Concat expression'); is("(" . ban "LMNO" ana . ")", "(YZAB)", 'Outer concat'); $ ./perl t/harness t/op/banana.t op/banana.t .. ok All tests successful. Files=1, Tests=4, 1 wallclock secs ( 0.02 usr 0.00 sys + 0.03 cusr 0.00 csys = 0.05 CPU) Result: PASS
Here we have used the is() testing function to test that various strings that we got the ban ... ana operator to generate are what we expected them to be. We've tested both uppercase and lowercase letters, and that non-letter characters such as numbers, symbols and spaces remain unaffected. In addition we've added some syntax tests as well, to check variables as well as literal string constants, and to demonstrate that the parser works correctly on the precedence of the operator mixed with string concatenation. All appears to be working fine.
Before we commit this one there is one last thing we have to do. Having added a new file to the distribution, one of the porting tests will now be unhappy:
leo@shy:~/src/bleadperl/perl [git] $ git add t/op/banana.t leo@shy:~/src/bleadperl/perl [git] $ make test_porting ... porting/manifest.t ........ 9848/? # Failed test 10502 - git ls-files gives the same number of files as MANIFEST lists at porting/manifest.t line 101 # got "6304" # expected "6303" # Failed test 10504 - Nothing added to the repo that isn't in MANIFEST at porting/manifest.t line 113 # got "1" # expected "0" # Failed test 10505 - Nothing added to the repo that isn't in MANIFEST at porting/manifest.t line 114 # got "not in MANIFEST: t/op/banana.t" # expected "not in MANIFEST: " porting/manifest.t ........ Failed 3/10507 subtests
To fix this one we need to manually add an entry in the MANIFEST file; unlike as is common practice for CPAN modules, this file is not automatically generated.
leo@shy:~/src/bleadperl/perl [git] $ nvim MANIFEST leo@shy:~/src/bleadperl/perl [git] $ git diff MANIFEST diff --git a/MANIFEST b/MANIFEST index 71d3b453da..03ecdda3d2 100644 --- a/MANIFEST +++ b/MANIFEST @@ -5779,6 +5779,7 @@ t/op/attrproto.t See if the prototype attribute works t/op/attrs.t See if attributes on declarations work t/op/auto.t See if autoincrement et all work t/op/avhv.t See if pseudo-hashes work +t/op/banana.t See if the ban ... ana syntax works t/op/bless.t See if bless works t/op/blocks.t See if BEGIN and friends work t/op/bop.t See if bitops work leo@shy:~/src/bleadperl/perl [git] $ make test_porting ... Result: PASS
Of course, in this test file we've added only 7 tests. It is likely that any actual real feature would have a lot more testing around it, to deal with a wider variety of situations and corner-cases. It's often that the really interesting cases only come to light after trying to use it for real and finding odd situations that don't quite work as expected; so after adding a new feature expect to spend a while expanding the test file to cover more things. It's especially useful to add new tests of new situations you find yourself using the feature in, even if they currently work just fine. The presence of such tests helps ensure the feature remains working in that manner.
No comments:
Post a Comment