Test::Spelling(3pm)

πŸ“¦
version 0.25 Β· 5 versions
, in debian bookworm, debian trixie, ubuntu jammy, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– Test::Spelling β€” the perl doc you're reading is a 3pm page, and it's giving "User Contributed Perl Documentation" realness

okay bestie, sit down, hydrate, because we're about to make sure your POD doesn't have any embarrassing typos in it before the whole internet sees it. this page is Test::Spelling(3pm) and it is genuinely one of the most useful little modules in the author-testing toolbox, no cap. 🌸


✨ NAME ✨

Test::Spelling β€” check for spelling errors in POD files. that's it, that's the whole job, and it does it beautifully. πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

okay so first move: you drop a file called pod-spell.t into your distribution's xt/author directory. iconic placement, love that for you.

use strict;
use warnings;
use Test::More;

use Test::Spelling;
use Pod::Wordlist;

add_stopwords(<DATA>);
all_pod_files_spelling_ok( qw( bin lib ) );

__DATA__
SomeBizarreWord
YetAnotherBIzarreWord

or!! alternatively!! you can gate the whole spelling test behind the environment variable AUTHOR_TESTING, which is lowkey the move for people who don't want randos running your author tests:

use strict;
use warnings;
use Test::More;

BEGIN {
    plan skip_all => "Spelling tests only for authors"
        unless $ENV{AUTHOR_TESTING};
}

use Test::Spelling;
use Pod::Wordlist;

all_pod_files_spelling_ok();

both vibes are valid, pick your fighter. 🫢


🌈 DESCRIPTION 🌈

Test::Spelling lets you check the spelling of a POD file, and report its results in standard Test::More fashion. periodt. This module requires a spellcheck program such as Hunspell <http://hunspell.github.io/>, aspell, spell, or, ispell. and we suggest using Hunspell, bestie, we're not being subtle about it. πŸ’–

use Test::Spelling;
pod_file_spelling_ok('lib/Foo/Bar.pm', 'POD file spelling OK');
NOT ME... about to tell you this but it matters: it is a bad idea to run spelling tests during an ordinary CPAN distribution install, or in a package that will run in an uncontrolled environment. there is literally no way of predicting whether the word list or spellcheck program used will give the same results. 😭 you can include the test in your distribution, but be sure to run it only for authors of the module by guarding it in a skip_all unless $ENV{AUTHOR_TESTING} clause, or by putting the test in your distribution's xt/author directory. anyway, people installing your module really do not need to run such tests, as it is unlikely that the documentation will acquire typos while in transit. facts.

you can add your own stop words, which are words that should be ignored by the spell check, like so:

add_stopwords(qw(asdf thiswordiscorrect));

adding stop words in this fashion affects all files checked for the remainder of the test script. the way that this works is genuinely documented over in Pod::Spell (which this module is built upon), and that page has a variety of ways to add per-file stop words to each .pm file. go look, iykyk. πŸŽ€

if you have a LOT of stop words, it's useful to put them in your test file's DATA section like so:

use strict;
use warnings;
use Test::More;

use Test::Spelling;
use Pod::Wordlist;

add_stopwords(<DATA>);
all_pod_files_spelling_ok();

__DATA__
folksonomy
Jifty
Zakirov

to maintain backwards compatibility, comment markers and some whitespace are ignored. and heads up β€” in the near future, the preprocessing we do on the arguments to add_stopwords in Test::Spelling will be changed and documented properly. so like, watch this space. πŸ‘€


πŸ”₯ FUNCTIONS πŸ”₯

Test::Spelling makes the following methods available. buckle up, there's a whole roster. πŸ‘‘

⭐ add_stopwords ⭐

add_stopwords(@words);
add_stopwords(<DATA>); # pull in stop words from the DATA section

add words that should be skipped by the spell checker. πŸ’œ note that Pod::Spell already skips words believed to be code, such as everything in verbatim (indented) blocks and code marked up with ""..."", as well as some common Perl jargon. so it's already doing the most, and this is your extra layer.

⭐ all_pod_files ⭐

all_pod_files();
all_pod_files(@list_of_directories);

returns a list of all the Perl files in each directory and its subdirectories, recursively. if no directories are passed, it defaults to blib if blib exists, or else lib if not. skips any files in CVS or .svn directories. (good riddance, honestly. πŸ’€)

a Perl file is:

  • any file that ends in .PL, .pl, .plx, .pm, .pod or .t.
  • any file that has a first line with a shebang and perl on it.

furthermore, files for which the filter set by set_pod_file_filter return false are skipped. by default, this filter passes everything through. no gatekeeping by default, we're all about openness here. 🌈

the order of the files returned is machine-dependent. if you want them sorted, you'll have to sort them yourself. bestie, the module is not gonna alphabetize for you, that's a you problem. 😌

⭐ all_pod_files_spelling_ok ⭐

all_pod_files_spelling_ok(@list_of_files);
all_pod_files_spelling_ok(@list_of_directories);

checks all the files for POD spelling. it gathers all_pod_files in Test::Spelling on each file/directory, and declares a plan in Test::More for you (one test for each file), so you must not call plan yourself. seriously. don't. it already did it. 🫠

if @files is empty, the function finds all POD files in the blib directory if it exists, or the lib directory if it does not. a POD file is one that ends with .pod, .pl, .plx, or .pm; or any file where the first line looks like a perl shebang line.

if there is no working spellchecker (determined by <Test:Spelling/"has_working_spellchecker">), this test will issue a "skip all" directive. so it won't just fail dramatically for no reason, it'll bow out gracefully. πŸ’…

if you're testing a distribution, just create an xt/author/pod-spell.t with the code in the "SYNOPSIS".

returns true if every POD file has correct spelling, or false if any of them fail. this function will show any spelling errors as diagnostics.

* NOTE: this only tests using bytes. this is not decoded content, etc. do not expect this to work with Unicode content, for example. this uses an open with no layers and no decoding. so like... keep that in mind before you come for the module. πŸ™ˆ

⭐ get_pod_parser ⭐

# a Pod::Spell -like object
my $object = get_pod_parser();

get the object we're using to parse the POD. a new Pod::Spell object should be used for every file. people providing custom parsers will have to do this themselves. main character responsibility, sorry bestie. πŸ‘‘

⭐ has_working_spellchecker ⭐

my $cmd = has_working_spellchecker;

"has_working_spellchecker" will return "undef" if there is no working spellchecker, or a true value (the spellchecker command itself) if there is. the module performs a dry-run to determine whether any of the spellcheckers it can will use work on the current system. you can use this to skip tests if there is no spellchecker. note that "all_pod_files_spelling_ok" will do this for you. ✨

a full list of spellcheckers which this method might test can be found in the source of the "spellchecker_candidates" method. go peek, it's right there. πŸ“

⭐ pod_file_spelling_ok ⭐

pod_file_spelling_ok('/path/to/Foo.pm');
pod_file_spelling_ok('/path/to/Foo.pm', 'Foo is well spelled!');

"pod_file_spelling_ok" will test that the given POD file has no spelling errors.

when it fails, "pod_file_spelling_ok" will show any spelling errors as diagnostics. 😭

the optional second argument is the name of the test. if it is omitted, "pod_file_spelling_ok" chooses a default test name "POD spelling for $filename".

* NOTE: this only tests using bytes. this is not decoded content, etc. do not expect this to work with Unicode content, for example. this uses an open with no layers and no decoding. same warning as before, same energy, it's important so it's said twice. πŸ’€

⭐ set_pod_file_filter ⭐

# code ref
set_pod_file_filter(sub {
    my $filename = shift;
    return 0 if $filename =~ /_ja.pod$/; # skip Japanese translations
    return 1;
});

if your project has POD documents written in languages other than English, then obviously you don't want to be running a spellchecker on every Perl file. "set_pod_file_filter" lets you filter out files returned from "all_pod_files" (and hence, the documents tested by "all_pod_files_spelling_ok"). it's giving "we respect other languages in this house". 🌸

⭐ set_pod_parser ⭐

my $object = Pod::Spell->new();
set_pod_parser($object);

by default Pod::Spell is used to generate text suitable for spellchecking from the input POD. if you want to use a different parser, perhaps a customized subclass of Pod::Spell, call "set_pod_parser" with an object that is-a Pod::Parser. be sure to create a fresh parser object for each file (don't use this with "all_pod_files_spelling_ok"). one object per file, no sharing, we're not passing around a communal parser here. 🧚

⭐ set_spell_cmd ⭐

set_spell_cmd('hunspell -l'); # current preferred
set_spell_cmd('aspell list');
set_spell_cmd('spell');
set_spell_cmd('ispell -l');

if you want to force this module to use a particular spellchecker, then you can specify which one with "set_spell_cmd". this is useful to ensure a more consistent lexicon between developers, or if you have an unusual environment. any command that takes text from standard input and prints a list of misspelled words, one per line, to standard output will do. that's the whole contract. simple. elegant. slay. πŸ’…


πŸ’œ SEE ALSO πŸ’œ

Pod::Spell

(that's the one, that's the entire list, but it's an important one fr fr)


πŸŽ€ AUTHOR πŸŽ€

Ivan Tubert-Brohman "<itub@cpan.org>"

heavily based on Test::Pod by Andy Lester and brian d foy. giving credit where credit is due, as one does. πŸ‘‘


Copyright 2005, Ivan Tubert-Brohman, All Rights Reserved.

you may use, modify, and distribute this package under the same terms as Perl itself. 🫢


and that's the whole page, bestie β€” go forth and spellcheck your POD, your docs will thank you, and honestly? so will everyone reading them. βœ¨πŸ¦‹πŸ’–

this has been Test::Spelling(3pm), part of perl v5.32.1, dated 2022-01-17 β€” mwah, no notes, drink some water. πŸŒΈπŸŽ€

πŸ“¦ where this page comes from