TestHelpers.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Xunit.Abstractions;
  6. using Xunit;
  7. using Terminal.Gui;
  8. using System.Text.RegularExpressions;
  9. using System.Reflection;
  10. using System.Diagnostics;
  11. using Rune = System.Rune;
  12. using Attribute = Terminal.Gui.Attribute;
  13. using Microsoft.VisualStudio.TestPlatform.Utilities;
  14. using NStack;
  15. using Xunit.Sdk;
  16. // This class enables test functions annotated with the [AutoInitShutdown] attribute to
  17. // automatically call Application.Init at start of the test and Application.Shutdown after the
  18. // test exits.
  19. //
  20. // This is necessary because a) Application is a singleton and Init/Shutdown must be called
  21. // as a pair, and b) all unit test functions should be atomic..
  22. [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
  23. public class AutoInitShutdownAttribute : Xunit.Sdk.BeforeAfterTestAttribute {
  24. /// <summary>
  25. /// Initializes a [AutoInitShutdown] attribute, which determines if/how Application.Init and
  26. /// Application.Shutdown are automatically called Before/After a test runs.
  27. /// </summary>
  28. /// <param name="autoInit">If true, Application.Init will be called Before the test runs.</param>
  29. /// <param name="autoShutdown">If true, Application.Shutdown will be called After the test runs.</param>
  30. /// <param name="consoleDriverType">Determines which ConsoleDriver (FakeDriver, WindowsDriver,
  31. /// CursesDriver, NetDriver) will be used when Application.Init is called. If null FakeDriver will be used.
  32. /// Only valid if <paramref name="autoInit"/> is true.</param>
  33. /// <param name="useFakeClipboard">If true, will force the use of <see cref="FakeDriver.FakeClipboard"/>.
  34. /// Only valid if <see cref="consoleDriver"/> == <see cref="FakeDriver"/> and <paramref name="autoInit"/> is true.</param>
  35. /// <param name="fakeClipboardAlwaysThrowsNotSupportedException">Only valid if <paramref name="autoInit"/> is true.
  36. /// Only valid if <see cref="consoleDriver"/> == <see cref="FakeDriver"/> and <paramref name="autoInit"/> is true.</param>
  37. /// <param name="fakeClipboardIsSupportedAlwaysTrue">Only valid if <paramref name="autoInit"/> is true.
  38. /// Only valid if <see cref="consoleDriver"/> == <see cref="FakeDriver"/> and <paramref name="autoInit"/> is true.</param>
  39. /// <param name="configLocation">Determines what config file locations <see cref="ConfigurationManager"/> will
  40. /// load from.</param>
  41. public AutoInitShutdownAttribute (bool autoInit = true, bool autoShutdown = true,
  42. Type consoleDriverType = null,
  43. bool useFakeClipboard = false,
  44. bool fakeClipboardAlwaysThrowsNotSupportedException = false,
  45. bool fakeClipboardIsSupportedAlwaysTrue = false,
  46. ConfigurationManager.ConfigLocations configLocation = ConfigurationManager.ConfigLocations.DefaultOnly)
  47. {
  48. //Assert.True (autoInit == false && consoleDriverType == null);
  49. AutoInit = autoInit;
  50. AutoShutdown = autoShutdown;
  51. DriverType = consoleDriverType ?? typeof (FakeDriver);
  52. FakeDriver.FakeBehaviors.UseFakeClipboard = useFakeClipboard;
  53. FakeDriver.FakeBehaviors.FakeClipboardAlwaysThrowsNotSupportedException = fakeClipboardAlwaysThrowsNotSupportedException;
  54. FakeDriver.FakeBehaviors.FakeClipboardIsSupportedAlwaysFalse = fakeClipboardIsSupportedAlwaysTrue;
  55. ConfigurationManager.Locations = configLocation;
  56. }
  57. static bool _init = false;
  58. bool AutoInit { get; }
  59. bool AutoShutdown { get; }
  60. Type DriverType;
  61. public override void Before (MethodInfo methodUnderTest)
  62. {
  63. Debug.WriteLine ($"Before: {methodUnderTest.Name}");
  64. if (AutoShutdown && _init) {
  65. throw new InvalidOperationException ("After did not run when AutoShutdown was specified.");
  66. }
  67. if (AutoInit) {
  68. Application.Init ((ConsoleDriver)Activator.CreateInstance (DriverType));
  69. _init = true;
  70. }
  71. }
  72. public override void After (MethodInfo methodUnderTest)
  73. {
  74. Debug.WriteLine ($"After: {methodUnderTest.Name}");
  75. if (AutoShutdown) {
  76. Application.Shutdown ();
  77. _init = false;
  78. }
  79. }
  80. }
  81. [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
  82. public class SetupFakeDriverAttribute : Xunit.Sdk.BeforeAfterTestAttribute {
  83. /// <summary>
  84. /// Enables test functions annotated with the [SetupFakeDriver] attribute to
  85. /// set Application.Driver to new FakeDriver().
  86. /// </summary>
  87. public SetupFakeDriverAttribute ()
  88. {
  89. }
  90. public override void Before (MethodInfo methodUnderTest)
  91. {
  92. Debug.WriteLine ($"Before: {methodUnderTest.Name}");
  93. Assert.Null (Application.Driver);
  94. Application.Driver = new FakeDriver ();
  95. }
  96. public override void After (MethodInfo methodUnderTest)
  97. {
  98. Debug.WriteLine ($"After: {methodUnderTest.Name}");
  99. Application.Driver = null;
  100. }
  101. }
  102. class TestHelpers {
  103. #pragma warning disable xUnit1013 // Public method should be marked as test
  104. public static void AssertDriverContentsAre (string expectedLook, ITestOutputHelper output, bool ignoreLeadingWhitespace = false)
  105. {
  106. #pragma warning restore xUnit1013 // Public method should be marked as test
  107. var sb = new StringBuilder ();
  108. var driver = ((FakeDriver)Application.Driver);
  109. var contents = driver.Contents;
  110. for (int r = 0; r < driver.Rows; r++) {
  111. for (int c = 0; c < driver.Cols; c++) {
  112. Rune rune = contents [r, c, 0];
  113. if (Rune.DecodeSurrogatePair (rune, out char [] spair)) {
  114. sb.Append (spair);
  115. } else {
  116. sb.Append ((char)rune);
  117. }
  118. if (Rune.ColumnWidth (rune) > 1) {
  119. c++;
  120. }
  121. }
  122. sb.AppendLine ();
  123. }
  124. var actualLook = sb.ToString ();
  125. if (!string.Equals (expectedLook, actualLook)) {
  126. // ignore trailing whitespace on each line
  127. var trailingWhitespace = new Regex (@"\s+$", RegexOptions.Multiline);
  128. var leadingWhitespace = new Regex (@"^\s+", RegexOptions.Multiline);
  129. // get rid of trailing whitespace on each line (and leading/trailing whitespace of start/end of full string)
  130. expectedLook = trailingWhitespace.Replace (expectedLook, "").Trim ();
  131. actualLook = trailingWhitespace.Replace (actualLook, "").Trim ();
  132. if (ignoreLeadingWhitespace) {
  133. expectedLook = leadingWhitespace.Replace (expectedLook, "").Trim ();
  134. actualLook = leadingWhitespace.Replace (actualLook, "").Trim ();
  135. }
  136. // standardize line endings for the comparison
  137. expectedLook = expectedLook.Replace ("\r\n", "\n");
  138. actualLook = actualLook.Replace ("\r\n", "\n");
  139. // If test is about to fail show user what things looked like
  140. if (!string.Equals (expectedLook, actualLook)) {
  141. output?.WriteLine ("Expected:" + Environment.NewLine + expectedLook);
  142. output?.WriteLine ("But Was:" + Environment.NewLine + actualLook);
  143. }
  144. Assert.Equal (expectedLook, actualLook);
  145. }
  146. }
  147. public static Rect AssertDriverContentsWithFrameAre (string expectedLook, ITestOutputHelper output)
  148. {
  149. var lines = new List<List<Rune>> ();
  150. var sb = new StringBuilder ();
  151. var driver = ((FakeDriver)Application.Driver);
  152. var x = -1;
  153. var y = -1;
  154. int w = -1;
  155. int h = -1;
  156. var contents = driver.Contents;
  157. for (int r = 0; r < driver.Rows; r++) {
  158. var runes = new List<Rune> ();
  159. for (int c = 0; c < driver.Cols; c++) {
  160. var rune = (Rune)contents [r, c, 0];
  161. if (rune != ' ') {
  162. if (x == -1) {
  163. x = c;
  164. y = r;
  165. for (int i = 0; i < c; i++) {
  166. runes.InsertRange (i, new List<Rune> () { ' ' });
  167. }
  168. }
  169. if (Rune.ColumnWidth (rune) > 1) {
  170. c++;
  171. }
  172. if (c + 1 > w) {
  173. w = c + 1;
  174. }
  175. h = r - y + 1;
  176. }
  177. if (x > -1) {
  178. runes.Add (rune);
  179. }
  180. }
  181. if (runes.Count > 0) {
  182. lines.Add (runes);
  183. }
  184. }
  185. // Remove unnecessary empty lines
  186. if (lines.Count > 0) {
  187. for (int r = lines.Count - 1; r > h - 1; r--) {
  188. lines.RemoveAt (r);
  189. }
  190. }
  191. // Remove trailing whitespace on each line
  192. for (int r = 0; r < lines.Count; r++) {
  193. List<Rune> row = lines [r];
  194. for (int c = row.Count - 1; c >= 0; c--) {
  195. var rune = row [c];
  196. if (rune != ' ' || (row.Sum (x => Rune.ColumnWidth (x)) == w)) {
  197. break;
  198. }
  199. row.RemoveAt (c);
  200. }
  201. }
  202. // Convert Rune list to string
  203. for (int r = 0; r < lines.Count; r++) {
  204. var line = NStack.ustring.Make (lines [r]).ToString ();
  205. if (r == lines.Count - 1) {
  206. sb.Append (line);
  207. } else {
  208. sb.AppendLine (line);
  209. }
  210. }
  211. var actualLook = sb.ToString ();
  212. if (!string.Equals (expectedLook, actualLook)) {
  213. // standardize line endings for the comparison
  214. expectedLook = expectedLook.Replace ("\r\n", "\n");
  215. actualLook = actualLook.Replace ("\r\n", "\n");
  216. // Remove the first and the last line ending from the expectedLook
  217. if (expectedLook.StartsWith ("\n")) {
  218. expectedLook = expectedLook [1..];
  219. }
  220. if (expectedLook.EndsWith ("\n")) {
  221. expectedLook = expectedLook [..^1];
  222. }
  223. output?.WriteLine ("Expected:" + Environment.NewLine + expectedLook);
  224. output?.WriteLine ("But Was:" + Environment.NewLine + actualLook);
  225. Assert.Equal (expectedLook, actualLook);
  226. }
  227. return new Rect (x > -1 ? x : 0, y > -1 ? y : 0, w > -1 ? w : 0, h > -1 ? h : 0);
  228. }
  229. #pragma warning disable xUnit1013 // Public method should be marked as test
  230. /// <summary>
  231. /// Verifies the console was rendered using the given <paramref name="expectedColors"/> at the given locations.
  232. /// Pass a bitmap of indexes into <paramref name="expectedColors"/> as <paramref name="expectedLook"/> and the
  233. /// test method will verify those colors were used in the row/col of the console during rendering
  234. /// </summary>
  235. /// <param name="expectedLook">Numbers between 0 and 9 for each row/col of the console. Must be valid indexes of <paramref name="expectedColors"/></param>
  236. /// <param name="expectedColors"></param>
  237. public static void AssertDriverColorsAre (string expectedLook, Attribute [] expectedColors)
  238. {
  239. #pragma warning restore xUnit1013 // Public method should be marked as test
  240. if (expectedColors.Length > 10) {
  241. throw new ArgumentException ("This method only works for UIs that use at most 10 colors");
  242. }
  243. expectedLook = expectedLook.Trim ();
  244. var driver = ((FakeDriver)Application.Driver);
  245. var contents = driver.Contents;
  246. int r = 0;
  247. foreach (var line in expectedLook.Split ('\n').Select (l => l.Trim ())) {
  248. for (int c = 0; c < line.Length; c++) {
  249. int val = contents [r, c, 1];
  250. var match = expectedColors.Where (e => e.Value == val).ToList ();
  251. if (match.Count == 0) {
  252. throw new Exception ($"Unexpected color {DescribeColor (val)} was used at row {r} and col {c} (indexes start at 0). Color value was {val} (expected colors were {string.Join (",", expectedColors.Select (c => DescribeColor (c.Value)))})");
  253. } else if (match.Count > 1) {
  254. throw new ArgumentException ($"Bad value for expectedColors, {match.Count} Attributes had the same Value");
  255. }
  256. var colorUsed = Array.IndexOf (expectedColors, match [0]).ToString () [0];
  257. var userExpected = line [c];
  258. if (colorUsed != userExpected) {
  259. throw new Exception ($"Colors used did not match expected at row {r} and col {c} (indexes start at 0). Color index used was {colorUsed} ({DescribeColor (val)}) but test expected {userExpected} ({DescribeColor (expectedColors [int.Parse (userExpected.ToString ())].Value)}) (these are indexes into the expectedColors array)");
  260. }
  261. }
  262. r++;
  263. }
  264. }
  265. private static object DescribeColor (int userExpected)
  266. {
  267. var a = new Attribute (userExpected);
  268. return $"{a.Foreground},{a.Background}";
  269. }
  270. #pragma warning disable xUnit1013 // Public method should be marked as test
  271. /// <summary>
  272. /// Verifies two strings are equivalent. If the assert fails, output will be generated to standard
  273. /// output showing the expected and actual look.
  274. /// </summary>
  275. /// <param name="output"></param>
  276. /// <param name="expectedLook">A string containing the expected look. Newlines should be specified as "\r\n" as
  277. /// they will be converted to <see cref="Environment.NewLine"/> to make tests platform independent.</param>
  278. /// <param name="actualLook"></param>
  279. public static void AssertEqual (ITestOutputHelper output, string expectedLook, string actualLook)
  280. {
  281. // Convert newlines to platform-specific newlines
  282. expectedLook = ReplaceNewLinesToPlatformSpecific (expectedLook);
  283. // If test is about to fail show user what things looked like
  284. if (!string.Equals (expectedLook, actualLook)) {
  285. output?.WriteLine ("Expected:" + Environment.NewLine + expectedLook);
  286. output?.WriteLine ("But Was:" + Environment.NewLine + actualLook);
  287. }
  288. Assert.Equal (expectedLook, actualLook);
  289. }
  290. /// <summary>
  291. /// Verifies two strings are equivalent. If the assert fails, output will be generated to standard
  292. /// output showing the expected and actual look.
  293. /// </summary>
  294. /// <param name="output">Uses <see cref="ustring.ToString()"/> on <paramref name="actualLook"/>.</param>
  295. /// <param name="expectedLook">A string containing the expected look. Newlines should be specified as "\r\n" as
  296. /// they will be converted to <see cref="Environment.NewLine"/> to make tests platform independent.</param>
  297. /// <param name="actualLook"></param>
  298. public static void AssertEqual (ITestOutputHelper output, string expectedLook, ustring actualLook)
  299. {
  300. // Convert newlines to platform-specific newlines
  301. expectedLook = ReplaceNewLinesToPlatformSpecific (expectedLook);
  302. // If test is about to fail show user what things looked like
  303. if (!string.Equals (expectedLook, actualLook.ToString ())) {
  304. output?.WriteLine ("Expected:" + Environment.NewLine + expectedLook);
  305. output?.WriteLine ("But Was:" + Environment.NewLine + actualLook.ToString ());
  306. }
  307. Assert.Equal (expectedLook, actualLook);
  308. }
  309. #pragma warning restore xUnit1013 // Public method should be marked as test
  310. private static string ReplaceNewLinesToPlatformSpecific (string toReplace)
  311. {
  312. var replaced = toReplace;
  313. if (Environment.NewLine.Length == 2 && !replaced.Contains ("\r\n")) {
  314. replaced = replaced.Replace ("\n", Environment.NewLine);
  315. } else if (Environment.NewLine.Length == 1) {
  316. replaced = replaced.Replace ("\r\n", Environment.NewLine);
  317. }
  318. return replaced;
  319. }
  320. }