BoardStreams/Diff.pm

filter: Statementstmt 100% 61 / 61 Branchbran 100% 38 / 38 Conditioncond 100% 35 / 35 MC/DCmcdc 100% 18 / 18 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 2 totaltotal 98.7% 161 / 163 CC 37 SCAR 23.7
CC
37
Cov
100%
diff
16.0
patch
12.0
_array_diff
7.0
CRAP 10.6
« BoardStreams/Hub.pm ↑ Summary BoardStreams/Client/Membership.pm »
1 package BoardStreams::Diff;
2 4 ▶use v5.44;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 4 our $VERSION = "v0.0.59";
5  
6 4 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Mojo::JSON qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 our @EXPORT_OK = qw(diff patch);
10  
11 # A diff is undef (no change) or a hash with one of these shapes:
12 # { v => $new } replace the value
13 # { o => { $key => $subdiff, ... }, change keys of an object
14 # d => [ $deleted_key, ... ] }
15 # { s => [ $start, $end ], change the ends of an array: keep
16 # p => [ $item, ... ], the old items from index $start up
17 # a => [ $item, ... ] } to, not including, $end (all of
18 # them without "s"), put the "p"
19 # items before them and the "a"
20 # items after them
21 # Each of "s", "p" and "a" is left out when it has nothing to say, so a
22 # push is { a => [...] }, a pop { s => [0, $n - 1] }, a shift
23 # { s => [1, $n] } and an unshift { p => [...] }, and they combine.
24 # The JavaScript client implements the same format in src/diff.ts.
25  
26 # How many places a run of old items is looked for in the new array.
27 4 ▶use constant ARRAY_TRIES => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 46 ▶sub diff ($old, $new) {
Subroutine diff: called CC=16 SCAR=27.7
Pod undocumented
30 46 ▶ if (ref $old eq 'HASH' and ref $new eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $old eq "HASH" and ref $new eq "HASH") 15 31 46
Condition100%3/3
ref $old eq "HASH" and ref $new eq "HASH"
!ll&&!rl&&r
30115
MC/DC100%2/2
ref $old eq "HASH" and ref $new eq "HASH"
ref $old eq "HASH" ref $new eq "HASH"
Truth table100%3/3
A and B
A= ref $old eq "HASH"B= ref $new eq "HASH"
ABresult
0X0
100
111
31 15 my %changed;
32 15 for my $key (keys %$new) {
33 19 ▶ if (exists $old->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $old->{$key}) { } 18 1 19
34 18 my $sub = diff($old->{$key}, $new->{$key});
35 18 ▶ $changed{$key} = $sub if $sub;
Branch: 1 branch
BranchTrueFalseTotal
if $sub 13 5 18
36 }
37 else {
38 1 $changed{$key} = { v => $new->{$key} };
39 }
40 }
41 15 my @deleted = sort grep { !exists $new->{$_} } keys %$old;
42 15 ▶ return undef unless %changed or @deleted;
Branch: 1 branch
BranchTrueFalseTotal
unless %changed or @deleted 1 14 15
Condition100%3/3
%changed or @deleted
l!l&&r!l&&!r
1311
MC/DC100%2/2
%changed or @deleted
%changed @deleted
Truth table100%3/3
A or B
A= %changedB= @deleted
ABresult
1X1
011
000
43 return {
44 14 ▶ (%changed ? (o => \%changed) : ()),
Branch: 2 branches
BranchTrueFalseTotal
%changed ? : 13 1 14
@deleted ? : 1 13 14
45 (@deleted ? (d => \@deleted) : ()),
46 };
47 }
48  
49 31 ▶ return undef if _json($old) eq _json($new);
Branch: 1 branch
BranchTrueFalseTotal
if _json($old) eq _json($new) 5 26 31
50  
51 26 ▶ if (ref $old eq 'ARRAY' and ref $new eq 'ARRAY' and @$old and @$new) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $old eq "ARRAY" and ref $new eq "ARRAY" and @$old and @$new) 19 7 26
Condition100%9/9
ref $old eq "ARRAY" and ref $new eq "ARRAY" and @$old and @$new
!ll&&!rl&&r
6119
ref $old eq "ARRAY" and ref $new eq "ARRAY" and @$old
!ll&&!rl&&r
5120
ref $old eq "ARRAY" and ref $new eq "ARRAY"
!ll&&!rl&&r
4121
MC/DC100%4/4
ref $old eq "ARRAY" and ref $new eq "ARRAY" and @$old and @$new
ref $old eq "ARRAY" ref $new eq "ARRAY" @$old @$new
Truth table100%5/5
A and B and C and D
A= ref $old eq "ARRAY"B= ref $new eq "ARRAY"C= @$oldD= @$new
ABCDresult
0XXX0
10XX0
110X0
11100
11111
52 19 my $ends = _array_diff($old, $new);
53 19 ▶ return $ends if $ends;
Branch: 1 branch
BranchTrueFalseTotal
if $ends 17 2 19
54 }
55  
56 9 return { v => $new };
57 }
58  
59 # The diff for an array whose ends changed while a run of its items stayed
60 # as it was: what push, pop, shift and unshift do, alone or together.
61 # Returns nothing if no such run is found. The run has to start with the
62 # first old item or with the first new one, which leaves out only an array
63 # that lost items at its start and gained others there at the same time.
64 19 ▶sub _array_diff ($old, $new) {
Subroutine _array_diff: called CC=7 SCAR=19.5
65 19 my @was = map { _json($_) } @$old;
66 19 my @is = map { _json($_) } @$new;
67 19 my ($kept, $from, $at) = (0, 0, 0); # length of the run, where it starts in old, and in new
68 21 ▶ my $consider = sub ($start, $position) {
Subroutine __ANON__: called CC=5 SCAR=16.1
69 21 my $run = 0;
70 21 ▶ $run++ while $start + $run < @was and $position + $run < @is and $was[$start + $run] eq $is[$position + $run];
Branch: 1 branch
BranchTrueFalseTotal
$start + $run < @was and $position + $run < @is and $was[$start + $run] eq $is[$position + $run] and do { ++$run } 36 21 57
Condition100%6/6
$start + $run < @was and $position + $run < @is and $was[$start + $run] eq $is[$position + $run]
!ll&&!rl&&r
18336
$start + $run < @was and $position + $run < @is
!ll&&!rl&&r
13539
MC/DC100%3/3
$start + $run < @was and $position + $run < @is and $was[$start + $run] eq $is[$position + $run]
$start + $run < @was $position + $run < @is $was[$start + $run] eq $is[$position + $run]
Truth table100%4/4
A and B and C
A= $start + $run < @wasB= $position + $run < @isC= $was[$start + $run] eq $is[$position + $run]
ABCresult
0XX0
10X0
1100
1111
71 21 ▶ ($kept, $from, $at) = ($run, $start, $position) if $run > $kept;
Branch: 1 branch
BranchTrueFalseTotal
if $run > $kept 18 3 21
72 19 };
73 # The old array from its first item on, wherever that is now: at the
74 # start (push, pop) or further in (unshift).
75 19 my @positions = grep { $is[$_] eq $was[0] } 0 .. $#is;
76 19 $consider->(0, $_) for splice @positions, 0, ARRAY_TRIES;
77 # The new array from its first item on, wherever that was: shift.
78 19 my @starts = grep { $was[$_] eq $is[0] } 1 .. $#was;
79 19 $consider->($_, 0) for splice @starts, 0, ARRAY_TRIES;
80 19 ▶ return undef unless $kept;
Branch: 1 branch
BranchTrueFalseTotal
unless $kept 2 17 19
81 return {
82 17 ▶ ($kept < @was ? (s => [$from, $from + $kept]) : ()),
Branch: 3 branches
BranchTrueFalseTotal
$kept < @was ? : 11 6 17
$at ? : 4 13 17
$at + $kept < @is ? : 7 10 17
83 ($at ? (p => [@$new[0 .. $at - 1]]) : ()),
84 ($at + $kept < @is ? (a => [@$new[$at + $kept .. $#$new]]) : ()),
85 };
86 }
87  
88 54 ▶sub patch ($value, $diff) {
Subroutine patch: called CC=12 SCAR=24.8
Pod undocumented
89 54 ▶ return $value unless $diff;
Branch: 1 branch
BranchTrueFalseTotal
unless $diff 1 53 54
90 53 ▶ return $diff->{v} if exists $diff->{v};
Branch: 1 branch
BranchTrueFalseTotal
if exists $diff->{"v"} 16 37 53
91 37 ▶ if ($diff->{s} or $diff->{p} or $diff->{a}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff->{"s"} or $diff->{"p"} or $diff->{"a"}) 17 20 37
Condition100%6/6
$diff->{"s"} or $diff->{"p"} or $diff->{"a"}
l!l&&r!l&&!r
14320
$diff->{"s"} or $diff->{"p"}
l!l&&r!l&&!r
11323
MC/DC100%3/3
$diff->{"s"} or $diff->{"p"} or $diff->{"a"}
$diff->{"s"} $diff->{"p"} $diff->{"a"}
Truth table100%4/4
A or B or C
A= $diff->{"s"}B= $diff->{"p"}C= $diff->{"a"}
ABCresult
1XX1
01X1
0011
0000
92 17 ▶ my ($start, $end) = $diff->{s} ? $diff->{s}->@* : (0, scalar @$value);
Branch: 1 branch
BranchTrueFalseTotal
$diff->{'s'} ? : 11 6 17
93 17 ▶ return [@{ $diff->{p} // [] }, @$value[$start .. $end - 1], @{ $diff->{a} // [] }];
Condition100%4/4
$diff->{"p"} // []
l!l
413
$diff->{"a"} // []
l!l
710
MC/DC100%1/1
$diff->{"p"} // []
$diff->{"p"}
MC/DC100%1/1
$diff->{"a"} // []
$diff->{"a"}
Truth table100%2/2
A
A= $diff->{"p"}
Aresult
11
00
Truth table100%2/2
A
A= $diff->{"a"}
Aresult
11
00
94 }
95 20 my %copy = %$value;
96 20 ▶ delete @copy{ @{ $diff->{d} // [] } };
Condition100%2/2
$diff->{"d"} // []
l!l
119
MC/DC100%1/1
$diff->{"d"} // []
$diff->{"d"}
Truth table100%2/2
A
A= $diff->{"d"}
Aresult
11
00
97 20 ▶ my $changed = $diff->{o} // {};
Condition100%2/2
$diff->{"o"} // {}
l!l
191
MC/DC100%1/1
$diff->{"o"} // {}
$diff->{"o"}
Truth table100%2/2
A
A= $diff->{"o"}
Aresult
11
00
98 20 $copy{$_} = patch($copy{$_}, $changed->{$_}) for keys %$changed;
99 20 return \%copy;
100 }
101  
102 # Mojo::JSON sorts hash keys, so equal structures encode identically.
103 164 ▶sub _json ($value) { encode_json([$value]) }
Subroutine _json: called CC=1 SCAR=0.0
104  
105 4 1;
« BoardStreams/Hub.pm ↑ Summary BoardStreams/Client/Membership.pm »