def RunTest(test_case_file): fp = open("tests/" + test_case_file + "/config", "r") assert fp != None work_dir = "tests/" + test_case_file passed = True try: for line in fp: wrapped = line.strip() if not wrapped: continue language_token, source_file, time, memory, data, expected = wrapped.split(" ") compiler.Compile(language_token = language_token, \ source_file = source_file, \ work_dir = work_dir) actual = runner.Run(language_token = language_token, \ source_file = source_file, \ cpu_time = int(time) / 1000.0, \ real_time = int(time) / 1000.0 * 2, \ memory = int(memory) * 1024 * 1024, \ test_case = data, \ data_dir = work_dir, \ work_dir = work_dir) assert actual.startswith(expected), \ "test case'%s': expected answer is %s but actual answer is %s." \ % (wrapped, expected, actual) except: traceback.print_exc() passed = False finally: fp.close() if passed: print "testing " + test_case_file + " \033[0;32mPASSED\033[m" else: print "testing " + test_case_file + " \033[0;31mFAILED\033[m"
def _AssertErrors(self, original, expected_errors): """Asserts that the error fixer corrects original to expected.""" # Trap gjslint's output parse it to get messages added. error_accumulator = erroraccumulator.ErrorAccumulator() runner.Run('testing.js', error_accumulator, source=original) error_nums = [e.code for e in error_accumulator.GetErrors()] error_nums.sort() expected_errors.sort() self.assertListEqual(error_nums, expected_errors)
def _AssertFixes(self, original, expected, include_header=True): """Asserts that the error fixer corrects original to expected.""" if include_header: original = self._GetHeader() + original expected = self._GetHeader() + expected actual = StringIO.StringIO() runner.Run('testing.js', error_fixer.ErrorFixer(actual), original) actual.seek(0) expected = [x + '\n' for x in expected] self.assertListEqual(actual.readlines(), expected)
def _CheckPath(path): """ Check a path and return any errors. Args: path: paths to check. Returns: A list of errorrecord.ErrorRecords for any found errors. """ error_handler = erroraccumulator.ErrorAccumulator() runner.Run(path, error_handler) make_error_record = lambda err: errorrecord.MakeErrorRecord(path, err) return map(make_error_record, error_handler.GetErrors())
def testRunOnMissingFile(self): mock_error_handler = self.mox.CreateMock(errorhandler.ErrorHandler) def ValidateError(err): return (isinstance(err, error.Error) and err.code is errors.FILE_NOT_FOUND and err.token is None) mock_error_handler.HandleFile('does_not_exist.js', None) mock_error_handler.HandleError(mox.Func(ValidateError)) mock_error_handler.FinishFile() self.mox.ReplayAll() runner.Run('does_not_exist.js', mock_error_handler) self.mox.VerifyAll()
def testBadTokenization(self): mock_error_handler = self.mox.CreateMock(errorhandler.ErrorHandler) def ValidateError(err): return (isinstance(err, error.Error) and err.code is errors.FILE_IN_BLOCK and err.token.string == '}') mock_error_handler.HandleFile('foo.js', mox.IsA(tokens.Token)) mock_error_handler.HandleError(mox.Func(ValidateError)) mock_error_handler.HandleError(mox.IsA(error.Error)) mock_error_handler.FinishFile() self.mox.ReplayAll() source = StringIO.StringIO(_BAD_TOKENIZATION_SCRIPT) runner.Run('foo.js', mock_error_handler, source) self.mox.VerifyAll()
def main(argv=None): """Main function. Args: argv: Sequence of command line arguments. """ if argv is None: argv = flags.FLAGS(sys.argv) suffixes = ['.js'] if FLAGS.additional_extensions: suffixes += ['.%s' % ext for ext in FLAGS.additional_extensions] files = fileflags.GetFileList(argv, 'JavaScript', suffixes) fixer = error_fixer.ErrorFixer() # Check the list of files. for filename in files: runner.Run(filename, fixer)
def testFixJsStyle(self): test_cases = [['fixjsstyle.in.js', 'fixjsstyle.out.js'], ['indentation.js', 'fixjsstyle.indentation.out.js'], ['fixjsstyle.html.in.html', 'fixjsstyle.html.out.html']] for [running_input_file, running_output_file] in test_cases: input_filename = None golden_filename = None current_filename = None try: input_filename = '%s/%s' % (_RESOURCE_PREFIX, running_input_file) current_filename = input_filename golden_filename = '%s/%s' % (_RESOURCE_PREFIX, running_output_file) current_filename = golden_filename except IOError as ex: raise IOError('Could not find testdata resource for %s: %s' % (current_filename, ex)) if running_input_file == 'fixjsstyle.in.js': with open(input_filename) as f: for line in f: # Go to last line. pass self.assertTrue( line == line.rstrip(), '%s file should not end ' 'with a new line.' % (input_filename)) # Autofix the file, sending output to a fake file. actual = StringIO.StringIO() runner.Run(input_filename, error_fixer.ErrorFixer(actual)) # Now compare the files. actual.seek(0) expected = open(golden_filename, 'r') self.assertEqual(actual.readlines(), expected.readlines())