SwitchesTest.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // SwitchesTest.cs:
  3. // NUnit Test Cases for System.Diagnostics.BooleanSwitch and
  4. // System.Diagnostics.TraceSwitch
  5. //
  6. // Authors:
  7. // Jonathan Pryor ([email protected])
  8. // Martin Willemoes Hansen ([email protected])
  9. //
  10. // (C) 2002 Jonathan Pryor
  11. // (C) 2003 Martin Willemoes Hansen
  12. //
  13. using NUnit.Framework;
  14. using System;
  15. using System.Text;
  16. using System.Collections;
  17. using System.Configuration;
  18. using System.Diagnostics;
  19. namespace MonoTests.System.Diagnostics {
  20. class TestNewSwitch : Switch {
  21. private string v;
  22. private StringBuilder ops = new StringBuilder ();
  23. private const string expected =
  24. ".ctor\n" +
  25. "get_Value\n" +
  26. "OnSwitchSettingChanged\n" +
  27. "GetSetting\n";
  28. public TestNewSwitch (string name, string desc)
  29. : base (name, desc)
  30. {
  31. ops.Append (".ctor\n");
  32. }
  33. public string Value {
  34. get {
  35. ops.Append ("get_Value\n");
  36. // ensure that the .config file is read in
  37. int n = base.SwitchSetting;
  38. return v;
  39. }
  40. }
  41. public bool Validate ()
  42. {
  43. return expected == ops.ToString();
  44. }
  45. private void GetSetting ()
  46. {
  47. ops.Append ("GetSetting\n");
  48. IDictionary d = (IDictionary) ConfigurationSettings.GetConfig ("system.diagnostics");
  49. if (d != null) {
  50. d = (IDictionary) d ["switches"];
  51. if (d != null) {
  52. v = d [DisplayName].ToString();
  53. }
  54. }
  55. }
  56. protected override void OnSwitchSettingChanged ()
  57. {
  58. ops.Append ("OnSwitchSettingChanged\n");
  59. GetSetting ();
  60. }
  61. }
  62. [TestFixture]
  63. public class SwitchesTest : Assertion {
  64. private static BooleanSwitch bon = new BooleanSwitch ("bool-true", "");
  65. private static BooleanSwitch bon2 = new BooleanSwitch ("bool-true-2", "");
  66. private static BooleanSwitch bon3 = new BooleanSwitch ("bool-true-3", "");
  67. private static BooleanSwitch boff = new BooleanSwitch ("bool-false", "");
  68. private static BooleanSwitch boff2 = new BooleanSwitch ("bool-default", "");
  69. private static TraceSwitch toff = new TraceSwitch ("trace-off", "");
  70. private static TraceSwitch terror = new TraceSwitch ("trace-error", "");
  71. private static TraceSwitch twarning = new TraceSwitch ("trace-warning", "");
  72. private static TraceSwitch tinfo = new TraceSwitch ("trace-info", "");
  73. private static TraceSwitch tverbose = new TraceSwitch ("trace-verbose", "");
  74. private static TraceSwitch tdefault = new TraceSwitch ("no-value", "");
  75. private static TraceSwitch tsv = new TraceSwitch ("string-value", "");
  76. private static TraceSwitch tnegative = new TraceSwitch ("trace-negative", "");
  77. private static TestNewSwitch tns = new TestNewSwitch ("string-value", "");
  78. [Test]
  79. public void BooleanSwitches ()
  80. {
  81. Assert ("#BS:T:1", bon.Enabled);
  82. Assert ("#BS:T:2", bon2.Enabled);
  83. Assert ("#BS:T:3", bon3.Enabled);
  84. Assert ("#BS:F:1", !boff.Enabled);
  85. Assert ("#BS:F:2", !boff2.Enabled);
  86. }
  87. [Test]
  88. public void TraceSwitches ()
  89. {
  90. // The levels 0..4:
  91. CheckTraceSwitch (toff, false, false, false, false);
  92. CheckTraceSwitch (terror, true, false, false, false);
  93. CheckTraceSwitch (twarning, true, true, false, false);
  94. CheckTraceSwitch (tinfo, true, true, true, false);
  95. CheckTraceSwitch (tverbose, true, true, true, true);
  96. // Default value is 0
  97. CheckTraceSwitch (tdefault, false, false, false, false);
  98. // string value can't be converted to int, so default is 0
  99. CheckTraceSwitch (tsv, false, false, false, false);
  100. // negative number is < 0, so all off
  101. CheckTraceSwitch (tnegative, false, false, false, false);
  102. }
  103. private void CheckTraceSwitch (TraceSwitch ts, bool te, bool tw, bool ti, bool tv)
  104. {
  105. string desc = string.Format ("#TS:{0}", ts.DisplayName);
  106. AssertEquals (desc + ":TraceError", te, ts.TraceError);
  107. AssertEquals (desc + ":TraceWarning", tw, ts.TraceWarning);
  108. AssertEquals (desc + ":TraceInfo", ti, ts.TraceInfo);
  109. AssertEquals (desc + ":TraceVerbose", tv, ts.TraceVerbose);
  110. }
  111. [Test]
  112. public void NewSwitch ()
  113. {
  114. AssertEquals ("#NS:Value", "0", tns.Value);
  115. Assert ("#NS:Validate", tns.Validate());
  116. }
  117. }
  118. }