Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TraceTest.cs « System.Diagnostics « Test « System « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e284f27407a51fb8b95ffe7e3e58269307aad7a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//
// TraceTest.cs - NUnit Test Cases for System.Diagnostics.Trace
//
// Jonathan Pryor (jonpryor@vt.edu)
//
// (C) Jonathan Pryor
// 

// We want tracing enabled, so...
#define TRACE

using NUnit.Framework;
using System;
using System.IO;
using System.Diagnostics;

namespace MonoTests.System.Diagnostics {

	public class TraceTest : TestCase {
    
		private StringWriter buffer;
		private TraceListener listener;

		public TraceTest () 
			: base ("System.Diagnostics.Trace testsuite")
		{
		}

		public TraceTest (string name)
			: base(name)
		{
		}

		protected override void SetUp ()
		{
			// We don't want to deal with the default listener, which can send the
			// output to various places (Debug stream, Console.Out, ...)
			// Trace.Listeners.Remove ("Default");

			buffer = new StringWriter ();

			listener = new TextWriterTraceListener (buffer, "TestOutput");

			Trace.Listeners.Add (listener);

			Trace.AutoFlush = true;

		}

		protected override void TearDown ()
		{
			// Trace.Listeners.Add (new DefaultTraceListener ());
			Trace.Listeners.Remove (listener);
		}

    public static ITest Suite {
 			get { 
				return new TestSuite (typeof (TraceTest)); 
			}
		}

		// Make sure that when we get the output we expect....
		public void TestTracing ()
		{
			string value =  
				"Entering Main" + Environment.NewLine +
				"Exiting Main" + Environment.NewLine;

			Trace.WriteLine ("Entering Main");
			Trace.WriteLine ("Exiting Main");

			AssertEquals ("#Tr01", value, buffer.ToString ());
		}

		// Make sure we get the output we expect in the presence of indenting...
		public void TestIndent ()
		{
			Console.Error.WriteLine ("TraceTest.TestIndent");
			string value =  
				"List of errors:" + Environment.NewLine +
				"    Error 1: File not found" + Environment.NewLine +
				"    Error 2: Directory not found" + Environment.NewLine +
				"End of list of errors" + Environment.NewLine;

			Trace.WriteLine ("List of errors:");
			Trace.Indent ();
			Trace.WriteLine ("Error 1: File not found");
			Trace.WriteLine ("Error 2: Directory not found");
			Trace.Unindent ();
			Trace.WriteLine ("End of list of errors");

			AssertEquals ("#In01", value, buffer.ToString());
		}

		// Make sure that TraceListener properties (IndentLevel, IndentSize) are
		// modified when the corresponding Trace properties are changed.
		public void TestAddedTraceListenerProperties ()
		{
			TraceListener t1 = new TextWriterTraceListener (Console.Out);
			TraceListener t2 = new TextWriterTraceListener (Console.Error);
			Trace.Listeners.Add(t1);
			Trace.Listeners.Add(t2);

			const int ExpectedSize = 5;
			const int ExpectedLevel = 2;

			Trace.IndentSize = ExpectedSize;
			Trace.IndentLevel = ExpectedLevel;

			foreach (TraceListener t in Trace.Listeners) {
				string ids = "#TATLP-S-" + t.Name;
				string idl = "#TATLP-L-" + t.Name;
				AssertEquals (ids, ExpectedSize, t.IndentSize);
				AssertEquals (idl, ExpectedLevel, t.IndentLevel);
			}

			Trace.Listeners.Remove(t1);
			Trace.Listeners.Remove(t2);
		}

		// Make sure that the TraceListener properties (IndentLevel, IndentSize)
		// are properly modified when the TraceListener is added to the
		// collection.
		public void TestListeners_Add_Values()
		{
			const int ExpectedLevel = 5;
			const int ExpectedSize = 3;
			Trace.IndentLevel = ExpectedLevel;
			Trace.IndentSize = ExpectedSize;
			TraceListener tl = new TextWriterTraceListener(Console.Out);

			tl.IndentLevel = 2*ExpectedLevel;
			tl.IndentSize = 2*ExpectedSize;

			Trace.Listeners.Add(tl);

			// Assert that the listener we added has been set to the correct indent
			// level.
			AssertEquals ("#LATL-L", ExpectedLevel, tl.IndentLevel);
			AssertEquals ("#LATL-S", ExpectedSize, tl.IndentSize);

			// Assert that all listeners in the collection have the same level.
			foreach (TraceListener t in Trace.Listeners)
			{
				string idl = "#LATL-L:" + t.Name;
				string ids = "#LATL-S:" + t.Name;
				AssertEquals(idl, ExpectedLevel, t.IndentLevel);
				AssertEquals(ids, ExpectedSize, t.IndentSize);
			}
		}

		// IndentSize, IndentLevel are thread-static
	}
}