-
-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathDifference.cs
203 lines (175 loc) · 6.14 KB
/
Difference.cs
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
namespace KellermanSoftware.CompareNetObjects
{
/// <summary>
/// Detailed information about the difference
/// </summary>
public class Difference
{
/// <summary>
/// Name of Expected Object
/// </summary>
public string ExpectedName { get; set; }
/// <summary>
/// Name of Actual Object
/// </summary>
public string ActualName { get; set; }
/// <summary>
/// Returns the parent property name
/// </summary>
public string ParentPropertyName
{
get
{
if (PropertyName.EndsWith("]") && PropertyName.Contains("["))
{
int lastLeftSquare = PropertyName.LastIndexOf('[');
return PropertyName.Substring(0, lastLeftSquare);
}
if (PropertyName.Contains("."))
{
int lastPeriod = PropertyName.LastIndexOf('.');
if (lastPeriod > 0)
return PropertyName.Substring(0, lastPeriod);
}
return string.Empty;
}
}
/// <summary>
/// The breadcrumb of the property leading up to the value
/// </summary>
public string PropertyName { get; set; }
/// <summary>
/// The child property name
/// </summary>
public string ChildPropertyName { get; set; }
/// <summary>
/// Object1 Value as a string
/// </summary>
public string Object1Value { get; set; }
/// <summary>
/// Object2 Value as a string
/// </summary>
public string Object2Value { get; set; }
/// <summary>
/// The type of the first object
/// </summary>
public string Object1TypeName { get; set; }
/// <summary>
/// The type of the second object
/// </summary>
public string Object2TypeName { get; set; }
/// <summary>
/// A reference to the parent of object1
/// </summary>
public object ParentObject1 { get; set; }
/// <summary>
/// A reference to the parent of object2
/// </summary>
public object ParentObject2 { get; set; }
/// <summary>
/// Object1 as a reference
/// </summary>
public object Object1 { get; set; }
/// <summary>
/// Object2 as a reference
/// </summary>
public object Object2 { get; set; }
/// <summary>
/// Prefix to put on the beginning of the message
/// </summary>
public string MessagePrefix { get; set; }
/// <summary>
/// Item and property name only
/// </summary>
/// <returns></returns>
public string GetShortItem()
{
string message;
if (!String.IsNullOrEmpty(PropertyName))
{
if (String.IsNullOrEmpty(ChildPropertyName))
{
message = String.Format("{0}", PropertyName);
}
else
{
message = String.Format("{0}.{1}",
PropertyName,
ChildPropertyName);
}
}
else
{
message = String.Format("{0} != {1}",
ExpectedName,
ActualName);
}
if (!String.IsNullOrEmpty(MessagePrefix))
message = String.Format("{0}: {1}", MessagePrefix, message);
message = message.Replace("..", ".");
message = message.Replace(".[", "[");
return message;
}
/// <summary>
/// The type and index of what is compared
/// </summary>
/// <returns></returns>
public string GetWhatIsCompared()
{
string message;
if (!String.IsNullOrEmpty(PropertyName))
{
if (String.IsNullOrEmpty(ChildPropertyName))
{
message = String.Format("Types [{3},{4}], Item {0}.{2} != {1}.{2}",
ExpectedName,
ActualName,
PropertyName,
Object1TypeName,
Object2TypeName);
}
else
{
message = String.Format("Types [{4},{5}], Item {0}.{2}.{3} != {1}.{2}.{3}",
ExpectedName,
ActualName,
PropertyName,
ChildPropertyName,
Object1TypeName,
Object2TypeName);
}
}
else if (!String.IsNullOrEmpty(ChildPropertyName))
{
message = String.Format("Types [{2}.{4},{3}.{4}], Item {0} != {1}",
ExpectedName,
ActualName,
Object1TypeName,
Object2TypeName,
ChildPropertyName);
}
else
{
message = String.Format("Types [{2},{3}], Item {0} != {1}",
ExpectedName,
ActualName,
Object1TypeName,
Object2TypeName);
}
if (!String.IsNullOrEmpty(MessagePrefix))
message = String.Format("{0}: {1}", MessagePrefix, message);
message = message.Replace("..", ".");
message = message.Replace(".[", "[");
return message;
}
/// <summary>
/// Nicely formatted string
/// </summary>
/// <returns></returns>
public override string ToString()
{
return String.Format("{0}, Values ({1},{2})", GetWhatIsCompared(), Object1Value, Object2Value);
}
}
}