Permalink
Browse files

Fixes #45 and creates a test that works with the new version

  • Loading branch information...
Bruno Perel
Bruno Perel committed Nov 15, 2012
1 parent 7b93f2c commit 2e67d0343a02a4c4408d8f1cbc2b6a0b677129d9
Showing with 94 additions and 1 deletion.
  1. +7 −1 functions/xdiff/xdiff_string_patch.js
  2. +87 −0 tests/xdiff/xdiff_string_patch.html
@@ -135,6 +135,9 @@ function xdiff_string_patch (originalStr, patch, flags, error) {
}
}
}
+ while (linePos < origLines.length) {
+ newStrArr[newStrArr.length] = origLines[linePos++];
+ }
} else if (flags & OPTS.XDIFF_PATCH_REVERSE) { // Only differs from above by a few lines
for (i = 0, ll = lines.length; i < ll; i++) {
ranges = lines[i].match(rangeExp);
@@ -165,9 +168,12 @@ function xdiff_string_patch (originalStr, patch, flags, error) {
}
}
}
+ while (linePos < origLines.length) {
+ newStrArr[newStrArr.length] = origLines[linePos++];
+ }
}
if (typeof(error) === 'string') {
this.window[error] = errors;
}
return newStrArr.join('\n');
-}
+}
@@ -0,0 +1,87 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
+ "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+<head>
+<style type="text/css">
+ textarea {
+ display: none;
+ }
+</style>
+<script src="http://code.jquery.com/jquery-latest.js"></script>
+<link rel="stylesheet" href="http://github.com/jquery/qunit/raw/master/qunit/qunit.css" type="text/css" media="screen" />
+<script type="text/javascript" src="http://github.com/jquery/qunit/raw/master/qunit/qunit.js"></script>
+<script type="text/javascript" src="../../../functions/xdiff/xdiff_string_patch.js"></script>
+
+
+<script>
+$(document).ready(function(){
+ test("Original file+added lines-removed lines has same number of lines as patched file", function() {
+ var original_text = document.getElementById('text').innerHTML;
+ var patch = document.getElementById('patch').innerHTML
+ var patched_text = xdiff_string_patch (original_text, patch);
+
+ var added_lines = patch.match(/(^|\n)\+/g).length;
+ var removed_lines = patch.match(/(^|\n)\-/g).length;
+ var nb_lines_original = original_text.match(/\n/g).length+1;
+ var nb_lines_patched = patched_text.match(/\n/g).length+1;
+ equal(nb_lines_original+added_lines-removed_lines, nb_lines_patched, "Line number is the same");
+ });
+});
+</script>
+
+</head>
+<body>
+ <h1 id="qunit-header">QUnit for xstring_diff_patch</h1>
+ <h2 id="qunit-banner"></h2>
+ <h2 id="qunit-userAgent"></h2>
+ <ol id="qunit-tests"></ol>
+ <div id="qunit-fixture">test markup, will be hidden</div>
+ <textarea id="text" cols="150" rows="30">header('Content-Type: text/html; charset=utf-8');
+header("Cache-Control: no-cache, must-revalidate");
+header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
+require_once('_priv/Admin.priv.class.php');
+require_once('work_in_progress.php');
+require_once('JS.class.php');
+require_once('Menu.class.php');
+require_once('Util.class.php');
+if (Util::isLocalHost() || isset($_GET['dbg'])) {
+ error_reporting(E_ALL);
+}
+else {
+ error_reporting(E_STRICT | E_WARNING);
+}
+
+if (isset($_GET['action']) && $_GET['action'] == 'logout') {
+ setCookie('user','',time()-3600);
+ setCookie('pass','',time()-3600);
+}
+else {
+ if (isset($_SESSION['user']) && isset($_SESSION['pass']) && !isset($_COOKIE['user']) ) {
+ setCookie('user',$_SESSION['user'],time()+3600);
+ }
+}
+
+echo 'Hi there';</textarea>
+
+<textarea id="patch" cols="100" rows="20">
+@@ -6,13 +6,12 @@
+ require_once('JS.class.php');
+ require_once('Menu.class.php');
+-require_once('Util.class.php');
++require_once('Util.class.php');
+ if (Util::isLocalHost() || isset($_GET['dbg'])) {
+ error_reporting(E_ALL);
+ }
+-else {
+- error_reporting(E_STRICT | E_WARNING);
+-}
++
++echo 'New line test';
+
+ if (isset($_GET['action']) && $_GET['action'] == 'logout') {
+ setCookie('user','',time()-3600);
+</textarea>
+
+<textarea id="patched_text" cols="150" rows="30"></textarea>
+</body>
+</html>

0 comments on commit 2e67d03

Please sign in to comment.