Skip to content

Commit

Permalink
Release/v0.2.1
Browse files Browse the repository at this point in the history
* Assert() の exception パラメーター省略時の `TestActual.Exception` 再スローでスタックトレースが失われていたので修正。
* readme.md を更新。
  • Loading branch information
in-async committed Nov 15, 2019
1 parent d83aa2f commit f3a238f
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 8 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ TestAA.Act(() => { /* ここでテスト対象のメソッドを呼ぶ */ })
.Act(() => int.Parse("123"))
.Assert(
@return: ret => { /* ここで戻り値の検証 */ },
exception: ex => { /* ここで例外の検証 */ },
exception: ex => { /* ここで例外の検証。省略した場合、Act() で例外が生じていれば再スローされる */ },
others: () => { /* ここで上記以外の検証。不要なら省略 */ }
);
```
Expand All @@ -51,8 +51,7 @@ TestAA.Act(() => { /* ここでテスト対象のメソッドを呼ぶ */ })
public void IntParseTest() {
// Success
TestAA.Act(() => int.Parse("123")).Assert(
ret => ret.Is(123),
ex => ex?.GetType().Is(null)
ret => ret.Is(123)
);

// FormatException
Expand Down
2 changes: 1 addition & 1 deletion src/Inasync.TestAA/Inasync.TestAA.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageProjectUrl>https://github.com/in-async/TestAA</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/in-async/TestAA/blob/master/LICENSE</PackageLicenseUrl>
<PackageTags>library test unittest aaa</PackageTags>
<Version>0.2.0</Version>
<Version>0.2.1</Version>
</PropertyGroup>

</Project>
4 changes: 2 additions & 2 deletions src/Inasync.TestAA/TestAA.Assert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static void Assert(this TestActual actual, Action<Exception> exception =
if (exception != null) {
exception(actual.Exception);
}
else if (actual.Exception != null) { throw actual.Exception; }
else if (actual.Exception != null) { throw new TestAssertException(null, innerException: actual.Exception); }

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

others?.Invoke();
}
Expand Down
40 changes: 40 additions & 0 deletions src/Inasync.TestAA/TestAssertException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;

#if !NETSTANDARD1_0

using System.Runtime.Serialization;

#endif

namespace Inasync {
/// <summary>
/// Assert 時に検証されなかった <see cref="TestActual.Exception"/> を内包する例外を表します。
/// </summary>
#if !NETSTANDARD1_0

[Serializable]
#endif
public class TestAssertException : Exception {
private const string _defaultMessage = "TestActual に例外が含まれていますが、検証されませんでした。";

/// <summary>
/// 指定したエラー メッセージおよびこの例外の原因となった内部例外への参照を使用して、<see cref="TestAssertException"/> クラスの新しいインスタンスを初期化します。
/// </summary>
/// <param name="message">例外の原因を説明するエラー メッセージ。</param>
/// <param name="innerException">現在の例外の原因である例外。内部例外が指定されていない場合は <c>null</c> 参照。</param>
public TestAssertException(string message, Exception innerException) : base(message ?? _defaultMessage, innerException) {
}

#if !NETSTANDARD1_0

/// <summary>
/// シリアル化したデータを使用して、<see cref="TestAssertException"/> クラスの新しいインスタンスを初期化します。
/// </summary>
/// <param name="info">スローされている例外に関するシリアル化済みオブジェクト データを保持している <see cref="SerializationInfo"/>。</param>
/// <param name="context">転送元または転送先についてのコンテキスト情報を含む <see cref="StreamingContext"/>。</param>
protected TestAssertException(SerializationInfo info, StreamingContext context) : base(info, context) {
}

#endif
}
}
4 changes: 2 additions & 2 deletions tests/Inasync.TestAA.Tests/TestAA.Assert.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Action TestCase(int testNumber, TestActual testActual, AssertException exception
TestCase( 0, new TestActual(null), null , new AssertOthers(), expected: (exception: null, others: true )),
TestCase( 1, new TestActual(null), new AssertException(), null , expected: (exception: null, others: false)),
TestCase( 2, new TestActual(null), new AssertException(), new AssertOthers(), expected: (exception: null, others: true )),
TestCase( 3, new TestActual(ex) , null , new AssertOthers(), expected: (exception: null, others: false), expectedExceptionType: typeof(DummyException)),
TestCase( 3, new TestActual(ex) , null , new AssertOthers(), expected: (exception: null, others: false), expectedExceptionType: typeof(TestAssertException)),
TestCase( 4, new TestActual(ex) , new AssertException(), null , expected: (exception: ex , others: false)),
TestCase( 5, new TestActual(ex) , new AssertException(), new AssertOthers(), expected: (exception: ex , others: true )),
}) { action(); }
Expand Down Expand Up @@ -66,7 +66,7 @@ Action TestCase(int testNumber, TestActual<DummyObject> testActual, AssertReturn
TestCase( 2, new TestActual<DummyObject>(obj , null), new AssertReturn(), new AssertException(), null , expected: (@return: obj , exception: null, others: false)),
TestCase( 3, new TestActual<DummyObject>(obj , null), new AssertReturn(), new AssertException(), new AssertOthers(), expected: (@return: obj , exception: null, others: true )),
TestCase( 4, new TestActual<DummyObject>(null, ex ), null , new AssertException(), new AssertOthers(), expected: (@return: null, exception: null, others: false), expectedExceptionType: typeof(ArgumentNullException)),
TestCase( 5, new TestActual<DummyObject>(null, ex ), new AssertReturn(), null , new AssertOthers(), expected: (@return: null, exception: null, others: false), expectedExceptionType: typeof(DummyException)),
TestCase( 5, new TestActual<DummyObject>(null, ex ), new AssertReturn(), null , new AssertOthers(), expected: (@return: null, exception: null, others: false), expectedExceptionType: typeof(TestAssertException)),
TestCase( 6, new TestActual<DummyObject>(null, ex ), new AssertReturn(), new AssertException(), null , expected: (@return: null, exception: ex , others: false)),
TestCase( 7, new TestActual<DummyObject>(null, ex ), new AssertReturn(), new AssertException(), new AssertOthers(), expected: (@return: null, exception: ex , others: true )),
}) { action(); }
Expand Down
22 changes: 22 additions & 0 deletions tests/Inasync.TestAA.Tests/TestAssertException.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Inasync.Tests {

[TestClass]
public class TestAssertException_Tests {

[TestMethod]
public void Ctor() {
var message = "abc";
var innerException = new Exception();

// Act
var @return = new TestAssertException(message, innerException);

// Assert
Assert.AreEqual(message, @return.Message);
Assert.AreSame(innerException, @return.InnerException);
}
}
}
14 changes: 14 additions & 0 deletions tests/Inasync.TestAA.Tests/Usage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,19 @@ Action TestCase(int testNumber, string input, int expected, Type expectedExcepti
TestCase( 2, "123", expected: 123),
}) { action(); }
}

[TestMethod]
public void IntParseTest() {
// Success
TestAA.Act(() => int.Parse("123")).Assert(
ret => Assert.AreEqual(123, ret)
);

// FormatException
TestAA.Act(() => int.Parse("abc")).Assert(
ret => { },
ex => Assert.AreEqual(typeof(FormatException), ex?.GetType())
);
}
}
}

0 comments on commit f3a238f

Please sign in to comment.