FormulaEngine.pm

filter: Statementstmt 93.4% 43 / 46 Branchbran 53.5% 15 / 28 Conditioncond 50.0% 6 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 17 / 17 Podpod 75.0% 3 / 4 totaltotal 74.7% 86 / 115 CC 19 SCAR 19.5
CC
19
Cov
74%
_coerce_instance
16.8
parse
4.9
evaluate
4.6
CRAP 7.0
« FormulaEngine/Error.pm ↑ Summary FormulaEngine/Namespace/Default.pm »
1 package Language::FormulaEngine;
2 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use Module::Runtime 'require_module';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # ABSTRACT: Parser/Interpreter/Compiler for simple spreadsheet formula language
8 7 our $VERSION = '0.08'; # VERSION
9  
10  
11 has parser => (
12 is => 'lazy',
13 6 ▶ builder => sub {},
Subroutine _build_parser: called CC=1 SCAR=0.0
14 7 ▶ coerce => sub { _coerce_instance($_[0], 'parse', 'Language::FormulaEngine::Parser') }
Subroutine __ANON__: called CC=1 SCAR=0.0
15 7 );
16 has namespace => (
17 is => 'lazy',
18 1 ▶ builder => sub {},
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _build_namespace: called CC=1 SCAR=0.0
19 7 ▶ coerce => sub { _coerce_instance($_[0], 'get_function', 'Language::FormulaEngine::Namespace::Default') },
Subroutine __ANON__: called CC=1 SCAR=0.0
20 3 ▶ trigger => sub { my ($self, $val)= @_; $self->compiler->namespace($val) },
Subroutine __ANON__: called CC=1 SCAR=0.0
21 7 );
22 has compiler => (
23 is => 'lazy',
24 6 ▶ builder => sub {},
Subroutine __ANON__: called CC=1 SCAR=0.0
25 7 ▶ coerce => sub { _coerce_instance($_[0], 'compile', 'Language::FormulaEngine::Compiler') }
Subroutine __ANON__: called CC=1 SCAR=0.0
26 7 );
27  
28 7 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
29 7 my $self= shift;
30 7 $self->compiler->namespace($self->namespace);
31 }
32  
33 21 ▶sub _coerce_instance {
Subroutine _coerce_instance: called CC=12 SCAR=28.2
34 21 my ($thing, $req_method, $default_class)= @_;
35 21 ▶ return $thing if ref $thing and ref($thing)->can($req_method);
Branch: 1 branch
BranchTrueFalseTotal
if ref $thing and ref($thing)->can($req_method) 1 20 21
Condition100%3/3
ref $thing and ref($thing)->can($req_method)
!ll&&!rl&&r
1731
MC/DC100%2/2
ref $thing and ref($thing)->can($req_method)
ref $thing ref($thing)->can($req_method)
Truth table100%3/3
A and B
A= ref $thingB= ref($thing)->can($req_method)
ABresult
0X0
100
111
36
37 my $class= !defined $thing? $default_class
38 20 ▶ : ref $thing eq 'HASH'? $thing->{CLASS} || $default_class
Branch: 4 branches
BranchTrueFalseTotal
!defined($thing) ? : 17 3 20
ref $thing eq 'HASH' ? : 3 0 3
ref $thing ? : 0 0 0
$req_method eq 'get_function' && $thing =~ /^[0-9]+$/ ? : 0 0 0
Condition33%2/6
$thing->{"CLASS"} or $default_class
l!l&&r!l&&!r
210
$req_method eq "get_function" and $thing =~ /^[0-9]+$/
!ll&&!rl&&r
000
MC/DC0%0/2
$thing->{"CLASS"} or $default_class
$thing->{"CLASS"} $default_class
MC/DC0%0/2
$req_method eq "get_function" and $thing =~ /^[0-9]+$/
$req_method eq "get_function" $thing =~ /^[0-9]+$/
Truth table66%2/3
A or B
A= $thing->{"CLASS"}B= $default_class
ABresult
1X1
011
000
Truth table0%0/3
A and B
A= $req_method eq "get_function"B= $thing =~ /^[0-9]+$/
ABresult
0X0
100
111
39 : ref $thing? $default_class
40 : ($req_method eq 'get_function' && $thing =~ /^[0-9]+$/)? "Language::FormulaEngine::Namespace::Default::V$thing"
41 : $thing;
42 20 ▶ require_module($class)
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can($req_method) 14 6 20
43 unless $class->can($req_method);
44
45 20 ▶ my @args= !ref $thing? ()
Branch: 2 branches
BranchTrueFalseTotal
!ref($thing) ? : 17 3 20
ref $thing eq 'ARRAY' ? : 0 3 3
46 : (ref $thing eq 'ARRAY')? @$thing
47 : $thing;
48 20 return $class->new(@args);
49 }
50  
51  
52 2 ▶sub parse {
Subroutine parse: called CC=3 SCAR=16.0
Pod documented
53 2 my ($self, $text, $error_ref)= @_;
54 2 ▶ unless ($self->parser->parse($text)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->parser->parse($text)) 0 2 2
55 0 ▶ die $self->parser->error unless $error_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $error_ref 0 0 0
56 0 $$error_ref= $self->parser->error;
57 0 return undef;
58 }
59 2 return Language::FormulaEngine::Formula->new(
60 engine => $self,
61 orig_text => $text,
62 parse_tree => $self->parser->parse_tree,
63 functions => $self->parser->functions,
64 symbols => $self->parser->symbols,
65 );
66 }
67  
68  
69 126 ▶sub evaluate {
Subroutine evaluate: called CC=4 SCAR=15.2
Pod documented
70 126 my ($self, $text, $vars)= @_;
71 126 ▶ $self->parser->parse($text)
Branch: 1 branch
BranchTrueFalseTotal
$self->parser->parse($text) or die $self->parser->error 126 0 126
72 or die $self->parser->error;
73 126 my $ns= $self->namespace;
74 126 ▶ $ns= $ns->clone_and_merge(variables => $vars) if $vars && %$vars;
Branch: 1 branch
BranchTrueFalseTotal
if $vars and %$vars 126 0 126
Condition33%1/3
$vars and %$vars
!ll&&!rl&&r
00126
MC/DC0%0/2
$vars and %$vars
$vars %$vars
Truth table33%1/3
A and B
A= $varsB= %$vars
ABresult
0X0
100
111
75 126 return $self->parser->parse_tree->evaluate($ns);
76 }
77  
78  
79 129 ▶sub compile {
Subroutine compile: called CC=3 SCAR=11.4
Pod documented
80 129 my ($self, $text)= @_;
81 129 ▶ $self->parser->parse($text)
Branch: 1 branch
BranchTrueFalseTotal
$self->parser->parse($text) or die $self->parser->error 129 0 129
82 or die $self->parser->error;
83 129 $self->compiler->namespace($self->namespace);
84 129 ▶ $self->compiler->compile($self->parser->parse_tree)
Branch: 1 branch
BranchTrueFalseTotal
$self->compiler->compile($self->parser->parse_tree) or die $self->compiler->error 129 0 129
85 or die $self->compiler->error;
86 }
87  
88  
89 7 require Language::FormulaEngine::Formula;
90 7 1;
91  
92 __END__
« FormulaEngine/Error.pm ↑ Summary FormulaEngine/Namespace/Default.pm »