SwitchesTest.cs 4.5 KB

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