Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to check outputs of tests #799

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/r-bridge/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ export class RShell {
*/
public clearEnvironment(): void {
this.log.debug('clearing environment')
this._sendCommand('rm(list=ls())')
// run rm(list=ls()) but ignore 'flowr_get_ast', which is the compile command installed
this._sendCommand('rm(list=setdiff(ls(), "flowr_get_ast"))')
}

/**
Expand Down
17 changes: 16 additions & 1 deletion test/functionality/_helper/label.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const uniqueTestId = (() => {
})()


export type TestLabelContext = 'parse' | 'desugar' | 'dataflow' | 'other' | 'slice'
export type TestLabelContext = 'parse' | 'desugar' | 'dataflow' | 'other' | 'slice' | 'output'

export interface TestLabel extends MergeableRecord {
readonly id: number
readonly name: string
Expand Down Expand Up @@ -64,6 +65,20 @@ function getFullNameOfLabel(label: TestLabel): string {
}


export function modifyLabelName(label: TestLabel, nameModification: (name: string) => string): TestLabel
export function modifyLabelName(label: string, nameModification: (name: string) => string): string
export function modifyLabelName(label: TestLabel | string, nameModification: (name: string) => string): TestLabel | string
export function modifyLabelName(label: TestLabel | string, nameModification: (name: string) => string): TestLabel | string {
if(typeof label === 'string') {
return nameModification(label)
}

return {
...label,
name: nameModification(label.name)
}
}

/**
* Returns the full name of the testlabel and adds the respective contexts
*/
Expand Down
53 changes: 45 additions & 8 deletions test/functionality/_helper/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { NAIVE_RECONSTRUCT } from '../../../src/core/steps/all/static-slicing/10
import { guard } from '../../../src/util/assert'
import { PipelineExecutor } from '../../../src/core/pipeline-executor'
import type { TestLabel } from './label'
import { decorateLabelContext } from './label'
import { modifyLabelName , decorateLabelContext } from './label'
import { printAsBuilder } from './dataflow/dataflow-builder-printer'
import { RShell } from '../../../src/r-bridge/shell'
import type { NoInfo, RNode } from '../../../src/r-bridge/lang-4.x/ast/model/model'
Expand Down Expand Up @@ -98,6 +98,12 @@ export interface TestConfiguration extends MergeableRecord {
needsNetworkConnection: boolean
}

export interface TestConfigurationWithOutput extends TestConfiguration {
/** HANDLE WITH UTTER CARE! Will run in an R-Shell on the host system! */
expectedOutput: string | RegExp
trimOutput: boolean
}

export const defaultTestConfiguration: TestConfiguration = {
minRVersion: undefined,
needsPackages: [],
Expand Down Expand Up @@ -179,12 +185,38 @@ function mapProblematicNodesToIds(problematic: readonly ProblematicDiffInfo[] |
return problematic === undefined ? undefined : new Set(problematic.map(p => p.tag === 'vertex' ? p.id : `${p.from}->${p.to}`))
}


/**
* Assert that the given input code produces the expected output in R. Trims by default.
*/
export function assertOutput(name: string | TestLabel, shell: RShell, input: string, expected: string | RegExp, userConfig?: Partial<TestConfigurationWithOutput>): void {
const effectiveName = decorateLabelContext(name, ['output'])
it(`${effectiveName} (input: ${input})`, async function() {
await ensureConfig(shell, this, userConfig)
const lines = await shell.sendCommandWithOutput(input, { automaticallyTrimOutput: userConfig?.trimOutput ?? true })
/* we have to reset in between such tests! */
shell.clearEnvironment()
if(typeof expected === 'string') {
assert.strictEqual(lines.join('\n'), expected, `for input ${input}`)
} else {
assert.match(lines.join('\n'), expected,`, for input ${input}`)
}
})
}

function handleAssertOutput(name: string | TestLabel, shell: RShell, input: string, userConfig?: Partial<TestConfigurationWithOutput>): void {
const e = userConfig?.expectedOutput
if(e) {
assertOutput(modifyLabelName(name, n => `[output] ${n}`), shell, input, e, userConfig)
}
}

export function assertDataflow(
name: string | TestLabel,
shell: RShell,
input: string,
expected: DataflowGraph,
userConfig?: Partial<TestConfiguration>,
userConfig?: Partial<TestConfigurationWithOutput>,
startIndexForDeterministicIds = 0
): void {
const effectiveName = decorateLabelContext(name, ['dataflow'])
Expand Down Expand Up @@ -212,7 +244,8 @@ export function assertDataflow(
console.error('diff:\n', diff)
throw e
}
}).timeout('3min')
}).timeout('4min')
handleAssertOutput(name, shell, input, userConfig)
}


Expand All @@ -222,11 +255,11 @@ function printIdMapping(ids: NodeId[], map: AstIdMap): string {
}

/**
* Please note, that this executes the reconstruction step separately, as it predefines the result of the slice with the given ids.
* Please note that this executes the reconstruction step separately, as it predefines the result of the slice with the given ids.
*/
export function assertReconstructed(name: string | TestLabel, shell: RShell, input: string, ids: NodeId | NodeId[], expected: string, userConfig?: Partial<TestConfiguration>, getId: IdGenerator<NoInfo> = deterministicCountingIdGenerator(0)): Mocha.Test {
export function assertReconstructed(name: string | TestLabel, shell: RShell, input: string, ids: NodeId | NodeId[], expected: string, userConfig?: Partial<TestConfigurationWithOutput>, getId: IdGenerator<NoInfo> = deterministicCountingIdGenerator(0)): Mocha.Test {
const selectedIds = Array.isArray(ids) ? ids : [ids]
return it(decorateLabelContext(name, ['slice']), async function() {
const t = it(decorateLabelContext(name, ['slice']), async function() {
await ensureConfig(shell, this, userConfig)

const result = await new PipelineExecutor(DEFAULT_NORMALIZE_PIPELINE, {
Expand All @@ -245,13 +278,15 @@ export function assertReconstructed(name: string | TestLabel, shell: RShell, inp
assert.strictEqual(reconstructed.code, expected,
`got: ${reconstructed.code}, vs. expected: ${expected}, for input ${input} (ids ${JSON.stringify(ids)}:\n${[...result.normalize.idMap].map(i => `${i[0]}: '${i[1].lexeme}'`).join('\n')})`)
})
handleAssertOutput(name, shell, input, userConfig)
return t
}


export function assertSliced(name: string | TestLabel, shell: RShell, input: string, criteria: SlicingCriteria, expected: string, getId: IdGenerator<NoInfo> = deterministicCountingIdGenerator(0)): Mocha.Test {
export function assertSliced(name: string | TestLabel, shell: RShell, input: string, criteria: SlicingCriteria, expected: string, userConfig?: Partial<TestConfigurationWithOutput>, getId: IdGenerator<NoInfo> = deterministicCountingIdGenerator(0)): Mocha.Test {
const fullname = decorateLabelContext(name, ['slice'])

return it(`${JSON.stringify(criteria)} ${fullname}`, async function() {
const t = it(`${JSON.stringify(criteria)} ${fullname}`, async function() {
const result = await new PipelineExecutor(DEFAULT_RECONSTRUCT_PIPELINE,{
getId,
request: requestFromInput(input),
Expand All @@ -270,4 +305,6 @@ export function assertSliced(name: string | TestLabel, shell: RShell, input: str
throw e
}
})
handleAssertOutput(name, shell, input, userConfig)
return t
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,71 +8,75 @@ import { EmptyArgument } from '../../../../../src/r-bridge/lang-4.x/ast/model/no

describe('Function Definition', withShell(shell => {
describe('Only functions', () => {
assertDataflow(label('unknown read in function', ['normal-definition', 'implicit-return', 'name-normal']), shell, 'function() { x }', emptyGraph()
.use('2', 'x', undefined, false)
.argument('3', '2')
.call('3', '{', [argumentInCall('2')], { returns: ['2'], reads: [BuiltIn], environment: defaultEnv().pushEnv() }, false)
.defineFunction('4', ['3'], {
out: [],
in: [{ nodeId: '2', name: 'x', controlDependencies: [] }],
unknownReferences: [],
entryPoint: '3',
graph: new Set(['2', '3']),
environment: defaultEnv().pushEnv()
})
assertDataflow(label('unknown read in function', ['normal-definition', 'implicit-return', 'name-normal']),
shell, 'function() { x }', emptyGraph()
.use('2', 'x', undefined, false)
.argument('3', '2')
.call('3', '{', [argumentInCall('2')], { returns: ['2'], reads: [BuiltIn], environment: defaultEnv().pushEnv() }, false)
.defineFunction('4', ['3'], {
out: [],
in: [{ nodeId: '2', name: 'x', controlDependencies: [] }],
unknownReferences: [],
entryPoint: '3',
graph: new Set(['2', '3']),
environment: defaultEnv().pushEnv()
})
)

assertDataflow(label('read of parameter', ['formals-named', 'implicit-return', 'name-normal']), shell, 'function(x) { x }', emptyGraph()
.use('4', 'x', undefined, false)
.reads('4', '0')
.argument('5', '4')
.call('5', '{', [argumentInCall('4')], { returns: ['4'], reads: [BuiltIn], environment: defaultEnv().pushEnv().defineParameter('x', '0', '1') }, false)
.defineVariable('0', 'x', { definedBy: [] }, false)
.defineFunction('6', ['5'], {
out: [],
in: [],
unknownReferences: [],
entryPoint: '5',
graph: new Set(['0', '4', '5']),
environment: defaultEnv().pushEnv().defineParameter('x', '0', '1')
})
)
assertDataflow(label('read of parameter in return', ['formals-named', 'return', 'name-normal']), shell, 'function(x) { return(x) }', emptyGraph()
.use('5', 'x', undefined, false)
.reads('5', '0')
.argument('7', '5')
.call('7', 'return', [argumentInCall('5')], { returns: ['5'], reads: [BuiltIn], onlyBuiltIn: true, environment: defaultEnv().pushEnv().defineParameter('x', '0', '1') }, false)
.argument('8', '7')
.call('8', '{', [argumentInCall('7')], { returns: ['7'], reads: [BuiltIn], environment: defaultEnv().pushEnv().defineParameter('x', '0', '1') }, false)
.defineVariable('0', 'x', { definedBy: [] }, false)
.defineFunction('9', ['8'], {
out: [],
in: [],
unknownReferences: [],
entryPoint: '8',
graph: new Set(['0', '5', '7', '8']),
environment: defaultEnv().pushEnv().defineParameter('x', '0', '1')
})
assertDataflow(label('read of parameter', ['formals-named', 'implicit-return', 'name-normal']),
shell, 'function(x) { x }', emptyGraph()
.use('4', 'x', undefined, false)
.reads('4', '0')
.argument('5', '4')
.call('5', '{', [argumentInCall('4')], { returns: ['4'], reads: [BuiltIn], environment: defaultEnv().pushEnv().defineParameter('x', '0', '1') }, false)
.defineVariable('0', 'x', { definedBy: [] }, false)
.defineFunction('6', ['5'], {
out: [],
in: [],
unknownReferences: [],
entryPoint: '5',
graph: new Set(['0', '4', '5']),
environment: defaultEnv().pushEnv().defineParameter('x', '0', '1')
})
)

describe('x', () => {
assertDataflow(label('return parameter named', ['formals-named', 'return', 'named-arguments']), shell, 'function(x) { return(x=x) }', emptyGraph()
.use('6', 'x', undefined, false)
.reads('6', '0')
.use('7', 'x', undefined, false)
.reads('7', '6')
.call('8', 'return', [argumentInCall('7', { name: 'x' } )], { returns: ['7'], reads: [BuiltIn], environment: defaultEnv().pushEnv().defineParameter('x', '0', '1') }, false)
assertDataflow(label('read of parameter in return', ['formals-named', 'return', 'name-normal']),
shell, 'function(x) { return(x) }', emptyGraph()
.use('5', 'x', undefined, false)
.reads('5', '0')
.argument('7', '5')
.call('7', 'return', [argumentInCall('5')], { returns: ['5'], reads: [BuiltIn], onlyBuiltIn: true, environment: defaultEnv().pushEnv().defineParameter('x', '0', '1') }, false)
.argument('8', '7')
.call('9', '{', [argumentInCall('8')], { returns: ['8'], reads: [BuiltIn], environment: defaultEnv().pushEnv().defineParameter('x', '0', '1') }, false)
.call('8', '{', [argumentInCall('7')], { returns: ['7'], reads: [BuiltIn], environment: defaultEnv().pushEnv().defineParameter('x', '0', '1') }, false)
.defineVariable('0', 'x', { definedBy: [] }, false)
.defineFunction('10', ['9'], {
.defineFunction('9', ['8'], {
out: [],
in: [],
unknownReferences: [],
entryPoint: '9',
graph: new Set(['0', '6', '7', '8', '9']),
entryPoint: '8',
graph: new Set(['0', '5', '7', '8']),
environment: defaultEnv().pushEnv().defineParameter('x', '0', '1')
})
)

describe('x', () => {
assertDataflow(label('return parameter named', ['formals-named', 'return', 'named-arguments']),
shell, 'function(x) { return(x=x) }', emptyGraph()
.use('6', 'x', undefined, false)
.reads('6', '0')
.use('7', 'x', undefined, false)
.reads('7', '6')
.call('8', 'return', [argumentInCall('7', { name: 'x' } )], { returns: ['7'], reads: [BuiltIn], environment: defaultEnv().pushEnv().defineParameter('x', '0', '1') }, false)
.argument('8', '7')
.call('9', '{', [argumentInCall('8')], { returns: ['8'], reads: [BuiltIn], environment: defaultEnv().pushEnv().defineParameter('x', '0', '1') }, false)
.defineVariable('0', 'x', { definedBy: [] }, false)
.defineFunction('10', ['9'], {
out: [],
in: [],
unknownReferences: [],
entryPoint: '9',
graph: new Set(['0', '6', '7', '8', '9']),
environment: defaultEnv().pushEnv().defineParameter('x', '0', '1')
})
)
})

Expand All @@ -81,7 +85,8 @@ describe('Function Definition', withShell(shell => {
const envWithXYParam = envWithXParam.defineParameter('y', '2', '3')
const envWithXYZParam = envWithXYParam.defineParameter('z', '4', '5')

assertDataflow(label('read of one parameter', ['formals-named', 'implicit-return', 'name-normal']), shell, 'function(x,y,z) y',
assertDataflow(label('read of one parameter', ['formals-named', 'implicit-return', 'name-normal']),
shell, 'function(x,y,z) y',
emptyGraph()
.defineFunction('8', ['6'], {
out: [],
Expand All @@ -99,37 +104,39 @@ describe('Function Definition', withShell(shell => {
)
})
describe('Scoping of body', () => {
assertDataflow(label('previously defined read in function', ['name-normal', ...OperatorDatabase['<-'].capabilities, 'numbers', 'semicolons', 'normal-definition', 'implicit-return']), shell, 'x <- 3; function() { x }', emptyGraph()
.use('5', 'x', undefined, false)
.call('2', '<-', [argumentInCall('0'), argumentInCall('1')], { returns: ['0'], reads: [BuiltIn] })
.argument('2', ['1', '0'])
.argument('6', '5')
.call('6', '{', [argumentInCall('5')], { returns: ['5'], reads: [BuiltIn], environment: defaultEnv().pushEnv() }, false)
.constant('1')
.defineVariable('0', 'x', { definedBy: ['1', '2'] })
.defineFunction('7', ['6'], {
out: [],
in: [{ nodeId: '5', name: 'x', controlDependencies: [] }],
unknownReferences: [],
entryPoint: '6',
graph: new Set(['5', '6']),
environment: defaultEnv().pushEnv()
})
assertDataflow(label('previously defined read in function', ['name-normal', ...OperatorDatabase['<-'].capabilities, 'numbers', 'semicolons', 'normal-definition', 'implicit-return']),
shell, 'x <- 3; function() { x }', emptyGraph()
.use('5', 'x', undefined, false)
.call('2', '<-', [argumentInCall('0'), argumentInCall('1')], { returns: ['0'], reads: [BuiltIn] })
.argument('2', ['1', '0'])
.argument('6', '5')
.call('6', '{', [argumentInCall('5')], { returns: ['5'], reads: [BuiltIn], environment: defaultEnv().pushEnv() }, false)
.constant('1')
.defineVariable('0', 'x', { definedBy: ['1', '2'] })
.defineFunction('7', ['6'], {
out: [],
in: [{ nodeId: '5', name: 'x', controlDependencies: [] }],
unknownReferences: [],
entryPoint: '6',
graph: new Set(['5', '6']),
environment: defaultEnv().pushEnv()
})
)
assertDataflow(label('local define with <- in function, read after', ['normal-definition', 'semicolons', 'name-normal', ...OperatorDatabase['<-'].capabilities, 'numbers']), shell, 'function() { x <- 3; }; x', emptyGraph()
.use('7', 'x')
.call('4', '<-', [argumentInCall('2'), argumentInCall('3')], { returns: ['2'], reads: [BuiltIn], environment: defaultEnv().pushEnv() }, false)
.call('5', '{', [argumentInCall('4')], { returns: ['4'], reads: [BuiltIn], environment: defaultEnv().pushEnv() }, false)
.constant('3', undefined, false)
.defineVariable('2', 'x', { definedBy: ['3', '4'] }, false)
.defineFunction('6', ['5'], {
out: [],
in: [],
unknownReferences: [],
entryPoint: '5',
graph: new Set(['3', '2', '4', '5']),
environment: defaultEnv().pushEnv().defineVariable('x', '2', '4')
})
assertDataflow(label('local define with <- in function, read after', ['normal-definition', 'semicolons', 'name-normal', ...OperatorDatabase['<-'].capabilities, 'numbers']),
shell, 'function() { x <- 3; }; x', emptyGraph()
.use('7', 'x')
.call('4', '<-', [argumentInCall('2'), argumentInCall('3')], { returns: ['2'], reads: [BuiltIn], environment: defaultEnv().pushEnv() }, false)
.call('5', '{', [argumentInCall('4')], { returns: ['4'], reads: [BuiltIn], environment: defaultEnv().pushEnv() }, false)
.constant('3', undefined, false)
.defineVariable('2', 'x', { definedBy: ['3', '4'] }, false)
.defineFunction('6', ['5'], {
out: [],
in: [],
unknownReferences: [],
entryPoint: '5',
graph: new Set(['3', '2', '4', '5']),
environment: defaultEnv().pushEnv().defineVariable('x', '2', '4')
})
)
assertDataflow(label('local define with = in function, read after', ['normal-definition', ...OperatorDatabase['='].capabilities, 'semicolons', 'name-normal', 'numbers']), shell, 'function() { x = 3; }; x', emptyGraph()
.use('7', 'x')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ cat(4 %a% 5)`)
assertSliced(label('Slice for variable in last filter', caps),
shell, code, ['12@Y'], 'Y')
})
describe('if-then-else formattings', () => {
describe('if-then-else format', () => {
const caps: SupportedFlowrCapabilityId[] = ['name-normal', ...OperatorDatabase['<-'].capabilities, 'numbers', 'if', 'logical', 'binary-operator', 'infix-calls', 'call-normal', 'newlines', 'unnamed-arguments', 'precedence']
const code = `x <- 3
{
Expand All @@ -493,10 +493,12 @@ if (x == 3)
y <- 2 }
else { x <- y <- 3 }
}
print(x)
print(x)
`
assertSliced(label('Slice for initial x should return noting else', caps),
shell, code, ['1@x'], 'x <- 3')
shell, code, ['1@x'], 'x <- 3', {
expectedOutput: '[1] 4'
})
assertSliced(label('Slice for first condition', caps),
shell, code, ['3@x'], 'x <- 3\nx')
assertSliced(label('Slice for last x', caps),
Expand Down
Loading