packages/deriver/tests/Unit/Deriver/Evaluator/Expression/GuardsTest.php
1<?php
2
3declare (strict_types=1);
4
5namespace Tests\Unit\Deriver\Evaluator\Expression;
6
7use Deriver\Deriver\Contributions;
8use Deriver\Deriver\DeriverConfig;
9use Deriver\Deriver\Evaluator\Call\Evaluators;
10use Deriver\Deriver\Evaluator\Call\Registrations;
11use Deriver\Deriver\Evaluator\Enumeration\Choices;
12use Deriver\Deriver\Evaluator\Evaluator;
13use Deriver\Deriver\Evaluator\EvaluatorConfig;
14use Deriver\Deriver\Evaluator\Index\DefinitionIndex;
15use Deriver\Deriver\Evaluator\Request\Budget;
16use Deriver\Deriver\Evaluator\Scalar\Identity;
17use Deriver\Deriver\Evaluator\Scope\Context;
18use Deriver\Deriver\Evaluator\Scope\Frame;
19use Deriver\Deriver\Evaluator\TargetProfile;
20use Deriver\Deriver\Evaluator\Term;
21use Deriver\Deriver\Lowerer\Declaration\Index;
22use Deriver\Deriver\MicroEvaluatorConversion;
23use Deriver\Deriver\Parser\Node\File;
24use Deriver\Deriver\Parser\Parser;
25use Deriver\Deriver\Parser\ParserConfig;
26use Deriver\Deriver\Providers\ProviderExpansion;
27use Deriver\Deriver\Resolver\Resolver;
28use Deriver\Deriver\Sources\Path;
29use Deriver\Deriver\Sources\Source;
30use Deriver\Deriver\Sources\Sources;
31use PHPUnit\Framework\Attributes\CoversNothing;
32use PHPUnit\Framework\TestCase;
33
34#[CoversNothing]
35final class GuardsTest extends TestCase
36{
37 public function testEdgeDoesNotEnumerateAnIrrelevantCondition(): void
38 {
39 $tree = (new Resolver())->resolve((new Parser(new ParserConfig('8.3')))->parse(new Source('guard.php', '<?php function target($flag) { if ($flag) { return 1; } return 2; }')));
40 $context = new Context(new DefinitionIndex(new Index('guard', [$tree])), new EvaluatorConfig(), new Evaluators(), new Budget(partitions: 2));
41 $graph = $context->index->graph('target');
42 self::assertNotNull($graph);
43 $frame = new Frame($graph, 'source:target');
44 $branch = $graph->body->blocks[0]->terminator;
45 $condition = (new Choices())->make([
46 [Term::parameter('a'), ['origin' => 'a']],
47 [Term::parameter('b'), ['origin' => 'b']],
48 [Term::parameter('c'), ['origin' => 'c']],
49 ]);
50 $context->values[$frame->identity . ':' . $branch->operand] = $condition;
51 $value = Term::constant(9);
52 $result = (new \Deriver\Deriver\Evaluator\Expression\Guards(new Evaluator($context)))->edge($frame, 0, $branch->targets[0], $value, 64);
53 $rows = iterator_to_array((new Choices())->alternatives($result), false);
54
55 self::assertCount(1, $rows);
56 self::assertSame(9, $rows[0][0]->native());
57 self::assertSame([], $context->recorded);
58 self::assertSame([$frame->identity . ':condition:' . $branch->operand => true], $rows[0][1]);
59 }
60
61 public function testAtKeepsBothSymbolicBranchIdentities(): void
62 {
63 $source = 'function target($x){if($x){return 1;}return 2;}';
64 $configuration = new DeriverConfig();
65 $budget = new Budget();
66 $contributions = new Contributions();
67 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
68 $providers = new ProviderExpansion(new Sources([new Source('candidate.php', '<?php ' . $source)]), array_map(Term::fromNative(...), $configuration->environment), $contributions);
69 $contributions1 = $providers->contributions;
70 $evaluators = new Evaluators(new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision), new Registrations($contributions1->evaluators, $contributions1->providers, $contributions1->domains, $contributions1->intrinsics, $contributions1->stateSlots), $configuration->standardEvaluators);
71 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
72 $snapshotId = 'test';
73 $input = $providers->input;
74 $parser = new Parser(new ParserConfig($target->version));
75 $trees = array_map(static fn (Source $source1): File => (new Resolver())->resolve($parser->parse(new Source(Path::normalize($source1->path), $source1->contents, $source1->declarationsOnly))), iterator_to_array($input, false));
76 $program = new Index($snapshotId, $trees);
77 $e = new Evaluator(new Context(new DefinitionIndex($program), $settings, $evaluators, $budget));
78 $symbol = 'target';
79 $graph = $e->context->index->graph($symbol);
80 self::assertNotNull($graph);
81 $r = $e->returns(new Frame($graph, 'source:' . $symbol), 64);
82 self::assertCount(2, iterator_to_array((new Choices())->alternatives($r), false));
83 }
84 public function testEdgeRetainsBothBranchesOfAConstantPredicate(): void
85 {
86 $source = 'function target(){if(false){return missing();}return 2;}';
87 $configuration = new DeriverConfig();
88 $budget = new Budget();
89 $contributions = new Contributions();
90 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
91 $providers = new ProviderExpansion(new Sources([new Source('candidate.php', '<?php ' . $source)]), array_map(Term::fromNative(...), $configuration->environment), $contributions);
92 $contributions1 = $providers->contributions;
93 $evaluators = new Evaluators(new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision), new Registrations($contributions1->evaluators, $contributions1->providers, $contributions1->domains, $contributions1->intrinsics, $contributions1->stateSlots), $configuration->standardEvaluators);
94 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
95 $snapshotId = 'test';
96 $input = $providers->input;
97 $parser = new Parser(new ParserConfig($target->version));
98 $trees = array_map(static fn (Source $source1): File => (new Resolver())->resolve($parser->parse(new Source(Path::normalize($source1->path), $source1->contents, $source1->declarationsOnly))), iterator_to_array($input, false));
99 $program = new Index($snapshotId, $trees);
100 $e = new Evaluator(new Context(new DefinitionIndex($program), $settings, $evaluators, $budget));
101 $symbol = 'target';
102 $graph = $e->context->index->graph($symbol);
103 self::assertNotNull($graph);
104 $alternatives = (new Choices())->alternatives($e->returns(new Frame($graph, 'source:' . $symbol), 64));
105 $values = array_reduce(iterator_to_array($alternatives, false), static function (array $values, array $alternative): array {
106 [$value, $_] = $alternative;
107 $values[(new Identity())->key($value)] = $value;
108 return $values;
109 }, []);
110 self::assertCount(2, $values);
111 $values = array_values($values);
112 self::assertInstanceOf(Term::class, $values[0]);
113 self::assertInstanceOf(Term::class, $values[1]);
114 self::assertSame(\Deriver\Deriver\Evaluator\TermKind::Call, $values[0]->kind);
115 self::assertSame(2, $values[1]->native());
116 }
117 public function testWalkKeepsAnExcludedBlockEmpty(): void
118 {
119 $source = 'function subject(){if(false)return 1;return 2;}';
120 $tree = (new Resolver())->resolve((new Parser(new ParserConfig('8.3')))->parse(new Source('a.php', '<?php ' . $source)));
121 $index = new DefinitionIndex(new Index('s', [$tree]));
122 $config = new EvaluatorConfig(evaluators: array_map((new MicroEvaluatorConversion())->convert(...), \Deriver\Deriver\BranchEvaluators::truth()));
123 $engine = new Evaluator(new Context($index, $config, new Evaluators($config->target), new Budget()));
124 $graph = $index->graph('subject');
125 self::assertNotNull($graph);
126 $frame = new Frame($graph, 'source:subject');
127 $value = (new \Deriver\Deriver\Evaluator\Expression\Guards($engine))->walk($frame, $graph->returns()[0][0], Term::constant(7), 32);
128 self::assertSame([], iterator_to_array((new Choices())->alternatives($value)));
129 }
130}
131