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 #if !MOBILE && !MONOMAC
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
51 public string [] ExposeSupportedAttributes ()
53 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");
81 class TestNullSwitch : Switch {
82 public TestNullSwitch () : base (null, null)
88 public class SwitchesTest {
90 private static BooleanSwitch bon = new BooleanSwitch ("bool-true", "");
91 private static BooleanSwitch bon2 = new BooleanSwitch ("bool-true-2", "");
92 private static BooleanSwitch bon3 = new BooleanSwitch ("bool-true-3", "");
93 private static BooleanSwitch boff = new BooleanSwitch ("bool-false", "");
94 private static BooleanSwitch boff2 = new BooleanSwitch ("bool-default", "");
96 private static TraceSwitch toff = new TraceSwitch ("trace-off", "");
97 private static TraceSwitch terror = new TraceSwitch ("trace-error", "");
98 private static TraceSwitch twarning = new TraceSwitch ("trace-warning", "");
99 private static TraceSwitch tinfo = new TraceSwitch ("trace-info", "");
100 private static TraceSwitch tverbose = new TraceSwitch ("trace-verbose", "");
101 private static TraceSwitch tdefault = new TraceSwitch ("no-value", "");
102 private static TraceSwitch tsv = new TraceSwitch ("string-value", "");
103 private static TraceSwitch tnegative = new TraceSwitch ("trace-negative", "");
105 private static TestNewSwitch tns = new TestNewSwitch ("custom-switch", "");
108 public void BooleanSwitches ()
110 Assert.IsTrue (bon.Enabled, "#BS:T:1");
111 Assert.IsTrue (bon2.Enabled, "#BS:T:2");
112 Assert.IsTrue (bon3.Enabled, "#BS:T:3");
113 Assert.IsTrue (!boff.Enabled, "#BS:F:1");
114 Assert.IsTrue (!boff2.Enabled, "#BS:F:2");
118 public void TraceSwitches ()
121 CheckTraceSwitch (toff, false, false, false, false);
122 CheckTraceSwitch (terror, true, false, false, false);
123 CheckTraceSwitch (twarning, true, true, false, false);
124 CheckTraceSwitch (tinfo, true, true, true, false);
125 CheckTraceSwitch (tverbose, true, true, true, true);
127 // Default value is 0
128 CheckTraceSwitch (tdefault, false, false, false, false);
130 // string value can't be converted to int, so default is 0
131 CheckTraceSwitch (tsv, false, false, false, false);
133 // negative number is < 0, so all off
134 CheckTraceSwitch (tnegative, false, false, false, false);
137 private void CheckTraceSwitch (TraceSwitch ts, bool te, bool tw, bool ti, bool tv)
139 string desc = string.Format ("#TS:{0}", ts.DisplayName);
140 Assert.AreEqual (te, ts.TraceError, desc + ":TraceError");
141 Assert.AreEqual (tw, ts.TraceWarning, desc + ":TraceWarning");
142 Assert.AreEqual (ti, ts.TraceInfo, desc + ":TraceInfo");
143 Assert.AreEqual (tv, ts.TraceVerbose, desc + ":TraceVerbose");
147 [Ignore ("this test depends on 1.x configuration type")]
148 public void NewSwitch ()
150 Assert.AreEqual ("42", tns.TestValue, "#NS:TestValue");
151 Assert.IsTrue (tns.Validate(), "#NS:Validate");
155 public void GetSupportedAttributes ()
157 Assert.IsNull (tns.ExposeSupportedAttributes ());
160 [Test] // no ArgumentNullException happens...
161 public void BooleanSwitchNullDefaultValue ()
163 new BooleanSwitch ("test", "", null);
167 public void BooleanSwitchValidDefaultValue ()
169 BooleanSwitch s = new BooleanSwitch ("test", "", "2");
170 Assert.IsTrue (s.Enabled, "#1");
171 s = new BooleanSwitch ("test", "", "0");
172 Assert.IsTrue (!s.Enabled, "#2");
173 s = new BooleanSwitch ("test", "", "true");
174 Assert.IsTrue (s.Enabled, "#3");
175 s = new BooleanSwitch ("test", "", "True");
176 Assert.IsTrue (s.Enabled, "#4");
177 s = new BooleanSwitch ("test", "", "truE");
178 Assert.IsTrue (s.Enabled, "#5");
182 [ExpectedException (typeof (FormatException))]
183 public void BooleanSwitchInvalidDefaultValue ()
185 BooleanSwitch s = new BooleanSwitch ("test", "", "hoge");
186 Assert.IsTrue (!s.Enabled);
190 public void NullSwitchHasEmptyDisplayNameAndDescription ()
192 var s = new TestNullSwitch ();
193 AssertHelper.IsEmpty (s.DisplayName);
194 AssertHelper.IsEmpty (s.Description);