TraceTest.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // TraceTest.cs - NUnit Test Cases for System.Diagnostics.Trace
  3. //
  4. // Authors:
  5. // Jonathan Pryor ([email protected])
  6. // Martin Willemoes Hansen ([email protected])
  7. //
  8. // (C) Jonathan Pryor
  9. // (C) 2003 Martin Willemoes Hansen
  10. //
  11. // We want tracing enabled, so...
  12. #define TRACE
  13. using NUnit.Framework;
  14. using System;
  15. using System.IO;
  16. using System.Diagnostics;
  17. namespace MonoTests.System.Diagnostics {
  18. [TestFixture]
  19. public class TraceTest {
  20. private StringWriter buffer;
  21. private TraceListener listener;
  22. [SetUp]
  23. public void GetReady ()
  24. {
  25. // We don't want to deal with the default listener, which can send the
  26. // output to various places (Debug stream, Console.Out, ...)
  27. // Trace.Listeners.Remove ("Default");
  28. buffer = new StringWriter ();
  29. listener = new TextWriterTraceListener (buffer, "TestOutput");
  30. Trace.Listeners.Clear ();
  31. Trace.Listeners.Add (listener);
  32. Trace.AutoFlush = true;
  33. }
  34. [TearDown]
  35. public void Clear ()
  36. {
  37. // Trace.Listeners.Add (new DefaultTraceListener ());
  38. Trace.Listeners.Remove (listener);
  39. }
  40. // Make sure that when we get the output we expect....
  41. [Test]
  42. public void Tracing ()
  43. {
  44. Trace.IndentLevel = 0;
  45. Trace.IndentSize = 4;
  46. string value =
  47. "Entering Main" + Environment.NewLine +
  48. "Exiting Main" + Environment.NewLine;
  49. Trace.WriteLine ("Entering Main");
  50. Trace.WriteLine ("Exiting Main");
  51. Assertion.AssertEquals ("#Tr01", value, buffer.ToString ());
  52. }
  53. // Make sure we get the output we expect in the presence of indenting...
  54. [Test]
  55. public void Indent ()
  56. {
  57. Trace.IndentLevel = 0;
  58. Trace.IndentSize = 4;
  59. string value =
  60. "List of errors:" + Environment.NewLine +
  61. " Error 1: File not found" + Environment.NewLine +
  62. " Error 2: Directory not found" + Environment.NewLine +
  63. "End of list of errors" + Environment.NewLine;
  64. Trace.WriteLine ("List of errors:");
  65. Trace.Indent ();
  66. Trace.WriteLine ("Error 1: File not found");
  67. Trace.WriteLine ("Error 2: Directory not found");
  68. Trace.Unindent ();
  69. Trace.WriteLine ("End of list of errors");
  70. Assertion.AssertEquals ("#In01", value, buffer.ToString());
  71. }
  72. // Make sure that TraceListener properties (IndentLevel, IndentSize) are
  73. // modified when the corresponding Trace properties are changed.
  74. [Test]
  75. public void AddedTraceListenerProperties ()
  76. {
  77. TraceListener t1 = new TextWriterTraceListener (Console.Out);
  78. TraceListener t2 = new TextWriterTraceListener (Console.Error);
  79. Trace.Listeners.Add(t1);
  80. Trace.Listeners.Add(t2);
  81. const int ExpectedSize = 5;
  82. const int ExpectedLevel = 2;
  83. Trace.IndentSize = ExpectedSize;
  84. Trace.IndentLevel = ExpectedLevel;
  85. foreach (TraceListener t in Trace.Listeners) {
  86. string ids = "#TATLP-S-" + t.Name;
  87. string idl = "#TATLP-L-" + t.Name;
  88. Assertion.AssertEquals (ids, ExpectedSize, t.IndentSize);
  89. Assertion.AssertEquals (idl, ExpectedLevel, t.IndentLevel);
  90. }
  91. Trace.Listeners.Remove(t1);
  92. Trace.Listeners.Remove(t2);
  93. }
  94. // Make sure that the TraceListener properties (IndentLevel, IndentSize)
  95. // are properly modified when the TraceListener is added to the
  96. // collection.
  97. [Test]
  98. public void Listeners_Add_Values()
  99. {
  100. const int ExpectedLevel = 0;
  101. const int ExpectedSize = 4;
  102. Trace.IndentLevel = ExpectedLevel;
  103. Trace.IndentSize = ExpectedSize;
  104. TraceListener tl = new TextWriterTraceListener(Console.Out);
  105. tl.IndentLevel = 2*ExpectedLevel;
  106. tl.IndentSize = 2*ExpectedSize;
  107. Trace.Listeners.Add(tl);
  108. // Assertion.Assert that the listener we added has been set to the correct indent
  109. // level.
  110. Assertion.AssertEquals ("#LATL-L", ExpectedLevel, tl.IndentLevel);
  111. Assertion.AssertEquals ("#LATL-S", ExpectedSize, tl.IndentSize);
  112. // Assertion.Assert that all listeners in the collection have the same level.
  113. foreach (TraceListener t in Trace.Listeners)
  114. {
  115. string idl = "#LATL-L:" + t.Name;
  116. string ids = "#LATL-S:" + t.Name;
  117. Assertion.AssertEquals(idl, ExpectedLevel, t.IndentLevel);
  118. Assertion.AssertEquals(ids, ExpectedSize, t.IndentSize);
  119. }
  120. }
  121. // IndentSize, IndentLevel are thread-static
  122. }
  123. }