diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..74e58ee --- /dev/null +++ b/.editorconfig @@ -0,0 +1,171 @@ +############################### +# Core EditorConfig Options # +############################### + +root = true + +# All files +[*] +indent_style = space +indent_size = 4 +insert_final_newline = true +charset = utf-8-bom +end_of_line = crlf +trim_trailing_whitespace = true + +# Code files +[*.{cs,csx,vb,vbx}] + +[*.{csproj,json}] +indent_size = 2 + +############################### +# .NET Coding Conventions # +############################### + +[*.{cs,vb}] +# Organize usings +dotnet_sort_system_directives_first = true + +# this. preferences +dotnet_style_qualification_for_field = false:none +dotnet_style_qualification_for_property = false:none +dotnet_style_qualification_for_method = false:none +dotnet_style_qualification_for_event = false:none + +# Language keywords vs BCL types preferences +dotnet_style_predefined_type_for_locals_parameters_members = true:none +dotnet_style_predefined_type_for_member_access = true:none + +# Parentheses preferences +dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:silent +dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:silent +dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:silent +dotnet_style_parentheses_in_other_operators = never_if_unnecessary:silent + +# Modifier preferences +dotnet_style_require_accessibility_modifiers = for_non_interface_members:none +dotnet_style_readonly_field = true:suggestion + +# Expression-level preferences +dotnet_style_object_initializer = true:suggestion +dotnet_style_collection_initializer = true:suggestion +dotnet_style_explicit_tuple_names = true:suggestion +dotnet_style_null_propagation = true:suggestion +dotnet_style_coalesce_expression = true:suggestion +dotnet_style_prefer_is_null_check_over_reference_equality_method = true:silent +dotnet_style_prefer_inferred_tuple_names = true:suggestion +dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion +dotnet_style_prefer_auto_properties = true:silent +dotnet_style_prefer_conditional_expression_over_assignment = true:silent +dotnet_style_prefer_conditional_expression_over_return = true:silent + +############################### +# Naming Conventions # +############################### + +# Style Definitions +dotnet_naming_style.pascal_case_style.capitalization = pascal_case +dotnet_naming_style.prefix_underscore.capitalization = camel_case +dotnet_naming_style.prefix_underscore.required_prefix = _ + +# Use PascalCase for constant fields +dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion +dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields +dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style +dotnet_naming_symbols.constant_fields.applicable_kinds = field +dotnet_naming_symbols.constant_fields.applicable_accessibilities = * +dotnet_naming_symbols.constant_fields.required_modifiers = const + +#dotnet_naming_symbols.properties.applicable_kinds = property +#dotnet_naming_symbols.properties.applicable_accessibilities = * +#dotnet_naming_rule.properties_should_be_pascal_case.severity = error +#dotnet_naming_rule.properties_should_be_pascal_case.symbols = properties +#dotnet_naming_rule.properties_should_be_pascal_case.style = pascal_case_style + +dotnet_naming_symbols.private_fields.applicable_kinds = field +dotnet_naming_symbols.private_fields.applicable_accessibilities = private +dotnet_naming_rule.private_fields_with_underscore.symbols = private_fields +dotnet_naming_rule.private_fields_with_underscore.style = prefix_underscore +dotnet_naming_rule.private_fields_with_underscore.severity = suggestion + + + +############################### +# C# Code Style Rules # +############################### + +[*.cs] +# var preferences +csharp_style_var_for_built_in_types = true:none +csharp_style_var_when_type_is_apparent = true:none +csharp_style_var_elsewhere = true:none + +# Expression-bodied members +csharp_style_expression_bodied_methods = false:none +csharp_style_expression_bodied_constructors = false:none +csharp_style_expression_bodied_operators = false:none +csharp_style_expression_bodied_properties = true:none +csharp_style_expression_bodied_indexers = true:none +csharp_style_expression_bodied_accessors = true:none + +# Pattern-matching preferences +csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion +csharp_style_pattern_matching_over_as_with_null_check = true:suggestion + +# Null-checking preferences +csharp_style_throw_expression = true:suggestion +csharp_style_conditional_delegate_call = true:suggestion + +# Modifier preferences +csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion + +# Expression-level preferences +csharp_prefer_braces = true:none +csharp_style_deconstructed_variable_declaration = true:suggestion +csharp_prefer_simple_default_expression = true:suggestion +csharp_style_pattern_local_over_anonymous_function = true:suggestion +csharp_style_inlined_variable_declaration = true:suggestion + +############################### +# C# Formatting Rules # +############################### + +# New line preferences +csharp_new_line_before_open_brace = none +csharp_new_line_before_else = true +csharp_new_line_before_catch = true +csharp_new_line_before_finally = true +csharp_new_line_before_members_in_object_initializers = true +csharp_new_line_before_members_in_anonymous_types = true +csharp_new_line_between_query_expression_clauses = true + +# Indentation preferences +csharp_indent_case_contents = true +csharp_indent_switch_labels = true +csharp_indent_labels = flush_left + +# Space preferences +csharp_space_after_cast = false +csharp_space_after_keywords_in_control_flow_statements = true +csharp_space_between_method_call_parameter_list_parentheses = false +csharp_space_between_method_declaration_parameter_list_parentheses = false +csharp_space_between_parentheses = false +csharp_space_before_colon_in_inheritance_clause = true +csharp_space_after_colon_in_inheritance_clause = true +csharp_space_around_binary_operators = before_and_after +csharp_space_between_method_declaration_empty_parameter_list_parentheses = false +csharp_space_between_method_call_name_and_opening_parenthesis = false +csharp_space_between_method_call_empty_parameter_list_parentheses = false + +# Wrapping preferences +csharp_preserve_single_line_statements = true +csharp_preserve_single_line_blocks = true + +################################## +# Visual Basic Code Style Rules # +################################## + +[*.vb] +# Modifier preferences +visual_basic_preferred_modifier_order = Partial,Default,Private,Protected,Public,Friend,NotOverridable,Overridable,MustOverride,Overloads,Overrides,MustInherit,NotInheritable,Static,Shared,Shadows,ReadOnly,WriteOnly,Dim,Const,WithEvents,Widening,Narrowing,Custom,Async:suggestion diff --git a/Directory.Build.props b/Directory.Build.props new file mode 100644 index 0000000..a95ac95 --- /dev/null +++ b/Directory.Build.props @@ -0,0 +1,5 @@ + + + latest + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..da2a6a6 --- /dev/null +++ b/README.md @@ -0,0 +1,104 @@ +# TestAA +[![Build status](https://ci.appveyor.com/api/projects/status/8a7wlfjt9oedlmy5/branch/master?svg=true)](https://ci.appveyor.com/project/inasync/testaa/branch/master) +[![NuGet](https://img.shields.io/nuget/v/Inasync.TestAA.svg)](https://www.nuget.org/packages/Inasync.TestAA/) + +***TestAA*** は Arrange-Act-Assert(AAA)パターンによるテストの記述をサポートする為のシンプルなライブラリです。 + + +## Target Frameworks +- .NET Standard 2.0+ +- .NET Standard 1.0+ +- .NET Framework 4.5+ + + +## Description +***TestAA*** は AAA パターンのうち Act-Assert の記述を直接的に補助します。 + +基本的な使い方は下記の通りです: +```cs +TestAA + .Act(テスト対象コード) + .Assert(テスト対象コードの戻り値の検証, 例外の検証, その他の検証); +``` + +Arrange に相当する処理は `TestAA.Act()` の呼び出しより前に記述します。 +```cs +// Arrange +// ... + +// Act +TestAA.Act(...) +``` + +`Act()` の引数には、テスト対象となるメソッドまたはコードのラムダ式やデリゲートを渡して下さい。テストの対象ではないメソッドまたはコードも含めますと、そこから発生した例外がテスト対象コードから生じたものとして扱われてしまい、正しい検証が行えなくなります。 +```cs +TestAA.Act(() => { /* ここでテスト対象のメソッドを呼ぶ */ }) +``` + +`Assert()` で `Act()` の結果を検証します。第1引数で `Act()` に渡されたテスト対象コードの戻り値を検証し、第2引数で `Act()` で生じた例外を検証(または例外が生じなかった事を検証)します。 +```cs + .Act(() => int.Parse("123")) + .Assert( + @return: ret => { /* ここで戻り値の検証 */ }, + exception: ex => { /* ここで例外の検証 */ }, + others: () => { /* ここで上記以外の検証。不要なら省略 */ } + ); +``` + + +## Usage +```cs +public void IntParseTest() { + // Success + TestAA.Act(() => int.Parse("123")).Assert( + ret => ret.Is(123), + ex => ex?.GetType().Is(null) + ); + + // FormatException + TestAA.Act(() => int.Parse("abc")).Assert( + ret => { }, + ex => ex?.GetType().Is(typeof(FormatException)) + ); +} +``` + +下記は *MSTest* を利用した、より実践的な例です: +```cs +[DataTestMethod] +[DataRow(0, null, null, typeof(ArgumentNullException))] +[DataRow(1, "123", 123, null)] +[DataRow(2, "abc", null, typeof(FormatException))] +public void IntParseTest(int testNumber, string input, int expected, Type expectedExceptionType) { + var msg = "No." + testNumber; + + TestAA.Act(() => int.Parse(input)).Assert( + ret => Assert.AreEqual(expected, ret, msg), + ex => Assert.AreEqual(expectedExceptionType, ex?.GetType(), msg) + ); +} +``` +または +```cs +[TestMethod] +public void IntParseTest() { + Action TestCase(int testNumber, string input, int expected, Type expectedExceptionType = null) => () => { + var msg = "No." + testNumber; + + TestAA.Act(() => int.Parse(input)).Assert( + ret => Assert.AreEqual(expected, ret, msg), + ex => Assert.AreEqual(expectedExceptionType, ex?.GetType(), msg) + ); + }; + + foreach (var action in new[] { + TestCase( 0, null , expected: 0 , expectedExceptionType: typeof(ArgumentNullException)), + TestCase( 1, "abc", expected: 0 , expectedExceptionType: typeof(FormatException)), + TestCase( 2, "123", expected: 123), + }) { action(); } +} +``` + + +## Licence +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details diff --git a/TestAA.sln b/TestAA.sln new file mode 100644 index 0000000..202cf26 --- /dev/null +++ b/TestAA.sln @@ -0,0 +1,41 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.852 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Inasync.TestAA", "src\Inasync.TestAA\Inasync.TestAA.csproj", "{1FA4FF51-C027-40EA-AAEA-199F2548A1A4}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Inasync.TestAA.Tests", "tests\Inasync.TestAA.Tests\Inasync.TestAA.Tests.csproj", "{25B86BC7-A35F-48E6-9255-DCE43263B194}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{7D0CB7FE-9D55-4FAF-9C3B-1688A3961B0C}" + ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig + .gitignore = .gitignore + appveyor.yml = appveyor.yml + Directory.Build.props = Directory.Build.props + LICENSE = LICENSE + README.md = README.md + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {1FA4FF51-C027-40EA-AAEA-199F2548A1A4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1FA4FF51-C027-40EA-AAEA-199F2548A1A4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1FA4FF51-C027-40EA-AAEA-199F2548A1A4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1FA4FF51-C027-40EA-AAEA-199F2548A1A4}.Release|Any CPU.Build.0 = Release|Any CPU + {25B86BC7-A35F-48E6-9255-DCE43263B194}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {25B86BC7-A35F-48E6-9255-DCE43263B194}.Debug|Any CPU.Build.0 = Debug|Any CPU + {25B86BC7-A35F-48E6-9255-DCE43263B194}.Release|Any CPU.ActiveCfg = Release|Any CPU + {25B86BC7-A35F-48E6-9255-DCE43263B194}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {93DA389C-05BA-4585-80A7-F081E7B73A80} + EndGlobalSection +EndGlobal diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000..489875e --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,21 @@ +version: ci-{build}-{branch} +image: Visual Studio 2017 +configuration: Release +nuget: + project_feed: true +before_build: +- cmd: nuget restore +build: + publish_nuget: true + verbosity: minimal +deploy: +- provider: GitHub + auth_token: + secure: eHI+nPFCmnPOdRRPRhGQBso/RlA5seuhkPBRgOMbGWDHzNWw+Us1FJrR7TTBLVR0 + on: + appveyor_repo_tag: true +- provider: NuGet + api_key: + secure: zUlOhbjj+3Jsywco3QlyLXz4zSXS9fqQdEWTOCpmwzEl5cBLHFSrYnOR8xnNaSaB + on: + appveyor_repo_tag: true diff --git a/src/Inasync.TestAA/Inasync.TestAA.csproj b/src/Inasync.TestAA/Inasync.TestAA.csproj new file mode 100644 index 0000000..ac6bfed --- /dev/null +++ b/src/Inasync.TestAA/Inasync.TestAA.csproj @@ -0,0 +1,15 @@ + + + + netstandard2.0;netstandard1.0;net45 + true + Inasync + inasync + TestAA is a simple library that supports writing tests using the Arrange-Act-Assert (AAA) pattern. + https://github.com/in-async/TestAA + https://github.com/in-async/TestAA/blob/master/LICENSE + library test unittest aaa + 0.1.0 + + + diff --git a/src/Inasync.TestAA/InternalsVisibleTo.cs b/src/Inasync.TestAA/InternalsVisibleTo.cs new file mode 100644 index 0000000..a469aa4 --- /dev/null +++ b/src/Inasync.TestAA/InternalsVisibleTo.cs @@ -0,0 +1,3 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("Inasync.TestAA.Tests")] diff --git a/src/Inasync.TestAA/TestAA.Act.cs b/src/Inasync.TestAA/TestAA.Act.cs new file mode 100644 index 0000000..a452659 --- /dev/null +++ b/src/Inasync.TestAA/TestAA.Act.cs @@ -0,0 +1,93 @@ +using System; +using System.Threading.Tasks; + +namespace Inasync { + + /// + /// AAA テストパターンのうち Act 及び Assert を表します。 + /// + public static partial class TestAA { + + /// + /// テスト対象のデリゲートを実行します。 + /// + /// テスト対象のデリゲート。 + /// デリゲートの実行結果。 + /// is null. + public static TestActual Act(Action act) { + if (act == null) { throw new ArgumentNullException(nameof(act)); } + + try { + act(); + return new TestActual(exception: null); + } + catch (Exception ex) { + return new TestActual(exception: ex); + } + } + + /// + /// テスト対象のデリゲートを実行します。 + /// + /// テスト対象のデリゲートの戻り値の型。 + /// テスト対象のデリゲート。 + /// デリゲートの実行結果。 + /// is null. + public static TestActual Act(Func act) { + if (act == null) { throw new ArgumentNullException(nameof(act)); } + + try { + var @return = act(); + return new TestActual(@return, exception: null); + } + catch (Exception ex) { + return new TestActual(default, exception: ex); + } + } + + /// + /// テスト対象の非同期デリゲートを実行します。 + /// + /// テスト対象の非同期デリゲート。 + /// 非同期デリゲートの実行結果。 + /// is null. + public static Task ActAsync(Func act) { + if (act == null) { throw new ArgumentNullException(nameof(act)); } + + return Internal(); + + async Task Internal() { + try { + await act().ConfigureAwait(false); + return new TestActual(exception: null); + } + catch (Exception ex) { + return new TestActual(exception: ex); + } + } + } + + /// + /// テスト対象の非同期デリゲートを実行します。 + /// + /// テスト対象の非同期デリゲートの戻り値の型。 + /// テスト対象の非同期デリゲート。 + /// 非同期デリゲートの実行結果。 + /// is null. + public static Task> ActAsync(Func> act) { + if (act == null) { throw new ArgumentNullException(nameof(act)); } + + return Internal(); + + async Task> Internal() { + try { + var @return = await act().ConfigureAwait(false); + return new TestActual(@return, exception: null); + } + catch (Exception ex) { + return new TestActual(default, exception: ex); + } + } + } + } +} diff --git a/src/Inasync.TestAA/TestAA.Assert.cs b/src/Inasync.TestAA/TestAA.Assert.cs new file mode 100644 index 0000000..ecfa775 --- /dev/null +++ b/src/Inasync.TestAA/TestAA.Assert.cs @@ -0,0 +1,43 @@ +using System; + +namespace Inasync { + + public static partial class TestAA { + + /// + /// Act の実行結果を検証します。 + /// + /// Act の実行結果。 + /// Act で生じた例外を検証するデリゲート。 + /// その他の Act の実行結果を検証するデリゲート。 + /// is null. + public static void Assert(this TestActual actual, Action exception, Action others = null) { + if (exception == null) { throw new ArgumentNullException(nameof(exception)); } + + exception(actual.Exception); + others?.Invoke(); + } + + /// + /// Act の実行結果を検証します。 + /// + /// Act の戻り値の型。 + /// Act の実行結果。 + /// Act の戻り値を検証するデリゲート。 + /// Act で生じた例外を検証するデリゲート。 + /// その他の Act の実行結果を検証するデリゲート。 + /// or is null. + public static void Assert(this TestActual actual, Action @return, Action exception, Action others = null) { + if (@return == null) { throw new ArgumentNullException(nameof(@return)); } + if (exception == null) { throw new ArgumentNullException(nameof(exception)); } + + // 戻り値の検証は非例外時のみ (例外時には actual.Return は必ず default なので、改めて検証する必要が無い)。 + if (actual.Exception == null) { + @return(actual.Return); + } + + exception(actual.Exception); + others?.Invoke(); + } + } +} diff --git a/src/Inasync.TestAA/TestActual.cs b/src/Inasync.TestAA/TestActual.cs new file mode 100644 index 0000000..63bf0ae --- /dev/null +++ b/src/Inasync.TestAA/TestActual.cs @@ -0,0 +1,97 @@ +using System; +using System.Collections.Generic; + +namespace Inasync { + + /// + /// Act の実行結果を表します。 + /// + public readonly struct TestActual : IEquatable { + + internal TestActual(Exception exception) { + Exception = exception; + } + + /// + /// Act 実行中に生じた例外。 + /// + public Exception Exception { get; } + + /// Auto Generated. + public override bool Equals(object obj) { + return obj is TestActual && Equals((TestActual)obj); + } + + /// Auto Generated. + public bool Equals(TestActual other) { + return EqualityComparer.Default.Equals(Exception, other.Exception); + } + + /// Auto Generated. + public override int GetHashCode() { + return -311220794 + EqualityComparer.Default.GetHashCode(Exception); + } + + /// Auto Generated. + public static bool operator ==(TestActual actual1, TestActual actual2) { + return actual1.Equals(actual2); + } + + /// Auto Generated. + public static bool operator !=(TestActual actual1, TestActual actual2) { + return !(actual1 == actual2); + } + } + + /// + /// Act の実行結果を表します。 + /// + /// Act の戻り値の型。 + public readonly struct TestActual : IEquatable> { + + internal TestActual(TReturn @return, Exception exception) { + Return = exception == null ? @return : default; + Exception = exception; + } + + /// + /// Act の戻り値。 + /// Act 実行中に例外が生じた場合は default。 + /// + public TReturn Return { get; } + + /// + /// Act 実行中に生じた例外。 + /// + public Exception Exception { get; } + + /// Auto Generated. + public override bool Equals(object obj) { + return obj is TestActual && Equals((TestActual)obj); + } + + /// Auto Generated. + public bool Equals(TestActual other) { + return EqualityComparer.Default.Equals(Return, other.Return) && + EqualityComparer.Default.Equals(Exception, other.Exception); + } + + /// Auto Generated. + public override int GetHashCode() { + var hashCode = -1738866163; + hashCode = hashCode * -1521134295 + EqualityComparer.Default.GetHashCode(Return); + hashCode = hashCode * -1521134295 + EqualityComparer.Default.GetHashCode(Exception); + return hashCode; + } + + /// Auto Generated. + public static bool operator ==(TestActual actual1, TestActual actual2) { + return actual1.Equals(actual2); + } + + /// Auto Generated. + public static bool operator !=(TestActual actual1, TestActual actual2) { + return !(actual1 == actual2); + } + } +} diff --git a/tests/Inasync.TestAA.Tests/Inasync.TestAA.Tests.csproj b/tests/Inasync.TestAA.Tests/Inasync.TestAA.Tests.csproj new file mode 100644 index 0000000..a7f0e55 --- /dev/null +++ b/tests/Inasync.TestAA.Tests/Inasync.TestAA.Tests.csproj @@ -0,0 +1,19 @@ + + + + netcoreapp2.2 + false + Inasync.Tests + + + + + + + + + + + + + diff --git a/tests/Inasync.TestAA.Tests/TestActTests.cs b/tests/Inasync.TestAA.Tests/TestActTests.cs new file mode 100644 index 0000000..2effe88 --- /dev/null +++ b/tests/Inasync.TestAA.Tests/TestActTests.cs @@ -0,0 +1,111 @@ +using System; +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Inasync.Tests { + + [TestClass] + public class TestActTests { + + [TestMethod] + public void Run() { + Action TestCase(int testNumber, Action act, Type exceptionType, Type expectedExceptionType = null) => () => { + var msg = "No." + testNumber; + + try { + var actual = TestAA.Act(act); + + Assert.AreEqual(exceptionType, actual.Exception?.GetType(), msg); + } + catch (Exception ex) { + Assert.AreEqual(expectedExceptionType, ex.GetType(), msg); + } + }; + + foreach (var action in new[]{ + TestCase( 0, act: null , exceptionType: null , expectedExceptionType: typeof(ArgumentNullException)), + TestCase( 1, act: () => { } , exceptionType: null ), + TestCase( 2, act: () => throw new DummyException(), exceptionType: typeof(DummyException)), + }) { action(); } + } + + [TestMethod] + public void Act_TReturn() { + Action TestCase(int testNumber, Func act, (DummyObject @return, Type exceptionType) expected, Type expectedExceptionType = null) => () => { + var msg = "No." + testNumber; + + try { + var actual = TestAA.Act(act); + + Assert.AreEqual(expected.exceptionType, actual.Exception?.GetType(), msg); + Assert.AreEqual(expected.@return, actual.Return, msg); + } + catch (Exception ex) { + Assert.AreEqual(expectedExceptionType, ex.GetType(), msg); + } + }; + + var obj = new DummyObject(); + foreach (var action in new[]{ + TestCase( 0, act: null , expected: default , expectedExceptionType: typeof(ArgumentNullException)), + TestCase( 1, act: () => obj , expected: (obj , null) ), + TestCase( 2, act: () => throw new DummyException(), expected: (null, typeof(DummyException))), + }) { action(); } + } + + [TestMethod] + public void ActAsync() { + Action TestCase(int testNumber, Func act, Type expected, Type expectedExceptionType = null) => () => { + var msg = "No." + testNumber; + + try { + var actual = TestAA.ActAsync(act).Result; + + Assert.AreEqual(expected, actual.Exception?.GetType(), msg); + } + catch (Exception ex) { + Console.WriteLine(ex); + Assert.AreEqual(expectedExceptionType, ex.GetType(), msg); + } + }; + + foreach (var action in new[]{ + TestCase( 0, act: null , expected: null , expectedExceptionType: typeof(ArgumentNullException)), + TestCase( 1, act: () => Task.CompletedTask , expected: null ), + TestCase( 2, act: () => throw new DummyException(), expected: typeof(DummyException)), + }) { action(); } + } + + [TestMethod] + public void ActAsync_TReturn() { + Action TestCase(int testNumber, Func> act, (DummyObject @return, Type exceptionType) expected, Type expectedExceptionType = null) => () => { + var msg = "No." + testNumber; + + try { + var actual = TestAA.ActAsync(act).Result; + + Assert.AreEqual(expected.exceptionType, actual.Exception?.GetType(), msg); + Assert.AreEqual(expected.@return, actual.Return, msg); + } + catch (Exception ex) { + Assert.AreEqual(expectedExceptionType, ex.GetType(), msg); + } + }; + + var obj = new DummyObject(); + foreach (var action in new[]{ + TestCase( 0, act: null , expected: default , expectedExceptionType: typeof(ArgumentNullException)), + TestCase( 1, act: () => Task.FromResult(obj) , expected: (obj , null) ), + TestCase( 2, act: () => throw new DummyException(), expected: (null, typeof(DummyException))), + }) { action(); } + } + + #region Helpers + + private sealed class DummyException : Exception { } + + private sealed class DummyObject { } + + #endregion Helpers + } +} diff --git a/tests/Inasync.TestAA.Tests/TestActualTests.cs b/tests/Inasync.TestAA.Tests/TestActualTests.cs new file mode 100644 index 0000000..3a2ddeb --- /dev/null +++ b/tests/Inasync.TestAA.Tests/TestActualTests.cs @@ -0,0 +1,195 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Inasync.Tests { + + [TestClass] + public class TestActualTests { + + [TestMethod] + public void Ctor() { + var ret = new TestActual(); + + Assert.IsNull(ret.Exception); + } + + [TestMethod] + public void Ctor_Args() { + Action TestCase(int testNumber, Exception exception) => () => { + var msg = "No." + testNumber; + + var ret = new TestActual(exception); + + Assert.AreEqual(exception, ret.Exception, msg); + }; + + foreach (var action in new[] { + TestCase( 0, null), + TestCase( 1, new DummyException()), + }) { action(); } + } + + [TestMethod] + public void Equals() { + Action TestCase(int testNumber, TestActual testActual, TestActual other, bool expected) => () => { + Assert.AreEqual(expected, testActual.Equals(other) + , message: $"No.{testNumber}: Equals" + ); + + Assert.AreEqual(expected, testActual.Equals((object)other) + , message: $"No.{testNumber}: Equals(object)" + ); + + // 全く関係のない object との比較なので、常に false。 + Assert.AreEqual(false, testActual.Equals(new object()) + , message: $"No.{testNumber}: Equals(new object)" + ); + + Assert.AreEqual(expected, testActual == other + , message: $"No.{testNumber}: ==" + ); + + Assert.AreEqual(!expected, testActual != other + , message: $"No.{testNumber}: !=" + ); + + Assert.AreEqual(expected, ((object)testActual).Equals((object)other) + , message: $"No.{testNumber}: object.Equals(object)" + ); + + // AbmPlacementId の == 演算子オーバーロードが機能しないので、常に false。 + Assert.AreEqual(false, (object)testActual == (object)other + , message: $"No.{testNumber}: object == object" + ); + }; + + var ex = new DummyException(); + foreach (var action in new[] { + TestCase( 0, new TestActual() , new TestActual() , true ), + TestCase( 1, new TestActual(ex), new TestActual(ex) , true ), + TestCase( 2, new TestActual(ex), new TestActual(new DummyException()), false), + }) { action(); } + } + + [TestMethod] + public new void GetHashCode() { + Action TestCase(int testNumber, DummyException exception) => () => { + var testActual = new TestActual(exception); + + // GetHashCode() は環境によって戻り値が変わるので、例外が起こらない事だけ確認。 + testActual.GetHashCode(); + }; + + foreach (var action in new[] { + TestCase( 0, null), + TestCase( 1, new DummyException()), + }) { action(); } + } + + #region Helpers + + private sealed class DummyException : Exception { } + + #endregion Helpers + } + + [TestClass] + public class TestActual_TReturnTests { + + [TestMethod] + public void Ctor() { + var ret = new TestActual(); + + Assert.IsNull(ret.Return); + Assert.IsNull(ret.Exception); + } + + [TestMethod] + public void Ctor_Args() { + Action TestCase(int testNumber, DummyObject @return, Exception exception, (DummyObject @return, Exception exception) expected) => () => { + var msg = "No." + testNumber; + + var ret = new TestActual(@return, exception); + + Assert.AreEqual(expected.@return, ret.Return, msg); + Assert.AreEqual(expected.exception, ret.Exception, msg); + }; + + var obj = new DummyObject(); + var ex = new DummyException(); + foreach (var action in new[] { + TestCase( 0, obj , ex , (@return: null, exception: ex)), + TestCase( 1, null, ex , (@return: null, exception: ex)), + TestCase( 2, obj , null, (@return: obj , exception: null)), + }) { action(); } + } + + [TestMethod] + public void Equals() { + Action TestCase(int testNumber, TestActual testActual, TestActual other, bool expected) => () => { + Assert.AreEqual(expected, testActual.Equals(other) + , message: $"No.{testNumber}: Equals" + ); + + Assert.AreEqual(expected, testActual.Equals((object)other) + , message: $"No.{testNumber}: Equals(object)" + ); + + // 全く関係のない object との比較なので、常に false。 + Assert.AreEqual(false, testActual.Equals(new object()) + , message: $"No.{testNumber}: Equals(new object)" + ); + + Assert.AreEqual(expected, testActual == other + , message: $"No.{testNumber}: ==" + ); + + Assert.AreEqual(!expected, testActual != other + , message: $"No.{testNumber}: !=" + ); + + Assert.AreEqual(expected, ((object)testActual).Equals((object)other) + , message: $"No.{testNumber}: object.Equals(object)" + ); + + // AbmPlacementId の == 演算子オーバーロードが機能しないので、常に false。 + Assert.AreEqual(false, (object)testActual == (object)other + , message: $"No.{testNumber}: object == object" + ); + }; + + var obj = new DummyObject(); + var ex = new DummyException(); + foreach (var action in new[] { + TestCase( 0, new TestActual() , new TestActual() , true ), + TestCase( 1, new TestActual(obj, ex), new TestActual(obj , ex ), true ), + TestCase( 2, new TestActual(obj, ex), new TestActual(new DummyObject(), ex ), true ), + TestCase( 3, new TestActual(obj, ex), new TestActual(obj , new DummyException()), false), + }) { action(); } + } + + [TestMethod] + public new void GetHashCode() { + Action TestCase(int testNumber, DummyObject @return, DummyException exception) => () => { + var testActual = new TestActual(@return, exception); + + // GetHashCode() は環境によって戻り値が変わるので、例外が起こらない事だけ確認。 + testActual.GetHashCode(); + }; + + foreach (var action in new[] { + TestCase( 0, new DummyObject(), null), + TestCase( 1, null , new DummyException()), + TestCase( 2, new DummyObject(), new DummyException()), + }) { action(); } + } + + #region Helpers + + private sealed class DummyException : Exception { } + + private sealed class DummyObject { } + + #endregion Helpers + } +} diff --git a/tests/Inasync.TestAA.Tests/TestAssertTests.cs b/tests/Inasync.TestAA.Tests/TestAssertTests.cs new file mode 100644 index 0000000..ee940c1 --- /dev/null +++ b/tests/Inasync.TestAA.Tests/TestAssertTests.cs @@ -0,0 +1,88 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Inasync.Tests { + + [TestClass] + public class TestAssertTests { + + [TestMethod] + public void Assert_() { + Action TestCase(int testNumber, TestActual testActual, AssertException exception, AssertOthers others, (Exception exception, bool others) expected, Type expectedExceptionType = null) => () => { + var msg = "No." + testNumber; + + try { + TestAA.Assert(testActual, exception?.Invoke, others?.Invoke); + } + catch (Exception ex_) { + Assert.AreEqual(expectedExceptionType, ex_.GetType(), msg); + } + + Assert.AreEqual(expected.exception, exception?.InvokedParams, msg); + Assert.AreEqual(expected.others, others?.Invoked ?? default, msg); + }; + + var ex = new DummyException(); + foreach (var action in new[]{ + TestCase( 1, new TestActual(null), new AssertException(), new AssertOthers(), expected: (exception: null, others: true )), + TestCase( 2, new TestActual(null), null , new AssertOthers(), expected: (exception: null, others: false), expectedExceptionType: typeof(ArgumentNullException)), + TestCase( 3, new TestActual(null), new AssertException(), null , expected: (exception: null, others: false)), + TestCase( 4, new TestActual(ex) , new AssertException(), new AssertOthers(), expected: (exception: ex , others: true )), + }) { action(); } + } + + [TestMethod] + public void Assert_TReturn() { + Action TestCase(int testNumber, TestActual testActual, AssertReturn @return, AssertException exception, AssertOthers others, (DummyObject @return, Exception exception, bool others) expected, Type expectedExceptionType = null) => () => { + var msg = "No." + testNumber; + + try { + TestAA.Assert(testActual, @return?.Invoke, exception?.Invoke, others?.Invoke); + } + catch (Exception ex_) { + Assert.AreEqual(expectedExceptionType, ex_.GetType(), msg); + } + + Assert.AreEqual(expected.@return, @return?.InvokedParams, msg); + Assert.AreEqual(expected.exception, exception?.InvokedParams, msg); + Assert.AreEqual(expected.others, others?.Invoked ?? default, msg); + }; + + var obj = new DummyObject(); + var ex = new DummyException(); + foreach (var action in new[]{ + TestCase( 1, new TestActual(obj , null), null , new AssertException(), new AssertOthers(), expected: (@return: null, exception: null, others: false), expectedExceptionType: typeof(ArgumentNullException)), + TestCase( 2, new TestActual(obj , null), new AssertReturn(), null , new AssertOthers(), expected: (@return: null, exception: null, others: false), expectedExceptionType: typeof(ArgumentNullException)), + TestCase( 2, new TestActual(obj , null), new AssertReturn(), new AssertException(), null , expected: (@return: obj , exception: null, others: false)), + TestCase(11, new TestActual(obj , null), new AssertReturn(), new AssertException(), new AssertOthers(), expected: (@return: obj , exception: null, others: true )), + TestCase(13, new TestActual(null, ex ), new AssertReturn(), new AssertException(), new AssertOthers(), expected: (@return: null, exception: ex , others: true )), + }) { action(); } + } + + #region Helpers + + private sealed class DummyException : Exception { } + + private sealed class DummyObject { } + + private sealed class AssertReturn { + public DummyObject InvokedParams { get; private set; } + + public Action Invoke => @return => InvokedParams = @return; + } + + private sealed class AssertException { + public Exception InvokedParams { get; private set; } + + public Action Invoke => ex => InvokedParams = ex; + } + + private sealed class AssertOthers { + public bool Invoked { get; private set; } + + public Action Invoke => () => Invoked = true; + } + + #endregion Helpers + } +} diff --git a/tests/Inasync.TestAA.Tests/Usage.cs b/tests/Inasync.TestAA.Tests/Usage.cs new file mode 100644 index 0000000..b273aa1 --- /dev/null +++ b/tests/Inasync.TestAA.Tests/Usage.cs @@ -0,0 +1,27 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Inasync.Tests { + + [TestClass] + public class Usage { + + [TestMethod] + public void Basic() { + Action TestCase(int testNumber, string input, int expected, Type expectedExceptionType = null) => () => { + var msg = "No." + testNumber; + + TestAA.Act(() => int.Parse(input)).Assert( + ret => Assert.AreEqual(expected, ret, msg), + ex => Assert.AreEqual(expectedExceptionType, ex?.GetType(), msg) + ); + }; + + foreach (var action in new[] { + TestCase( 0, null , expected: 0 , expectedExceptionType: typeof(ArgumentNullException)), + TestCase( 1, "abc", expected: 0 , expectedExceptionType: typeof(FormatException)), + TestCase( 2, "123", expected: 123), + }) { action(); } + } + } +}