eliminate variable name requirement from query()
[scpubgit/DKit.git] / t / findall.t
CommitLineData
138613a8 1use strictures 1;
2use Test::More;
3use DX::Solver;
4use DX::OrderedSet;
5
6my $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
f5a02c91 30$solver->add_rule(has_shell => [ 'Srv', 'Shell' ],
7d384eca 31 [ member_of => 'SI', \'shell_installed' ],
32 [ prop => 'SI' => \'server' => 'Name' ],
33 [ prop => 'Srv' => \'name' => 'Name' ],
34 [ prop => 'SI' => \'shell' => 'Shell' ]
f5a02c91 35);
36
7d384eca 37my @r = $solver->query(
138613a8 38 [ findall => X => S =>
39 [ member_of => 'S', \'server' ],
f5a02c91 40 [ has_shell => 'S', \'bash' ],
138613a8 41 ]
42)->results;
43
44is_deeply(
45 [ map $_->name, $r[0]->value_for('X')->all ],
46 [ qw(one three four) ]
47);
48
7d384eca 49@r = $solver->query(
f5a02c91 50 [ foreach => S => [ [ member_of => 'S', \'server' ] ],
51 [ [ has_shell => 'S' => \'bash' ] ] ]
52)->results;
53
54ok(!@r, 'No results for only bash');
55
56$solver->add_rule(has_any_shell => [ 'S' ] => [ has_shell => 'S' => \'bash' ]);
57$solver->add_rule(has_any_shell => [ 'S' ] => [ has_shell => 'S' => \'csh' ]);
58
7d384eca 59@r = $solver->query(
f5a02c91 60 [ foreach => S => [ [ member_of => 'S', \'server' ] ],
61 [ [ has_any_shell => 'S' ] ] ]
62)->results;
63
64# only three matches both legs of has_any_shell
65
66is(scalar(@r), 2, 'Two solutions for any shell');
67
138613a8 68done_testing;