#!/usr/bin/env python import difflib def run_test(expected_tag, oldtext, newtext): a = oldtext.splitlines() b = newtext.splitlines() opcodes = difflib.SequenceMatcher(None, a, b).get_opcodes() print "Running '%s' test case..." % expected_tag if len(opcodes) == 2 and \ opcodes[0][0] == "replace" and \ opcodes[1][0] == expected_tag: print "\tPassed." % expected_tag else: print "\tFailed. Should have seen:" print "\t\treplace" print "\t\t%s" % expected_tag print print "\tInstead, saw:" for tag, i1, i2, j1, j2 in opcodes: print "\t\t%s" % tag print run_test("insert", "This is my old file, containing only one line.", "This is my new file.\ It contains multiple lines.\ SequenceMatcher should see two blocks as a result.") run_test("delete", "This is my old file.\ It contains multiple lines.\ SequenceMatcher should see two blocks as a result.", "This is my new file, containing only one line.")