PageRenderTime 67ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/framework/Attributes/ExpectedExceptionAttribute.cs

#
C# | 123 lines | 67 code | 12 blank | 44 comment | 0 complexity | ad744a1b043c3dd758d5b24642b195f8 MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // This is free software licensed under the NUnit license. You may
  3. // obtain a copy of the license at http://nunit.org.
  4. // ****************************************************************
  5. namespace NUnit.Framework
  6. {
  7. using System;
  8. /// <summary>
  9. /// Enumeration indicating how the expected message parameter is to be used
  10. /// </summary>
  11. public enum MessageMatch
  12. {
  13. /// Expect an exact match
  14. Exact,
  15. /// Expect a message containing the parameter string
  16. Contains,
  17. /// Match the regular expression provided as a parameter
  18. Regex,
  19. /// Expect a message that starts with the parameter string
  20. StartsWith
  21. }
  22. /// <summary>
  23. /// ExpectedExceptionAttribute
  24. /// </summary>
  25. ///
  26. [AttributeUsage(AttributeTargets.Method, AllowMultiple=false, Inherited=false)]
  27. public class ExpectedExceptionAttribute : Attribute
  28. {
  29. private Type expectedException;
  30. private string expectedExceptionName;
  31. private string expectedMessage;
  32. private MessageMatch matchType;
  33. private string userMessage;
  34. private string handler;
  35. /// <summary>
  36. /// Constructor for a non-specific exception
  37. /// </summary>
  38. public ExpectedExceptionAttribute()
  39. {
  40. }
  41. /// <summary>
  42. /// Constructor for a given type of exception
  43. /// </summary>
  44. /// <param name="exceptionType">The type of the expected exception</param>
  45. public ExpectedExceptionAttribute(Type exceptionType)
  46. {
  47. this.expectedException = exceptionType;
  48. this.expectedExceptionName = exceptionType.FullName;
  49. }
  50. /// <summary>
  51. /// Constructor for a given exception name
  52. /// </summary>
  53. /// <param name="exceptionName">The full name of the expected exception</param>
  54. public ExpectedExceptionAttribute(string exceptionName)
  55. {
  56. this.expectedExceptionName = exceptionName;
  57. }
  58. /// <summary>
  59. /// Gets or sets the expected exception type
  60. /// </summary>
  61. public Type ExpectedException
  62. {
  63. get{ return expectedException; }
  64. set
  65. {
  66. expectedException = value;
  67. expectedExceptionName = expectedException.FullName;
  68. }
  69. }
  70. /// <summary>
  71. /// Gets or sets the full Type name of the expected exception
  72. /// </summary>
  73. public string ExpectedExceptionName
  74. {
  75. get{ return expectedExceptionName; }
  76. set{ expectedExceptionName = value; }
  77. }
  78. /// <summary>
  79. /// Gets or sets the expected message text
  80. /// </summary>
  81. public string ExpectedMessage
  82. {
  83. get { return expectedMessage; }
  84. set { expectedMessage = value; }
  85. }
  86. /// <summary>
  87. /// Gets or sets the user message displayed in case of failure
  88. /// </summary>
  89. public string UserMessage
  90. {
  91. get { return userMessage; }
  92. set { userMessage = value; }
  93. }
  94. /// <summary>
  95. /// Gets or sets the type of match to be performed on the expected message
  96. /// </summary>
  97. public MessageMatch MatchType
  98. {
  99. get { return matchType; }
  100. set { matchType = value; }
  101. }
  102. /// <summary>
  103. /// Gets the name of a method to be used as an exception handler
  104. /// </summary>
  105. public string Handler
  106. {
  107. get { return handler; }
  108. set { handler = value; }
  109. }
  110. }
  111. }