File Coverage

blib/lib/RxPerl/Test.pm
Criterion Covered Total %
statement 65 65 100.0
branch 18 20 90.0
condition 2 2 100.0
subroutine 12 12 100.0
pod 0 3 0.0
total 97 102 95.1


line stmt bran cond sub pod time code
1             package RxPerl::Test;
2              
3 4     4   941075 use strict;
  4         27  
  4         117  
4 4     4   26 use warnings;
  4         5  
  4         103  
5              
6 4     4   1662 use RxPerl::SyncTimers ':all';
  4         13  
  4         2139  
7              
8 4     4   30 use Carp 'croak';
  4         18  
  4         192  
9 4     4   55 use Test2::V0;
  4         19  
  4         30  
10              
11 4     4   5267 use Exporter 'import';
  4         13  
  4         3203  
12             our @EXPORT = qw/ obs_is cold /;
13              
14             our $VERSION = "v6.28.0";
15              
16             sub cold {
17 188     188 0 173157 my ($marble, $mapping) = @_;
18              
19 188         600 $marble =~ s/\-\z/\|/;
20 188   100     814 $mapping //= {};
21              
22             # Syntax check
23 188 50       2077 croak 'Invalid syntax in marble diagram of cold' if $marble !~ /^((?:\((?:[a-z0-9|#])+?\))|[a-z0-9|#-])*\z/gx;
24 188         488 pos $marble = 0;
25              
26 188         1291 my @tokens = $marble =~ /[a-z0-9\(\)\-\|\#]/g;
27              
28 188         284 my @components;
29 188         336 my $time = 0;
30 188         236 my $have_waited = 0;
31 188         232 my $in_brackets = 0;
32              
33 188         456 TOKEN: for (my $i = 0; $i < @tokens; $i++) {
34 1474         2349 my $token = $tokens[$i];
35 1474 100       4214 if ($token =~ /^[a-z0-9]\z/) {
    100          
    100          
    100          
    100          
    50          
36 668 100       1411 $token = $mapping->{$token} if exists $mapping->{$token};
37 668         1504 push @components, rx_of($token)->pipe(op_delay($time));
38 668         950 $have_waited = $time;
39 668 100       1921 $time++ unless $in_brackets;
40             } elsif ($token eq '(') {
41 28         71 $in_brackets = 1;
42             } elsif ($token eq ')') {
43 27         44 $in_brackets = 0;
44 27         86 $time++;
45             } elsif ($token eq '-') {
46 688         1284 $time++;
47             } elsif ($token eq '|') {
48 44 100       105 if ($time > $have_waited) {
49 43         122 push @components, rx_timer($time)->pipe(op_ignore_elements);
50             }
51 44         120 last TOKEN;
52             } elsif ($token eq '#') {
53 19         52 push @components, rx_concat(
54             rx_timer($time)->pipe(op_ignore_elements),
55             rx_throw_error,
56             );
57 19         55 last TOKEN;
58             }
59             }
60              
61 188         449 return rx_merge(@components);
62             }
63              
64             sub get_timeline {
65 234     234 0 373 my ($observable) = @_;
66              
67 234         319 my %timeline;
68              
69 234         701 RxPerl::SyncTimers->reset;
70              
71             $observable->subscribe({
72             next => sub {
73 760     760   1370 my ($value) = @_;
74              
75 760         1136 my $time = $RxPerl::SyncTimers::time;
76 760         929 push @{$timeline{$time}}, { next => $value };
  760         2986  
77             },
78             error => sub {
79 20     20   33 my ($error) = @_;
80              
81 20         31 my $time = $RxPerl::SyncTimers::time;
82 20         32 push @{$timeline{$time}}, { error => $error };
  20         69  
83             },
84             complete => sub {
85 214     214   322 my $time = $RxPerl::SyncTimers::time;
86 214         282 push @{$timeline{$time}}, {complete => undef};
  214         618  
87             },
88 234         1812 });
89              
90 234         1034 RxPerl::SyncTimers->start;
91              
92 234         757 return \%timeline;
93             }
94              
95             sub obs_is {
96 117     117 0 9458 my ($o, $expected, $name) = @_;
97              
98 117         212 my ($marble, $mapping) = @$expected;
99              
100 117         226 return is(get_timeline($o), get_timeline(cold($marble, $mapping)), $name);
101             }
102              
103             1;