SpellCheck.pm

filter: Statementstmt 91.3% 21 / 23 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 93.5% 29 / 31 CC 1 SCAR 0.4
CC
1
Cov
91%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« SpellCheck/Plugin/PerlWords.pm ↑ Summary SpellCheck/Plugin.pm »
1 package Test::SpellCheck;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use 5.026;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use experimental qw( signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Ref::Util qw( is_ref is_blessed_ref is_plain_arrayref );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Globstar qw( globstar );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Test2::API qw( context );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Text::Hunspell::FFI 0.04;
Subroutine BEGIN: called CC=1 SCAR=2.6
11 use Carp qw( croak );
12 use Module::Load qw( load );
13 use Text::HumanComputerWords 0.02;
14 use List::Util 1.29 qw( pairmap );
15 use base qw( Exporter );
16  
17 our @EXPORT = qw ( spell_check spell_check_ini );
18  
19 # ABSTRACT: Check spelling of POD and other documents
20 our $VERSION = '0.02'; # VERSION
21  
22  
23 sub _default_file { 'bin/* script/* lib/**/*.pm lib/**/*.pod' }
24  
25 our $VERBOSE = 0;
26 state $ppi_cache;
27  
28 sub spell_check
29 {
30 my $plugin;
31 my @diag;
32 my $spell;
33  
34 if(defined $ENV{TEST_SPELLCHECK_PPI_CACHE})
35 {
36 unless(defined $ppi_cache)
37 {
38 my($class, @args) = split / /, $ENV{TEST_SPELLCHECK_PPI_CACHE};
39 my $pm = "$class.pm";
40 $pm =~ s{::}{/}g;
41 require PPI::Document;
42 require $pm;
43 $ppi_cache = $class->new(@args);
44 PPI::Document->set_cache($ppi_cache);
45 }
46 }
47  
48 if(defined $_[0] && is_blessed_ref $_[0])
49 {
50 $plugin = shift;
51 }
52 elsif(defined $_[0] && is_plain_arrayref $_[0])
53 {
54 my($class, @args) = shift->@*;
55 $class = "Test::SpellCheck::Plugin::$class";
56 load $class unless $class->can('new');
57 $plugin = $class->new(@args);
58 }
59 else
60 {
61 require Test::SpellCheck::Plugin::Perl;
62 $plugin = Test::SpellCheck::Plugin::Perl->new;
63 }
64  
65 my @files = sort map { globstar $_ } split(/\s+/, shift // _default_file());
66 my $test_name = shift // 'spell check';
67  
68 my $ctx = context();
69  
70 if($plugin->can('primary_dictionary'))
71 {
72 my($affix, $dic) = $plugin->primary_dictionary;
73 $spell = Text::Hunspell::FFI->new($affix, $dic);
74 $ctx->note("using affix file $affix") if $VERBOSE;
75 $ctx->note("using dictionary file $dic") if $VERBOSE;
76 }
77 else
78 {
79 $ctx->release;
80 croak("plugin provides no primary dictionaries");
81 }
82  
83 if($plugin->can('dictionary'))
84 {
85 foreach my $dic ($plugin->dictionary)
86 {
87 $spell->add_dic($dic);
88 $ctx->note("using dictionary file $dic") if $VERBOSE;
89 }
90 }
91  
92 my %global;
93  
94 if($plugin->can('stopwords'))
95 {
96 $global{$_} = 1 for $plugin->stopwords;
97 }
98  
99 my $splitter = do {
100 my @cpu;
101 @cpu = $plugin->splitter if $plugin->can('splitter');
102 my @bad = pairmap { $a !~ /^(url_link|module|skip)$/n ? $a : () } @cpu;
103 croak("bad splitter type@{[ @bad > 1 ? 's' : '' ]} @bad") if @bad;
104 Text::HumanComputerWords->new( @cpu );
105 };
106  
107 my %bad_words;
108  
109 foreach my $file (@files)
110 {
111 my %stopwords;
112 $ctx->note("check $file") if $VERBOSE;
113 $plugin->stream($file, $splitter, sub ($type, $fn, $ln, $word) {
114 if($type eq 'word')
115 {
116 foreach my $word (split /_/, $word)
117 {
118 return if $global{$word};
119 return if $stopwords{$word};
120 return if $spell->check($word);
121 return if $spell->check(ucfirst $word);
122 if($word =~ /^[A-Z0-9]+$/)
123 {
124 return if $spell->check(lc $word);
125 return if $spell->check(ucfirst lc $word);
126 }
127 push $bad_words{$word}->@*, [$fn,$ln];
128 }
129 }
130 elsif($type eq 'stopword')
131 {
132 $stopwords{$word} = 1;
133 }
134 elsif($type eq 'module')
135 {
136 # TODO
137 }
138 elsif($type eq 'name')
139 {
140 # TODO
141 }
142 elsif($type eq 'url_link')
143 {
144 # TODO
145 }
146 elsif($type eq 'pod_link')
147 {
148 my($name, $section) = @$word;
149 # TODO
150 }
151 elsif($type eq 'man_link')
152 {
153 my($name, $section) = @$word;
154 # TODO
155 }
156 elsif($type eq 'section')
157 {
158 # TODO
159 }
160 elsif($type eq 'error')
161 {
162 push @diag, $word;
163 }
164 }) if $plugin->can('stream');
165 }
166  
167 foreach my $word (sort keys %bad_words)
168 {
169 my $diag = "Misspelled: $word\n";
170 my @suggestions = $spell->suggest($word);
171 $diag .= " maybe: @suggestions\n" if @suggestions;
172 foreach my $loc ($bad_words{$word}->@*)
173 {
174 my($fn, $ln) = @$loc;
175 $diag .= " found at $fn line $ln.\n";
176 }
177 push @diag, $diag;
178 }
179  
180 push @diag, "total errors: @{[ scalar @diag ]}" if @diag > 0;
181  
182 if(@diag)
183 {
184 $ctx->fail($test_name, @diag);
185 }
186 else
187 {
188 $ctx->pass($test_name);
189 }
190 $ctx->release;
191  
192 return !scalar @diag;
193 }
194  
195  
196 sub spell_check_ini ($filename='spellcheck.ini', $test_name=undef)
197 {
198 require Test::SpellCheck::INI;
199 my @config = Test::SpellCheck::INI->read_file($filename)->@*;
200 my $root = shift @config;
201 shift @$root;
202 $root = { @$root };
203 my $file;
204 if(defined $root->{file})
205 {
206 if(is_ref $root->{file})
207 {
208 $file = join(' ', $root->{file}->@*);
209 }
210 else
211 {
212 $file = $root->{file};
213 }
214 }
215 else
216 {
217 }
218 my @plugin;
219 if(@config == 0)
220 {
221 # do nothing
222 }
223 elsif(@config == 1)
224 {
225 @plugin = @config;
226 }
227 else
228 {
229 @plugin = ([Combo => @config]);
230 }
231 my $ctx = context();
232 my $ret = spell_check @plugin, $file, $test_name;
233 $ctx->release;
234  
235 $ret;
236 }
237  
238 1;
239  
240 __END__
« SpellCheck/Plugin/PerlWords.pm ↑ Summary SpellCheck/Plugin.pm »