Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Suggested fix for NullReferenceException in C# #140

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions csharp/DiffMatchPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
16 changes: 16 additions & 0 deletions csharp/tests/DiffMatchPatchTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Operation>();
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<Diff> diffs) {
string[] text = { "", "" };
foreach (Diff myDiff in diffs) {
Expand Down Expand Up @@ -1225,6 +1239,8 @@ public static void Main(string[] args) {
dmp.patch_addPaddingTest();
dmp.patch_applyTest();

dmp.diff_DiffNullTextTest();

Console.WriteLine("All tests passed.");
}
}