def testFilesUntouched(self): fake_file = test_util.FakeFile('', 'goo.xml') self.scrubber.ScrubFile(fake_file, None) self.assertFalse(fake_file.written) fake_file = test_util.FakeFile('<module></module>') self.scrubber.ScrubFile(fake_file, None) self.assertFalse(fake_file.written)
def testBatch(self): scrubber = java_scrubber.EmptyJavaFileScrubber(base.ACTION_DELETE) empty_file = test_util.FakeFile('', 'EmptyFile.java') nonempty_file = test_util.FakeFile('public class Foo {}', 'Foo.java') fake_context = FakeContext() scrubber.BatchScrubFiles([empty_file, nonempty_file], fake_context) self.assert_(empty_file.deleted, 'ScrubFile did not delete empty file') self.assert_(not nonempty_file.deleted, 'ScrubFile deleted non-empty file')
def assertTestSizeScrubbed(self, original, expected=None): if expected is None: expected = original fake_file = test_util.FakeFile(original) self.scrubber.ScrubFile(fake_file, None) self.assertMultiLineEqual(expected, fake_file.Contents())
def TestCommentExtractor(test_case, extractor, source_file, expected_file): comments = extractor.ExtractComments( test_util.FakeFile(filename=source_file)) expected_text = file_util.Read(expected_file) expected_json = simplejson.loads(expected_text) expected_comments = comment_scrubber.CommentsFromJson(expected_json) test_case.assertListEqual(expected_comments, comments)
def assertContentsError(self, lines): context = FakeContext() scrubber = comment_scrubber.CommentScrubber( comment_scrubber.CLikeCommentExtractor()) file_obj = test_util.FakeFile(contents=u'\n'.join(lines)) scrubber.ScrubFile(file_obj, context) self.assertEqual(1, len(context.errors)) error, = context.errors self.assertEqual('COMMENT_SCRUBBER_ERROR', error.filter)
def assertCoalesces(self, original, expected=None, maximum_blank_lines=1): scrubber = java_scrubber.CoalesceBlankLinesScrubber( maximum_blank_lines) if not expected: expected = original fake_file = test_util.FakeFile(original) scrubber.ScrubFile(fake_file, None) self.assertMultiLineEqual(expected, fake_file.Contents())
def assertNotScrubbed(self, text, internal_package, public_package, filename=''): scrubber = java_scrubber.JavaRenameScrubber(internal_package, public_package) f = test_util.FakeFile(text, filename) scrubber.ScrubFile(f, None) if f.Contents() != text: self.fail("Text %s was scrubbed, but shouldn't be" % text)
def RunScenario(self, scenario_name, extractor=None): if not extractor: extractor = comment_scrubber.CLikeCommentExtractor() scrubber = comment_scrubber.CommentScrubber(extractor) scenario_dir = os.path.join(TEST_DATA_DIR, 'directives', scenario_name) unstripped = os.path.join(scenario_dir, 'input.txt') file_obj = test_util.FakeFile(filename=unstripped) scrubber.ScrubFile(file_obj, None) expected = os.path.join(scenario_dir, 'expected.txt') out = os.path.join(FLAGS.test_tmpdir, scenario_name + '.out.txt') file_util.Write(out, file_obj.new_contents) basetest.DiffTestFiles(out, expected)
def assertScrubbed(self, expected, text, internal_package, public_package, filename=''): scrubber = java_scrubber.JavaRenameScrubber(internal_package, public_package) f = test_util.FakeFile(text, filename) scrubber.ScrubFile(f, None) if f.Contents() == text: self.fail("Text %s was not scrubbed, but should've been" % text) self.assertEqual(expected, f.Contents())
def assertScrubbing(self, contents, expected): fake_file = test_util.FakeFile(contents, 'fake_file.gwt.xml') self.scrubber.ScrubFile(fake_file, None) basetest.DiffTestStrings(expected, fake_file.Contents())
def testRaises(self): scrubber = java_scrubber.EmptyJavaFileScrubber(base.ACTION_IGNORE) fake_file = test_util.FakeFile('', 'EmptyFile.java') fake_context = FakeContext() self.assertRaises(base.Error, scrubber.BatchScrubFiles, [fake_file], fake_context)
def assertContents(self, scrubber, expected_lines, actual_lines): context = FakeContext() file_obj = test_util.FakeFile(contents=u'\n'.join(actual_lines)) scrubber.ScrubFile(file_obj, context) self.assertMultiLineEqual(u'\n'.join(expected_lines), file_obj.new_contents)
def testRegexSucceeds(self): subs = [(r'fo{2}', 'bar'), (r'(he)(l)\2o', r'\1\2\2 no')] repl = replacer.RegexReplacerScrubber(subs) fake_file = test_util.FakeFile('hello foo') repl.ScrubFile(fake_file, None) self.assertEqual('hell no bar', fake_file.Contents())
def testNoRegex(self): subs = [(r'fo*', 'bar')] repl = replacer.ReplacerScrubber(subs) fake_file = test_util.FakeFile('hello fo*') # Matches literal 'fo*' repl.ScrubFile(fake_file, None) self.assertEqual('hello bar', fake_file.Contents())
def assertContentsPreserved(self, contents, extractor): scrubber = comment_scrubber.CommentScrubber(extractor) fake_file = test_util.FakeFile(contents=contents) scrubber.ScrubFile(fake_file, None) self.assertMultiLineEqual(contents, fake_file.new_contents)
def testScrubbing(self): contents = '<module><inherits name="banana.banana"/></module>' fake_file = test_util.FakeFile(contents) self.scrubber.ScrubFile(fake_file, None) self.assertEquals(contents, fake_file.Contents())
def assertNotMeaningful(self, text, filename=None): scrubber = java_scrubber.EmptyJavaFileScrubber(base.ACTION_DELETE) file_obj = test_util.FakeFile(text, filename) scrubber.BatchScrubFiles([file_obj], None) if not file_obj.deleted: self.fail('Text %s is not meaningful, but should be' % text)
def testPlainSucceeds(self): subs = [('foo', 'bar'), ('hello', 'goodbye')] repl = replacer.RegexReplacerScrubber(subs) fake_file = test_util.FakeFile('hello foo') repl.ScrubFile(fake_file, None) self.assertEqual('goodbye bar', fake_file.Contents())
def testError(self): scrubber = java_scrubber.EmptyJavaFileScrubber(base.ACTION_ERROR) fake_file = test_util.FakeFile('') fake_context = FakeContext() scrubber.BatchScrubFiles([fake_file], fake_context) self.assert_(fake_context.error_added, 'ScrubFile did not error')
def testNoMatch(self): subs = [(r'bl(a)h', 'bar')] repl = replacer.RegexReplacerScrubber(subs) fake_file = test_util.FakeFile('hello foo') # r'bl(a)h' not found here repl.ScrubFile(fake_file, None) self.assertEqual('hello foo', fake_file.Contents()) # No new contents
def testDelete(self): scrubber = java_scrubber.EmptyJavaFileScrubber(base.ACTION_DELETE) fake_file = test_util.FakeFile('', 'EmptyFile.java') fake_context = FakeContext() scrubber.BatchScrubFiles([fake_file], fake_context) self.assert_(fake_file.deleted, 'ScrubFile did not delete')