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
14 using NUnit.Framework;
17 using System.Collections;
18 using System.Configuration;
19 using System.Diagnostics;
21 namespace MonoTests.System.Diagnostics {
23 class TestNewSwitch : Switch {
25 private StringBuilder ops = new StringBuilder ();
26 private const string expected =
29 "OnSwitchSettingChanged\n" +
32 public TestNewSwitch (string name, string desc)
35 ops.Append (".ctor\n");
38 public string TestValue {
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
50 public string [] ExposeSupportedAttributes ()
52 return GetSupportedAttributes ();
56 public bool Validate ()
58 return expected == ops.ToString();
61 private void GetSetting ()
63 ops.Append ("GetSetting\n");
64 IDictionary d = (IDictionary) ConfigurationSettings.GetConfig ("system.diagnostics");
66 d = (IDictionary) d ["switches"];
68 v = d [DisplayName].ToString();
73 protected override void OnSwitchSettingChanged ()
75 ops.Append ("OnSwitchSettingChanged\n");
82 public class SwitchesTest : Assertion {
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", "");
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", "");
99 private static TestNewSwitch tns = new TestNewSwitch ("custom-switch", "");
102 public void BooleanSwitches ()
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);
112 public void TraceSwitches ()
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);
121 // Default value is 0
122 CheckTraceSwitch (tdefault, false, false, false, false);
124 // string value can't be converted to int, so default is 0
125 CheckTraceSwitch (tsv, false, false, false, false);
127 // negative number is < 0, so all off
128 CheckTraceSwitch (tnegative, false, false, false, false);
131 private void CheckTraceSwitch (TraceSwitch ts, bool te, bool tw, bool ti, bool tv)
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);
142 [Ignore ("this test depends on 1.x configuration type")]
144 public void NewSwitch ()
146 AssertEquals ("#NS:TestValue", "42", tns.TestValue);
147 Assert ("#NS:Validate", tns.Validate());
152 public void GetSupportedAttributes ()
154 AssertNull (tns.ExposeSupportedAttributes ());
157 [Test] // no ArgumentNullException happens...
158 public void BooleanSwitchNullDefaultValue ()
160 new BooleanSwitch ("test", "", null);
164 public void BooleanSwitchValidDefaultValue ()
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);
179 [ExpectedException (typeof (FormatException))]
180 public void BooleanSwitchInvalidDefaultValue ()
182 BooleanSwitch s = new BooleanSwitch ("test", "", "hoge");