blib/lib/Discord/Client/UI.pm

filter: Statementstmt 100% 64 / 64 Branchbran 84.2% 32 / 38 Conditioncond 61.9% 39 / 63 MC/DCmcdc 41.9% 13 / 31 Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 77.2% 163 / 211 CC 46 SCAR 25.6
CC
46
Cov
82%
text_input
23.6
modal
11.9
row
9.1
CRAP 12.9
« blib/lib/Discord/Client/Gateway/IdentifyLimiter.pm ↑ Summary blib/lib/Discord/Client/Cache.pm »
1 package Discord::Client::UI;
2  
3 4 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 our $VERSION = '0.004';
11 4 our @EXPORT_OK = qw(row button text_input modal);
12  
13 38 ▶sub _text {
Subroutine _text: called CC=5 SCAR=16.6
14 38 my ( $value, $maximum, $name ) = @_;
15  
16 # Validate character length before coercing a Perl scalar into a JSON string.
17 38 ▶ croak "$name must be a string of 1 to $maximum characters"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value and length $value >= 1 and length $value <= $maximum 2 36 38
Condition66%6/9
defined $value and not ref $value and length $value >= 1 and length $value <= $maximum
!ll&&!rl&&r
1136
defined $value and not ref $value and length $value >= 1
!ll&&!rl&&r
0137
defined $value and not ref $value
!ll&&!rl&&r
0038
MC/DC50%2/4
defined $value and not ref $value and length $value >= 1 and length $value <= $maximum
defined $value not ref $value length $value >= 1 length $value <= $maximum
Truth table60%3/5
A and not(B) and C and D
A= defined $valueB= not ref $valueC= length $value >= 1D= length $value <= $maximum
ABCDresult
0XXX0
10XX0
110X0
11100
11111
18 unless defined($value)
19 && !ref($value)
20 && length($value) >= 1
21 && length($value) <= $maximum;
22 36 return "$value";
23 }
24  
25 14 ▶sub button {
Subroutine button: called CC=8 SCAR=20.9
Pod documented
26 14 my ( $custom_id, %options ) = @_;
27  
28 # Named styles keep numeric Discord component values out of application code.
29 14 my %styles = ( primary => 1, secondary => 2, success => 3, danger => 4 );
30 14 ▶ my $style = delete( $options{style} ) // 'primary';
Condition100%2/2
delete $options{"style"} // "primary"
l!l
212
MC/DC100%1/1
delete $options{"style"} // "primary"
delete $options{"style"}
Truth table100%2/2
A
A= delete $options{"style"}
Aresult
11
00
31 14 ▶ croak 'Unknown button style' unless !ref($style) && exists $styles{$style};
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $style and exists $styles{$style} 1 13 14
Condition66%2/3
not ref $style and exists $styles{$style}
!ll&&!rl&&r
0113
MC/DC50%1/2
not ref $style and exists $styles{$style}
not ref $style exists $styles{$style}
Truth table66%2/3
not(A) and B
A= not ref $styleB= exists $styles{$style}
ABresult
0X0
100
111
32 croak 'Use native JSON for link or premium buttons'
33 13 ▶ if exists( $options{url} ) || exists( $options{sku_id} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"url"} or exists $options{"sku_id"} 1 12 13
Condition66%2/3
exists $options{"url"} or exists $options{"sku_id"}
l!l&&r!l&&!r
1012
MC/DC50%1/2
exists $options{"url"} or exists $options{"sku_id"}
exists $options{"url"} exists $options{"sku_id"}
Truth table66%2/3
A or B
A= exists $options{"url"}B= exists $options{"sku_id"}
ABresult
1X1
011
000
34  
35 # Normalize optional wire fields before constructing the component payload.
36 12 $options{label} = _text( $options{label}, 80, 'Button label' );
37 $options{disabled} = $options{disabled} ? JSON::PP::true : JSON::PP::false
38 11 ▶ if exists $options{disabled};
Branch: 2 branches
BranchTrueFalseTotal
if exists $options{"disabled"} 1 10 11
$options{'disabled'} ? : 1 0 1
39 return {
40 %options,
41 type => 2,
42 11 style => $styles{$style},
43 custom_id => _text( $custom_id, 100, 'Button ID' ),
44 };
45 }
46  
47 8 ▶sub row {
Subroutine row: called CC=9 SCAR=22.0
Pod documented
48 8 my (@components) = @_;
49  
50 # Message Action Rows contain up to five buttons or exactly one select menu.
51 8 ▶ croak 'An action row requires one to five components' unless @components >= 1 && @components <= 5;
Branch: 1 branch
BranchTrueFalseTotal
unless @components >= 1 and @components <= 5 2 6 8
Condition100%3/3
@components >= 1 and @components <= 5
!ll&&!rl&&r
116
MC/DC100%2/2
@components >= 1 and @components <= 5
@components >= 1 @components <= 5
Truth table100%3/3
A and B
A= @components >= 1B= @components <= 5
ABresult
0X0
100
111
52 6 for my $component (@components) {
53 croak 'Invalid action row component'
54 unless ref($component) eq 'HASH'
55 6 ▶ && ( $component->{type} // '' ) =~ /\A[235678]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $component eq "HASH" and ($component->{"type"} // "") =~ /\A[235678]\z/u 1 5 6
Condition60%3/5
ref $component eq "HASH" and ($component->{"type"} // "") =~ /\A[235678]\z/u
!ll&&!rl&&r
015
$component->{"type"} // ""
l!l
60
MC/DC0%0/2
ref $component eq "HASH" and ($component->{"type"} // "") =~ /\A[235678]\z/u
ref $component eq "HASH" $component->{"type"}
Truth table33%1/3
A and B
A= ref $component eq "HASH"B= $component->{"type"}
ABresult
0X0
100
111
56 croak 'A select menu must occupy its own action row'
57 5 ▶ if $component->{type} != 2 && @components != 1;
Branch: 1 branch
BranchTrueFalseTotal
if $component->{"type"} != 2 and @components != 1 1 4 5
Condition100%3/3
$component->{"type"} != 2 and @components != 1
!ll&&!rl&&r
311
MC/DC100%2/2
$component->{"type"} != 2 and @components != 1
$component->{"type"} != 2 @components != 1
Truth table100%3/3
A and B
A= $component->{"type"} != 2B= @components != 1
ABresult
0X0
100
111
58 }
59 4 return { type => 1, components => \@components };
60 }
61  
62 4 ▶sub text_input {
Subroutine text_input: called CC=18 SCAR=31.6
Pod documented
63 4 my ( $custom_id, %options ) = @_;
64  
65 # Modal Text Inputs belong inside Labels; callers describe only the field.
66 4 my $label = _text( delete( $options{label} ), 45, 'Input label' );
67 4 my $description = delete $options{description};
68 4 my %styles = ( short => 1, paragraph => 2 );
69 4 ▶ my $style = delete( $options{style} ) // 'short';
Condition100%2/2
delete $options{"style"} // "short"
l!l
22
MC/DC100%1/1
delete $options{"style"} // "short"
delete $options{"style"}
Truth table100%2/2
A
A= delete $options{"style"}
Aresult
11
00
70 4 ▶ croak 'Unknown text input style' unless !ref($style) && exists $styles{$style};
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $style and exists $styles{$style} 0 4 4
Condition33%1/3
not ref $style and exists $styles{$style}
!ll&&!rl&&r
004
MC/DC0%0/2
not ref $style and exists $styles{$style}
not ref $style exists $styles{$style}
Truth table33%1/3
not(A) and B
A= not ref $styleB= exists $styles{$style}
ABresult
0X0
100
111
71  
72 # Normalize optional bounds before assembling the nested Label and Text Input.
73 4 for my $name (qw(min_length max_length)) {
74 8 ▶ next unless exists $options{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{$name} 3 5 8
75 5 ▶ my $minimum = $name eq 'min_length' ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$name eq 'min_length' ? : 2 3 5
76 5 my $value = $options{$name};
77 5 ▶ croak "Invalid $name"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value and $value =~ /\A\d+\z/u and $value >= $minimum and $value <= 4000 0 5 5
Condition33%4/12
defined $value and not ref $value and $value =~ /\A\d+\z/u and $value >= $minimum and $value <= 4000
!ll&&!rl&&r
005
defined $value and not ref $value and $value =~ /\A\d+\z/u and $value >= $minimum
!ll&&!rl&&r
005
defined $value and not ref $value and $value =~ /\A\d+\z/u
!ll&&!rl&&r
005
defined $value and not ref $value
!ll&&!rl&&r
005
MC/DC0%0/5
defined $value and not ref $value and $value =~ /\A\d+\z/u and $value >= $minimum and $value <= 4000
defined $value not ref $value $value =~ /\A\d+\z/u $value >= $minimum $value <= 4000
Truth table16%1/6
A and not(B) and C and D and E
A= defined $valueB= not ref $valueC= $value =~ /\A\d+\z/uD= $value >= $minimumE= $value <= 4000
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
78 unless defined($value)
79 && !ref($value)
80 && $value =~ /\A\d+\z/
81 && $value >= $minimum
82 && $value <= 4000;
83 5 $options{$name} = 0 + $value;
84 }
85 croak 'min_length exceeds max_length'
86 4 ▶ if ( $options{min_length} // 0 ) > ( $options{max_length} // 4000 );
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"min_length"} // 0) > ($options{"max_length"} // 4000) 1 3 4
Condition100%4/4
$options{"min_length"} // 0
l!l
22
$options{"max_length"} // 4000
l!l
31
MC/DC100%1/1
$options{"min_length"} // 0
$options{"min_length"}
MC/DC100%1/1
$options{"max_length"} // 4000
$options{"max_length"}
Truth table100%2/2
A
A= $options{"min_length"}
Aresult
11
00
Truth table100%2/2
A
A= $options{"max_length"}
Aresult
11
00
87 $options{required} = $options{required} ? JSON::PP::true : JSON::PP::false
88 3 ▶ if exists $options{required};
Branch: 2 branches
BranchTrueFalseTotal
if exists $options{"required"} 1 2 3
$options{'required'} ? : 0 1 1
89  
90 # Keep Label metadata separate from the nested input properties.
91 my $field = {
92 type => 18,
93 label => $label,
94 component => {
95 %options,
96 type => 4,
97 3 style => $styles{$style},
98 custom_id => _text( $custom_id, 100, 'Input ID' ),
99 },
100 };
101 3 ▶ $field->{description} = _text( $description, 100, 'Input description' ) if defined $description;
Branch: 1 branch
BranchTrueFalseTotal
if defined $description 0 3 3
102 3 return $field;
103 }
104  
105 4 ▶sub modal {
Subroutine modal: called CC=10 SCAR=24.8
Pod documented
106 4 my ( $custom_id, %options ) = @_;
107 4 my $fields = delete $options{fields};
108  
109 # Build the native payload expected by Interaction->modal without sending it.
110 4 ▶ croak 'A modal requires one to five fields'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fields eq "ARRAY" and @$fields >= 1 and @$fields <= 5 1 3 4
Condition66%4/6
ref $fields eq "ARRAY" and @$fields >= 1 and @$fields <= 5
!ll&&!rl&&r
103
ref $fields eq "ARRAY" and @$fields >= 1
!ll&&!rl&&r
013
MC/DC33%1/3
ref $fields eq "ARRAY" and @$fields >= 1 and @$fields <= 5
ref $fields eq "ARRAY" @$fields >= 1 @$fields <= 5
Truth table50%2/4
A and B and C
A= ref $fields eq "ARRAY"B= @$fields >= 1C= @$fields <= 5
ABCresult
0XX0
10X0
1100
1111
111 unless ref($fields) eq 'ARRAY' && @$fields >= 1 && @$fields <= 5;
112  
113 # Reject duplicate input IDs so field lookup identifies exactly one submitted value.
114 3 my %ids;
115 3 for my $field (@$fields) {
116 croak 'A modal field must be a Label'
117 unless ref($field) eq 'HASH'
118 && ( $field->{type} // 0 ) == 18
119 4 ▶ && ref( $field->{component} ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $field eq "HASH" and ($field->{"type"} // 0) == 18 and ref $field->{"component"} eq "HASH" 0 4 4
Condition37%3/8
ref $field eq "HASH" and ($field->{"type"} // 0) == 18 and ref $field->{"component"} eq "HASH"
!ll&&!rl&&r
004
ref $field eq "HASH" and ($field->{"type"} // 0) == 18
!ll&&!rl&&r
004
$field->{"type"} // 0
l!l
40
MC/DC0%0/3
ref $field eq "HASH" and ($field->{"type"} // 0) == 18 and ref $field->{"component"} eq "HASH"
ref $field eq "HASH" $field->{"type"} ref $field->{"component"} eq "HASH"
Truth table25%1/4
A and B and C
A= ref $field eq "HASH"B= $field->{"type"}C= ref $field->{"component"} eq "HASH"
ABCresult
0XX0
10X0
1100
1111
120 4 my $id = _text( $field->{component}{custom_id}, 100, 'Input ID' );
121 4 ▶ croak 'Duplicate modal field ID' if $ids{$id};
Branch: 1 branch
BranchTrueFalseTotal
if $ids{$id} 1 3 4
122 3 $ids{$id} = 1;
123 }
124 return {
125 %options,
126 custom_id => _text( $custom_id, 100, 'Modal ID' ),
127 2 title => _text( $options{title}, 45, 'Modal title' ),
128 components => $fields,
129 };
130 }
131  
132 4 1;
133  
134 __END__
« blib/lib/Discord/Client/Gateway/IdentifyLimiter.pm ↑ Summary blib/lib/Discord/Client/Cache.pm »