Пример #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()));
        }
Пример #3
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;
		}