~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/apps/tutorialservice/test/TestFramework.py

  • Committer: mattgiuca
  • Date: 2008-03-28 07:03:14 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:716
Test Framework: Numerous bug fixes.
    (With bernie pope)
    parse_exercise.py: Now able to parse "result" elements of the function
    element, in the same way as stdout and stderr.
    TestFramework.py:
        Fixed a fatal error when a function is to be tested (KeyError -
            "code").
        (This was manifesting itself as the submit button just being greyed
        out forever - Now this should be fixed for at least this set of
        cases).
        Fixed another fatal error whereby "result" and "exception" tests were
        being executed without being converted from strings into functions.
            (This was reported as "Cannot call unicode object")
            (This MAY have now fixed the problem with checking for
            exceptions).

Show diffs side-by-side

added added

removed removed

Lines of Context:
179
179
        
180
180
        (test_type, function) = self._stderr_test
181
181
        self._stderr_test = (test_type, self._validate_function(function, included_code))
 
182
        
 
183
        (test_type, function) = self._result_test
 
184
        self._result_test = (test_type, self._validate_function(function, included_code))
 
185
        
 
186
        (test_type, function) = self._exception_test
 
187
        self._exception_test = (test_type, self._validate_function(function, included_code))
182
188
 
183
189
        for filename, (test_type, function) in self._file_tests.items():
184
190
            self._file_tests[filename] = (test_type, self._validate_function(function, included_code))
390
396
            if not self._function == None:
391
397
                if self._function not in global_space_copy:
392
398
                    raise FunctionNotFoundError(self._function)
393
 
                solution_data = self._run_function(lambda: global_space_copy[self._function](*self._list_args, **self._keyword_args))
 
399
                func_to_exec = lambda: global_space_copy[self._function](
 
400
                                    *self._list_args, **self._keyword_args)
 
401
                solution_data = self._run_function(func_to_exec, solution)
394
402
                
395
403
        except:
396
404
            raise ScriptExecutionError(sys.exc_info())
404
412
            if not self._function == None:
405
413
                if self._function not in global_space_copy:
406
414
                    raise FunctionNotFoundError(self._function)
407
 
                attempt_data = self._run_function(lambda: global_space_copy[self._function](*self._list_args, **self._keyword_args))
 
415
                func_to_exec = lambda: global_space_copy[self._function](
 
416
                    *self._list_args, **self._keyword_args)
 
417
                attempt_data = self._run_function(func_to_exec, attempt_code)
408
418
        except:
409
419
            case_dict['exception'] = ScriptExecutionError(sys.exc_info()).to_dict()
410
420
            case_dict['passed'] = False
442
452
    def _execfile(self, filename, global_space):
443
453
        """ Execute the file given by 'filename' in global_space, and return the outputs. """
444
454
        self._initialise_global_space(global_space)
445
 
        data = self._run_function(lambda: execfile(filename, global_space))
446
 
        data['code'] = open(filename).read()
 
455
        data = self._run_function(lambda: execfile(filename, global_space),
 
456
            code = open(filename).read())
447
457
        return data
448
458
 
449
459
    def _execstring(self, string, global_space):
453
463
        def f():
454
464
            exec string in global_space
455
465
            
456
 
        data = self._run_function(f)
457
 
        data['code'] = string
 
466
        data = self._run_function(f, code=string)
458
467
        return data
459
468
 
460
469
    def _initialise_global_space(self, global_space):
467
476
        global_space['raw_input'] = lambda x=None: raw_input()
468
477
        return global_space
469
478
 
470
 
    def _run_function(self, function):
 
479
    def _run_function(self, function, code):
471
480
        """ Run the provided function with the provided stdin, capturing stdout and stderr
472
481
        and the return value.
473
482
        Return all the output data.
 
483
        code: The full text of the code, which needs to be stored as part of
 
484
        the returned dictionary.
474
485
        """
475
486
        import sys, StringIO
476
487
        sys_stdout, sys_stdin, sys_stderr = sys.stdout, sys.stdin, sys.stderr
493
504
 
494
505
        self._current_filespace_copy.flush_all()
495
506
            
496
 
        return {'result': result,
 
507
        return {'code': code,
 
508
                'result': result,
497
509
                'exception': exception_name,
498
510
                'stdout': output_stream.getvalue(),
499
511
                'stderr': output_stream.getvalue(),