-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### Summary of Changes Move the method `callToParameterValue` to `SafeDsNodeMapper`. Previously, it was private in `SafeDsAnnotations`. Given a call and a parameter (or its name), it returns the value assigned to the parameter in the call. It also takes default values into account.
- Loading branch information
1 parent
061d3b1
commit 275366a
Showing
3 changed files
with
222 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
...ages/safe-ds-lang/tests/language/helpers/safe-ds-node-mapper/callToParameterValue.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
import { EmptyFileSystem } from 'langium'; | ||
import { describe, expect, it } from 'vitest'; | ||
import { | ||
isSdsModule, | ||
SdsAbstractCall, | ||
SdsFunction, | ||
SdsParameter, | ||
SdsPipeline, | ||
} from '../../../../src/language/generated/ast.js'; | ||
import { createSafeDsServices } from '../../../../src/language/index.js'; | ||
import { Constant, IntConstant } from '../../../../src/language/partialEvaluation/model.js'; | ||
import { getNodeOfType } from '../../../helpers/nodeFinder.js'; | ||
import { getModuleMembers, getParameters } from '../../../../src/language/helpers/nodeProperties.js'; | ||
|
||
const services = createSafeDsServices(EmptyFileSystem).SafeDs; | ||
const callGraphComputer = services.flow.CallGraphComputer; | ||
const nodeMapper = services.helpers.NodeMapper; | ||
const partialEvaluator = services.evaluation.PartialEvaluator; | ||
|
||
const code = ` | ||
fun myFunction(p1: String, p2: String = 0) | ||
pipeline myPipeline { | ||
myFunction(1, 2); | ||
myFunction(); | ||
unresolved(); | ||
} | ||
`; | ||
const module = await getNodeOfType(services, code, isSdsModule); | ||
const myFunction = getModuleMembers(module)[0] as SdsFunction; | ||
const p1 = getParameters(myFunction)[0]!; | ||
const p2 = getParameters(myFunction)[1]!; | ||
const myPipeline = module?.members[1] as SdsPipeline; | ||
const call1 = callGraphComputer.getCalls(myPipeline)[0]!; | ||
const call2 = callGraphComputer.getCalls(myPipeline)[1]!; | ||
const call3 = callGraphComputer.getCalls(myPipeline)[2]!; | ||
|
||
describe('SafeDsNodeMapper', () => { | ||
const testCases: CallToParameterValueTest[] = [ | ||
{ | ||
testName: 'undefined call, undefined parameter', | ||
call: undefined, | ||
parameter: undefined, | ||
expectedResult: undefined, | ||
}, | ||
{ | ||
testName: 'undefined call, defined parameter', | ||
call: undefined, | ||
parameter: p1, | ||
expectedResult: undefined, | ||
}, | ||
{ | ||
testName: 'defined call, undefined parameter', | ||
call: call1, | ||
parameter: undefined, | ||
expectedResult: undefined, | ||
}, | ||
{ | ||
testName: 'parameter is object, required parameter, value provided', | ||
call: call1, | ||
parameter: p1, | ||
expectedResult: new IntConstant(1n), | ||
}, | ||
{ | ||
testName: 'parameter is object, optional parameter, value provided', | ||
call: call1, | ||
parameter: p2, | ||
expectedResult: new IntConstant(2n), | ||
}, | ||
{ | ||
testName: 'parameter is object, required parameter, no value provided', | ||
call: call2, | ||
parameter: p1, | ||
expectedResult: undefined, | ||
}, | ||
{ | ||
testName: 'parameter is object, optional parameter, no value provided', | ||
call: call2, | ||
parameter: p2, | ||
expectedResult: new IntConstant(0n), | ||
}, | ||
{ | ||
testName: 'parameter is string, required parameter, value provided', | ||
call: call1, | ||
parameter: 'p1', | ||
expectedResult: new IntConstant(1n), | ||
}, | ||
{ | ||
testName: 'parameter is string, optional parameter, value provided', | ||
call: call1, | ||
parameter: 'p2', | ||
expectedResult: new IntConstant(2n), | ||
}, | ||
{ | ||
testName: 'parameter is string, required parameter, no value provided', | ||
call: call2, | ||
parameter: 'p1', | ||
expectedResult: undefined, | ||
}, | ||
{ | ||
testName: 'parameter is string, optional parameter, no value provided', | ||
call: call2, | ||
parameter: 'p2', | ||
expectedResult: new IntConstant(0n), | ||
}, | ||
{ | ||
testName: 'parameter is object, required parameter, unresolved callable', | ||
call: call3, | ||
parameter: p1, | ||
expectedResult: undefined, | ||
}, | ||
{ | ||
testName: 'parameter is object, optional parameter, unresolved callable', | ||
call: call3, | ||
parameter: p2, | ||
expectedResult: undefined, | ||
}, | ||
{ | ||
testName: 'parameter is string, required parameter, unresolved callable', | ||
call: call3, | ||
parameter: 'p1', | ||
expectedResult: undefined, | ||
}, | ||
{ | ||
testName: 'parameter is string, optional parameter, unresolved callable', | ||
call: call3, | ||
parameter: 'p2', | ||
expectedResult: undefined, | ||
}, | ||
]; | ||
|
||
describe.each(testCases)('callToParameterValue', ({ testName, call, parameter, expectedResult }) => { | ||
it(testName, () => { | ||
const parameterValue = nodeMapper.callToParameterValue(call, parameter); | ||
if (expectedResult === undefined) { | ||
expect(parameterValue).toBeUndefined(); | ||
return; | ||
} | ||
|
||
const evaluatedParameterValue = partialEvaluator.evaluate(parameterValue); | ||
expect(evaluatedParameterValue).toStrictEqual(expectedResult); | ||
}); | ||
}); | ||
}); | ||
|
||
/** | ||
* A test case for {@link SafeDsNodeMapper.callToParameterValue}. | ||
*/ | ||
interface CallToParameterValueTest { | ||
/** | ||
* A short description of the test case. | ||
*/ | ||
testName: string; | ||
|
||
/** | ||
* The abstract call to test. | ||
*/ | ||
call: SdsAbstractCall | undefined; | ||
|
||
/** | ||
* The parameter to test. | ||
*/ | ||
parameter: SdsParameter | string | undefined; | ||
|
||
/** | ||
* The expected result. | ||
*/ | ||
expectedResult: Constant | undefined; | ||
} |