Test::Count(3pm)

๐Ÿ“ฆ version 0.1105, in alpine 3.24

๐ŸŽ€ NAME ๐ŸŽ€

okay bestie, welcome to Test::Count(3pm) โ€” the User Contributed Perl Documentation page for this little test-counting icon. ๐Ÿ’…

  • Test::Count - Module for keeping track of the number of tests in a test script. โœจ

โœจ VERSION โœจ

version 0.1105 ๐Ÿซถ

๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

$ cat "t/mytest.t" | perl -MTest::Count::Filter -e 'filter()'

๐Ÿ’– DESCRIPTION ๐Ÿ’–

Test::Count is a set of perl modules for keeping track of the number of tests in a test file. It works by putting in comments of the form "# TEST" (one test), "# TEST*$EXPR" or "# TEST+$EXPR" (both are multiple tests). Test::Count counts these tests all through the file and returns all of their results. ๐Ÿ’ฏ

You can put any mathematical expressions in there (using parentheses, "+", "-", "*", "/" and "%"). You can also assign variables using # TEST:$myvar=5+6;$second_var=$myvar+3 and later use them in the add-to-count expressions. A $var++ construct is also available. ๐Ÿงฎโœจ

You can find example test scripts under t/. ๐Ÿš

A simple Vim (<http://www.vim.org/>) function to update the count of the tests in the file is:

function! Perl_Tests_Count()
    %!perl -MTest::Count::Filter -e 'Test::Count::Filter->new({})->process()'
endfunction

๐Ÿงธ FUNCTIONS ๐Ÿงธ

my $counter = Test::Count->new({'input_fh' => \*MYFILEHANDLE});

Creates a new Test::Count object that processes the filehandle specified in 'input_fh'. Optional keys are:

  • 'assert_prefix_regex' => qr{; TEST} โ€” A regular expression for specifying the prefix for a "TEST" assertion that updates the grammar. Defaults to "# TEST".

$counter->process();

Process the filehandle specified in 'input_fh' in ->new(), and return a hash ref with the following keys:

  • tests_count โ€” The count of the test. ๐Ÿ”ข
  • lines โ€” The lines of the stream as is. ๐Ÿ“œ

๐ŸŒธ GRAMMAR DESCRIPTION ๐ŸŒธ

You can put any mathematical expressions in there (using parentheses, "+", "-", "*", "/" and "%"). You can also assign variables using # TEST:$myvar=5+6;$second_var=$myvar+3 and later use them in the add-to-count expressions. A $var++ construct is also available. Also available are "+=", "-=" and "*=". ๐Ÿง ๐Ÿ’…

You can also do # TEST:source "path-to-file-here.txt" where the filename comes in quotes, in order to include the filename and process it (similar to the C-shell or Bash "source" command) . You can use the special variable $^CURRENT_DIRNAME there for the dirname of the current file. ๐Ÿ“โœจ

Finally, # TEST*EXPR() and # TEST+$EXPR() add tests to the count. ๐ŸŽฏ

๐ŸŒˆ EXAMPLES ๐ŸŒˆ

๐Ÿ“ Trivial

The first example is very trivial:

#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 2;

# TEST
ok (1, "True is true.");

{
    my $val = 'foobar';

    # TEST
    is ($val, 'foobar', 'The variable $val has the right value.');
}

As you can see, the "# TEST" comments are very close to the assertions where they are easily noticable and easy to maintain by the tests (if more tests are added or removed). ๐Ÿ‘€๐Ÿ’–

๐ŸŒ€ Loops

Now, let's suppose you have several files which you'd like to make sure validate according to the spec, and are processed well using the processor.

#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 18;
use IO::All qw/ io /;
use Test::Differences qw/ eq_or_diff /;

use MyFormatProcessor ();

# TEST:$num_files=6;
my @basenames =
(qw(
    basic
    with_ampersands
    with_comments
    with_bold
    with_italics
    with_bold_and_italics
));

foreach my $basename (@basenames)
{
    my $processor = MyFormatProcessor->new(
        {
            filename => "t/data/input/$basename.myformat",
        }
    );

    # TEST*$num_files
    ok ($processor,
        "Construction of a processor for '$basename' was successful."
    );

    # TEST*$num_files
    ok (scalar($processor->is_valid()), "'$basename' is valid.");

    # TEST*$num_files
    eq_or_diff ($processor->convert_to_xhtml,
        scalar(io("t/data/want-output/$basename.xhtml")->slurp()),
        "Converting '$basename' is successful."
    );
}

As you can see, the number of files is kept in one central place, and each assertion inside the loop is multiplied by it. So if we add or remove files, we only need to add or remove them from their declarations. ๐Ÿ”โœจ

๐Ÿงš Loops using FILTER()

Starting from Test::Count version 0.1100 one can also use the "FILTER(MULT($expr))" and ENDFILTER() notations.

#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 18;
use IO::All qw/ io /;
use Test::Differences qw/ eq_or_diff /;

use MyFormatProcessor ();

# TEST:$num_files=6;
my @basenames =
(qw(
    basic
    with_ampersands
    with_comments
    with_bold
    with_italics
    with_bold_and_italics
));

# TEST:FILTER(MULT($num_files))
foreach my $basename (@basenames)
{
    my $processor = MyFormatProcessor->new(
        {
            filename => "t/data/input/$basename.myformat",
        }
    );

    # TEST
    ok ($processor,
        "Construction of a processor for '$basename' was successful."
    );

    # TEST
    ok (scalar($processor->is_valid()), "'$basename' is valid.");

    # TEST
    eq_or_diff ($processor->convert_to_xhtml,
        scalar(io("t/data/want-output/$basename.xhtml")->slurp()),
        "Converting '$basename' is successful."
    );
}
# TEST:ENDFILTER()

๐Ÿ’œ AUTHOR ๐Ÿ’œ

Shlomi Fish, <http://www.shlomifish.org/> .

๐Ÿฆ‹ ACKNOWLEDGEMENTS ๐Ÿฆ‹

This section is in the original, but there's literally nothing listed under it. Just empty-space energy, bestie. ๐Ÿซถ

Copyright 2006 Shlomi Fish. ๐Ÿ’–

This program is released under the following license: MIT X11. ๐Ÿ“œ

๐Ÿ‘‘ SUPPORT ๐Ÿ‘‘

Websites

The following websites have more information about this module, and may be of help to you. As always, in addition to those websites please use your favorite search engine to discover more resources. ๐Ÿ”Ž

Bugs / Feature Requests

Please report any bugs or feature requests by email to "bug-test-count at rt.cpan.org", or through the web interface at <https://rt.cpan.org/Public/Bug/Report.html?Queue=Test-Count>. You will be automatically notified of any progress on the request by the system. ๐Ÿ“ฌ

Source Code

The code is open to the world, and available for you to hack on. Please feel free to browse it and play with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull from your repository :) ๐Ÿ› ๏ธ

<https://github.com/shlomif/perl-test-count>

git clone git://github.com/shlomif/perl-test-count.git

๐Ÿ’… AUTHOR ๐Ÿ’…

Shlomi Fish <shlomif@cpan.org>

๐Ÿ’€ BUGS ๐Ÿ’€

Please report any bugs or feature requests on the bugtracker website <https://github.com/shlomif/perl-test-count/issues> ๐Ÿ“

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature. ๐Ÿงช

This software is Copyright (c) 2006 by Shlomi Fish. ๐Ÿซถ

This is free software, licensed under:

The MIT (X11) License ๐Ÿ“œ


That's the whole page, bestie! ๐Ÿ’– This one is Test::Count(3pm), and it came from perl v5.42.2 on 2023-09-28. Byeee! ๐Ÿ‘‘โœจ

๐Ÿ“ฆ where this page comes from