PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/Machine.Specifications.Specs/CatchSpecs.cs

https://github.com/machine/machine.specifications
C# | 131 lines | 100 code | 31 blank | 0 comment | 0 complexity | 0065a40f73fe536b649e2cecc0de7a1f MD5 | raw file
Possible License(s): MIT, CC-BY-SA-3.0
  1. using System;
  2. using FluentAssertions;
  3. namespace Machine.Specifications.Specs
  4. {
  5. [Subject(typeof(Catch))]
  6. public class when_calling_Catch_Exception_with_an_Action
  7. {
  8. [Subject(typeof(Catch))]
  9. public class with_a_throwing_Action
  10. {
  11. static ArgumentException AnException;
  12. static Exception Result;
  13. Establish context = () => { AnException = new ArgumentException(); };
  14. Because of = () => { Result = Catch.Exception(() => { throw AnException; }); };
  15. It should_return_the_same_exception =
  16. () => Result.Should().BeSameAs(AnException);
  17. }
  18. [Subject(typeof(Catch))]
  19. public class with_a_non_throwing_Action
  20. {
  21. static string ActionSideEffect;
  22. static Exception Result;
  23. Because of = () => { Result = Catch.Exception(() => { ActionSideEffect = "hi"; }); };
  24. It should_access_the_propety =
  25. () => ActionSideEffect.Should().Be("hi");
  26. It should_return_null =
  27. () => Result.Should().BeNull();
  28. }
  29. }
  30. [Subject(typeof(Catch))]
  31. public class when_calling_Catch_Exception_with_a_Func
  32. {
  33. class Dummy
  34. {
  35. public static readonly ArgumentException AnException = new ArgumentException();
  36. public static string ThrowingProperty
  37. {
  38. get { throw AnException; }
  39. }
  40. public static string NonThrowingProperty
  41. {
  42. get { return "hi"; }
  43. }
  44. }
  45. [Subject(typeof(Catch))]
  46. public class with_a_throwing_Func
  47. {
  48. static Exception Result;
  49. Because of = () => { Result = Catch.Exception(() => Dummy.ThrowingProperty); };
  50. It should_return_the_same_exception =
  51. () => Result.Should().BeSameAs(Dummy.AnException);
  52. }
  53. [Subject(typeof(Catch))]
  54. public class with_a_non_throwing_Func
  55. {
  56. static Exception Result;
  57. static string PropertyValue;
  58. Because of = () => { Result = Catch.Exception(() => PropertyValue = Dummy.NonThrowingProperty); };
  59. It should_access_the_propety =
  60. () => PropertyValue.Should().Be("hi");
  61. It should_return_null =
  62. () => Result.Should().BeNull();
  63. }
  64. }
  65. [Subject(typeof(Catch))]
  66. public class when_calling_Catch_Only_with_an_Action
  67. {
  68. [Subject(typeof(Catch))]
  69. public class with_a_throwing_Action_which_matches_exception_to_be_caught
  70. {
  71. static ArgumentException AnException;
  72. static Exception Result;
  73. Establish context = () => { AnException = new ArgumentException(); };
  74. Because of = () => { Result = Catch.Only<ArgumentException>(() => { throw AnException; }); };
  75. It should_return_the_same_exception =
  76. () => Result.Should().BeSameAs(AnException);
  77. }
  78. [Subject(typeof(Catch))]
  79. public class with_a_throwing_Action_which_doesnt_match_exception_to_be_caught
  80. {
  81. static ArgumentException AnException;
  82. static Exception Result;
  83. Establish context = () => { AnException = new ArgumentException(); };
  84. Because of = () => { Result = Catch.Exception(() => Catch.Only<InvalidOperationException>(() => { throw AnException; })); };
  85. It should_return_the_same_exception =
  86. () => Result.Should().BeSameAs(AnException);
  87. }
  88. [Subject(typeof(Catch))]
  89. public class with_a_non_throwing_Action
  90. {
  91. static string ActionSideEffect;
  92. static Exception Result;
  93. Because of = () => { Result = Catch.Only<ArgumentException>((() => { ActionSideEffect = "hi"; })); };
  94. It should_access_the_propety =
  95. () => ActionSideEffect.Should().Be("hi");
  96. It should_return_null =
  97. () => Result.Should().BeNull();
  98. }
  99. }
  100. }