コード例 #1
0
 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"));
 }
コード例 #2
0
ファイル: update.php プロジェクト: ZerGabriel/cms-1
 private function _apply_patch()
 {
     $patch = $this->request->post('patch');
     try {
         Patch::apply($patch);
     } catch (Validation_Exception $ex) {
         Messages::errors($ex->errors());
     } catch (Kohana_Exception $ex) {
         Messages::errors($ex->getMessage());
     }
     $this->go_back();
 }
コード例 #3
0
 /**
  * Merge a set of patches onto the text.  Return a patched text, as well
  * as a list of true/false values indicating which patches were applied.
  *
  * @param PatchObject[] $patches Array of PatchObjects.
  * @param string        $text    Old text.
  *
  * @return array Two element Array, containing the new text and an array of boolean values.
  */
 public function patch_apply($patches, $text)
 {
     return $this->patch->apply($patches, $text);
 }