packages/deriver/tests/Unit/Deriver/Evaluator/Call/ArgumentDemandTest.php
1<?php
2
3declare (strict_types=1);
4
5namespace Tests\Unit\Deriver\Evaluator\Call;
6
7use Deriver\Deriver\Contributions;
8use Deriver\Deriver\DeriverConfig;
9use Deriver\Deriver\Evaluator\Call\ArgumentDemand;
10use Deriver\Deriver\Evaluator\Call\CallDecision;
11use Deriver\Deriver\Evaluator\Call\CallDescription;
12use Deriver\Deriver\Evaluator\Call\CallDescriptor;
13use Deriver\Deriver\Evaluator\Call\Evaluators;
14use Deriver\Deriver\Evaluator\Call\Plan\Action;
15use Deriver\Deriver\Evaluator\Call\Plan\Expression;
16use Deriver\Deriver\Evaluator\Call\Plan\SemanticPlan;
17use Deriver\Deriver\Evaluator\Call\Registrations;
18use Deriver\Deriver\Evaluator\Declaration\Parameter;
19use Deriver\Deriver\Evaluator\Declaration\Signature;
20use Deriver\Deriver\Evaluator\EvaluatorConfig;
21use Deriver\Deriver\Evaluator\Evidence\Frontier;
22use Deriver\Deriver\Evaluator\Index\DefinitionIndex;
23use Deriver\Deriver\Evaluator\Request\ValueQuery;
24use Deriver\Deriver\Evaluator\TargetProfile;
25use Deriver\Deriver\Evaluator\Term;
26use Deriver\Deriver\Lowerer\Declaration\Index;
27use Deriver\Deriver\MicroEvaluatorConversion;
28use Deriver\Deriver\Parser\Node\File;
29use Deriver\Deriver\Parser\Parser;
30use Deriver\Deriver\Parser\ParserConfig;
31use Deriver\Deriver\Providers\ProviderExpansion;
32use Deriver\Deriver\Publication\CandidateQuery;
33use Deriver\Deriver\Resolver\Resolver;
34use Deriver\Deriver\Selection\CallObservations;
35use Deriver\Deriver\SnapshotIdentity;
36use Deriver\Deriver\Sources\Path;
37use Deriver\Deriver\Sources\Source;
38use Deriver\Deriver\Sources\Sources;
39use Override;
40use PHPUnit\Framework\Attributes\CoversNothing;
41use PHPUnit\Framework\TestCase;
42
43#[CoversNothing]
44final class ArgumentDemandTest extends TestCase
45{
46 public function testDemandEvaluatesOnlyTheSelectionParameter(): void
47 {
48 $prepared1Input = new Sources([new Source('candidate.php', '<?php ' . 'function target(){observe(heavy("fast",missing()));}')]);
49 $prepared1Configuration = new DeriverConfig();
50 $prepared1Contributions = new Contributions(evaluators: [new class () implements ArgumentDemand {
51 /**
52 * Declares the two fixture parameters.
53 */
54 #[Override]
55 public function descriptor(): CallDescriptor
56 {
57 return new CallDescriptor('fixture.selective', '1', 'heavy', new Signature([new Parameter('mode'), new Parameter('unused')]));
58 }
59 /**
60 * @return list<string> Input needed only for evaluator selection
61 */
62 #[Override]
63 public function demand(CallDescription $call): array
64 {
65 return ['mode'];
66 }
67 /**
68 * Chooses a constant plan or explicitly declines source replacement.
69 */
70 #[Override]
71 public function evaluate(CallDescription $call): CallDecision
72 {
73 return $call->arguments->arguments['mode']->value->literal === 'fast' ? CallDecision::handled(new SemanticPlan([Action::returns(Expression::literal(Term::constant(10)))])) : CallDecision::declined();
74 }
75 }]);
76 $prepared1Target = new TargetProfile($prepared1Configuration->phpVersion, $prepared1Configuration->integerBits, $prepared1Configuration->floatPrecision);
77 $prepared1Providers = new ProviderExpansion($prepared1Input, array_map(Term::fromNative(...), $prepared1Configuration->environment), $prepared1Contributions);
78 $contributions = $prepared1Providers->contributions;
79 $prepared1Evaluators = new Evaluators(new TargetProfile($prepared1Configuration->phpVersion, $prepared1Configuration->integerBits, $prepared1Configuration->floatPrecision), new Registrations($contributions->evaluators, $contributions->providers, $contributions->domains, $contributions->intrinsics, $contributions->stateSlots), $prepared1Configuration->standardEvaluators);
80 $prepared1Settings = new EvaluatorConfig($prepared1Target, $prepared1Providers->environment, $prepared1Configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $prepared1Configuration->evaluators));
81 $prepared1Identity = new SnapshotIdentity($prepared1Providers->input, $prepared1Settings, $prepared1Evaluators, $prepared1Providers->manifest);
82 $snapshotId = $prepared1Identity->id;
83 $input = $prepared1Providers->input;
84 $parser = new Parser(new ParserConfig($prepared1Target->version));
85 $trees = array_map(static fn (Source $source): File => (new Resolver())->resolve($parser->parse(new Source(Path::normalize($source->path), $source->contents, $source->declarationsOnly))), iterator_to_array($input, false));
86 $prepared1Program = new Index($snapshotId, $trees);
87 $prepared1Snapshot = $prepared1Identity->snapshot(array_map(Frontier::fromDiagnostic(...), $prepared1Program->diagnostics()));
88 $s = new CandidateQuery(new DefinitionIndex($prepared1Program), $prepared1Settings, $prepared1Evaluators, $prepared1Snapshot);
89 $r = $s->derive(new ValueQuery((new CallObservations($prepared1Program, $prepared1Evaluators))->find('observe')[0]->argument(0)));
90 $slot = 'value';
91 self::assertSame([10], array_map(static fn (Term $value) => $value->native(), array_map(static fn ($candidate): Term => $candidate->result instanceof Term ? $candidate->result : Term::fromNative($candidate->result), iterator_to_array($r, false))));
92 self::assertSame(0, $r->statistics->bodyExpansions);
93 }
94}
95