コード例 #1
0
        public void TestConflictResolution(string original, string changeOne, string changeTwo, string expected)
        {
            var patcher = new diff_match_patch();
            var patchOne = patcher.patch_toText(patcher.patch_make(original, changeOne));
            var patchTwo = patcher.patch_toText(patcher.patch_make(original, changeTwo));

            var page = new VersionedWikiPage("test", original);
            var updater = new WikiPageUpdater(patcher);

            updater.ApplyUpdate(page, 0, patchOne);
            updater.ApplyUpdate(page, 0, patchTwo);

            Assert.AreEqual(expected, page.Text);
        }
        public void getChangesTest()
        {
            diff_match_patch testDiff = new diff_match_patch();

            string name = "TestName3.txt", contents = "Here is the contents of the file.";
            Document testDoc = new Document(name, contents);

            string text2 = "Here are the contents of the file.";
            List<Patch> testPatch = testDiff.patch_make(testDoc.FileContents, text2);

            testDoc.changeDocument(testPatch);

            Assert.AreEqual(testDiff.patch_toText(testPatch), testDiff.patch_toText(testDoc.getChanges(0).Values.Last()));
        }
        public void changeDocumentTest()
        {
            diff_match_patch testDIff = new diff_match_patch();

            string name = "TestName2.txt", contents = "Here is the contents of the file.";
            Document testDoc = new Document(name, contents);

            string text2 = "Here are the contents of the file.";
            List<Patch> testPatch = testDIff.patch_make(testDoc.FileContents, text2);

            testDoc.changeDocument(testPatch);
            testDoc.saveDocument();

            Assert.AreEqual(text2, testDoc.FileContents);
        }
コード例 #4
0
		/// <summary>
		/// Diff two JSON objects.
		/// 
		/// The output is a JObject that contains enough information to represent the
		/// delta between the two objects and to be able perform patch and reverse operations.
		/// </summary>
		/// <param name="left">The base JSON object</param>
		/// <param name="right">The JSON object to compare against the base</param>
		/// <returns>JSON Patch Document</returns>
		public JToken Diff(JToken left, JToken right)
		{
			if (left == null)
				left = new JValue("");
			if (right == null)
				right = new JValue("");

			if (left.Type == JTokenType.Object && right.Type == JTokenType.Object)
			{
				return ObjectDiff((JObject)left, (JObject)right);
			}

			if (_options.ArrayDiff == ArrayDiffMode.Efficient
				&& left.Type == JTokenType.Array
				&& right.Type == JTokenType.Array)
			{
				return ArrayDiff((JArray)left, (JArray)right);
			}

			if (_options.TextDiff == TextDiffMode.Efficient
				&& left.Type == JTokenType.String
				&& right.Type == JTokenType.String
				&& (left.ToString().Length > _options.MinEfficientTextDiffLength || right.ToString().Length > _options.MinEfficientTextDiffLength))
			{
				var dmp = new diff_match_patch();
				List<Patch> patches = dmp.patch_make(left.ToObject<string>(), right.ToObject<string>());
				return patches.Any()
					? new JArray(dmp.patch_toText(patches), 0, (int)DiffOperation.TextDiff)
					: null;
			}

			if (!JToken.DeepEquals(left, right))
				return new JArray(left, right);

			return null;
		}
コード例 #5
0
ファイル: TaskUtils.cs プロジェクト: fqncom/tomcraporigami
 private static String GetRevisedTextByDiff(String original, String delta, String revised)
 {
     if (string.Equals(delta, revised) || string.Equals(delta, original))
     {
         return revised;
     }
     // TODO 由于不知道这里的业务逻辑,所以暂时不做处理  ,后续.找到对应的类
     diff_match_patch diff = new diff_match_patch();
     return (String)diff.patch_apply(diff.patch_make(original, delta), revised)[0];
 }
コード例 #6
0
        public void UpdateRequestServer_ExistingIsXA1B1A2UpdateRequestXC1_PatchXA1B1A2()
        {
            const string memberIdA = "1";
            const string memberIdB = "2";
            const string memberIdC = "3";
            const string documentId = "MyDoc";
            const string initialContent = "test";
            const string contentA1 = "tests";
            const string contentA2 = "testst";
            const string contentB1 = "testi";
            const string contentC1 = "test ";
            const string contentA1B1 = "testsi";
            const string contentA1B1A2 = "teststi";
            const string resultingContent = "teststi ";
            const string owner = "max";
            const string host = "http://localhost:9000";
            SHA1 sha1 = new SHA1CryptoServiceProvider();
            byte[] initialHash = sha1.ComputeHash(Encoding.UTF8.GetBytes(initialContent));
            byte[] hashA1 = sha1.ComputeHash(Encoding.UTF8.GetBytes(contentA1));
            var diffMatchPath = new diff_match_patch();
            var editor = MockRepository.GenerateStub<SharedTextEditor>();
            editor.Stub(x => x.GetText(documentId)).Return(initialContent).Repeat.Once();
            editor.Stub(x => x.GetText(documentId)).Return(contentA1).Repeat.Once();
            editor.Stub(x => x.GetText(documentId)).Return(contentA1B1).Repeat.Once();
            editor.Stub(x => x.GetText(documentId)).Return(contentA1B1A2).Repeat.Once();
            var communication = MockRepository.GenerateStub<IClientServerCommunication>();

            //act
            var logic = new SharedTextEditorPatchingLogic(owner, host, editor, communication);

            editor.Raise(x => x.FindDocumentRequest += null, editor, documentId);
            logic.OpenDocument(new DocumentDto
            {
                DocumentId = documentId,
                Content = initialContent,
                Owner = owner,
                OwnerHost = host,
                RevisionId = 1
            });

            logic.UpdateRequest(new UpdateDto
            {
                DocumentId = documentId,
                MemberName = memberIdA,
                PreviousHash = initialHash,
                PreviousRevisionId = 1,
                Patch = diffMatchPath.patch_make(initialContent, contentA1)
            });

            logic.UpdateRequest(new UpdateDto
            {
                DocumentId = documentId,
                MemberName = memberIdB,
                PreviousHash = initialHash,
                PreviousRevisionId = 1,
                Patch = diffMatchPath.patch_make(initialContent, contentB1)
            });

            logic.UpdateRequest(new UpdateDto
            {
                DocumentId = documentId,
                MemberName = memberIdA,
                PreviousHash = hashA1,
                PreviousRevisionId = 2,
                Patch = diffMatchPath.patch_make(contentA1, contentA2)
            });

            logic.UpdateRequest(new UpdateDto
            {
                DocumentId = documentId,
                MemberName = memberIdC,
                PreviousHash = initialHash,
                PreviousRevisionId = 1,
                Patch = diffMatchPath.patch_make(initialContent, contentC1)
            });

            //assert
            var args = editor.GetArgumentsForCallsMadeOn(x => x.UpdateText(null, null), x => x.IgnoreArguments());
            Assert.That(args[0][1], Is.EqualTo(initialContent));
            Assert.That(args[1][1], Is.EqualTo(contentA1));
            Assert.That(args[2][1], Is.EqualTo(contentA1B1));
            Assert.That(args[3][1], Is.EqualTo(contentA1B1A2));
            Assert.That(args[4][1], Is.EqualTo(resultingContent));
        }