**** Merged from MCS ****
[mono-project.git] / mcs / class / System / Test / System.Diagnostics / SwitchesTest.cs
blob1fde5d0926d1eb9dfbde67c7f5fd05e110d9be8c
1 //
2 // SwitchesTest.cs:
3 // NUnit Test Cases for System.Diagnostics.BooleanSwitch and
4 // System.Diagnostics.TraceSwitch
5 //
6 // Authors:
7 // Jonathan Pryor (jonpryor@vt.edu)
8 // Martin Willemoes Hansen (mwh@sysrq.dk)
9 //
10 // (C) 2002 Jonathan Pryor
11 // (C) 2003 Martin Willemoes Hansen
14 using NUnit.Framework;
15 using System;
16 using System.Text;
17 using System.Collections;
18 using System.Configuration;
19 using System.Diagnostics;
21 namespace MonoTests.System.Diagnostics {
23 class TestNewSwitch : Switch {
24 private string v;
25 private StringBuilder ops = new StringBuilder ();
26 private const string expected =
27 ".ctor\n" +
28 "get_Value\n" +
29 "OnSwitchSettingChanged\n" +
30 "GetSetting\n";
32 public TestNewSwitch (string name, string desc)
33 : base (name, desc)
35 ops.Append (".ctor\n");
38 public string Value {
39 get {
40 ops.Append ("get_Value\n");
41 // ensure that the .config file is read in
42 int n = base.SwitchSetting;
43 return v;
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");
56 if (d != null) {
57 d = (IDictionary) d ["switches"];
58 if (d != null) {
59 v = d [DisplayName].ToString();
64 protected override void OnSwitchSettingChanged ()
66 ops.Append ("OnSwitchSettingChanged\n");
68 GetSetting ();
72 [TestFixture]
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", "");
92 [Test]
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);
102 [Test]
103 public void TraceSwitches ()
105 // The levels 0..4:
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);
131 [Test]
132 public void NewSwitch ()
134 AssertEquals ("#NS:Value", "0", tns.Value);
135 Assert ("#NS:Validate", tns.Validate());