a9813ade91d7eabc6749a6e279258d28eeeb19b7
[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_TestValue\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 TestValue {
39                         get {
40                                 ops.Append ("get_TestValue\n");
41                                 // ensure that the .config file is read in
42                                 int n = base.SwitchSetting;
43                                 // remove warning about unused variable
44                                 n = 5;
45                                 return v;
46                         }
47                 }
48
49 #if NET_2_0
50                 public string [] ExposeSupportedAttributes ()
51                 {
52                         return GetSupportedAttributes ();
53                 }
54 #endif
55
56                 public bool Validate ()
57                 {
58                         return expected == ops.ToString();
59                 }
60
61                 private void GetSetting ()
62                 {
63                         ops.Append ("GetSetting\n");
64                         IDictionary d = (IDictionary) ConfigurationSettings.GetConfig ("system.diagnostics");
65                         if (d != null) {
66                                 d = (IDictionary) d ["switches"];
67                                 if (d != null) {
68                                         v = d [DisplayName].ToString();
69                                 }
70                         }
71                 }
72
73                 protected override void OnSwitchSettingChanged ()
74                 {
75                         ops.Append ("OnSwitchSettingChanged\n");
76
77                         GetSetting ();
78                 }
79         }
80
81         [TestFixture]
82         public class SwitchesTest : Assertion {
83     
84                 private static BooleanSwitch bon = new BooleanSwitch ("bool-true", "");
85                 private static BooleanSwitch bon2 = new BooleanSwitch ("bool-true-2", "");
86                 private static BooleanSwitch bon3 = new BooleanSwitch ("bool-true-3", "");
87                 private static BooleanSwitch boff = new BooleanSwitch ("bool-false", "");
88                 private static BooleanSwitch boff2 = new BooleanSwitch ("bool-default", "");
89
90                 private static TraceSwitch toff = new TraceSwitch ("trace-off", "");
91                 private static TraceSwitch terror = new TraceSwitch ("trace-error", "");
92                 private static TraceSwitch twarning = new TraceSwitch ("trace-warning", "");
93                 private static TraceSwitch tinfo = new TraceSwitch ("trace-info", "");
94                 private static TraceSwitch tverbose = new TraceSwitch ("trace-verbose", "");
95                 private static TraceSwitch tdefault = new TraceSwitch ("no-value", "");
96                 private static TraceSwitch tsv = new TraceSwitch ("string-value", "");
97                 private static TraceSwitch tnegative = new TraceSwitch ("trace-negative", "");
98
99                 private static TestNewSwitch tns = new TestNewSwitch ("custom-switch", "");
100
101                 [Test]
102                 public void BooleanSwitches ()
103                 {
104                         Assert ("#BS:T:1", bon.Enabled);
105                         Assert ("#BS:T:2", bon2.Enabled);
106                         Assert ("#BS:T:3", bon3.Enabled);
107                         Assert ("#BS:F:1", !boff.Enabled);
108                         Assert ("#BS:F:2", !boff2.Enabled);
109                 }
110
111                 [Test]
112                 public void TraceSwitches ()
113                 {
114                         // The levels 0..4:
115                         CheckTraceSwitch (toff,      false, false, false, false);
116                         CheckTraceSwitch (terror,    true,  false, false, false);
117                         CheckTraceSwitch (twarning,  true,  true,  false, false);
118                         CheckTraceSwitch (tinfo,     true,  true,  true,  false);
119                         CheckTraceSwitch (tverbose,  true,  true,  true,  true);
120
121                         // Default value is 0
122                         CheckTraceSwitch (tdefault,  false, false, false, false);
123
124                         // string value can't be converted to int, so default is 0
125                         CheckTraceSwitch (tsv,       false, false, false, false);
126
127                         // negative number is < 0, so all off
128                         CheckTraceSwitch (tnegative, false, false, false, false);
129                 }
130
131                 private void CheckTraceSwitch (TraceSwitch ts, bool te, bool tw, bool ti, bool tv)
132                 {
133                         string desc = string.Format ("#TS:{0}", ts.DisplayName);
134                         AssertEquals (desc + ":TraceError",   te, ts.TraceError);
135                         AssertEquals (desc + ":TraceWarning", tw, ts.TraceWarning);
136                         AssertEquals (desc + ":TraceInfo",    ti, ts.TraceInfo);
137                         AssertEquals (desc + ":TraceVerbose", tv, ts.TraceVerbose);
138                 }
139
140                 [Test]
141 #if NET_2_0
142                 [Ignore ("this test depends on 1.x configuration type")]
143 #endif
144                 public void NewSwitch ()
145                 {
146                         AssertEquals ("#NS:TestValue", "42", tns.TestValue);
147                         Assert ("#NS:Validate", tns.Validate());
148                 }
149
150 #if NET_2_0
151                 [Test]
152                 public void GetSupportedAttributes ()
153                 {
154                         AssertNull (tns.ExposeSupportedAttributes ());
155                 }
156
157                 [Test] // no ArgumentNullException happens...
158                 public void BooleanSwitchNullDefaultValue ()
159                 {
160                         new BooleanSwitch ("test", "", null);
161                 }
162
163                 [Test]
164                 public void BooleanSwitchValidDefaultValue ()
165                 {
166                         BooleanSwitch s = new BooleanSwitch ("test", "", "2");
167                         Assert ("#1", s.Enabled);
168                         s = new BooleanSwitch ("test", "", "0");
169                         Assert ("#2", !s.Enabled);
170                         s = new BooleanSwitch ("test", "", "true");
171                         Assert ("#3", s.Enabled);
172                         s = new BooleanSwitch ("test", "", "True");
173                         Assert ("#4", s.Enabled);
174                         s = new BooleanSwitch ("test", "", "truE");
175                         Assert ("#5", s.Enabled);
176                 }
177
178                 [Test]
179                 [ExpectedException (typeof (FormatException))]
180                 public void BooleanSwitchInvalidDefaultValue ()
181                 {
182                         BooleanSwitch s = new BooleanSwitch ("test", "", "hoge");
183                         Assert (!s.Enabled);
184                 }
185 #endif
186         }
187 }
188