public function testApply() { $this->m->setDistance(1000); $this->m->setThreshold(0.5); $this->p->setDeleteTreshold(0.5); // Null case. $patches = $this->p->make("", ""); $this->assertEquals(array("Hello world.", array()), $this->p->apply($patches, "Hello world.")); // Exact match. $patches = $this->p->make("The quick brown fox jumps over the lazy dog.", "That quick brown fox jumped over a lazy dog."); $this->assertEquals(array("That quick brown fox jumped over a lazy dog.", array(true, true)), $this->p->apply($patches, "The quick brown fox jumps over the lazy dog.")); // Partial match. $this->assertEquals(array("That quick red rabbit jumped over a tired tiger.", array(true, true)), $this->p->apply($patches, "The quick red rabbit jumps over the tired tiger.")); // Failed match. $this->assertEquals(array("I am the very model of a modern major general.", array(false, false)), $this->p->apply($patches, "I am the very model of a modern major general.")); // Big delete, small change. $patches = $this->p->make("x1234567890123456789012345678901234567890123456789012345678901234567890y", "xabcy"); $this->assertEquals(array("xabcy", array(true, true)), $this->p->apply($patches, "x123456789012345678901234567890-----++++++++++-----123456789012345678901234567890y")); // Big delete, big change 1. $patches = $this->p->make("x1234567890123456789012345678901234567890123456789012345678901234567890y", "xabcy"); $this->assertEquals(array("xabc12345678901234567890---------------++++++++++---------------12345678901234567890y", array(false, true)), $this->p->apply($patches, "x12345678901234567890---------------++++++++++---------------12345678901234567890y")); // Big delete, big change 2. $this->p->setDeleteTreshold(0.6); $patches = $this->p->make("x1234567890123456789012345678901234567890123456789012345678901234567890y", "xabcy"); $this->assertEquals(array("xabcy", array(true, true)), $this->p->apply($patches, "x12345678901234567890---------------++++++++++---------------12345678901234567890y")); $this->p->setDeleteTreshold(0.5); // Compensate for failed patch. $this->m->setDistance(0); $this->m->setThreshold(0.0); $patches = $this->p->make("abcdefghijklmnopqrstuvwxyz--------------------1234567890", "abcXXXXXXXXXXdefghijklmnopqrstuvwxyz--------------------1234567YYYYYYYYYY890"); $this->assertEquals(array("ABCDEFGHIJKLMNOPQRSTUVWXYZ--------------------1234567YYYYYYYYYY890", array(false, true)), $this->p->apply($patches, "ABCDEFGHIJKLMNOPQRSTUVWXYZ--------------------1234567890")); $this->m->setDistance(1000); $this->m->setThreshold(0.5); // No side effects. $patches = $this->p->make("", "test"); $patchesText = $this->p->toText($patches); $this->p->apply($patches, ""); $this->assertEquals($patchesText, $this->p->toText($patches)); // No side effects with major delete. $patches = $this->p->make("The quick brown fox jumps over the lazy dog.", "Woof"); $patchesText = $this->p->toText($patches); $this->p->apply($patches, "The quick brown fox jumps over the lazy dog."); $this->assertEquals($patchesText, $this->p->toText($patches)); // Edge exact match. $patches = $this->p->make("", "test"); $this->assertEquals(array("test", array(true)), $this->p->apply($patches, "")); // Near edge exact match. $patches = $this->p->make("XY", "XtestY"); $this->assertEquals(array("XtestY", array(true)), $this->p->apply($patches, "XY")); // Edge partial match. $patches = $this->p->make("y", "y123"); $this->assertEquals(array("x123", array(true)), $this->p->apply($patches, "x")); }
/** * Compute a list of patches to turn text1 into text2. * Use diffs if provided, otherwise compute it ourselves. * There are four ways to call this function, depending on what data is * available to the caller: * Method 1: * a = text1, b = text2 * Method 2: * a = diffs * Method 3 (optimal): * a = text1, b = diffs * Method 4 (deprecated, use method 3): * a = text1, b = text2, c = diffs * * @param string|array $a text1 (methods 1,3,4) or Array of diff arrays for text1 to text2 (method 2). * @param string|array|null $b text2 (methods 1,4) or Array of diff arrays for text1 to text2 (method 3) * or null (method 2). * @param array|null $c Array of diff arrays for text1 to text2 (method 4) or null (methods 1,2,3). * * @throws \InvalidArgumentException If unknown call format. * @return PatchObject[] Array of PatchObjects. */ public function patch_make($a, $b = null, $c = null) { return $this->patch->make($a, $b, $c); }