-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathDifferenceTests.cs
More file actions
147 lines (125 loc) · 3.29 KB
/
DifferenceTests.cs
File metadata and controls
147 lines (125 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System.IO;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Waher.Runtime.Text.Test
{
[TestClass]
public class DifferenceTests
{
[TestMethod]
public void Difference_Test_01_Insert()
{
TestStrings("HelloWorld", "Hello World", "Hello__ __World");
}
[TestMethod]
public void Difference_Test_02_Delete()
{
TestStrings("Hello World", "Hello", "Hello~~ World~~");
}
[TestMethod]
public void Difference_Test_03_Replace()
{
TestStrings("aaaa", "bbbb", "__bbbb__~~aaaa~~");
}
[TestMethod]
public void Difference_Test_04_NoChange()
{
TestStrings("aaaa", "aaaa", "aaaa");
}
[TestMethod]
public void Difference_Test_05_New()
{
TestStrings(string.Empty, "aaaa", "__aaaa__");
}
[TestMethod]
public void Difference_Test_06_DeleteAll()
{
TestStrings("aaaa", string.Empty, "~~aaaa~~");
}
[TestMethod]
public void Difference_Test_07_Empty()
{
TestStrings(string.Empty, string.Empty, string.Empty);
}
[TestMethod]
public void Difference_Test_08_Diff2_1()
{
TestStrings("ABCABBA", "CBABA", "~~AB~~C__B__A~~B~~BA");
}
[TestMethod]
public void Difference_Test_09_Rows()
{
// Ref: https://en.wikipedia.org/wiki/Diff
string Org = File.ReadAllText("Data\\Test_09_Org.txt");
string New = File.ReadAllText("Data\\Test_09_New.txt");
string Diff = File.ReadAllText("Data\\Test_09_Diff.txt");
TestRows(Org, New, Diff, true);
}
[TestMethod]
public void Difference_Test_10_Rows_Large()
{
string Org = File.ReadAllText("Data\\Test_10_Org.txt");
string New = File.ReadAllText("Data\\Test_10_New.txt");
string Diff = File.ReadAllText("Data\\Test_10_Diff.txt");
TestRows(Org, New, Diff, false);
}
private static void TestStrings(string s1, string s2, string Expected)
{
EditScript<char> Script = Difference.AnalyzeStrings(s1, s2);
StringBuilder sb = new();
foreach (Step<char> Step in Script.Steps)
{
switch (Step.Operation)
{
case EditOperation.Keep:
Append(sb, Step.Symbols, string.Empty, string.Empty);
break;
case EditOperation.Insert:
sb.Append("__");
Append(sb, Step.Symbols, string.Empty, string.Empty);
sb.Append("__");
break;
case EditOperation.Delete:
sb.Append("~~");
Append(sb, Step.Symbols, string.Empty, string.Empty);
sb.Append("~~");
break;
}
}
string Result = sb.ToString();
Assert.AreEqual(Expected, Result);
}
private static void TestRows(string s1, string s2, string Expected, bool ShowKeep)
{
EditScript<string> Script = Difference.AnalyzeRows(s1, s2);
StringBuilder sb = new();
foreach (Step<string> Step in Script.Steps)
{
switch (Step.Operation)
{
case EditOperation.Keep:
if (ShowKeep)
Append(sb, Step.Symbols, " ", "\r\n");
break;
case EditOperation.Insert:
Append(sb, Step.Symbols, "+ ", "\r\n");
break;
case EditOperation.Delete:
Append(sb, Step.Symbols, "- ", "\r\n");
break;
}
}
string Result = sb.ToString();
Assert.AreEqual(Expected, Result);
}
private static void Append<T>(StringBuilder sb, T[] Symbols, string Pre, string Post)
{
foreach (T Symbol in Symbols)
{
sb.Append(Pre);
sb.Append(Symbol.ToString());
sb.Append(Post);
}
}
}
}