File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ( `
9389def __fake_input(arg=None):
9490 return ""
9591
9692input = __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.
Original file line number Diff line number Diff 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} ) ;
You can’t perform that action at this time.
0 commit comments