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 {
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.IsTrue (bon.Enabled, "#BS:T:1");
105 Assert.IsTrue (bon2.Enabled, "#BS:T:2");
106 Assert.IsTrue (bon3.Enabled, "#BS:T:3");
107 Assert.IsTrue (!boff.Enabled, "#BS:F:1");
108 Assert.IsTrue (!boff2.Enabled, "#BS:F:2");
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 Assert.AreEqual (te, ts.TraceError, desc + ":TraceError");
135 Assert.AreEqual (tw, ts.TraceWarning, desc + ":TraceWarning");
136 Assert.AreEqual (ti, ts.TraceInfo, desc + ":TraceInfo");
137 Assert.AreEqual (tv, ts.TraceVerbose, desc + ":TraceVerbose");
142 [Ignore ("this test depends on 1.x configuration type")]
144 public void NewSwitch ()
146 Assert.AreEqual ("42", tns.TestValue, "#NS:TestValue");
147 Assert.IsTrue (tns.Validate(), "#NS:Validate");
152 public void GetSupportedAttributes ()
154 Assert.IsNull (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.IsTrue (s.Enabled, "#1");
168 s = new BooleanSwitch ("test", "", "0");
169 Assert.IsTrue (!s.Enabled, "#2");
170 s = new BooleanSwitch ("test", "", "true");
171 Assert.IsTrue (s.Enabled, "#3");
172 s = new BooleanSwitch ("test", "", "True");
173 Assert.IsTrue (s.Enabled, "#4");
174 s = new BooleanSwitch ("test", "", "truE");
175 Assert.IsTrue (s.Enabled, "#5");
179 [ExpectedException (typeof (FormatException))]
180 public void BooleanSwitchInvalidDefaultValue ()
182 BooleanSwitch s = new BooleanSwitch ("test", "", "hoge");
183 Assert.IsTrue (!s.Enabled);