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