df84bfe44dadaf93509e62a587f1362287331c9b
[scpubgit/DKit.git] / t / findall.t
1 use strictures 1;
2 use Test::More;
3 use DX::Solver;
4 use DX::OrderedSet;
5
6 my $solver = DX::Solver->new;
7
8 { package My::Server;
9   use Moo;
10   has name => (is => 'ro', required => 1);
11
12   package My::ShellInstalled;
13   use Moo;
14   has server => (is => 'ro', required => 1);
15   has shell => (is => 'ro', required => 1);
16 }
17
18 $solver->facts->{server} = DX::OrderedSet->new(
19   values => [ map My::Server->new(name => $_), qw(one two three four five) ]
20 );
21 $solver->facts->{shell_installed} = DX::OrderedSet->new(
22   values => [
23     (map My::ShellInstalled->new(server => $_, shell => 'bash'),
24        qw(one three four)),
25     (map My::ShellInstalled->new(server => $_, shell => 'csh'),
26        qw(two three five)),
27   ],
28 );
29
30 $solver->add_rule(has_shell => [ 'Srv', 'Shell' ],
31   [ exists => [ qw(Name SI) ] =>
32     [ member_of => 'SI', \'shell_installed' ],
33     [ prop => 'SI' => \'server' => 'Name' ],
34     [ prop => 'Srv' => \'name' => 'Name' ],
35     [ prop => 'SI' => \'shell' => 'Shell' ]
36   ],
37 );
38
39 my @r = $solver->query([ 'X' ],
40   [ findall => X => S =>
41     [ member_of => 'S', \'server' ],
42     [ has_shell => 'S', \'bash' ],
43   ]
44 )->results;
45
46 is_deeply(
47   [ map $_->name, $r[0]->value_for('X')->all ],
48   [ qw(one three four) ]
49 );
50
51 @r = $solver->query([],
52   [ foreach => S => [ [ member_of => 'S', \'server' ] ],
53     [ [ has_shell => 'S' => \'bash' ] ] ]
54 )->results;
55
56 ok(!@r, 'No results for only bash');
57
58 $solver->add_rule(has_any_shell => [ 'S' ] => [ has_shell => 'S' => \'bash' ]);
59 $solver->add_rule(has_any_shell => [ 'S' ] => [ has_shell => 'S' => \'csh' ]);
60
61 @r = $solver->query([],
62   [ foreach => S => [ [ member_of => 'S', \'server' ] ],
63     [ [ has_any_shell => 'S' ] ] ]
64 )->results;
65
66 # only three matches both legs of has_any_shell
67
68 is(scalar(@r), 2, 'Two solutions for any shell');
69
70 done_testing;