stop using return_stack directly in tests
[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),
46894e63 22 qw(joe.example.com kitty.scsys.co.uk) },
60cda014 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
5622b4df 39sub 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
54817920 51sub 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
62my $op = make_op;
60cda014 63
5622b4df 64sub make_state {
65 my ($vars, $op) = @_;
60cda014 66
5622b4df 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}
60cda014 78
5622b4df 79my $stream = DX::ResultStream->new(for_state => make_state([ 'S' ], $op));
94565614 80
81is($stream->next->{'S'}, $_)
82 for qw(jim.example.com joe.example.com bob.example.com);
60cda014 83
5622b4df 84is($stream->next, undef, 'No more');
85
5622b4df 86my $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
99my $cstream = DX::ResultStream->new(
100 for_state => make_state([ qw(S P) ], $complex_op)
101);
102
26300a7d 103is_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);
60cda014 113
46894e63 114my $pop_stack = FromCode->new(
b40d5c51 115 code => sub { $_[1]->pop_return_stack }
46894e63 116);
117
118my $inner_op = make_op($pop_stack);
54817920 119
71d26209 120my $call_op = FromCode->new(
121 code => sub {
122 my ($self, $state) = @_;
71d26209 123 my $save_scope = $state->scope;
124 my %scope = (S => $save_scope->{S});
125 my $ret_op = FromCode->new(
03079510 126 code => sub { $_[1]->but(scope => $save_scope, next_op => $_[0]->next) },
71d26209 127 next => $self->next,
128 );
12face77 129 $state->but(scope => \%scope)->push_return_then($ret_op, $inner_op);
71d26209 130 },
131 next => FromCode->new(
132 code => bind_array(P => \@shells),
133 next => FromCode->new(
134 code => test_values([ qw(S P) ], sub { $shells{$_[1]}{$_[0]} }),
135 )
136 )
137);
138
139my $callstream = DX::ResultStream->new(
140 for_state => make_state([ qw(S P) ], $call_op)
141);
142
143is_deeply(
144 [ $callstream->results ],
145 [
146 { P => 'csh', S => 'jim.example.com' },
147 { P => 'csh', S => 'joe.example.com' },
148 { P => 'bash', S => 'joe.example.com' },
149 { P => 'csh', S => 'bob.example.com' },
150 ],
151 'Call stream'
152);
153
46894e63 154my $has_csh = FromCode->new(
155 code => test_values([ 'S' ], sub { $shells{csh}{$_[0]} }),
156 next => $pop_stack
157);
158my $has_bash = FromCode->new(
159 code => test_values([ 'S' ], sub { $shells{bash}{$_[0]} }),
160 next => $pop_stack
161);
162
163my $or_code = sub {
164 my ($self, $state) = @_;
165 my $var = DX::Var->new(id => 'OR')->with_stream(
166 my $stream = ArrayStream->from_array($has_csh, $has_bash)
167 );
168 my $inner_or = FromCode->new(
169 code => sub { $_[1]->then($var->bound_value) }
170 );
12face77 171 $state->push_return_then($self->next, $inner_or)
172 ->mark_choice($var);
46894e63 173};
174
175my $top_or = FromCode->new(
176 code => bind_array(S => \@servers),
177 next => FromCode->new(code => $or_code),
178);
179
180my $orstream = DX::ResultStream->new(
181 for_state => make_state([ qw(S) ], $top_or)
182);
183
184is_deeply(
185 [ $orstream->results ],
186 [
187 {
188 S => "kitty.scsys.co.uk"
189 },
190 {
191 S => "jim.example.com"
192 },
193 {
194 S => "joe.example.com"
195 },
196 {
197 S => "joe.example.com"
198 },
199 {
200 S => "bob.example.com"
201 }
202 ],
203 'Or stream'
204);
205
206my $top_or_2 = FromCode->new(
207 code => bind_array(S => \@servers),
208 next => FromCode->new(
209 code => $or_code,
210 next => FromCode->new(
211 code => test_values([ 'S' ], sub { $_[0] =~ /\.example\.com$/ }),
212 ),
213 ),
214);
215
216my $orstream_2 = DX::ResultStream->new(
217 for_state => make_state([ qw(S) ], $top_or_2)
218);
219
220is_deeply(
221 [ $orstream_2->results ],
222 [
223 {
224 S => "jim.example.com"
225 },
226 {
227 S => "joe.example.com"
228 },
229 {
230 S => "joe.example.com"
231 },
232 {
233 S => "bob.example.com"
234 }
235 ],
236 'Or stream'
237);
238
60cda014 239done_testing;