From 6eb36a3a28911578cabc6a99e3260fdc362c9aaa Mon Sep 17 00:00:00 2001 From: Sebastian Lackner Date: Sat, 2 Apr 2016 20:25:37 +0200 Subject: [PATCH] patchutils.py: Add tests for apply_patch function. --- staging/patchutils.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/staging/patchutils.py b/staging/patchutils.py index ecf15c27..1b9c81c5 100644 --- a/staging/patchutils.py +++ b/staging/patchutils.py @@ -691,6 +691,7 @@ def generate_ifdef_patch(original, patched, ifdef): if __name__ == "__main__": import unittest + # Basic tests for _parse_author() and _parse_subject() class PatchParserTests(unittest.TestCase): def test_author(self): author = _parse_author("Author Name ") @@ -727,6 +728,39 @@ if __name__ == "__main__": subject = _parse_subject("[PATCH] component: Subject (resend).") self.assertEqual(subject, ("component: Subject", 1)) + # Basic tests for apply_patch() + class PatchApplyTests(unittest.TestCase): + def test_apply(self): + source = ["line1();", "line2();", "line3();", + "function(arg1);", + "line5();", "line6();", "line7();"] + original = tempfile.NamedTemporaryFile() + original.write("\n".join(source + [""])) + original.flush() + + source = ["@@ -1,7 +1,7 @@", + " line1();", " line2();", " line3();", + "-function(arg1);", + "+function(arg2);", + " line5();", " line6();", " line7();"] + patchfile = tempfile.NamedTemporaryFile() + patchfile.write("\n".join(source + [""])) + patchfile.flush() + + expected = ["line1();", "line2();", "line3();", + "function(arg2);", + "line5();", "line6();", "line7();"] + result = apply_patch(original, patchfile, fuzz=0) + lines = result.read().rstrip("\n").split("\n") + self.assertEqual(lines, expected) + + expected = ["line1();", "line2();", "line3();", + "function(arg1);", + "line5();", "line6();", "line7();"] + result = apply_patch(result, patchfile, reverse=True, fuzz=0) + lines = result.read().rstrip("\n").split("\n") + self.assertEqual(lines, expected) + # Basic tests for _preprocess_source() class PreprocessorTests(unittest.TestCase): def test_preprocessor(self):