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