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   701150 use strict;
  4         9  
  4         154  
4 4     4   41 use warnings;
  4         12  
  4         239  
5              
6 4     4   1981 use RxPerl::SyncTimers ':all';
  4         16  
  4         2331  
7              
8 4     4   42 use Carp 'croak';
  4         10  
  4         267  
9 4     4   60 use Test2::V0;
  4         9  
  4         42  
10              
11 4     4   5497 use Exporter 'import';
  4         9  
  4         3641  
12             our @EXPORT = qw/ obs_is cold /;
13              
14             our $VERSION = "v6.29.8";
15              
16             sub cold {
17 198     198 0 195291 my ($marble, $mapping) = @_;
18              
19 198         633 $marble =~ s/\-\z/\|/;
20 198   100     809 $mapping //= {};
21              
22             # Syntax check
23 198 50       3223 croak 'Invalid syntax in marble diagram of cold' if $marble !~ /^((?:\((?:[a-z0-9|#])+?\))|[a-z0-9|#-])*\z/gx;
24 198         666 pos $marble = 0;
25              
26 198         1642 my @tokens = $marble =~ /[a-z0-9\(\)\-\|\#]/g;
27              
28 198         301 my @components;
29 198         280 my $time = 0;
30 198         283 my $have_waited = 0;
31 198         254 my $in_brackets = 0;
32              
33 198         544 TOKEN: for (my $i = 0; $i < @tokens; $i++) {
34 1531         2253 my $token = $tokens[$i];
35 1531 100       4225 if ($token =~ /^[a-z0-9]\z/) {
    100          
    100          
    100          
    100          
    50          
36 704 100       1463 $token = $mapping->{$token} if exists $mapping->{$token};
37 704         1742 push @components, rx_of($token)->pipe(op_delay($time));
38 704         942 $have_waited = $time;
39 704 100       1957 $time++ unless $in_brackets;
40             } elsif ($token eq '(') {
41 32         79 $in_brackets = 1;
42             } elsif ($token eq ')') {
43 31         100 $in_brackets = 0;
44 31         78 $time++;
45             } elsif ($token eq '-') {
46 700         1337 $time++;
47             } elsif ($token eq '|') {
48 44 100       152 if ($time > $have_waited) {
49 43         181 push @components, rx_timer($time)->pipe(op_ignore_elements);
50             }
51 44         183 last TOKEN;
52             } elsif ($token eq '#') {
53 20         73 push @components, rx_concat(
54             rx_timer($time)->pipe(op_ignore_elements),
55             rx_throw_error,
56             );
57 20         63 last TOKEN;
58             }
59             }
60              
61 198         629 return rx_merge(@components);
62             }
63              
64             sub get_timeline {
65 254     254 0 437 my ($observable) = @_;
66              
67 254         351 my %timeline;
68              
69 254         1027 RxPerl::SyncTimers->reset;
70              
71             $observable->subscribe({
72             next => sub {
73 832     832   1439 my ($value) = @_;
74              
75 832         1153 my $time = $RxPerl::SyncTimers::time;
76 832         969 push @{$timeline{$time}}, { next => $value };
  832         3254  
77             },
78             error => sub {
79 22     22   39 my ($error) = @_;
80              
81 22         35 my $time = $RxPerl::SyncTimers::time;
82 22         32 push @{$timeline{$time}}, { error => $error };
  22         72  
83             },
84             complete => sub {
85 232     232   330 my $time = $RxPerl::SyncTimers::time;
86 232         396 push @{$timeline{$time}}, {complete => undef};
  232         885  
87             },
88 254         2541 });
89              
90 254         1417 RxPerl::SyncTimers->start;
91              
92 254         1186 return \%timeline;
93             }
94              
95             sub obs_is {
96 127     127 0 7446 my ($o, $expected, $name) = @_;
97              
98 127         249 my ($marble, $mapping) = @$expected;
99              
100 127         351 return is(get_timeline($o), get_timeline(cold($marble, $mapping)), $name);
101             }
102              
103             1;