Skip to content

Commit

Permalink
1108 stepper programs that end with a declaration dont show correct r…
Browse files Browse the repository at this point in the history
…esult (#1597)

* Solved the problem by adding extra check for empty reduced program

* Change the test cases, the new expected behaviour should be "undefined;" instead of "" (epsilon)

---------

Co-authored-by: Martin Henz <[email protected]>
  • Loading branch information
FYL2003 and martin-henz authored Mar 22, 2024
1 parent 650f30d commit a958537
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/stepper/__tests__/__snapshots__/stepper.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2105,6 +2105,7 @@ false ? 1 : 100 * factorial(100 - 1);

exports[`Evaluation of empty code and imports Evaluate empty program 1`] = `
"
undefined;
"
`;

Expand Down
6 changes: 3 additions & 3 deletions src/stepper/__tests__/stepper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ describe('Test single line of code is evaluated', () => {
`
const steps = await testEvalSteps(code)
expect(steps.length).toBe(4)
expect(getLastStepAsString(steps)).toEqual('')
expect(getLastStepAsString(steps)).toEqual('undefined;')
})

test('Function Declaration', async () => {
Expand All @@ -231,7 +231,7 @@ describe('Test single line of code is evaluated', () => {
`
const steps = await testEvalSteps(code)
expect(steps.length).toBe(4)
expect(getLastStepAsString(steps)).toEqual('')
expect(getLastStepAsString(steps)).toEqual('undefined;')
})

test('Value', async () => {
Expand Down Expand Up @@ -1598,7 +1598,7 @@ describe(`Evaluation of empty code and imports`, () => {
const code = ``
const steps = await testEvalSteps(code, mockContext())
expect(steps.map(x => codify(x[0])).join('\n')).toMatchSnapshot()
expect(getLastStepAsString(steps)).toEqual('')
expect(getLastStepAsString(steps)).toEqual('undefined;')
})
})

Expand Down
10 changes: 10 additions & 0 deletions src/stepper/stepper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3377,6 +3377,16 @@ export async function getEvaluationSteps(
}
if (!limitExceeded && steps.length > 0) {
steps[steps.length - 1][2] = 'Evaluation complete'
/**
* this is an extra check
* if the last step's program part is empty, we just manually add undefined to it
* also works when program is epsilon(empty program)
*/
if ((reducedWithPath[0] as es.Program).body.length == 0) {
steps[steps.length - 1][0] = ast.program([
ast.expressionStatement(ast.identifier('undefined'))
])
}
}
if (steps.length === 0) {
steps.push([reducedWithPath[0] as es.Program, [], 'Nothing to evaluate'])
Expand Down

0 comments on commit a958537

Please sign in to comment.