Skip to content

Commit

Permalink
Merge 3285d00 into 2e8309f
Browse files Browse the repository at this point in the history
  • Loading branch information
yyc committed Aug 28, 2018
2 parents 2e8309f + 3285d00 commit 2b70c45
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/__tests__/examples/util.ts
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
}
41 changes: 41 additions & 0 deletions src/__tests__/test_utils.ts
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'},
])
})
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type TimeoutResult = {

export const runAll = async (event: AwsEvent): Promise<Output[]> => {
require('./graphics/rune_library.js')
require('./util')
evaluateGlobals(event.library.globals)
const stdPrg = event.studentProgram
const promises = event.graderPrograms.map(
Expand Down
34 changes: 34 additions & 0 deletions src/util.ts
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;

0 comments on commit 2b70c45

Please sign in to comment.