Skip to content

Commit f3a238f

Browse files
committed
Release/v0.2.1
* Assert() の exception パラメーター省略時の `TestActual.Exception` 再スローでスタックトレースが失われていたので修正。 * readme.md を更新。
1 parent d83aa2f commit f3a238f

File tree

7 files changed

+83
-8
lines changed

7 files changed

+83
-8
lines changed

README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ TestAA.Act(() => { /* ここでテスト対象のメソッドを呼ぶ */ })
4040
.Act(() => int.Parse("123"))
4141
.Assert(
4242
@return: ret => { /* ここで戻り値の検証 */ },
43-
exception: ex => { /* ここで例外の検証 */ },
43+
exception: ex => { /* ここで例外の検証。省略した場合、Act() で例外が生じていれば再スローされる */ },
4444
others: () => { /* ここで上記以外の検証。不要なら省略 */ }
4545
);
4646
```
@@ -51,8 +51,7 @@ TestAA.Act(() => { /* ここでテスト対象のメソッドを呼ぶ */ })
5151
public void IntParseTest() {
5252
// Success
5353
TestAA.Act(() => int.Parse("123")).Assert(
54-
ret => ret.Is(123),
55-
ex => ex?.GetType().Is(null)
54+
ret => ret.Is(123)
5655
);
5756

5857
// FormatException

src/Inasync.TestAA/Inasync.TestAA.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<PackageProjectUrl>https://github.com/in-async/TestAA</PackageProjectUrl>
1010
<PackageLicenseUrl>https://github.com/in-async/TestAA/blob/master/LICENSE</PackageLicenseUrl>
1111
<PackageTags>library test unittest aaa</PackageTags>
12-
<Version>0.2.0</Version>
12+
<Version>0.2.1</Version>
1313
</PropertyGroup>
1414

1515
</Project>

src/Inasync.TestAA/TestAA.Assert.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static void Assert(this TestActual actual, Action<Exception> exception =
1414
if (exception != null) {
1515
exception(actual.Exception);
1616
}
17-
else if (actual.Exception != null) { throw actual.Exception; }
17+
else if (actual.Exception != null) { throw new TestAssertException(null, innerException: actual.Exception); }
1818

1919
others?.Invoke();
2020
}
@@ -39,7 +39,7 @@ public static void Assert<TReturn>(this TestActual<TReturn> actual, Action<TRetu
3939
if (exception != null) {
4040
exception(actual.Exception);
4141
}
42-
else if (actual.Exception != null) { throw actual.Exception; }
42+
else if (actual.Exception != null) { throw new TestAssertException(null, innerException: actual.Exception); }
4343

4444
others?.Invoke();
4545
}
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
3+
#if !NETSTANDARD1_0
4+
5+
using System.Runtime.Serialization;
6+
7+
#endif
8+
9+
namespace Inasync {
10+
/// <summary>
11+
/// Assert 時に検証されなかった <see cref="TestActual.Exception"/> を内包する例外を表します。
12+
/// </summary>
13+
#if !NETSTANDARD1_0
14+
15+
[Serializable]
16+
#endif
17+
public class TestAssertException : Exception {
18+
private const string _defaultMessage = "TestActual に例外が含まれていますが、検証されませんでした。";
19+
20+
/// <summary>
21+
/// 指定したエラー メッセージおよびこの例外の原因となった内部例外への参照を使用して、<see cref="TestAssertException"/> クラスの新しいインスタンスを初期化します。
22+
/// </summary>
23+
/// <param name="message">例外の原因を説明するエラー メッセージ。</param>
24+
/// <param name="innerException">現在の例外の原因である例外。内部例外が指定されていない場合は <c>null</c> 参照。</param>
25+
public TestAssertException(string message, Exception innerException) : base(message ?? _defaultMessage, innerException) {
26+
}
27+
28+
#if !NETSTANDARD1_0
29+
30+
/// <summary>
31+
/// シリアル化したデータを使用して、<see cref="TestAssertException"/> クラスの新しいインスタンスを初期化します。
32+
/// </summary>
33+
/// <param name="info">スローされている例外に関するシリアル化済みオブジェクト データを保持している <see cref="SerializationInfo"/>。</param>
34+
/// <param name="context">転送元または転送先についてのコンテキスト情報を含む <see cref="StreamingContext"/>。</param>
35+
protected TestAssertException(SerializationInfo info, StreamingContext context) : base(info, context) {
36+
}
37+
38+
#endif
39+
}
40+
}

tests/Inasync.TestAA.Tests/TestAA.Assert.Tests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Action TestCase(int testNumber, TestActual testActual, AssertException exception
3131
TestCase( 0, new TestActual(null), null , new AssertOthers(), expected: (exception: null, others: true )),
3232
TestCase( 1, new TestActual(null), new AssertException(), null , expected: (exception: null, others: false)),
3333
TestCase( 2, new TestActual(null), new AssertException(), new AssertOthers(), expected: (exception: null, others: true )),
34-
TestCase( 3, new TestActual(ex) , null , new AssertOthers(), expected: (exception: null, others: false), expectedExceptionType: typeof(DummyException)),
34+
TestCase( 3, new TestActual(ex) , null , new AssertOthers(), expected: (exception: null, others: false), expectedExceptionType: typeof(TestAssertException)),
3535
TestCase( 4, new TestActual(ex) , new AssertException(), null , expected: (exception: ex , others: false)),
3636
TestCase( 5, new TestActual(ex) , new AssertException(), new AssertOthers(), expected: (exception: ex , others: true )),
3737
}) { action(); }
@@ -66,7 +66,7 @@ Action TestCase(int testNumber, TestActual<DummyObject> testActual, AssertReturn
6666
TestCase( 2, new TestActual<DummyObject>(obj , null), new AssertReturn(), new AssertException(), null , expected: (@return: obj , exception: null, others: false)),
6767
TestCase( 3, new TestActual<DummyObject>(obj , null), new AssertReturn(), new AssertException(), new AssertOthers(), expected: (@return: obj , exception: null, others: true )),
6868
TestCase( 4, new TestActual<DummyObject>(null, ex ), null , new AssertException(), new AssertOthers(), expected: (@return: null, exception: null, others: false), expectedExceptionType: typeof(ArgumentNullException)),
69-
TestCase( 5, new TestActual<DummyObject>(null, ex ), new AssertReturn(), null , new AssertOthers(), expected: (@return: null, exception: null, others: false), expectedExceptionType: typeof(DummyException)),
69+
TestCase( 5, new TestActual<DummyObject>(null, ex ), new AssertReturn(), null , new AssertOthers(), expected: (@return: null, exception: null, others: false), expectedExceptionType: typeof(TestAssertException)),
7070
TestCase( 6, new TestActual<DummyObject>(null, ex ), new AssertReturn(), new AssertException(), null , expected: (@return: null, exception: ex , others: false)),
7171
TestCase( 7, new TestActual<DummyObject>(null, ex ), new AssertReturn(), new AssertException(), new AssertOthers(), expected: (@return: null, exception: ex , others: true )),
7272
}) { action(); }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
4+
namespace Inasync.Tests {
5+
6+
[TestClass]
7+
public class TestAssertException_Tests {
8+
9+
[TestMethod]
10+
public void Ctor() {
11+
var message = "abc";
12+
var innerException = new Exception();
13+
14+
// Act
15+
var @return = new TestAssertException(message, innerException);
16+
17+
// Assert
18+
Assert.AreEqual(message, @return.Message);
19+
Assert.AreSame(innerException, @return.InnerException);
20+
}
21+
}
22+
}

tests/Inasync.TestAA.Tests/Usage.cs

+14
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,19 @@ Action TestCase(int testNumber, string input, int expected, Type expectedExcepti
2323
TestCase( 2, "123", expected: 123),
2424
}) { action(); }
2525
}
26+
27+
[TestMethod]
28+
public void IntParseTest() {
29+
// Success
30+
TestAA.Act(() => int.Parse("123")).Assert(
31+
ret => Assert.AreEqual(123, ret)
32+
);
33+
34+
// FormatException
35+
TestAA.Act(() => int.Parse("abc")).Assert(
36+
ret => { },
37+
ex => Assert.AreEqual(typeof(FormatException), ex?.GetType())
38+
);
39+
}
2640
}
2741
}

0 commit comments

Comments
 (0)