diff --git a/csharp/DiffMatchPatch.cs b/csharp/DiffMatchPatch.cs index 6e196418..dc3717cf 100644 --- a/csharp/DiffMatchPatch.cs +++ b/csharp/DiffMatchPatch.cs @@ -69,6 +69,9 @@ public class Diff { public Diff(Operation operation, string text) { // Construct a diff with the specified operation and text. this.operation = operation; + if (text == null) { + throw new NullReferenceException("text must contain a value."); + } this.text = text; } diff --git a/csharp/tests/DiffMatchPatchTest.cs b/csharp/tests/DiffMatchPatchTest.cs index 5a226ee2..9474c045 100644 --- a/csharp/tests/DiffMatchPatchTest.cs +++ b/csharp/tests/DiffMatchPatchTest.cs @@ -1095,6 +1095,20 @@ public void patch_applyTest() { assertEquals("patch_apply: Edge partial match.", "x123\tTrue", resultStr); } + public void diff_DiffNullTextTest() { + var operations = Enum.GetValues(); + foreach (var operation in operations) { + try { + var diff = new Diff(operation, null); + assertFail($"diff_DiffNullTextTest: operation {operation} with null text."); + } catch (NullReferenceException) { + // Exception expected. + } catch { + assertFail($"diff_DiffNullTextTest: operation {operation} with null text throwed an invalid exception."); + } + } + } + private string[] diff_rebuildtexts(List diffs) { string[] text = { "", "" }; foreach (Diff myDiff in diffs) { @@ -1225,6 +1239,8 @@ public static void Main(string[] args) { dmp.patch_addPaddingTest(); dmp.patch_applyTest(); + dmp.diff_DiffNullTextTest(); + Console.WriteLine("All tests passed."); } }