packages/deriver/tests/Unit/Deriver/Evaluator/Expression/ConditionalEvaluatorTest.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\Expression\ConditionalEvaluator;
15use Deriver\Deriver\Evaluator\Index\DefinitionIndex;
16use Deriver\Deriver\Evaluator\Request\Budget;
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 ConditionalEvaluatorTest extends TestCase
36{
37 public function testEvaluateBothAlternativesWithoutEvaluatingThePredicate(): void
38 {
39 $source = 'function target(){return false ? external() : 7;}';
40 $configuration = new DeriverConfig();
41 $budget = new Budget();
42 $contributions = new Contributions();
43 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
44 $providers = new ProviderExpansion(new Sources([new Source('candidate.php', '<?php ' . $source)]), array_map(Term::fromNative(...), $configuration->environment), $contributions);
45 $contributions1 = $providers->contributions;
46 $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);
47 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
48 $snapshotId = 'test';
49 $input = $providers->input;
50 $parser = new Parser(new ParserConfig($target->version));
51 $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));
52 $program = new Index($snapshotId, $trees);
53 $engine = new Evaluator(new Context(new DefinitionIndex($program), $settings, $evaluators, $budget));
54 $symbol = 'target';
55 $graph = $engine->context->index->graph($symbol);
56 self::assertNotNull($graph);
57 $frame = new Frame($graph, 'source:' . $symbol);
58 $operation = 'phi';
59 $instruction = array_values(array_filter($frame->graph->definitions, static fn ($instruction): bool => $instruction->operation === $operation))[0] ?? null;
60 self::assertNotNull($instruction);
61 $value = (new ConditionalEvaluator($engine))->evaluate($frame, $instruction, 20);
62 $values = iterator_to_array((new Choices())->alternatives($value), false);
63 self::assertCount(2, $values);
64 self::assertSame(\Deriver\Deriver\Evaluator\TermKind::Call, $values[0][0]->kind);
65 self::assertSame(7, $values[1][0]->native());
66 self::assertArrayNotHasKey($frame->identity . ':' . $instruction->operands[2], $engine->context->recorded);
67 }
68 public function testAlternativesCanProjectTruthWithoutPruningValueChoices(): void
69 {
70 $source = 'function subject($x){return $x?1:2;}';
71 $tree = (new Resolver())->resolve((new Parser(new ParserConfig('8.3')))->parse(new Source('a.php', '<?php ' . $source)));
72 $index = new DefinitionIndex(new Index('s', [$tree]));
73 $config = new EvaluatorConfig();
74 $engine = new Evaluator(new Context($index, $config, new Evaluators($config->target), new Budget()));
75 $graph = $index->graph('subject');
76 self::assertNotNull($graph);
77 $frame = new Frame($graph, 'source:subject');
78 $instruction = $graph->definitions[$graph->returns()[0][1]];
79 $value = (new ConditionalEvaluator($engine))->alternatives($frame, $instruction, 32, true, 3);
80 self::assertSame(true, $value->native());
81 }
82
83}
84