-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtutorialTestClasses.py
57 lines (47 loc) · 2.39 KB
/
tutorialTestClasses.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# tutorialTestClasses.py
# ----------------------
# Licensing Information: You are free to use or extend these projects for
# educational purposes provided that (1) you do not distribute or publish
# solutions, (2) you retain this notice, and (3) you provide clear
# attribution to UC Berkeley, including a link to
# http://inst.eecs.berkeley.edu/~cs188/pacman/pacman.html
#
# Attribution Information: The Pacman AI projects were developed at UC Berkeley.
# The core projects and autograders were primarily created by John DeNero
# ([email protected]) and Dan Klein ([email protected]).
# Student side autograding was added by Brad Miller, Nick Hay, and
# Pieter Abbeel ([email protected]).
import testClasses
# Simple test case which evals an arbitrary piece of python code.
# The test is correct if the output of the code given the student's
# solution matches that of the instructor's.
class EvalTest(testClasses.TestCase):
def __init__(self, question, testDict):
super(EvalTest, self).__init__(question, testDict)
self.preamble = compile(testDict.get('preamble', ""), "%s.preamble" % self.getPath(), 'exec')
self.test = compile(testDict['test'], "%s.test" % self.getPath(), 'eval')
self.success = testDict['success']
self.failure = testDict['failure']
def evalCode(self, moduleDict):
bindings = dict(moduleDict)
exec self.preamble in bindings
return str(eval(self.test, bindings))
def execute(self, grades, moduleDict, solutionDict):
result = self.evalCode(moduleDict)
if result == solutionDict['result']:
grades.addMessage('PASS: %s' % self.path)
grades.addMessage('\t%s' % self.success)
return True
else:
grades.addMessage('FAIL: %s' % self.path)
grades.addMessage('\t%s' % self.failure)
grades.addMessage('\tstudent result: "%s"' % result)
grades.addMessage('\tcorrect result: "%s"' % solutionDict['result'])
return False
def writeSolution(self, moduleDict, filePath):
handle = open(filePath, 'w')
handle.write('# This is the solution file for %s.\n' % self.path)
handle.write('# The result of evaluating the test must equal the below when cast to a string.\n')
handle.write('result: "%s"\n' % self.evalCode(moduleDict))
handle.close()
return True