move backtracking into a stream
[scpubgit/DKit.git] / t / basic.t
CommitLineData
60cda014 1use strictures 1;
2use Test::More;
3use aliased 'DX::Op::FromCode';
4use aliased 'DX::ArrayStream';
94565614 5use DX::ResultStream;
60cda014 6use DX::Var;
7use DX::State;
94565614 8use Test::Exception;
60cda014 9
10my @servers = qw(
11 kitty.scsys.co.uk
12 jim.example.com
13 joe.example.com
14 pryde.scsys.co.uk
15 bob.example.com
16);
17
18my @shells = qw(csh bash);
19
20my %shells = (
21 bash => { map +($_ => 1),
22 qw(joe.example.com kitty.scsys.co.uk pryde.scsys.co.uk) },
23 csh => { map +($_ => 1),
24 qw(jim.example.com joe.example.com bob.example.com) },
25);
26
27sub bind_array {
28 my ($var, $array) = @_;
29 sub {
30 my ($self, $state) = @_;
31 $state->bind_stream_then(
32 $state->scope_var($var),
33 ArrayStream->from_array(@$array),
34 $self->next
35 )
36 }
37}
38
39my $op = FromCode->new(
40 code => bind_array(S => \@servers),
41 next => FromCode->new(
42 code => sub {
43 my ($self, $state) = @_;
44 my $server = $state->scope_var('S')->bound_value;
45 if ($server =~ /\.example\.com$/) {
46 return $state->then($self->next);
47 }
48 return $state->backtrack;
49 },
50 )
51);
52
53my %scope = map +($_ => $_), qw(S);
54my %by_id = map +($_ => DX::Var->new(id => $_)), qw(S);
55
56my $state = DX::State->new(
57 next_op => $op,
58 return_stack => [],
59 by_id => \%by_id,
60 scope => \%scope,
61 last_choice => []
62);
63
94565614 64my $stream = DX::ResultStream->new(for_state => $state);
65
66is($stream->next->{'S'}, $_)
67 for qw(jim.example.com joe.example.com bob.example.com);
60cda014 68
94565614 69dies_ok { $stream->next } 'No more';
60cda014 70
71done_testing;