summaryrefslogtreecommitdiff
path: root/src/internal/WixBuildTools.TestSupport/XunitExtensions/WixAssert.cs
diff options
context:
space:
mode:
authorRob Mensching <rob@firegiant.com>2022-11-08 14:58:05 -0800
committerRob Mensching <rob@firegiant.com>2022-11-08 16:20:25 -0800
commitc843b47d6233153fa961c6d0e61edf7cedf255bb (patch)
tree9eae6badd42d3badb8665b7414b4d44ca48d6ae1 /src/internal/WixBuildTools.TestSupport/XunitExtensions/WixAssert.cs
parent7e498d6348c26583972ea1cdf7d51dadc8f5b792 (diff)
downloadwix-c843b47d6233153fa961c6d0e61edf7cedf255bb.tar.gz
wix-c843b47d6233153fa961c6d0e61edf7cedf255bb.tar.bz2
wix-c843b47d6233153fa961c6d0e61edf7cedf255bb.zip
Separate WixInternal content from official WixToolset namespace
Diffstat (limited to 'src/internal/WixBuildTools.TestSupport/XunitExtensions/WixAssert.cs')
-rw-r--r--src/internal/WixBuildTools.TestSupport/XunitExtensions/WixAssert.cs201
1 files changed, 0 insertions, 201 deletions
diff --git a/src/internal/WixBuildTools.TestSupport/XunitExtensions/WixAssert.cs b/src/internal/WixBuildTools.TestSupport/XunitExtensions/WixAssert.cs
deleted file mode 100644
index a8513bfb..00000000
--- a/src/internal/WixBuildTools.TestSupport/XunitExtensions/WixAssert.cs
+++ /dev/null
@@ -1,201 +0,0 @@
1// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
2
3namespace WixBuildTools.TestSupport
4{
5 using System;
6 using System.Collections.Generic;
7 using System.Linq;
8 using System.Text;
9 using System.Xml.Linq;
10 using WixBuildTools.TestSupport.XunitExtensions;
11 using Xunit;
12 using Xunit.Sdk;
13
14 public class WixAssert : Assert
15 {
16 public static void CompareLineByLine(string[] expectedLines, string[] actualLines)
17 {
18 var lineNumber = 0;
19
20 for (; lineNumber < expectedLines.Length && lineNumber < actualLines.Length; ++lineNumber)
21 {
22 WixAssert.StringEqual($"{lineNumber}: {expectedLines[lineNumber]}", $"{lineNumber}: {actualLines[lineNumber]}");
23 }
24
25 var additionalExpectedLines = expectedLines.Length > lineNumber ? String.Join(Environment.NewLine, expectedLines.Skip(lineNumber).Select((s, i) => $"{lineNumber + i}: {s}")) : $"Missing {actualLines.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";
27
28 Assert.Equal<object>(additionalExpectedLines, additionalActualLines, StringObjectEqualityComparer.InvariantCulture);
29 }
30
31 public static void CompareXml(XContainer xExpected, XContainer xActual)
32 {
33 var expecteds = xExpected.Descendants().Select(x => $"{x.Name.LocalName}:{String.Join(",", x.Attributes().OrderBy(a => a.Name.LocalName).Select(a => $"{a.Name.LocalName}={a.Value}"))}");
34 var actuals = xActual.Descendants().Select(x => $"{x.Name.LocalName}:{String.Join(",", x.Attributes().OrderBy(a => a.Name.LocalName).Select(a => $"{a.Name.LocalName}={a.Value}"))}");
35
36 CompareLineByLine(expecteds.OrderBy(s => s).ToArray(), actuals.OrderBy(s => s).ToArray());
37 }
38
39 public static void CompareXml(string expectedPath, string actualPath)
40 {
41 var expectedDoc = XDocument.Load(expectedPath, LoadOptions.PreserveWhitespace | LoadOptions.SetBaseUri | LoadOptions.SetLineInfo);
42 var actualDoc = XDocument.Load(actualPath, LoadOptions.PreserveWhitespace | LoadOptions.SetBaseUri | LoadOptions.SetLineInfo);
43
44 CompareXml(expectedDoc, actualDoc);
45 }
46
47 /// <summary>
48 /// Dynamically skips the test.
49 /// Requires that the test was marked with a fact attribute derived from <see cref="WixBuildTools.TestSupport.XunitExtensions.SkippableFactAttribute" />
50 /// or <see cref="WixBuildTools.TestSupport.XunitExtensions.SkippableTheoryAttribute" />
51 /// </summary>
52 public static void Skip(string message)
53 {
54 throw new SkipTestException(message);
55 }
56
57 public static void SpecificReturnCode(int hrExpected, int hr, string format, params object[] formatArgs)
58 {
59 if (hrExpected != hr)
60 {
61 throw new SpecificReturnCodeException(hrExpected, hr, String.Format(format, formatArgs));
62 }
63 }
64
65 public static void Succeeded(int hr, string format, params object[] formatArgs)
66 {
67 if (0 > hr)
68 {
69 throw new SucceededException(hr, String.Format(format, formatArgs));
70 }
71 }
72
73 public static void StringCollectionEmpty(IList<string> collection)
74 {
75 if (collection.Count > 0)
76 {
77 Assert.True(false, $"The collection was expected to be empty, but instead was [{Environment.NewLine}\"{String.Join($"\", {Environment.NewLine}\"", collection)}\"{Environment.NewLine}]");
78 }
79 }
80
81 public static void StringEqual(string expected, string actual, bool ignoreCase = false)
82 {
83 WixStringEqualException.ThrowIfNotEqual(expected, actual, ignoreCase);
84 }
85
86 public static void NotStringEqual(string expected, string actual, bool ignoreCase = false)
87 {
88 var comparer = ignoreCase ? StringObjectEqualityComparer.InvariantCultureIgnoreCase : StringObjectEqualityComparer.InvariantCulture;
89 Assert.NotEqual<object>(expected, actual, comparer);
90 }
91
92 // There appears to have been a bug in VC++, which might or might not have been partially
93 // or completely corrected. It was unable to disambiguate a call to:
94 // Xunit::Assert::Throws(System::Type^, System::Action^)
95 // from a call to:
96 // Xunit::Assert::Throws(System::Type^, System::Func<System::Object^>^)
97 // that implicitly ignores its return value.
98 //
99 // The ambiguity may have been reported by some versions of the compiler and not by others.
100 // Some versions of the compiler may not have emitted any code in this situation, making it
101 // appear that the test has passed when, in fact, it hasn't been run.
102 //
103 // This situation is not an issue for C#.
104 //
105 // The following method is used to isolate DUtilTests in order to overcome the above problem.
106
107 /// <summary>
108 /// This shim allows C++/CLR code to call the Xunit method with the same signature
109 /// without getting an ambiguous overload error. If the specified test code
110 /// fails to generate an exception of the exact specified type, an assertion
111 /// exception is thrown. Otherwise, execution flow proceeds as normal.
112 /// </summary>
113 /// <typeparam name="T">The type name of the expected exception.</typeparam>
114 /// <param name="testCode">An Action delegate to run the test code.</param>
115 public static new void Throws<T>(System.Action testCode)
116 where T : System.Exception
117 {
118 Xunit.Assert.Throws<T>(testCode);
119 }
120
121 // This shim has been tested, but is not currently used anywhere. It was provided
122 // at the same time as the preceding shim because it involved the same overload
123 // resolution conflict.
124
125 /// <summary>
126 /// This shim allows C++/CLR code to call the Xunit method with the same signature
127 /// without getting an ambiguous overload error. If the specified test code
128 /// fails to generate an exception of the exact specified type, an assertion
129 /// exception is thrown. Otherwise, execution flow proceeds as normal.
130 /// </summary>
131 /// <param name="exceptionType">The type object associated with exceptions of the expected type.</param>
132 /// <param name="testCode">An Action delegate to run the test code.</param>
133 /// <returns>An exception of a type other than the type specified, is such an exception is thrown.</returns>
134 public static new System.Exception Throws(System.Type exceptionType, System.Action testCode)
135 {
136 return Xunit.Assert.Throws(exceptionType, testCode);
137 }
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 }
201}