3 // NUnit Test Cases for System.Diagnostics.BooleanSwitch and
4 // System.Diagnostics.TraceSwitch
7 // Jonathan Pryor (jonpryor@vt.edu)
8 // Martin Willemoes Hansen (mwh@sysrq.dk)
10 // (C) 2002 Jonathan Pryor
11 // (C) 2003 Martin Willemoes Hansen
16 using NUnit.Framework;
19 using System.Collections;
20 using System.Configuration;
21 using System.Diagnostics;
23 namespace MonoTests.System.Diagnostics {
25 class TestNewSwitch : Switch {
27 private StringBuilder ops = new StringBuilder ();
28 private const string expected =
31 "OnSwitchSettingChanged\n" +
34 public TestNewSwitch (string name, string desc)
37 ops.Append (".ctor\n");
40 public string TestValue {
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
52 public string [] ExposeSupportedAttributes ()
54 return GetSupportedAttributes ();
58 public bool Validate ()
60 return expected == ops.ToString();
63 private void GetSetting ()
65 ops.Append ("GetSetting\n");
66 IDictionary d = (IDictionary) ConfigurationSettings.GetConfig ("system.diagnostics");
68 d = (IDictionary) d ["switches"];
70 v = d [DisplayName].ToString();
75 protected override void OnSwitchSettingChanged ()
77 ops.Append ("OnSwitchSettingChanged\n");
83 class TestNullSwitch : Switch {
84 public TestNullSwitch () : base (null, null)
90 public class SwitchesTest {
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", "");
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", "");
107 private static TestNewSwitch tns = new TestNewSwitch ("custom-switch", "");
110 public void BooleanSwitches ()
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");
120 public void TraceSwitches ()
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);
129 // Default value is 0
130 CheckTraceSwitch (tdefault, false, false, false, false);
132 // string value can't be converted to int, so default is 0
133 CheckTraceSwitch (tsv, false, false, false, false);
135 // negative number is < 0, so all off
136 CheckTraceSwitch (tnegative, false, false, false, false);
139 private void CheckTraceSwitch (TraceSwitch ts, bool te, bool tw, bool ti, bool tv)
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");
150 [Ignore ("this test depends on 1.x configuration type")]
152 public void NewSwitch ()
154 Assert.AreEqual ("42", tns.TestValue, "#NS:TestValue");
155 Assert.IsTrue (tns.Validate(), "#NS:Validate");
160 public void GetSupportedAttributes ()
162 Assert.IsNull (tns.ExposeSupportedAttributes ());
165 [Test] // no ArgumentNullException happens...
166 public void BooleanSwitchNullDefaultValue ()
168 new BooleanSwitch ("test", "", null);
172 public void BooleanSwitchValidDefaultValue ()
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");
187 [ExpectedException (typeof (FormatException))]
188 public void BooleanSwitchInvalidDefaultValue ()
190 BooleanSwitch s = new BooleanSwitch ("test", "", "hoge");
191 Assert.IsTrue (!s.Enabled);
195 public void NullSwitchHasEmptyDisplayNameAndDescription ()
197 var s = new TestNullSwitch ();
198 Assert.IsEmpty (s.DisplayName);
199 Assert.IsEmpty (s.Description);