Skip to content

Commit 1f299cf

Browse files
fix: input mock order (#404)
It's important to mock input so that unexpected input statements don't immediately break tests but it needs to be possible to override that with beforeEach
1 parent 9c870a1 commit 1f299cf

2 files changed

Lines changed: 43 additions & 4 deletions

File tree

packages/python-evaluator/src/python-test-evaluator.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,16 @@ class PythonTestEvaluator implements TestEvaluator {
8484
/* eslint-enable @typescript-eslint/no-unused-vars */
8585

8686
try {
87-
eval(opts.hooks?.beforeEach ?? "");
88-
89-
const iifeTest = createAsyncIife(testString);
90-
9187
// If input isn't reassigned, it will throw when called during testing.
9288
runPython(`
9389
def __fake_input(arg=None):
9490
return ""
9591
9692
input = __fake_input
9793
`);
94+
eval(opts.hooks?.beforeEach ?? "");
95+
96+
const iifeTest = createAsyncIife(testString);
9897

9998
// Evaluates the learner's code so that any variables they
10099
// define are available to the test.

packages/tests/integration-tests/index.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,5 +1431,45 @@ pattern = re.compile('l+')`;
14311431

14321432
expect(result).toEqual({ pass: true });
14331433
});
1434+
1435+
it("should supporting input mocking", async () => {
1436+
const beforeEach = `runPython('input = lambda: "test input"')`;
1437+
const result = await page.evaluate(async (beforeEach) => {
1438+
const runner = await window.FCCTestRunner.createTestRunner({
1439+
type: "python",
1440+
hooks: {
1441+
beforeEach,
1442+
},
1443+
});
1444+
return runner?.runTest(
1445+
`assert.equal(runPython('input()'), "test input")`,
1446+
);
1447+
}, beforeEach);
1448+
1449+
expect(result).toEqual({ pass: true });
1450+
});
1451+
1452+
it("should support input mocking in user code", async () => {
1453+
const source = `name = input()`;
1454+
const beforeEach = `runPython('input = lambda: "mocked input"')`;
1455+
const result = await page.evaluate(
1456+
async (source, beforeEach) => {
1457+
const runner = await window.FCCTestRunner.createTestRunner({
1458+
type: "python",
1459+
source,
1460+
hooks: {
1461+
beforeEach,
1462+
},
1463+
});
1464+
return runner?.runTest(
1465+
`assert.equal(runPython('name'), "mocked input")`,
1466+
);
1467+
},
1468+
source,
1469+
beforeEach,
1470+
);
1471+
1472+
expect(result).toEqual({ pass: true });
1473+
});
14341474
});
14351475
});

0 commit comments

Comments
 (0)