/src/libraries/System.Diagnostics.Debug/tests/DebugTestsNoListeners.cs

https://github.com/dotnet/runtime · C# · 230 lines · 176 code · 39 blank · 15 comment · 0 complexity · 255e040a639596f0db633a76f787c222 MD5 · raw file

  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. #define DEBUG
  4. using System.Reflection;
  5. using Xunit;
  6. namespace System.Diagnostics.Tests
  7. {
  8. // These tests test the static Debug class. They cannot be run in parallel
  9. // DebugTestsNoListeners: tests Debug behavior before Debug is set with Trace Listeners.
  10. [Collection("System.Diagnostics.Debug")]
  11. public class DebugTestsNoListeners : DebugTests
  12. {
  13. protected override bool DebugUsesTraceListeners { get { return false; } }
  14. protected void GoToNextLine()
  15. {
  16. // Start from next line before running next test: refer to nameof(Debug_WriteLine_WontIndentAfterWrite)
  17. VerifyLogged(() => Debug.WriteLine(""), Environment.NewLine);
  18. }
  19. [Fact]
  20. public void Debug_Write_Indents()
  21. {
  22. // This test when run alone verifies Debug.Write indentation, even on first call, is correct.
  23. Debug.Indent();
  24. VerifyLogged(() => Debug.Write("pizza"), new string(' ', Debug.IndentLevel * Debug.IndentSize) + "pizza");
  25. Debug.Unindent();
  26. GoToNextLine();
  27. }
  28. [Fact]
  29. public void Debug_WriteLine_Indents()
  30. {
  31. // This test when run alone verifies Debug.WriteLine indentation, even on first call, is correct.
  32. Debug.Indent();
  33. VerifyLogged(() => Debug.WriteLine("pizza"), new string(' ', Debug.IndentLevel * Debug.IndentSize) + "pizza" + Environment.NewLine);
  34. Debug.Unindent();
  35. }
  36. [Fact]
  37. public void Debug_WriteLine_WontIndentAfterWrite()
  38. {
  39. Debug.Indent();
  40. int expectedIndentation = Debug.IndentLevel * Debug.IndentSize;
  41. VerifyLogged(() => Debug.Write("pizza"), new string(' ', expectedIndentation) + "pizza");
  42. // WriteLine wont indent after Write:
  43. VerifyLogged(() => Debug.WriteLine("pizza"), "pizza" + Environment.NewLine);
  44. VerifyLogged(() => Debug.WriteLine("pizza"), new string(' ', expectedIndentation) + "pizza" + Environment.NewLine);
  45. VerifyLogged(() => Debug.Write("pizza"), new string(' ', expectedIndentation) + "pizza");
  46. // WriteLine wont indent after Write:
  47. VerifyLogged(() => Debug.WriteLine("pizza"), "pizza" + Environment.NewLine);
  48. VerifyLogged(() => Debug.WriteLine("pizza"), new string(' ', expectedIndentation) + "pizza" + Environment.NewLine);
  49. Debug.Unindent();
  50. }
  51. [Fact]
  52. public void Debug_WriteNull_SkipsIndentation()
  53. {
  54. Debug.Indent();
  55. VerifyLogged(() => Debug.Write(null), "");
  56. Debug.Unindent();
  57. }
  58. [Fact]
  59. public void Debug_WriteLineNull_IndentsEmptyStringProperly()
  60. {
  61. Debug.Indent();
  62. int expected = Debug.IndentSize * Debug.IndentLevel;
  63. VerifyLogged(() => Debug.WriteLine(null), new string(' ', expected) + Environment.NewLine);
  64. // reset
  65. Debug.Unindent();
  66. }
  67. [Fact]
  68. public void Asserts()
  69. {
  70. VerifyLogged(() => Debug.Assert(true), "");
  71. VerifyLogged(() => Debug.Assert(true, "assert passed"), "");
  72. VerifyLogged(() => Debug.Assert(true, "assert passed", "nothing is wrong"), "");
  73. VerifyLogged(() => Debug.Assert(true, "assert passed", "nothing is wrong {0} {1}", 'a', 'b'), "");
  74. VerifyAssert(() => Debug.Assert(false), "");
  75. VerifyAssert(() => Debug.Assert(false, "assert passed"), "assert passed");
  76. VerifyAssert(() => Debug.Assert(false, "assert passed", "nothing is wrong"), "assert passed", "nothing is wrong");
  77. VerifyAssert(() => Debug.Assert(false, "assert passed", "nothing is wrong {0} {1}", 'a', 'b'), "assert passed", "nothing is wrong a b");
  78. }
  79. [Fact]
  80. public void Fail()
  81. {
  82. VerifyAssert(() => Debug.Fail("something bad happened"), "something bad happened");
  83. VerifyAssert(() => Debug.Fail("something bad happened", "really really bad"), "something bad happened", "really really bad");
  84. }
  85. [Fact]
  86. public void Write()
  87. {
  88. VerifyLogged(() => Debug.Write(5), "5");
  89. VerifyLogged(() => Debug.Write((string)null), "");
  90. VerifyLogged(() => Debug.Write((object)null), "");
  91. VerifyLogged(() => Debug.Write(5, "category"), "category: 5");
  92. VerifyLogged(() => Debug.Write((object)null, "category"), "category: ");
  93. VerifyLogged(() => Debug.Write("logged"), "logged");
  94. VerifyLogged(() => Debug.Write("logged", "category"), "category: logged");
  95. VerifyLogged(() => Debug.Write("logged", (string)null), "logged");
  96. string longString = new string('d', 8192);
  97. VerifyLogged(() => Debug.Write(longString), longString);
  98. GoToNextLine();
  99. }
  100. [Fact]
  101. public void Print()
  102. {
  103. VerifyLogged(() => Debug.Print("logged"), "logged" + Environment.NewLine);
  104. VerifyLogged(() => Debug.Print("logged {0}", 5), "logged 5" + Environment.NewLine);
  105. GoToNextLine();
  106. }
  107. [Fact]
  108. public void WriteLine()
  109. {
  110. VerifyLogged(() => Debug.WriteLine(5), "5" + Environment.NewLine);
  111. VerifyLogged(() => Debug.WriteLine((string)null), Environment.NewLine);
  112. VerifyLogged(() => Debug.WriteLine((object)null), Environment.NewLine);
  113. VerifyLogged(() => Debug.WriteLine(5, "category"), "category: 5" + Environment.NewLine);
  114. VerifyLogged(() => Debug.WriteLine((object)null, "category"), "category: " + Environment.NewLine);
  115. VerifyLogged(() => Debug.WriteLine("logged"), "logged" + Environment.NewLine);
  116. VerifyLogged(() => Debug.WriteLine("logged", "category"), "category: logged" + Environment.NewLine);
  117. VerifyLogged(() => Debug.WriteLine("logged", (string)null), "logged" + Environment.NewLine);
  118. VerifyLogged(() => Debug.WriteLine("{0} {1}", 'a', 'b'), "a b" + Environment.NewLine);
  119. }
  120. [Fact]
  121. public void WriteIf()
  122. {
  123. VerifyLogged(() => Debug.WriteIf(true, 5), "5");
  124. VerifyLogged(() => Debug.WriteIf(false, 5), "");
  125. VerifyLogged(() => Debug.WriteIf(true, 5, "category"), "category: 5");
  126. VerifyLogged(() => Debug.WriteIf(false, 5, "category"), "");
  127. VerifyLogged(() => Debug.WriteIf(true, "logged"), "logged");
  128. VerifyLogged(() => Debug.WriteIf(false, "logged"), "");
  129. VerifyLogged(() => Debug.WriteIf(true, "logged", "category"), "category: logged");
  130. VerifyLogged(() => Debug.WriteIf(false, "logged", "category"), "");
  131. GoToNextLine();
  132. }
  133. [Fact]
  134. public void WriteLineIf()
  135. {
  136. VerifyLogged(() => Debug.WriteLineIf(true, 5), "5" + Environment.NewLine);
  137. VerifyLogged(() => Debug.WriteLineIf(false, 5), "");
  138. VerifyLogged(() => Debug.WriteLineIf(true, 5, "category"), "category: 5" + Environment.NewLine);
  139. VerifyLogged(() => Debug.WriteLineIf(false, 5, "category"), "");
  140. VerifyLogged(() => Debug.WriteLineIf(true, "logged"), "logged" + Environment.NewLine);
  141. VerifyLogged(() => Debug.WriteLineIf(false, "logged"), "");
  142. VerifyLogged(() => Debug.WriteLineIf(true, "logged", "category"), "category: logged" + Environment.NewLine);
  143. VerifyLogged(() => Debug.WriteLineIf(false, "logged", "category"), "");
  144. }
  145. [Theory]
  146. [InlineData(-1, 0)]
  147. [InlineData(0, 0)]
  148. [InlineData(1, 1)]
  149. [InlineData(2, 2)]
  150. [InlineData(3, 3)]
  151. [InlineData(4, 4)]
  152. public void IndentSize_Set_GetReturnsExpected(int indentSize, int expectedIndentSize)
  153. {
  154. Debug.IndentLevel = 0;
  155. Debug.IndentSize = indentSize;
  156. Assert.Equal(expectedIndentSize, Debug.IndentSize);
  157. VerifyLogged(() => Debug.WriteLine("pizza"), "pizza" + Environment.NewLine);
  158. // Indent once.
  159. Debug.Indent();
  160. string expectedIndentOnce = new string(' ', expectedIndentSize);
  161. VerifyLogged(() => Debug.WriteLine("pizza"), expectedIndentOnce + "pizza" + Environment.NewLine);
  162. // Indent again.
  163. Debug.Indent();
  164. string expectedIndentTwice = new string(' ', expectedIndentSize * 2);
  165. VerifyLogged(() => Debug.WriteLine("pizza"), expectedIndentTwice + "pizza" + Environment.NewLine);
  166. // Unindent.
  167. Debug.Unindent();
  168. Debug.Unindent();
  169. }
  170. [Theory]
  171. [InlineData(-1, 0)]
  172. [InlineData(0, 0)]
  173. [InlineData(1, 1)]
  174. public void IndentLevel_Set_GetReturnsExpected(int indentLevel, int expectedIndentLevel)
  175. {
  176. Debug.IndentLevel = indentLevel;
  177. Assert.Equal(expectedIndentLevel, Debug.IndentLevel);
  178. string expectedIndentOnce = new string(' ', expectedIndentLevel * Debug.IndentSize);
  179. VerifyLogged(() => Debug.WriteLine("pizza"), expectedIndentOnce + "pizza" + Environment.NewLine);
  180. // Indent once.
  181. Debug.Indent();
  182. Assert.Equal(expectedIndentLevel + 1, Debug.IndentLevel);
  183. string expectedIndentTwice = new string(' ', (expectedIndentLevel + 1) * Debug.IndentSize);
  184. VerifyLogged(() => Debug.WriteLine("pizza"), expectedIndentTwice + "pizza" + Environment.NewLine);
  185. // Unindent.
  186. Debug.Unindent();
  187. Assert.Equal(expectedIndentLevel, Debug.IndentLevel);
  188. Debug.Unindent();
  189. }
  190. }
  191. }