936a8473fb993fb5e8583db78458dd2797905aed
[scpubgit/DKit.git] / t / basic.t
1 use strictures 1;
2 use Test::More;
3 use aliased 'DX::Op::FromCode';
4 use aliased 'DX::ArrayStream';
5 use DX::ResultStream;
6 use DX::Var;
7 use DX::State;
8 use Test::Exception;
9
10 my @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
18 my @shells = qw(csh bash);
19
20 my %shells = (
21   bash => { map +($_ => 1),
22              qw(joe.example.com kitty.scsys.co.uk) },
23   csh => { map +($_ => 1),
24              qw(jim.example.com joe.example.com bob.example.com) },
25 );
26
27 sub 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
39 sub test_values {
40   my ($vars, $test) = @_;
41   sub {
42     my ($self, $state) = @_;
43     my @values = map $state->scope_var($_)->bound_value, @$vars;
44     if ($test->(@values)) {
45       return $state->then($self->next);
46     }
47     return $state->backtrack;
48   }
49 }
50
51 sub make_op {
52   my ($inner) = @_;
53   FromCode->new(
54     code => bind_array(S => \@servers),
55     next => FromCode->new(
56       code => test_values([ 'S' ], sub { $_[0] =~ /\.example\.com$/ }),
57       next => $inner,
58     )
59   );
60 }
61
62 my $op = make_op;
63
64 sub make_state {
65   my ($vars, $op) = @_;
66
67   my %scope = map +($_ => $_), @{$vars};
68   my %by_id = map +($_ => DX::Var->new(id => $_)), @{$vars};
69
70   DX::State->new(
71     next_op => $op,
72     return_stack => [],
73     by_id => \%by_id,
74     scope => \%scope,
75     last_choice => []
76   );
77 }
78
79 my $stream = DX::ResultStream->new(for_state => make_state([ 'S' ], $op));
80
81 is($stream->next->{'S'}, $_)
82   for qw(jim.example.com joe.example.com bob.example.com);
83
84 is($stream->next, undef, 'No more');
85
86 my $complex_op = FromCode->new(
87   code => bind_array(S => \@servers),
88   next => FromCode->new(
89     code => test_values([ 'S' ], sub { $_[0] =~ /\.example\.com$/ }),
90     next => FromCode->new(
91       code => bind_array(P => \@shells),
92       next => FromCode->new(
93         code => test_values([ qw(S P) ], sub { $shells{$_[1]}{$_[0]} }),
94       )
95     )
96   )
97 );
98
99 my $cstream = DX::ResultStream->new(
100   for_state => make_state([ qw(S P) ], $complex_op)
101 );
102
103 is_deeply(
104   [ $cstream->results ],
105   [
106     { P => 'csh', S => 'jim.example.com' },
107     { P => 'csh', S => 'joe.example.com' },
108     { P => 'bash', S => 'joe.example.com' },
109     { P => 'csh', S => 'bob.example.com' },
110   ],
111   'Complex stream'
112 );
113
114 my $pop_stack = FromCode->new(
115   code => sub { $_[1]->pop_return_stack }
116 );
117
118 my $inner_op = make_op($pop_stack);
119
120 my $call_op = FromCode->new(
121   code => sub {
122     my ($self, $state) = @_;
123     my @rst = @{$state->return_stack};
124     my $save_scope = $state->scope;
125     my %scope = (S => $save_scope->{S});
126     my $ret_op = FromCode->new(
127       code => sub { $_[1]->but(scope => $save_scope, next_op => $_[0]->next) },
128       next => $self->next,
129     );
130     $state->but(
131       scope => \%scope,
132       return_stack => [ @rst, $ret_op ],
133       next_op => $inner_op
134     );
135   },
136   next => FromCode->new(
137     code => bind_array(P => \@shells),
138     next => FromCode->new(
139       code => test_values([ qw(S P) ], sub { $shells{$_[1]}{$_[0]} }),
140     )
141   )
142 );
143
144 my $callstream = DX::ResultStream->new(
145   for_state => make_state([ qw(S P) ], $call_op)
146 );
147
148 is_deeply(
149   [ $callstream->results ],
150   [
151     { P => 'csh', S => 'jim.example.com' },
152     { P => 'csh', S => 'joe.example.com' },
153     { P => 'bash', S => 'joe.example.com' },
154     { P => 'csh', S => 'bob.example.com' },
155   ],
156   'Call stream'
157 );
158
159 my $has_csh = FromCode->new(
160   code => test_values([ 'S' ], sub { $shells{csh}{$_[0]} }),
161   next => $pop_stack
162 );
163 my $has_bash = FromCode->new(
164   code => test_values([ 'S' ], sub { $shells{bash}{$_[0]} }),
165   next => $pop_stack
166 );
167
168 my $or_code = sub {
169   my ($self, $state) = @_;
170   my $var = DX::Var->new(id => 'OR')->with_stream(
171     my $stream = ArrayStream->from_array($has_csh, $has_bash)
172   );
173   my $inner_or = FromCode->new(
174     code => sub { $_[1]->then($var->bound_value) }
175   );
176   $state->but(
177     return_stack => [ @{$state->return_stack}, $self->next ],
178     next_op => $inner_or
179   )->mark_choice($var);
180 };
181
182 my $top_or = FromCode->new(
183   code => bind_array(S => \@servers),
184   next => FromCode->new(code => $or_code),
185 );
186
187 my $orstream = DX::ResultStream->new(
188   for_state => make_state([ qw(S) ], $top_or)
189 );
190
191 is_deeply(
192   [ $orstream->results ],
193   [
194     {
195       S => "kitty.scsys.co.uk"
196     },
197     {
198       S => "jim.example.com"
199     },
200     {
201       S => "joe.example.com"
202     },
203     {
204       S => "joe.example.com"
205     },
206     {
207       S => "bob.example.com"
208     }
209   ],
210   'Or stream'
211 );
212
213 my $top_or_2 = FromCode->new(
214   code => bind_array(S => \@servers),
215   next => FromCode->new(
216     code => $or_code,
217     next => FromCode->new(
218       code => test_values([ 'S' ], sub { $_[0] =~ /\.example\.com$/ }),
219     ),
220   ),
221 );
222
223 my $orstream_2 = DX::ResultStream->new(
224   for_state => make_state([ qw(S) ], $top_or_2)
225 );
226
227 is_deeply(
228   [ $orstream_2->results ],
229   [
230     {
231       S => "jim.example.com"
232     },
233     {
234       S => "joe.example.com"
235     },
236     {
237       S => "joe.example.com"
238     },
239     {
240       S => "bob.example.com"
241     }
242   ],
243   'Or stream'
244 );
245
246 done_testing;