aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSean Hall <r.sean.hall@gmail.com>2022-06-15 17:07:27 -0500
committerSean Hall <r.sean.hall@gmail.com>2022-06-15 18:27:22 -0500
commitec61ceb6b2bc0b6cf12259e08c1f8db2a9335773 (patch)
tree98dd2471c70cb25146acaadd534d717081247ba4
parent98c369a92891244bde76448ae4a2b623b3ab394c (diff)
downloadwix-ec61ceb6b2bc0b6cf12259e08c1f8db2a9335773.tar.gz
wix-ec61ceb6b2bc0b6cf12259e08c1f8db2a9335773.tar.bz2
wix-ec61ceb6b2bc0b6cf12259e08c1f8db2a9335773.zip
Improve WixAssert.StringEqual to print where the first difference is.
-rw-r--r--src/internal/WixBuildTools.TestSupport/XunitExtensions/WixAssert.cs92
1 files changed, 66 insertions, 26 deletions
diff --git a/src/internal/WixBuildTools.TestSupport/XunitExtensions/WixAssert.cs b/src/internal/WixBuildTools.TestSupport/XunitExtensions/WixAssert.cs
index 1ede55b3..d8d02746 100644
--- a/src/internal/WixBuildTools.TestSupport/XunitExtensions/WixAssert.cs
+++ b/src/internal/WixBuildTools.TestSupport/XunitExtensions/WixAssert.cs
@@ -5,9 +5,11 @@ namespace WixBuildTools.TestSupport
5 using System; 5 using System;
6 using System.Collections.Generic; 6 using System.Collections.Generic;
7 using System.Linq; 7 using System.Linq;
8 using System.Text;
8 using System.Xml.Linq; 9 using System.Xml.Linq;
9 using WixBuildTools.TestSupport.XunitExtensions; 10 using WixBuildTools.TestSupport.XunitExtensions;
10 using Xunit; 11 using Xunit;
12 using Xunit.Sdk;
11 13
12 public class WixAssert : Assert 14 public class WixAssert : Assert
13 { 15 {
@@ -23,7 +25,7 @@ namespace WixBuildTools.TestSupport
23 var additionalExpectedLines = expectedLines.Length > lineNumber ? String.Join(Environment.NewLine, expectedLines.Skip(lineNumber).Select((s, i) => $"{lineNumber + i}: {s}")) : $"Missing {actualLines.Length - lineNumber} lines"; 25 var additionalExpectedLines = expectedLines.Length > lineNumber ? String.Join(Environment.NewLine, expectedLines.Skip(lineNumber).Select((s, i) => $"{lineNumber + i}: {s}")) : $"Missing {actualLines.Length - lineNumber} lines";
24 var additionalActualLines = actualLines.Length > lineNumber ? String.Join(Environment.NewLine, actualLines.Skip(lineNumber).Select((s, i) => $"{lineNumber + i}: {s}")) : $"Missing {expectedLines.Length - lineNumber} lines"; 26 var additionalActualLines = actualLines.Length > lineNumber ? String.Join(Environment.NewLine, actualLines.Skip(lineNumber).Select((s, i) => $"{lineNumber + i}: {s}")) : $"Missing {expectedLines.Length - lineNumber} lines";
25 27
26 WixAssert.StringEqual(additionalExpectedLines, additionalActualLines); 28 Assert.Equal<object>(additionalExpectedLines, additionalActualLines, StringObjectEqualityComparer.InvariantCulture);
27 } 29 }
28 30
29 public static void CompareXml(XContainer xExpected, XContainer xActual) 31 public static void CompareXml(XContainer xExpected, XContainer xActual)
@@ -78,8 +80,7 @@ namespace WixBuildTools.TestSupport
78 80
79 public static void StringEqual(string expected, string actual, bool ignoreCase = false) 81 public static void StringEqual(string expected, string actual, bool ignoreCase = false)
80 { 82 {
81 var comparer = ignoreCase ? StringObjectEqualityComparer.InvariantCultureIgnoreCase : StringObjectEqualityComparer.InvariantCulture; 83 WixStringEqualException.ThrowIfNotEqual(expected, actual, ignoreCase);
82 Assert.Equal<object>(expected, actual, comparer);
83 } 84 }
84 85
85 public static void NotStringEqual(string expected, string actual, bool ignoreCase = false) 86 public static void NotStringEqual(string expected, string actual, bool ignoreCase = false)
@@ -88,29 +89,6 @@ namespace WixBuildTools.TestSupport
88 Assert.NotEqual<object>(expected, actual, comparer); 89 Assert.NotEqual<object>(expected, actual, comparer);
89 } 90 }
90 91
91 private class StringObjectEqualityComparer : IEqualityComparer<object>
92 {
93 public static readonly StringObjectEqualityComparer InvariantCultureIgnoreCase = new StringObjectEqualityComparer(true);
94 public static readonly StringObjectEqualityComparer InvariantCulture = new StringObjectEqualityComparer(false);
95
96 private readonly StringComparer stringComparer;
97
98 public StringObjectEqualityComparer(bool ignoreCase)
99 {
100 this.stringComparer = ignoreCase ? StringComparer.InvariantCultureIgnoreCase : StringComparer.InvariantCulture;
101 }
102
103 public new bool Equals(object x, object y)
104 {
105 return this.stringComparer.Equals((string)x,(string)y);
106 }
107
108 public int GetHashCode(object obj)
109 {
110 return this.stringComparer.GetHashCode((string)obj);
111 }
112 }
113
114 // There appears to have been a bug in VC++, which might or might not have been partially 92 // There appears to have been a bug in VC++, which might or might not have been partially
115 // or completely corrected. It was unable to disambiguate a call to: 93 // or completely corrected. It was unable to disambiguate a call to:
116 // Xunit::Assert::Throws(System::Type^, System::Action^) 94 // Xunit::Assert::Throws(System::Type^, System::Action^)
@@ -158,4 +136,66 @@ namespace WixBuildTools.TestSupport
158 return Xunit.Assert.Throws(exceptionType, testCode); 136 return Xunit.Assert.Throws(exceptionType, testCode);
159 } 137 }
160 } 138 }
139
140 internal class StringObjectEqualityComparer : IEqualityComparer<object>
141 {
142 public static readonly StringObjectEqualityComparer InvariantCultureIgnoreCase = new StringObjectEqualityComparer(true);
143 public static readonly StringObjectEqualityComparer InvariantCulture = new StringObjectEqualityComparer(false);
144
145 private readonly StringComparer stringComparer;
146
147 public StringObjectEqualityComparer(bool ignoreCase)
148 {
149 this.stringComparer = ignoreCase ? StringComparer.InvariantCultureIgnoreCase : StringComparer.InvariantCulture;
150 }
151
152 public new bool Equals(object x, object y)
153 {
154 return this.stringComparer.Equals((string)x, (string)y);
155 }
156
157 public int GetHashCode(object obj)
158 {
159 return this.stringComparer.GetHashCode((string)obj);
160 }
161 }
162
163 public class WixStringEqualException : XunitException
164 {
165 public WixStringEqualException(string userMessage) : base(userMessage) { }
166
167 public static void ThrowIfNotEqual(string expected, string actual, bool ignoreCase)
168 {
169 var comparer = ignoreCase ? StringObjectEqualityComparer.InvariantCultureIgnoreCase : StringObjectEqualityComparer.InvariantCulture;
170 if (comparer.Equals(expected, actual))
171 {
172 return;
173 }
174
175 var sbMessage = new StringBuilder();
176
177 try
178 {
179 Assert.Equal(expected, actual, ignoreCase);
180 }
181 catch (XunitException xe)
182 {
183 // If either string is not completely in the message, then make sure it gets in there.
184 if (!xe.Message.Contains(expected) || !xe.Message.Contains(actual))
185 {
186 sbMessage.AppendLine(xe.Message);
187 sbMessage.AppendLine();
188 sbMessage.AppendFormat("Expected: {0}", expected);
189 sbMessage.AppendLine();
190 sbMessage.AppendFormat("Actual: {0}", actual);
191 }
192 else
193 {
194 throw;
195 }
196 }
197
198 throw new WixStringEqualException(sbMessage.ToString());
199 }
200 }
161} 201}