2 // TraceTest.cs - NUnit Test Cases for System.Diagnostics.Trace
5 // Jonathan Pryor (jonpryor@vt.edu)
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
9 // (C) 2003 Martin Willemoes Hansen
12 // We want tracing enabled, so...
15 using NUnit
.Framework
;
18 using System
.Diagnostics
;
20 namespace MonoTests
.System
.Diagnostics
{
23 public class TraceTest
{
25 private StringWriter buffer
;
26 private TraceListener listener
;
29 public void GetReady ()
31 // We don't want to deal with the default listener, which can send the
32 // output to various places (Debug stream, Console.Out, ...)
33 // Trace.Listeners.Remove ("Default");
35 buffer
= new StringWriter ();
36 listener
= new TextWriterTraceListener (buffer
, "TestOutput");
37 Trace
.Listeners
.Clear ();
38 Trace
.Listeners
.Add (listener
);
39 Trace
.AutoFlush
= true;
45 // Trace.Listeners.Add (new DefaultTraceListener ());
46 Trace
.Listeners
.Remove (listener
);
49 // Make sure that when we get the output we expect....
51 public void Tracing ()
53 Trace
.IndentLevel
= 0;
57 "Entering Main" + Environment
.NewLine
+
58 "Exiting Main" + Environment
.NewLine
;
60 Trace
.WriteLine ("Entering Main");
61 Trace
.WriteLine ("Exiting Main");
63 Assertion
.AssertEquals ("#Tr01", value, buffer
.ToString ());
66 // Make sure we get the output we expect in the presence of indenting...
70 Trace
.IndentLevel
= 0;
74 "List of errors:" + Environment
.NewLine
+
75 " Error 1: File not found" + Environment
.NewLine
+
76 " Error 2: Directory not found" + Environment
.NewLine
+
77 "End of list of errors" + Environment
.NewLine
;
79 Trace
.WriteLine ("List of errors:");
81 Trace
.WriteLine ("Error 1: File not found");
82 Trace
.WriteLine ("Error 2: Directory not found");
84 Trace
.WriteLine ("End of list of errors");
86 Assertion
.AssertEquals ("#In01", value, buffer
.ToString());
89 // Make sure that TraceListener properties (IndentLevel, IndentSize) are
90 // modified when the corresponding Trace properties are changed.
92 public void AddedTraceListenerProperties ()
94 TraceListener t1
= new TextWriterTraceListener (Console
.Out
);
95 TraceListener t2
= new TextWriterTraceListener (Console
.Error
);
96 Trace
.Listeners
.Add(t1
);
97 Trace
.Listeners
.Add(t2
);
99 const int ExpectedSize
= 5;
100 const int ExpectedLevel
= 2;
102 Trace
.IndentSize
= ExpectedSize
;
103 Trace
.IndentLevel
= ExpectedLevel
;
105 foreach (TraceListener t
in Trace
.Listeners
) {
106 string ids
= "#TATLP-S-" + t
.Name
;
107 string idl
= "#TATLP-L-" + t
.Name
;
108 Assertion
.AssertEquals (ids
, ExpectedSize
, t
.IndentSize
);
109 Assertion
.AssertEquals (idl
, ExpectedLevel
, t
.IndentLevel
);
112 Trace
.Listeners
.Remove(t1
);
113 Trace
.Listeners
.Remove(t2
);
116 // Make sure that the TraceListener properties (IndentLevel, IndentSize)
117 // are properly modified when the TraceListener is added to the
120 public void Listeners_Add_Values()
122 const int ExpectedLevel
= 0;
123 const int ExpectedSize
= 4;
124 Trace
.IndentLevel
= ExpectedLevel
;
125 Trace
.IndentSize
= ExpectedSize
;
126 TraceListener tl
= new TextWriterTraceListener(Console
.Out
);
128 tl
.IndentLevel
= 2*ExpectedLevel
;
129 tl
.IndentSize
= 2*ExpectedSize
;
131 Trace
.Listeners
.Add(tl
);
133 // Assertion.Assert that the listener we added has been set to the correct indent
135 Assertion
.AssertEquals ("#LATL-L", ExpectedLevel
, tl
.IndentLevel
);
136 Assertion
.AssertEquals ("#LATL-S", ExpectedSize
, tl
.IndentSize
);
138 // Assertion.Assert that all listeners in the collection have the same level.
139 foreach (TraceListener t
in Trace
.Listeners
)
141 string idl
= "#LATL-L:" + t
.Name
;
142 string ids
= "#LATL-S:" + t
.Name
;
143 Assertion
.AssertEquals(idl
, ExpectedLevel
, t
.IndentLevel
);
144 Assertion
.AssertEquals(ids
, ExpectedSize
, t
.IndentSize
);
148 // IndentSize, IndentLevel are thread-static