Sub/To/FuncBase.pm

filter: Statementstmt 25.2% 32 / 127 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 40 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 41.1% 7 / 17 Podpod 0.0% 0 / 10 totaltotal 15.2% 39 / 256 CC 40 SCAR 55.9
CC
40
Cov
15%
gen_doc_section_arguments
600.0
BUILD
42.0
add_doc_lines
30.0
CRAP 268.1
« Sub/To/Text.pm ↑ Summary To/POD.pm »
1 package Perinci::Sub::To::FuncBase;
2  
3 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Data::Dump::OneLine qw(dump1);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Data::Sah::Terse qw(terse_schema);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Perinci::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Perinci::Sub::Normalize qw(normalize_function_metadata);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 with 'Perinci::To::Doc::Role::Section';
13  
14 3 has meta => (is=>'rw');
15 3 has name => (is=>'rw');
16 3 has url => (is=>'rw');
17 3 has _pa => (is=>'rw');
18 3 has parent => (is=>'rw'); # points fo Perinci::To::* object
19 3 has export => (is=>'rw', default=>undef); # undef=unknown, 0=not exported by default but exportable, 1=exported by default, -1=never export
20  
21 3 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
22 3 our $DATE = '2023-07-09'; # DATE
23 3 our $DIST = 'Perinci-To-Doc'; # DIST
24 3 our $VERSION = '0.881'; # VERSION
25  
26 0 ▶sub BUILD {
Subroutine BUILD: not called CC=6 SCAR=37.4
Pod undocumented
27 0 my ($self, $args) = @_;
28  
29 0 ▶ $args->{meta} or die "Please specify meta";
Branch: 1 branch
BranchTrueFalseTotal
$args->{"meta"} or die "Please specify meta" 0 0 0
30  
31 0 my $parent = $self->{parent};
32 0 my $pdres = $self->{parent}{_doc_res};
33  
34 0 ▶ if ($pdres &&
Branch: 1 branch
BranchTrueFalseTotal
if ($pdres and @{$pdres->{"function_names_by_meta_addr"}{qq[$args->{"meta"}]};} > 1) 0 0 0
Condition0%0/3
$pdres and @{$pdres->{"function_names_by_meta_addr"}{qq[$args->{"meta"}]};} > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$pdres and @{$pdres->{"function_names_by_meta_addr"}{qq[$args->{"meta"}]};} > 1
$pdres @{$pdres->{"function_names_by_meta_addr"}{qq[$args->{"meta"}]};} > 1
Truth table0%0/3
A and B
A= $pdresB= @{$pdres->{"function_names_by_meta_addr"}{qq[$args->{"meta"}]};} > 1
ABresult
0X0
100
111
35 0 @{ $pdres->{function_names_by_meta_addr}{"$args->{meta}"} } > 1) {
36 # function is an alias to another function, no need to duplicate
37 # documenting the function, just mention that this function is alias to
38 # another.
39 $self->{doc_sections} //= [
40 # actually not needed but gen_doc_section_summary() et al currently
41 # sets dres->{name}, dres->{summary}, etc. this will be refactored
42 # later, gen_doc_section_summary() et al should've just add doc
43 # lines.
44 0 ▶ 'summary',
Condition0%0/2
$self->{"doc_sections"} //= ["summary", "arguments", "result", "alias"]
l!l
00
MC/DC0%0/1
$self->{"doc_sections"} //= ["summary", "arguments", "result", "alias"]
$self->{"doc_sections"}
Truth table0%0/2
A
A= $self->{"doc_sections"}
Aresult
11
00
45 'arguments',
46 'result',
47  
48 'alias',
49 ];
50 }
51  
52 $self->{doc_sections} //= [
53 0 ▶ 'summary',
Condition0%0/2
$self->{"doc_sections"} //= ["summary", "synopsis", "description", "arguments", "result", "links"]
l!l
00
MC/DC0%0/1
$self->{"doc_sections"} //= ["summary", "synopsis", "description", "arguments", "result", "links"]
$self->{"doc_sections"}
Truth table0%0/2
A
A= $self->{"doc_sections"}
Aresult
11
00
54 'synopsis',
55 'description',
56 'arguments',
57 'result',
58 'links',
59 ];
60 0 $self->{_pa} = do {
61 0 require Perinci::Access;
62 0 Perinci::Access->new;
63 };
64 }
65  
66 0 ▶sub before_gen_doc {
Subroutine before_gen_doc: not called CC=1 SCAR=6.9
Pod undocumented
67 0 my ($self, %opts) = @_;
68 0 log_trace("=> FuncBase's before_gen_doc(opts=%s)", \%opts);
69  
70 0 $self->{_orig_meta} = $self->{meta};
71 0 $self->{meta} = normalize_function_metadata($self->{meta});
72  
73 # initialize hash to store [intermediate] result
74 0 $self->{_doc_res} = {};
75 }
76  
77 # provide simple default implementation without any text wrapping. subclass such
78 # as Perinci::Sub::To::Text will use another implementation, one that supports
79 # text wrapping for example (provided by
80 # Perinci::To::Doc::Role::Section::AddTextLines).
81 0 ▶sub add_doc_lines {
Subroutine add_doc_lines: not called CC=5 SCAR=34.0
Pod undocumented
82 0 my $self = shift;
83 0 my $opts;
84 0 ▶ if (ref($_[0]) eq 'HASH') { $opts = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "HASH") 0 0 0
85 0 ▶ $opts //= {};
Condition0%0/2
$opts //= {}
l!l
00
MC/DC0%0/1
$opts //= {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
86  
87 0 ▶ my @lines = map { $_ . (/\n\z/s ? "" : "\n") }
Branch: 1 branch
BranchTrueFalseTotal
/\n\z/s ? : 0 0 0
88 0 ▶ map {/\n/ ? split /\n/ : $_} @_;
Branch: 1 branch
BranchTrueFalseTotal
/\n/ ? : 0 0 0
89  
90 0 my $indent = $self->doc_indent_str x $self->doc_indent_level;
91 0 push @{$self->doc_lines},
92 0 map {"$indent$_"} @lines;
93 }
94  
95 0 ▶sub gen_doc_section_alias {
Subroutine gen_doc_section_alias: not called CC=1 SCAR=0.0
Pod undocumented
96 # currently in after_gen_doc()
97 }
98  
99 0 ▶sub gen_doc_section_summary {
Subroutine gen_doc_section_summary: not called CC=3 SCAR=24.8
Pod undocumented
100 0 my ($self) = @_;
101  
102 0 my $rimeta = rimeta($self->meta);
103 0 my $dres = $self->{_doc_res};
104  
105 0 ▶ my $name = $self->name // $rimeta->langprop("name") //
Condition0%0/5
($self->name // $rimeta->langprop("name")) // "unnamed_function"
l!l
00
$self->name // $rimeta->langprop("name")
l!l&&r!l&&!r
000
MC/DC0%0/2
($self->name // $rimeta->langprop("name")) // "unnamed_function"
$self->name $rimeta->langprop("name")
Truth table0%0/3
A // B
A= $self->nameB= $rimeta->langprop("name")
ABresult
1X1
011
000
106 "unnamed_function";
107 0 my $summary = $rimeta->langprop("summary");
108  
109 0 $dres->{name} = $name;
110 0 $dres->{summary} = $summary;
111 }
112  
113 0 ▶sub gen_doc_section_synopsis {
Subroutine gen_doc_section_synopsis: not called CC=1 SCAR=0.0
Pod undocumented
114 # currently in after_gen_doc()
115 }
116  
117 0 ▶sub gen_doc_section_description {
Subroutine gen_doc_section_description: not called CC=2 SCAR=17.9
Pod undocumented
118 0 my ($self) = @_;
119  
120 0 my $rimeta = rimeta($self->meta);
121 0 my $dres = $self->{_doc_res};
122  
123 # XXX proper alt. search
124 0 ▶ $dres->{description} = $self->{meta}{'description.alt.env.perl'} //
Condition0%0/3
$self->{"meta"}{"description.alt.env.perl"} // $rimeta->langprop("description")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"meta"}{"description.alt.env.perl"} // $rimeta->langprop("description")
$self->{"meta"}{"description.alt.env.perl"} $rimeta->langprop("description")
Truth table0%0/3
A // B
A= $self->{"meta"}{"description.alt.env.perl"}B= $rimeta->langprop("description")
ABresult
1X1
011
000
125 $rimeta->langprop("description");
126 }
127  
128 0 ▶sub gen_doc_section_arguments {
Subroutine gen_doc_section_arguments: not called CC=24 SCAR=64.0
Pod undocumented
129 0 my ($self) = @_;
130  
131 0 my $meta = $self->meta;
132 0 my $rimeta = rimeta($meta);
133 0 my $dres = $self->{_doc_res};
134 0 ▶ my $args_p = $meta->{args} // {};
Condition0%0/2
$meta->{"args"} // {}
l!l
00
MC/DC0%0/1
$meta->{"args"} // {}
$meta->{"args"}
Truth table0%0/2
A
A= $meta->{"args"}
Aresult
11
00
135  
136 # perl term for args, whether '$arg1, $arg2, ...', or '%args', etc
137 0 ▶ my $aa = $meta->{_orig_args_as} // 'hash';
Condition0%0/2
$meta->{"_orig_args_as"} // "hash"
l!l
00
MC/DC0%0/1
$meta->{"_orig_args_as"} // "hash"
$meta->{"_orig_args_as"}
Truth table0%0/2
A
A= $meta->{"_orig_args_as"}
Aresult
11
00
138 0 my $aplt;
139 0 ▶ if (!keys(%$args_p)) {
Branch: 4 branches
BranchTrueFalseTotal
if (not keys %$args_p) { } 0 0 0
elsif ($aa eq 'hash') { } 0 0 0
elsif ($aa eq 'hashref') { } 0 0 0
elsif ($aa =~ /\Aarray(ref)?\z/) { } 0 0 0
140 0 $aplt = '()';
141 } elsif ($aa eq 'hash') {
142 0 $aplt = '(%args)';
143 } elsif ($aa eq 'hashref') {
144 0 $aplt = '(\%args)';
145 } elsif ($aa =~ /\Aarray(ref)?\z/) {
146 0 my @pos_args;
147 my @named_args;
148 0 my $all_named_args_are_optional = 1;
149 0 for (sort { ($args_p->{$a}{pos} // 9999) <=>
150 0 ▶ ($args_p->{$b}{pos} // 9999) } keys %$args_p) {
Condition0%0/4
$args_p->{$a}{"pos"} // 9999
l!l
00
$args_p->{$b}{"pos"} // 9999
l!l
00
MC/DC0%0/1
$args_p->{$a}{"pos"} // 9999
$args_p->{$a}{"pos"}
MC/DC0%0/1
$args_p->{$b}{"pos"} // 9999
$args_p->{$b}{"pos"}
Truth table0%0/2
A
A= $args_p->{$a}{"pos"}
Aresult
11
00
Truth table0%0/2
A
A= $args_p->{$b}{"pos"}
Aresult
11
00
151 0 ▶ if (defined $args_p->{$_}{pos}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args_p->{$_}{'pos'}) { } 0 0 0
152 0 push @pos_args, $_;
153 } else {
154 0 push @named_args, $_;
155 0 ▶ $all_named_args_are_optional = 0 if $args_p->{$_}{req};
Branch: 1 branch
BranchTrueFalseTotal
if $args_p->{$_}{"req"} 0 0 0
156 }
157 }
158  
159 $aplt = join(
160 '',
161 '(',
162 ($aa eq 'arrayref' ? '[' : ''),
163 join(
164 ', ',
165 (@named_args ? ($all_named_args_are_optional ? (" [ \\\%optional_named_args ] ") : ("\\\%named_args")) : ()),
166 map {
167 0 ▶ my $var = $_; $var =~ s/[^A-Za-z0-9_]+/_/g;
Branch: 3 branches
BranchTrueFalseTotal
$aa eq 'arrayref' ? : 0 0 0
@named_args ? : 0 0 0
$all_named_args_are_optional ? : 0 0 0
168 0 ▶ "\$$var" . (($args_p->{$_}{slurpy} // $args_p->{$_}{greedy}) ? ', ...' : '');
Branch: 2 branches
BranchTrueFalseTotal
$args_p->{$_}{'slurpy'} // $args_p->{$_}{'greedy'} ? : 0 0 0
$aa eq 'arrayref' ? : 0 0 0
Condition0%0/3
$args_p->{$_}{"slurpy"} // $args_p->{$_}{"greedy"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args_p->{$_}{"slurpy"} // $args_p->{$_}{"greedy"}
$args_p->{$_}{"slurpy"} $args_p->{$_}{"greedy"}
Truth table0%0/3
A // B
A= $args_p->{$_}{"slurpy"}B= $args_p->{$_}{"greedy"}
ABresult
1X1
011
000
169 } @pos_args,
170 ),
171 ($aa eq 'arrayref' ? ']' : ''),
172 ')'
173 );
174 } else {
175 0 die "BUG: Unknown value of args_as '$aa'";
176 }
177 0 $dres->{args_plterm} = $aplt;
178  
179 0 ▶ my $args = $meta->{args} // {};
Condition0%0/2
$meta->{"args"} // {}
l!l
00
MC/DC0%0/1
$meta->{"args"} // {}
$meta->{"args"}
Truth table0%0/2
A
A= $meta->{"args"}
Aresult
11
00
180 0 $dres->{args} = {};
181 0 my $raa = $dres->{args};
182 0 for my $name (keys %$args) {
183 0 my $arg = $args->{$name};
184 0 my $riargmeta = rimeta($arg);
185 0 ▶ $arg->{default_lang} //= $meta->{default_lang};
Condition0%0/3
$arg->{"default_lang"} //= $meta->{"default_lang"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$arg->{"default_lang"} //= $meta->{"default_lang"}
$arg->{"default_lang"} $meta->{"default_lang"}
Truth table0%0/3
A //= B
A= $arg->{"default_lang"}B= $meta->{"default_lang"}
ABresult
1X1
011
000
186 0 ▶ $arg->{schema} //= ['any'=>{}];
Condition0%0/2
$arg->{"schema"} //= ["any", {}]
l!l
00
MC/DC0%0/1
$arg->{"schema"} //= ["any", {}]
$arg->{"schema"}
Truth table0%0/2
A
A= $arg->{"schema"}
Aresult
11
00
187 0 my $s = $arg->{schema};
188 0 my $ra = $raa->{$name} = {schema=>$s};
189 0 $ra->{human_arg} = terse_schema($s);
190 0 ▶ if (exists $arg->{default}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $arg->{'default'}) { } 0 0 0
elsif (defined $s->[1]{'default'}) { } 0 0 0
191 0 $ra->{human_arg_default} = dump1($arg->{default});
192 } elsif (defined $s->[1]{default}) {
193 0 $ra->{human_arg_default} = dump1($s->[1]{default});
194 }
195 0 $ra->{summary} = $riargmeta->langprop('summary');
196 0 $ra->{description} = $riargmeta->langprop('description');
197 0 $ra->{arg} = $arg;
198  
199 0 $raa->{$name} = $ra;
200 }
201 }
202  
203 0 ▶sub gen_doc_section_result {
Subroutine gen_doc_section_result: not called CC=5 SCAR=34.0
Pod undocumented
204 0 my ($self) = @_;
205  
206 0 my $meta = $self->meta;
207 0 my $riresmeta = rimeta($meta->{result});
208 0 my $dres = $self->{_doc_res};
209  
210 0 ▶ my $orig_result_naked = $meta->{_orig_result_naked} // $meta->{result_naked};
Condition0%0/3
$meta->{"_orig_result_naked"} // $meta->{"result_naked"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$meta->{"_orig_result_naked"} // $meta->{"result_naked"}
$meta->{"_orig_result_naked"} $meta->{"result_naked"}
Truth table0%0/3
A // B
A= $meta->{"_orig_result_naked"}B= $meta->{"result_naked"}
ABresult
1X1
011
000
211  
212 0 ▶ $dres->{res_schema} = $meta->{result} ? $meta->{result}{schema} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$meta->{'result'} ? : 0 0 0
213 0 ▶ $dres->{res_schema} //= [any => {}];
Condition0%0/2
$dres->{"res_schema"} //= ["any", {}]
l!l
00
MC/DC0%0/1
$dres->{"res_schema"} //= ["any", {}]
$dres->{"res_schema"}
Truth table0%0/2
A
A= $dres->{"res_schema"}
Aresult
11
00
214 0 $dres->{human_res} = terse_schema($dres->{res_schema});
215  
216 0 ▶ if ($orig_result_naked) {
Branch: 1 branch
BranchTrueFalseTotal
if ($orig_result_naked) { } 0 0 0
217 0 $dres->{human_ret} = $dres->{human_res};
218 } else {
219 0 $dres->{human_ret} = '[$status_code, $reason, $payload, \%result_meta]';
220 }
221  
222 0 $dres->{res_summary} = $riresmeta->langprop("summary");
223 0 $dres->{res_description} = $riresmeta->langprop("description");
224 }
225  
226 0 ▶sub gen_doc_section_links {
Subroutine gen_doc_section_links: not called CC=1 SCAR=0.0
Pod undocumented
227 # not yet
228 }
229  
230 3 1;
231 # ABSTRACT: Base class for Perinci::Sub::To::* function documentation generators
232  
233 __END__
« Sub/To/Text.pm ↑ Summary To/POD.pm »