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");
40 ops
.Append ("get_Value\n");
41 // ensure that the .config file is read in
42 int n
= base.SwitchSetting
;
47 public bool Validate ()
49 return expected
== ops
.ToString();
52 private void GetSetting ()
54 ops
.Append ("GetSetting\n");
55 IDictionary d
= (IDictionary
) ConfigurationSettings
.GetConfig ("system.diagnostics");
57 d
= (IDictionary
) d
["switches"];
59 v
= d
[DisplayName
].ToString();
64 protected override void OnSwitchSettingChanged ()
66 ops
.Append ("OnSwitchSettingChanged\n");
73 public class SwitchesTest
: Assertion
{
75 private static BooleanSwitch bon
= new BooleanSwitch ("bool-true", "");
76 private static BooleanSwitch bon2
= new BooleanSwitch ("bool-true-2", "");
77 private static BooleanSwitch bon3
= new BooleanSwitch ("bool-true-3", "");
78 private static BooleanSwitch boff
= new BooleanSwitch ("bool-false", "");
79 private static BooleanSwitch boff2
= new BooleanSwitch ("bool-default", "");
81 private static TraceSwitch toff
= new TraceSwitch ("trace-off", "");
82 private static TraceSwitch terror
= new TraceSwitch ("trace-error", "");
83 private static TraceSwitch twarning
= new TraceSwitch ("trace-warning", "");
84 private static TraceSwitch tinfo
= new TraceSwitch ("trace-info", "");
85 private static TraceSwitch tverbose
= new TraceSwitch ("trace-verbose", "");
86 private static TraceSwitch tdefault
= new TraceSwitch ("no-value", "");
87 private static TraceSwitch tsv
= new TraceSwitch ("string-value", "");
88 private static TraceSwitch tnegative
= new TraceSwitch ("trace-negative", "");
90 private static TestNewSwitch tns
= new TestNewSwitch ("string-value", "");
93 public void BooleanSwitches ()
95 Assert ("#BS:T:1", bon
.Enabled
);
96 Assert ("#BS:T:2", bon2
.Enabled
);
97 Assert ("#BS:T:3", bon3
.Enabled
);
98 Assert ("#BS:F:1", !boff
.Enabled
);
99 Assert ("#BS:F:2", !boff2
.Enabled
);
103 public void TraceSwitches ()
106 CheckTraceSwitch (toff
, false, false, false, false);
107 CheckTraceSwitch (terror
, true, false, false, false);
108 CheckTraceSwitch (twarning
, true, true, false, false);
109 CheckTraceSwitch (tinfo
, true, true, true, false);
110 CheckTraceSwitch (tverbose
, true, true, true, true);
112 // Default value is 0
113 CheckTraceSwitch (tdefault
, false, false, false, false);
115 // string value can't be converted to int, so default is 0
116 CheckTraceSwitch (tsv
, false, false, false, false);
118 // negative number is < 0, so all off
119 CheckTraceSwitch (tnegative
, false, false, false, false);
122 private void CheckTraceSwitch (TraceSwitch ts
, bool te
, bool tw
, bool ti
, bool tv
)
124 string desc
= string.Format ("#TS:{0}", ts
.DisplayName
);
125 AssertEquals (desc
+ ":TraceError", te
, ts
.TraceError
);
126 AssertEquals (desc
+ ":TraceWarning", tw
, ts
.TraceWarning
);
127 AssertEquals (desc
+ ":TraceInfo", ti
, ts
.TraceInfo
);
128 AssertEquals (desc
+ ":TraceVerbose", tv
, ts
.TraceVerbose
);
132 public void NewSwitch ()
134 AssertEquals ("#NS:Value", "0", tns
.Value
);
135 Assert ("#NS:Validate", tns
.Validate());