|
| 1 | +using System; |
| 2 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 3 | + |
| 4 | +namespace Inasync.Tests { |
| 5 | + |
| 6 | + [TestClass] |
| 7 | + public class TestAA_Act_Tests { |
| 8 | + |
| 9 | + [TestMethod] |
| 10 | + public void Act() { |
| 11 | + Action TestCase(int testNumber, Action act, Type exceptionType, Type expectedExceptionType = null) => () => { |
| 12 | + var msg = "No." + testNumber; |
| 13 | + |
| 14 | + // Act |
| 15 | + TestActual testActual = default; |
| 16 | + Exception exception = null; |
| 17 | + try { |
| 18 | + testActual = TestAA.Act(act); |
| 19 | + } |
| 20 | + catch (Exception ex) { |
| 21 | + exception = ex; |
| 22 | + } |
| 23 | + |
| 24 | + // Assert |
| 25 | + if (exception == null) { |
| 26 | + Assert.AreEqual(typeof(TestActual), testActual.GetType(), msg); |
| 27 | + Assert.AreEqual(exceptionType, testActual.Exception?.GetType(), msg); |
| 28 | + } |
| 29 | + Assert.AreEqual(expectedExceptionType, exception?.GetType(), msg); |
| 30 | + }; |
| 31 | + |
| 32 | + foreach (var action in new[]{ |
| 33 | + TestCase( 0, act: null , exceptionType: null , expectedExceptionType: typeof(ArgumentNullException)), |
| 34 | + TestCase( 1, act: () => { } , exceptionType: null ), |
| 35 | + TestCase( 2, act: () => throw new DummyException(), exceptionType: typeof(DummyException)), |
| 36 | + }) { action(); } |
| 37 | + } |
| 38 | + |
| 39 | + [TestMethod] |
| 40 | + public void Act_TReturn() { |
| 41 | + Action TestCase(int testNumber, Func<DummyObject> act, (DummyObject @return, Type exceptionType) expected, Type expectedExceptionType = null) => () => { |
| 42 | + var msg = "No." + testNumber; |
| 43 | + |
| 44 | + // Act |
| 45 | + TestActual<DummyObject> testActual = default; |
| 46 | + Exception exception = null; |
| 47 | + try { |
| 48 | + testActual = TestAA.Act(act); |
| 49 | + } |
| 50 | + catch (Exception ex) { |
| 51 | + exception = ex; |
| 52 | + } |
| 53 | + |
| 54 | + // Assert |
| 55 | + if (exception == null) { |
| 56 | + Assert.AreEqual(expected.exceptionType, testActual.Exception?.GetType(), msg); |
| 57 | + Assert.AreEqual(expected.@return, testActual.Return, msg); |
| 58 | + } |
| 59 | + Assert.AreEqual(expectedExceptionType, exception?.GetType(), msg); |
| 60 | + }; |
| 61 | + |
| 62 | + var obj = new DummyObject(); |
| 63 | + foreach (var action in new[]{ |
| 64 | + TestCase( 0, act: null , expected: default , expectedExceptionType: typeof(ArgumentNullException)), |
| 65 | + TestCase( 1, act: () => obj , expected: (obj , null) ), |
| 66 | + TestCase( 2, act: () => throw new DummyException(), expected: (null, typeof(DummyException))), |
| 67 | + }) { action(); } |
| 68 | + } |
| 69 | + |
| 70 | + #region Helpers |
| 71 | + |
| 72 | + private sealed class DummyException : Exception { } |
| 73 | + |
| 74 | + private sealed class DummyObject { } |
| 75 | + |
| 76 | + #endregion Helpers |
| 77 | + } |
| 78 | +} |
0 commit comments