New test.
[mono.git] / mcs / class / System / Test / System.Diagnostics / SwitchesTest.cs
1 //
2 // SwitchesTest.cs:
3 //              NUnit Test Cases for System.Diagnostics.BooleanSwitch and
4 //              System.Diagnostics.TraceSwitch
5 //
6 // Authors:
7 //   Jonathan Pryor (jonpryor@vt.edu)
8 //   Martin Willemoes Hansen (mwh@sysrq.dk)
9 //
10 // (C) 2002 Jonathan Pryor
11 // (C) 2003 Martin Willemoes Hansen
12 //
13
14 using NUnit.Framework;
15 using System;
16 using System.Text;
17 using System.Collections;
18 using System.Configuration;
19 using System.Diagnostics;
20
21 namespace MonoTests.System.Diagnostics {
22
23         class TestNewSwitch : Switch {
24                 private string v;
25                 private StringBuilder ops = new StringBuilder ();
26                 private const string expected = 
27                         ".ctor\n" +
28                         "get_Value\n" +
29                         "OnSwitchSettingChanged\n" +
30                         "GetSetting\n";
31
32                 public TestNewSwitch (string name, string desc)
33                         : base (name, desc)
34                 {
35                         ops.Append (".ctor\n");
36                 }
37
38                 public string Value {
39                         get {
40                                 ops.Append ("get_Value\n");
41                                 // ensure that the .config file is read in
42                                 int n = base.SwitchSetting;
43                                 // remove warning about unused variable
44                                 n = n;
45                                 return v;
46                         }
47                 }
48
49                 public bool Validate ()
50                 {
51                         return expected == ops.ToString();
52                 }
53
54                 private void GetSetting ()
55                 {
56                         ops.Append ("GetSetting\n");
57                         IDictionary d = (IDictionary) ConfigurationSettings.GetConfig ("system.diagnostics");
58                         if (d != null) {
59                                 d = (IDictionary) d ["switches"];
60                                 if (d != null) {
61                                         v = d [DisplayName].ToString();
62                                 }
63                         }
64                 }
65
66                 protected override void OnSwitchSettingChanged ()
67                 {
68                         ops.Append ("OnSwitchSettingChanged\n");
69
70                         GetSetting ();
71                 }
72         }
73
74         [TestFixture]
75         public class SwitchesTest : Assertion {
76     
77                 private static BooleanSwitch bon = new BooleanSwitch ("bool-true", "");
78                 private static BooleanSwitch bon2 = new BooleanSwitch ("bool-true-2", "");
79                 private static BooleanSwitch bon3 = new BooleanSwitch ("bool-true-3", "");
80                 private static BooleanSwitch boff = new BooleanSwitch ("bool-false", "");
81                 private static BooleanSwitch boff2 = new BooleanSwitch ("bool-default", "");
82
83                 private static TraceSwitch toff = new TraceSwitch ("trace-off", "");
84                 private static TraceSwitch terror = new TraceSwitch ("trace-error", "");
85                 private static TraceSwitch twarning = new TraceSwitch ("trace-warning", "");
86                 private static TraceSwitch tinfo = new TraceSwitch ("trace-info", "");
87                 private static TraceSwitch tverbose = new TraceSwitch ("trace-verbose", "");
88                 private static TraceSwitch tdefault = new TraceSwitch ("no-value", "");
89                 private static TraceSwitch tsv = new TraceSwitch ("string-value", "");
90                 private static TraceSwitch tnegative = new TraceSwitch ("trace-negative", "");
91
92                 private static TestNewSwitch tns = new TestNewSwitch ("custom-switch", "");
93
94                 [Test]
95                 public void BooleanSwitches ()
96                 {
97                         Assert ("#BS:T:1", bon.Enabled);
98                         Assert ("#BS:T:2", bon2.Enabled);
99                         Assert ("#BS:T:3", bon3.Enabled);
100                         Assert ("#BS:F:1", !boff.Enabled);
101                         Assert ("#BS:F:2", !boff2.Enabled);
102                 }
103
104                 [Test]
105                 public void TraceSwitches ()
106                 {
107                         // The levels 0..4:
108                         CheckTraceSwitch (toff,      false, false, false, false);
109                         CheckTraceSwitch (terror,    true,  false, false, false);
110                         CheckTraceSwitch (twarning,  true,  true,  false, false);
111                         CheckTraceSwitch (tinfo,     true,  true,  true,  false);
112                         CheckTraceSwitch (tverbose,  true,  true,  true,  true);
113
114                         // Default value is 0
115                         CheckTraceSwitch (tdefault,  false, false, false, false);
116
117                         // string value can't be converted to int, so default is 0
118                         CheckTraceSwitch (tsv,       false, false, false, false);
119
120                         // negative number is < 0, so all off
121                         CheckTraceSwitch (tnegative, false, false, false, false);
122                 }
123
124                 private void CheckTraceSwitch (TraceSwitch ts, bool te, bool tw, bool ti, bool tv)
125                 {
126                         string desc = string.Format ("#TS:{0}", ts.DisplayName);
127                         AssertEquals (desc + ":TraceError",   te, ts.TraceError);
128                         AssertEquals (desc + ":TraceWarning", tw, ts.TraceWarning);
129                         AssertEquals (desc + ":TraceInfo",    ti, ts.TraceInfo);
130                         AssertEquals (desc + ":TraceVerbose", tv, ts.TraceVerbose);
131                 }
132
133                 [Test]
134 #if NET_2_0
135                 [Ignore ("this test depends on 1.x configuration type")]
136 #endif
137                 public void NewSwitch ()
138                 {
139                         AssertEquals ("#NS:Value", "42", tns.Value);
140                         Assert ("#NS:Validate", tns.Validate());
141                 }
142         }
143 }
144