示例#1
0
    def test_error_context(self):
        student_code = dedent('''
                    def get_input():
                        return int(input("Gimme the number"))
                ''')
        set_source(student_code, "student.py")
        student = Sandbox()
        student.run()
        result = student.call("get_input", inputs="Banana")
        print("--", [c.inputs for c in student._context])
        self.assertEqual(3, student.feedback.location.line)
        self.assertEqual(
            dedent("""A ValueError occurred:

    Invalid literal for int() with base 10: 'banana'

I ran the code:
    get_input()

And I entered as input:
    Banana

The traceback was:
Line 3 of file student.py in get_input
        return int(input("Gimme the number"))


A ValueError occurs when you pass the wrong type of value to a function. For example, you try to convert a string without numbers to an integer (like `int('Five')`).

Suggestion: Read the error message to see which function had the issue. Check what type the function expects. Then trace your code to make sure you pass in that type."""
                   ).strip(), student.feedback.message)
示例#2
0
    def test_runtime_error_inputs(self):
        student_code = dedent('''
                    def x():
                        value = input("Gimme a number")
                        return 7 % value
                    x
                ''')
        student = Sandbox()
        set_source(student_code)
        student.run()
        result = student.call("x", inputs=["0"])
        print([[c.kind, c.inputs] for c in student._context])

        self.assertEqual(
            """A TypeError occurred:

    Unsupported operand type(s) for %: 'int' and 'str'

I ran the code:
    x()

And I entered as input:
    0

The traceback was:
Line 4 of file answer.py in x
        return 7 % value


Type errors occur when you use an operator or function on the wrong type of value. For example, using `+` to add to a list (instead of `.append`), or dividing a string by a number.

Suggestion: To fix a type error, you should trace through your code. Make sure each expression has the type you expect it to have.
""".strip(), student.feedback.message)
示例#3
0
 def test_oo(self):
     # Load the "bank.py" code
     student_code = dedent('''
         class Bank:
             def __init__(self, balance):
                 self.balance = balance
             def save(self, amount):
                 self.balance += amount
                 return self.balance > 0
             def take(self, amount):
                 self.balance -= amount
                 return self.balance > 0''')
     student = Sandbox()
     student.run(student_code, filename='bank.py')
     # Check that we created the class
     self.assertIn('Bank', student.data)
     # Now let's try making an instance
     student.call('Bank', 50, target='bank')
     self.assertIsInstance(student.data['bank'], student.data['Bank'])
     # Can we save money?
     student.call('bank.save', 32)
     self.assertTrue(student.result)
     # What about extracting money?
     student.data['bank'].balance += 100
     student.call('bank.take', 100)
     self.assertTrue(student.result)
示例#4
0
    def test_unittest(self):
        student_code = dedent('''
            x = 0
        ''')
        student = Sandbox()
        student.run(student_code)
        student.call('x')
        self.assertIsNotNone(student.exception)

        student_code = dedent('''
        class Fruit:
            def __init__(self, name, weight=0):
                self.name = name
                self.weight = weight
        def do_math(a, b):
            return a + b - 5
        def weigh_fruits(fruits):
            return sum(fruit.weight for fruit in fruits)    
        ''')
        student.report.contextualize(Submission(main_code=student_code))
        student = Sandbox()
        student.run()
        result = student.call('do_math', 15, 20)
        self.assertEqual(result, 30)
        self.assertEqual(['do_math(15, 20)'],
                         [context.code for context in student.get_context()])
        banana = student.call('Fruit', "Banana")
        self.assertIsInstance(banana, student.data['Fruit'])
        self.assertEqual(["Fruit('Banana')"],
                         [context.code for context in student.get_context()])
        student.start_grouping_context()
        student.run()
        orange = student.call("Fruit", "Orange", 30, target="orange")
        self.assertIsInstance(orange, student.data['Fruit'])
        student.call("Fruit", "Pineapple", 60, target="pineapple")
        student.run("fruits = [orange, pineapple]")
        total_weight = student.call('weigh_fruits', args_locals=["fruits"])
        self.assertEqual(total_weight, 90)
        self.assertEqual([
            student_code, "orange = Fruit('Orange', 30)",
            "pineapple = Fruit('Pineapple', 60)",
            "fruits = [orange, pineapple]", "weigh_fruits(fruits)"
        ], [context.code for context in student.get_context()])
示例#5
0
 def test_call(self):
     student_code = "def average(a,b):\n return (a+b)/2"
     student = Sandbox()
     student.run(student_code, filename='student.py')
     student.call('average', 10, 12)
示例#6
0
student = Sandbox()
student.run("""
def add_together(a, b):
    return -7
print(add_together)
""", as_filename='student.py')
#pprint(student.data)
print(student.data)
print(student.output)


from pedal.assertions import assertEqual, phase

#assertEqual(student.data['a'], 2)

result = student.call("add_together", 2, 2)
#as_int = str(result)
#print("Result was:", as_int)
assertEqual(result, 4)

@phase('input_tryit')
def try_it():
    print("Executed")

from pedal.resolvers import simple

print(simple.resolve())