}
}
+sub test_values {
+ my ($vars, $test) = @_;
+ sub {
+ my ($self, $state) = @_;
+ my @values = map $state->scope_var($_)->bound_value, @$vars;
+ if ($test->(@values)) {
+ return $state->then($self->next);
+ }
+ return $state->backtrack;
+ }
+}
+
my $op = FromCode->new(
code => bind_array(S => \@servers),
next => FromCode->new(
- code => sub {
- my ($self, $state) = @_;
- my $server = $state->scope_var('S')->bound_value;
- if ($server =~ /\.example\.com$/) {
- return $state->then($self->next);
- }
- return $state->backtrack;
- },
+ code => test_values([ 'S' ], sub { $_[0] =~ /\.example\.com$/ })
)
);
-my %scope = map +($_ => $_), qw(S);
-my %by_id = map +($_ => DX::Var->new(id => $_)), qw(S);
+sub make_state {
+ my ($vars, $op) = @_;
-my $state = DX::State->new(
- next_op => $op,
- return_stack => [],
- by_id => \%by_id,
- scope => \%scope,
- last_choice => []
-);
+ my %scope = map +($_ => $_), @{$vars};
+ my %by_id = map +($_ => DX::Var->new(id => $_)), @{$vars};
+
+ DX::State->new(
+ next_op => $op,
+ return_stack => [],
+ by_id => \%by_id,
+ scope => \%scope,
+ last_choice => []
+ );
+}
-my $stream = DX::ResultStream->new(for_state => $state);
+my $stream = DX::ResultStream->new(for_state => make_state([ 'S' ], $op));
is($stream->next->{'S'}, $_)
for qw(jim.example.com joe.example.com bob.example.com);
-dies_ok { $stream->next } 'No more';
+is($stream->next, undef, 'No more');
+
+my $complex_op = FromCode->new(
+ code => bind_array(S => \@servers),
+ next => FromCode->new(
+ code => test_values([ 'S' ], sub { $_[0] =~ /\.example\.com$/ }),
+ next => FromCode->new(
+ code => bind_array(P => \@shells),
+ next => FromCode->new(
+ code => test_values([ qw(S P) ], sub { $shells{$_[1]}{$_[0]} }),
+ )
+ )
+ )
+);
+
+my $cstream = DX::ResultStream->new(
+ for_state => make_state([ qw(S P) ], $complex_op)
+);
+
+is_deeply(
+ [ $cstream->results ],
+ [
+ { P => 'csh', S => 'jim.example.com' },
+ { P => 'csh', S => 'joe.example.com' },
+ { P => 'bash', S => 'joe.example.com' },
+ { P => 'csh', S => 'bob.example.com' },
+ ],
+ 'Complex stream'
+);
+
+my $call_op = FromCode->new(
+ code => sub {
+ my ($self, $state) = @_;
+ my @rst = @{$state->return_stack};
+ my $save_scope = $state->scope;
+ my %scope = (S => $save_scope->{S});
+ my $ret_op = FromCode->new(
+ code => sub { $_[1]->new(%{$_[1]}, scope => $save_scope, next_op => $_[0]->next) },
+ next => $self->next,
+ );
+ $state->new(%$state,
+ scope => \%scope,
+ return_stack => [ @rst, $ret_op ],
+ next_op => $op
+ );
+ },
+ next => FromCode->new(
+ code => bind_array(P => \@shells),
+ next => FromCode->new(
+ code => test_values([ qw(S P) ], sub { $shells{$_[1]}{$_[0]} }),
+ )
+ )
+);
+
+my $callstream = DX::ResultStream->new(
+ for_state => make_state([ qw(S P) ], $call_op)
+);
+
+is_deeply(
+ [ $callstream->results ],
+ [
+ { P => 'csh', S => 'jim.example.com' },
+ { P => 'csh', S => 'joe.example.com' },
+ { P => 'bash', S => 'joe.example.com' },
+ { P => 'csh', S => 'bob.example.com' },
+ ],
+ 'Call stream'
+);
done_testing;