remove the _then bind methods
[scpubgit/DKit.git] / t / basic.t
index 2175524..8612ea7 100644 (file)
--- a/t/basic.t
+++ b/t/basic.t
@@ -19,7 +19,7 @@ my @shells = qw(csh bash);
 
 my %shells = (
   bash => { map +($_ => 1),
-             qw(joe.example.com kitty.scsys.co.uk pryde.scsys.co.uk) },
+             qw(joe.example.com kitty.scsys.co.uk) },
   csh => { map +($_ => 1),
              qw(jim.example.com joe.example.com bob.example.com) },
 );
@@ -28,10 +28,9 @@ sub bind_array {
   my ($var, $array) = @_;
   sub {
     my ($self, $state) = @_;
-    $state->bind_stream_then(
+    $state->then($self->next)->bind_stream(
       $state->scope_var($var),
       ArrayStream->from_array(@$array),
-      $self->next
     )
   }
 }
@@ -78,7 +77,7 @@ sub make_state {
 
 my $stream = DX::ResultStream->new(for_state => make_state([ 'S' ], $op));
 
-is($stream->next->{'S'}, $_)
+is($stream->next->value_for('S'), $_)
   for qw(jim.example.com joe.example.com bob.example.com);
 
 is($stream->next, undef, 'No more');
@@ -96,12 +95,21 @@ my $complex_op = FromCode->new(
   )
 );
 
+sub bound_values {
+  map {
+    my $v = $_;
+    +{
+       map +($_ => $v->value_for($_)), $v->var_names,
+    }
+  } @_
+}
+
 my $cstream = DX::ResultStream->new(
   for_state => make_state([ qw(S P) ], $complex_op)
 );
 
 is_deeply(
-  [ $cstream->results ],
+  [ bound_values $cstream->results ],
   [
     { P => 'csh', S => 'jim.example.com' },
     { P => 'csh', S => 'joe.example.com' },
@@ -111,30 +119,22 @@ is_deeply(
   'Complex stream'
 );
 
-my $inner_op = make_op(FromCode->new(
-  code => sub {
-    my ($self, $state) = @_;
-    my @stack = @{$state->return_stack};
-    my $top = pop @stack;
-    $state->new(%$state, return_stack => \@stack, next_op => $top);
-  }
-));
+my $pop_stack = FromCode->new(
+  code => sub { $_[1]->pop_return_stack }
+);
+
+my $inner_op = make_op($pop_stack);
 
 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) },
+      code => sub { $_[1]->but(scope => $save_scope, next_op => $_[0]->next) },
       next => $self->next,
     );
-    $state->new(%$state,
-      scope => \%scope,
-      return_stack => [ @rst, $ret_op ],
-      next_op => $inner_op
-    );
+    $state->but(scope => \%scope)->push_return_then($ret_op, $inner_op);
   },
   next => FromCode->new(
     code => bind_array(P => \@shells),
@@ -149,7 +149,7 @@ my $callstream = DX::ResultStream->new(
 );
 
 is_deeply(
-  [ $callstream->results ],
+  [ bound_values $callstream->results ],
   [
     { P => 'csh', S => 'jim.example.com' },
     { P => 'csh', S => 'joe.example.com' },
@@ -159,4 +159,89 @@ is_deeply(
   'Call stream'
 );
 
+my $has_csh = FromCode->new(
+  code => test_values([ 'S' ], sub { $shells{csh}{$_[0]} }),
+  next => $pop_stack
+);
+my $has_bash = FromCode->new(
+  code => test_values([ 'S' ], sub { $shells{bash}{$_[0]} }),
+  next => $pop_stack
+);
+
+my $or_code = sub {
+  my ($self, $state) = @_;
+  my $var = DX::Var->new(id => 'OR')->with_stream(
+    my $stream = ArrayStream->from_array($has_csh, $has_bash)
+  );
+  my $inner_or = FromCode->new(
+    code => sub { $_[1]->then($var->bound_value) }
+  );
+  $state->push_return_then($self->next, $inner_or)
+        ->mark_choice($var);
+};
+
+my $top_or = FromCode->new(
+  code => bind_array(S => \@servers),
+  next => FromCode->new(code => $or_code),
+);
+
+my $orstream = DX::ResultStream->new(
+  for_state => make_state([ qw(S) ], $top_or)
+);
+
+is_deeply(
+  [ bound_values $orstream->results ],
+  [
+    {
+      S => "kitty.scsys.co.uk"
+    },
+    {
+      S => "jim.example.com"
+    },
+    {
+      S => "joe.example.com"
+    },
+    {
+      S => "joe.example.com"
+    },
+    {
+      S => "bob.example.com"
+    }
+  ],
+  'Or stream'
+);
+
+my $top_or_2 = FromCode->new(
+  code => bind_array(S => \@servers),
+  next => FromCode->new(
+    code => $or_code,
+    next => FromCode->new(
+      code => test_values([ 'S' ], sub { $_[0] =~ /\.example\.com$/ }),
+    ),
+  ),
+);
+
+my $orstream_2 = DX::ResultStream->new(
+  for_state => make_state([ qw(S) ], $top_or_2)
+);
+
+is_deeply(
+  [ bound_values $orstream_2->results ],
+  [
+    {
+      S => "jim.example.com"
+    },
+    {
+      S => "joe.example.com"
+    },
+    {
+      S => "joe.example.com"
+    },
+    {
+      S => "bob.example.com"
+    }
+  ],
+  'Or stream'
+);
+
 done_testing;