Skip to content

Commit

Permalink
Fix evaluation error when no evaluation code is provided (#1472)
Browse files Browse the repository at this point in the history
* Fix invalid array access for stepper steps

* Resolves bug where no execution steps will lead to access error

* Add evaluation message to indicate no evaluation

* Returns empty step with message

* Test evaluation of empty programs in stepper

---------

Co-authored-by: Martin Henz <[email protected]>
  • Loading branch information
sayomaki and martin-henz authored Aug 28, 2023
1 parent 02ff280 commit afcff0d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/stepper/__tests__/__snapshots__/stepper.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2095,6 +2095,8 @@ false ? 1 : 100 * factorial(100 - 1);
"
`;

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

exports[`Infinite recursion 1`] = `
"function f() {
return f();
Expand Down
10 changes: 10 additions & 0 deletions src/stepper/__tests__/stepper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,16 @@ describe(`#1342: Test the fix of #1341: Stepper limit off by one`, () => {
})
})

describe(`Evaluation of empty code and imports`, () => {
test('Evaluate empty program', () => {
const code = ``
const program = parse(code, mockContext())!
const steps = getEvaluationSteps(program, mockContext(), 1000)
expect(steps.map(x => codify(x[0])).join('\n')).toMatchSnapshot()
expect(getLastStepAsString(steps)).toEqual('')
})
})

// describe(`#1223: Stepper: Import statements cause errors`, () => {
// test('import a module and invoke its functions', () => {
// const code = `
Expand Down
5 changes: 4 additions & 1 deletion src/stepper/stepper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3383,9 +3383,12 @@ export function getEvaluationSteps(
reducedWithPath = reduceMain(reducedWithPath[0], context)
i += 2
}
if (!limitExceeded) {
if (!limitExceeded && steps.length > 0) {
steps[steps.length - 1][2] = 'Evaluation complete'
}
if (steps.length === 0) {
steps.push([reducedWithPath[0] as es.Program, [], 'Nothing to evaluate'])
}
return steps
} catch (error) {
context.errors.push(error)
Expand Down

0 comments on commit afcff0d

Please sign in to comment.