-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Here, the student is asked to return a joke string for 2 marks | ||
* 1 mark is deducted if the student does not use make_funny | ||
*/ | ||
|
||
import { Grader, Student } from './types' | ||
|
||
const validStudentCorrect = | ||
`const make_joke = str => make_funny(str);` | ||
|
||
const validStudentWrong = | ||
`const make_joke = str => str;` | ||
|
||
const validStudentPartial = | ||
`const make_joke = str => str + ', haha!';` | ||
|
||
export const student: Student = { | ||
invalid: { | ||
}, | ||
valid: { | ||
correct: validStudentCorrect, | ||
wrong: validStudentWrong, | ||
partial: validStudentPartial | ||
} | ||
} | ||
|
||
const validGrader = [ | ||
`function ek0chei0y1() { | ||
const result_1 = make_joke("hello") === "hello, haha!" ? 1 : 0; | ||
__reset_function_count("make_funny"); | ||
make_joke("hello"); | ||
const result_2 = __get_function_count("make_funny") > 0 ? 2 : 1; | ||
return result_1 * result_2; | ||
} | ||
ek0chei0y1(); | ||
` | ||
] | ||
|
||
export const grader: Grader = { | ||
invalid: {}, | ||
valid: validGrader | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { grader, student } from './examples/util' | ||
import { awsEventFactory } from './helpers' | ||
import { runAll } from '../index' | ||
|
||
const makeAwsEvent = awsEventFactory({ | ||
chapter: 1, | ||
external: { | ||
name: 'NONE', | ||
symbols: [ | ||
'__track_function', | ||
'__reset_function_count', | ||
'__get_function_count', | ||
'make_funny', | ||
] | ||
}, | ||
globals: [ | ||
['make_funny', 'str => str + ", haha!";'], | ||
['__tracking_function', 'global.__track_function("make_funny");'], | ||
] | ||
}) | ||
|
||
test('correct wrapped function', async () => { | ||
const results = await runAll(makeAwsEvent(grader.valid, student.valid.correct)) | ||
expect(results).toEqual([ | ||
{'grade': 2, 'resultType': 'pass'}, | ||
]) | ||
}) | ||
|
||
test('did not use function', async () => { | ||
const results = await runAll(makeAwsEvent(grader.valid, student.valid.partial)) | ||
expect(results).toEqual([ | ||
{'grade': 1, 'resultType': 'pass'}, | ||
]) | ||
}) | ||
|
||
test('incorrect answer', async () => { | ||
const results = await runAll(makeAwsEvent(grader.valid, student.valid.wrong)) | ||
expect(results).toEqual([ | ||
{'grade': 0, 'resultType': 'pass'}, | ||
]) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
var table = {}; | ||
// to stop typescript complaining | ||
const globalAny:any = global; | ||
|
||
/** | ||
Utility function that wraps a function and provides two tracking functions | ||
to allow you to track whether a function has been called | ||
Type: (fn) -> fn | ||
**/ | ||
const track_function = (name: string) => { | ||
if (table[name] !== undefined) { | ||
reset_count(name); | ||
return; | ||
} | ||
reset_count(name); | ||
var fn = global[name]; | ||
function wrapped_function(...args: any[]): any { | ||
table[name]++; | ||
return fn(...args); | ||
} | ||
global[name] = wrapped_function; | ||
}; | ||
|
||
const reset_count = (name: string) => { | ||
table[name] = 0; | ||
}; | ||
|
||
const get_count = (name: string) => { | ||
return table[name]; | ||
}; | ||
|
||
globalAny.__track_function = track_function; | ||
globalAny.__reset_function_count = reset_count; | ||
globalAny.__get_function_count = get_count; |