From 93ec15d24709a67fcbb43e46ffc03b4eea35e83f Mon Sep 17 00:00:00 2001 From: domysee Date: Fri, 23 Feb 2018 14:25:00 +0100 Subject: [PATCH 1/4] Update Packages of ResolverTests --- .../Pather.CSharp.UnitTests.csproj | 8 ++++---- test/Pather.CSharp.UnitTests/ResolverTests.cs | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/test/Pather.CSharp.UnitTests/Pather.CSharp.UnitTests.csproj b/test/Pather.CSharp.UnitTests/Pather.CSharp.UnitTests.csproj index c47fb31..fa644f3 100644 --- a/test/Pather.CSharp.UnitTests/Pather.CSharp.UnitTests.csproj +++ b/test/Pather.CSharp.UnitTests/Pather.CSharp.UnitTests.csproj @@ -17,10 +17,10 @@ - - - - + + + + diff --git a/test/Pather.CSharp.UnitTests/ResolverTests.cs b/test/Pather.CSharp.UnitTests/ResolverTests.cs index f8f7e01..e34f889 100644 --- a/test/Pather.CSharp.UnitTests/ResolverTests.cs +++ b/test/Pather.CSharp.UnitTests/ResolverTests.cs @@ -126,7 +126,7 @@ public void SelectionResolution_CorrectSetup_Success() var result = r.Resolve(array, path); result.Should().BeOfType(typeof(Selection)); - result.ShouldBeEquivalentTo(new[] { 1, 2 }); + result.Should().BeEquivalentTo(new[] { 1, 2 }); } [Fact] @@ -200,7 +200,7 @@ public void SinglePropertyResolution_NoPathElementTypeForPath_FailWithNoApplicab var o = new { Property = "1" }; var path = "Property^%#"; - r.Invoking(re => re.Resolve(o, path)).ShouldThrow(); + r.Invoking(re => re.Resolve(o, path)).Should().Throw(); } [Fact] @@ -210,7 +210,7 @@ public void SinglePropertyResolution_NonExistingProperty_FailWithPropertyCouldNo var o = new { Property = "1" }; var path = "NonExistingProperty"; - r.Invoking(re => re.Resolve(o, path)).ShouldThrow(); + r.Invoking(re => re.Resolve(o, path)).Should().Throw(); } [Fact] @@ -220,7 +220,7 @@ public void ArrayIndexResolution_IndexHigher_FailWithIndexTooHigh() var array = new[] { "1", "2" }; var path = "[3]"; - r.Invoking(re => re.Resolve(array, path)).ShouldThrow(); + r.Invoking(re => re.Resolve(array, path)).Should().Throw(); } [Fact] @@ -230,7 +230,7 @@ public void ArrayIndexResolution_IndexLower_FailWithNoApplicablePathElementType( var array = new[] { "1", "2" }; var path = "[-2]"; - r.Invoking(re => re.Resolve(array, path)).ShouldThrow(); + r.Invoking(re => re.Resolve(array, path)).Should().Throw(); } [Fact] @@ -240,7 +240,7 @@ public void DictionaryKeyResolution_KeyNotExisting_FailWithKeyNotExisting() var dictionary = new Dictionary { { "Key", "Value" } }; var path = "[NonExistingKey]"; - r.Invoking(re => re.Resolve(dictionary, path)).ShouldThrow(); + r.Invoking(re => re.Resolve(dictionary, path)).Should().Throw(); } [Fact] From eccc7b41da29aad59d526da10f614ba4dde79d22 Mon Sep 17 00:00:00 2001 From: domysee Date: Fri, 23 Feb 2018 14:38:58 +0100 Subject: [PATCH 2/4] Change Unit Testing to MTest --- Pather.CSharp.sln | 17 ++++--- .../Pather.CSharp.UnitTests.csproj | 26 ++++------- .../Properties/AssemblyInfo.cs | 19 -------- test/Pather.CSharp.UnitTests/ResolverTests.cs | 45 ++++++++++--------- 4 files changed, 41 insertions(+), 66 deletions(-) delete mode 100644 test/Pather.CSharp.UnitTests/Properties/AssemblyInfo.cs diff --git a/Pather.CSharp.sln b/Pather.CSharp.sln index f7a51bf..08272f8 100644 --- a/Pather.CSharp.sln +++ b/Pather.CSharp.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26228.9 +VisualStudioVersion = 15.0.27130.2036 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{16603959-8D03-4A8D-A93B-A5B8A7D257CE}" EndProject @@ -11,7 +11,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{071C74B6-0 EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pather.CSharp", "src\Pather.CSharp\Pather.CSharp.csproj", "{0A0CEA4B-96AE-4913-9FF9-F6DFD2553FE0}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pather.CSharp.UnitTests", "test\Pather.CSharp.UnitTests\Pather.CSharp.UnitTests.csproj", "{746C2447-C281-45A6-A8F4-8CBD71167D69}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pather.CSharp.UnitTests", "test\Pather.CSharp.UnitTests\Pather.CSharp.UnitTests.csproj", "{6D5AA87A-2B5D-4192-9DAB-F24A57C5F994}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -23,16 +23,19 @@ Global {0A0CEA4B-96AE-4913-9FF9-F6DFD2553FE0}.Debug|Any CPU.Build.0 = Debug|Any CPU {0A0CEA4B-96AE-4913-9FF9-F6DFD2553FE0}.Release|Any CPU.ActiveCfg = Release|Any CPU {0A0CEA4B-96AE-4913-9FF9-F6DFD2553FE0}.Release|Any CPU.Build.0 = Release|Any CPU - {746C2447-C281-45A6-A8F4-8CBD71167D69}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {746C2447-C281-45A6-A8F4-8CBD71167D69}.Debug|Any CPU.Build.0 = Debug|Any CPU - {746C2447-C281-45A6-A8F4-8CBD71167D69}.Release|Any CPU.ActiveCfg = Release|Any CPU - {746C2447-C281-45A6-A8F4-8CBD71167D69}.Release|Any CPU.Build.0 = Release|Any CPU + {6D5AA87A-2B5D-4192-9DAB-F24A57C5F994}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6D5AA87A-2B5D-4192-9DAB-F24A57C5F994}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6D5AA87A-2B5D-4192-9DAB-F24A57C5F994}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6D5AA87A-2B5D-4192-9DAB-F24A57C5F994}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution {0A0CEA4B-96AE-4913-9FF9-F6DFD2553FE0} = {16603959-8D03-4A8D-A93B-A5B8A7D257CE} - {746C2447-C281-45A6-A8F4-8CBD71167D69} = {071C74B6-0271-4D58-B49B-33AE63D02F1C} + {6D5AA87A-2B5D-4192-9DAB-F24A57C5F994} = {071C74B6-0271-4D58-B49B-33AE63D02F1C} + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {CD9289F3-8FBA-43CE-BC32-E8EB852AC24B} EndGlobalSection EndGlobal diff --git a/test/Pather.CSharp.UnitTests/Pather.CSharp.UnitTests.csproj b/test/Pather.CSharp.UnitTests/Pather.CSharp.UnitTests.csproj index fa644f3..2cf17d4 100644 --- a/test/Pather.CSharp.UnitTests/Pather.CSharp.UnitTests.csproj +++ b/test/Pather.CSharp.UnitTests/Pather.CSharp.UnitTests.csproj @@ -1,30 +1,20 @@ - + - netcoreapp1.0 - Pather.CSharp.UnitTests - Pather.CSharp.UnitTests - true - 1.6.0 - 1.0.4 - false - false - false - + netcoreapp2.0 - - - + false + - - - + + + - + diff --git a/test/Pather.CSharp.UnitTests/Properties/AssemblyInfo.cs b/test/Pather.CSharp.UnitTests/Properties/AssemblyInfo.cs deleted file mode 100644 index 133b08f..0000000 --- a/test/Pather.CSharp.UnitTests/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("Pather.CSharp.UnitTests")] -[assembly: AssemblyTrademark("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("746c2447-c281-45a6-a8f4-8cbd71167d69")] diff --git a/test/Pather.CSharp.UnitTests/ResolverTests.cs b/test/Pather.CSharp.UnitTests/ResolverTests.cs index e34f889..d642206 100644 --- a/test/Pather.CSharp.UnitTests/ResolverTests.cs +++ b/test/Pather.CSharp.UnitTests/ResolverTests.cs @@ -1,17 +1,18 @@ using FluentAssertions; +using Microsoft.VisualStudio.TestTools.UnitTesting; using Pather.CSharp.UnitTests.TestHelper; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using Xunit; namespace Pather.CSharp.UnitTests { + [TestClass] public class ResolverTests { - [Fact] + [TestMethod] public void SinglePropertyResolution_CorrectSetup_Success() { var value = "1"; @@ -23,7 +24,7 @@ public void SinglePropertyResolution_CorrectSetup_Success() result.Should().Be(value); } - [Fact] + [TestMethod] public void SinglePropertyResolution_BaseClass_Success() { var value = "1"; @@ -35,7 +36,7 @@ public void SinglePropertyResolution_BaseClass_Success() result.Should().Be(value); } - [Fact] + [TestMethod] public void MultiplePropertyResolution_CorrectSetup_Success() { var value = "1"; @@ -47,7 +48,7 @@ public void MultiplePropertyResolution_CorrectSetup_Success() result.Should().Be(value); } - [Fact] + [TestMethod] public void DictionaryKeyResolutionWithProperty_CorrectSetup_Success() { var r = new Resolver(); @@ -59,7 +60,7 @@ public void DictionaryKeyResolutionWithProperty_CorrectSetup_Success() result.Should().Be("Value"); } - [Fact] + [TestMethod] public void DictionaryKeyResolution_CorrectSetup_Success() { var r = new Resolver(); @@ -70,7 +71,7 @@ public void DictionaryKeyResolution_CorrectSetup_Success() result.Should().Be("Value"); } - [Fact] + [TestMethod] public void MultipleDictionaryKeyResolution_CorrectSetup_Success() { var r = new Resolver(); @@ -83,7 +84,7 @@ public void MultipleDictionaryKeyResolution_CorrectSetup_Success() result.Should().Be("Value"); } - [Fact] + [TestMethod] public void ArrayIndexResolutionWithProperty_CorrectSetup_Success() { var r = new Resolver(); @@ -95,7 +96,7 @@ public void ArrayIndexResolutionWithProperty_CorrectSetup_Success() result.Should().Be("1"); } - [Fact] + [TestMethod] public void ArrayIndexResolution_CorrectSetup_Success() { var r = new Resolver(); @@ -106,7 +107,7 @@ public void ArrayIndexResolution_CorrectSetup_Success() result.Should().Be("1"); } - [Fact] + [TestMethod] public void MultipleArrayIndexResolution_CorrectSetup_Success() { var r = new Resolver(); @@ -117,7 +118,7 @@ public void MultipleArrayIndexResolution_CorrectSetup_Success() result.Should().Be("1"); } - [Fact] + [TestMethod] public void SelectionResolution_CorrectSetup_Success() { var r = new Resolver(); @@ -129,7 +130,7 @@ public void SelectionResolution_CorrectSetup_Success() result.Should().BeEquivalentTo(new[] { 1, 2 }); } - [Fact] + [TestMethod] public void SelectionPropertyResolution_CorrectSetup_Success() { var r = new Resolver(); @@ -145,7 +146,7 @@ public void SelectionPropertyResolution_CorrectSetup_Success() result.Should().BeEquivalentTo(new[] { "1", "2" }); } - [Fact] + [TestMethod] public void SelectionDictionaryKeyResolution_CorrectSetup_Success() { var r = new Resolver(); @@ -161,7 +162,7 @@ public void SelectionDictionaryKeyResolution_CorrectSetup_Success() result.Should().BeEquivalentTo(new[] { "1", "2" }); } - [Fact] + [TestMethod] public void SelectionArrayIndexResolution_CorrectSetup_Success() { var r = new Resolver(); @@ -177,7 +178,7 @@ public void SelectionArrayIndexResolution_CorrectSetup_Success() result.Should().BeEquivalentTo(new[] { "2", "4" }); } - [Fact] + [TestMethod] public void SelectionFlattening_CorrectSetup_Success() { var r = new Resolver(); @@ -193,7 +194,7 @@ public void SelectionFlattening_CorrectSetup_Success() result.Should().BeEquivalentTo(new[] { 1, 2, 3 }); } - [Fact] + [TestMethod] public void SinglePropertyResolution_NoPathElementTypeForPath_FailWithNoApplicablePathElementType() { var r = new Resolver(); @@ -203,7 +204,7 @@ public void SinglePropertyResolution_NoPathElementTypeForPath_FailWithNoApplicab r.Invoking(re => re.Resolve(o, path)).Should().Throw(); } - [Fact] + [TestMethod] public void SinglePropertyResolution_NonExistingProperty_FailWithPropertyCouldNotBeFound() { var r = new Resolver(); @@ -213,7 +214,7 @@ public void SinglePropertyResolution_NonExistingProperty_FailWithPropertyCouldNo r.Invoking(re => re.Resolve(o, path)).Should().Throw(); } - [Fact] + [TestMethod] public void ArrayIndexResolution_IndexHigher_FailWithIndexTooHigh() { var r = new Resolver(); @@ -223,7 +224,7 @@ public void ArrayIndexResolution_IndexHigher_FailWithIndexTooHigh() r.Invoking(re => re.Resolve(array, path)).Should().Throw(); } - [Fact] + [TestMethod] public void ArrayIndexResolution_IndexLower_FailWithNoApplicablePathElementType() { var r = new Resolver(); @@ -233,7 +234,7 @@ public void ArrayIndexResolution_IndexLower_FailWithNoApplicablePathElementType( r.Invoking(re => re.Resolve(array, path)).Should().Throw(); } - [Fact] + [TestMethod] public void DictionaryKeyResolution_KeyNotExisting_FailWithKeyNotExisting() { var r = new Resolver(); @@ -243,7 +244,7 @@ public void DictionaryKeyResolution_KeyNotExisting_FailWithKeyNotExisting() r.Invoking(re => re.Resolve(dictionary, path)).Should().Throw(); } - [Fact] + [TestMethod] public void IndexerResolution_Int_Success() { var target = new IntIndexerClassNoIEnumerable("Test"); @@ -254,7 +255,7 @@ public void IndexerResolution_Int_Success() res.Should().Be("Test123456"); } - [Fact] + [TestMethod] public void IndexerResolution_String_Success() { var target = new StringIndexerClassNoIEnumerable("Test"); From 62a9e9d1c097cea0b13402a85a3f0649e41a1ae1 Mon Sep 17 00:00:00 2001 From: domysee Date: Fri, 23 Feb 2018 14:48:35 +0100 Subject: [PATCH 3/4] Add ResolveSafe Method and Tests for it --- src/Pather.CSharp/IResolver.cs | 31 ++ src/Pather.CSharp/Resolver.cs | 24 ++ .../ResolveSafeTests.cs | 280 ++++++++++++++++++ .../{ResolverTests.cs => ResolveTests.cs} | 12 +- 4 files changed, 346 insertions(+), 1 deletion(-) create mode 100644 test/Pather.CSharp.UnitTests/ResolveSafeTests.cs rename test/Pather.CSharp.UnitTests/{ResolverTests.cs => ResolveTests.cs} (95%) diff --git a/src/Pather.CSharp/IResolver.cs b/src/Pather.CSharp/IResolver.cs index 8dd0ac4..c3b2a4d 100644 --- a/src/Pather.CSharp/IResolver.cs +++ b/src/Pather.CSharp/IResolver.cs @@ -8,7 +8,38 @@ public interface IResolver IList PathElementFactories { get; set; } IList CreatePath(string path); + /// + /// Returns the object defined by the path elements. + /// Any access exception (e.g. NullReference) is propagated. + /// + /// + /// + /// object Resolve(object target, IList pathElements); + + /// + /// Returns the object defined by the path. + /// Any access exception (e.g. NullReference) is propagated. + /// + /// + /// + /// object Resolve(object target, string path); + + /// + /// Returns null if any object in the path is null + /// + /// + /// + /// + object ResolveSafe(object target, IList pathElements); + + /// + /// Returns null if any object in the path is null + /// + /// + /// + /// + object ResolveSafe(object target, string path); } } \ No newline at end of file diff --git a/src/Pather.CSharp/Resolver.cs b/src/Pather.CSharp/Resolver.cs index d72f17b..6251b60 100644 --- a/src/Pather.CSharp/Resolver.cs +++ b/src/Pather.CSharp/Resolver.cs @@ -87,5 +87,29 @@ private IPathElement createPathElement(string path, out string newPath) IPathElement result = pathElementFactory.Create(path, out newPath); return result; } + + public object ResolveSafe(object target, IList pathElements) + { + try + { + return Resolve(target, pathElements); + } + catch (NullReferenceException) + { + return null; + } + } + + public object ResolveSafe(object target, string path) + { + try + { + return Resolve(target, path); + } + catch (NullReferenceException) + { + return null; + } + } } } diff --git a/test/Pather.CSharp.UnitTests/ResolveSafeTests.cs b/test/Pather.CSharp.UnitTests/ResolveSafeTests.cs new file mode 100644 index 0000000..008ef11 --- /dev/null +++ b/test/Pather.CSharp.UnitTests/ResolveSafeTests.cs @@ -0,0 +1,280 @@ +using FluentAssertions; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Pather.CSharp.UnitTests.TestHelper; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Pather.CSharp.UnitTests +{ + [TestClass] + public class ResolveSafeTests + { + [TestMethod] + public void SinglePropertyResolution_CorrectSetup_Success() + { + var value = "1"; + var r = new Resolver(); + var o = new { Property = value }; + var path = "Property"; + + var result = r.ResolveSafe(o, path); + result.Should().Be(value); + } + + [TestMethod] + public void SinglePropertyResolution_BaseClass_Success() + { + var value = "1"; + var r = new Resolver(); + var o = new ChildClass { Property1 = value }; + var path = "Property1"; + + var result = r.ResolveSafe(o, path); + result.Should().Be(value); + } + + [TestMethod] + public void MultiplePropertyResolution_CorrectSetup_Success() + { + var value = "1"; + var r = new Resolver(); + var o = new { Property1 = new { Property2 = value } }; + var path = "Property1.Property2"; + + var result = r.ResolveSafe(o, path); + result.Should().Be(value); + } + + [TestMethod] + public void MultiplePropertyResolution_Null_ShouldThrow() + { + var r = new Resolver(); + var o = new { Property1 = (string)null }; + var path = "Property1.Property2"; + + var result = r.ResolveSafe(o, path); + result.Should().Be(null); + } + + [TestMethod] + public void DictionaryKeyResolutionWithProperty_CorrectSetup_Success() + { + var r = new Resolver(); + var dictionary = new Dictionary { { "Key", "Value" } }; + var o = new { Dictionary = dictionary }; + var path = "Dictionary[Key]"; + + var result = r.ResolveSafe(o, path); + result.Should().Be("Value"); + } + + [TestMethod] + public void DictionaryKeyResolution_CorrectSetup_Success() + { + var r = new Resolver(); + var dictionary = new Dictionary { { "Key", "Value" } }; + var path = "[Key]"; + + var result = r.ResolveSafe(dictionary, path); + result.Should().Be("Value"); + } + + [TestMethod] + public void MultipleDictionaryKeyResolution_CorrectSetup_Success() + { + var r = new Resolver(); + var dictionary = new Dictionary> { + { "Key", new Dictionary { { "Key", "Value" } } } + }; + var path = "[Key][Key]"; + + var result = r.ResolveSafe(dictionary, path); + result.Should().Be("Value"); + } + + [TestMethod] + public void ArrayIndexResolutionWithProperty_CorrectSetup_Success() + { + var r = new Resolver(); + var array = new[] { "1", "2" }; + var o = new { Array = array }; + var path = "Array[0]"; + + var result = r.ResolveSafe(o, path); + result.Should().Be("1"); + } + + [TestMethod] + public void ArrayIndexResolution_CorrectSetup_Success() + { + var r = new Resolver(); + var array = new[] { "1", "2" }; + var path = "[0]"; + + var result = r.ResolveSafe(array, path); + result.Should().Be("1"); + } + + [TestMethod] + public void MultipleArrayIndexResolution_CorrectSetup_Success() + { + var r = new Resolver(); + var array = new[] { new[] { "1", "2" } }; + var path = "[0][0]"; + + var result = r.ResolveSafe(array, path); + result.Should().Be("1"); + } + + [TestMethod] + public void SelectionResolution_CorrectSetup_Success() + { + var r = new Resolver(); + var array = new[] { 1, 2 }; + var path = "[]"; + + var result = r.ResolveSafe(array, path); + result.Should().BeOfType(typeof(Selection)); + result.Should().BeEquivalentTo(new[] { 1, 2 }); + } + + [TestMethod] + public void SelectionPropertyResolution_CorrectSetup_Success() + { + var r = new Resolver(); + var array = new[] + { + new { P1 = "1" }, + new { P1 = "2" } + }; + var path = "[].P1"; + + var result = r.ResolveSafe(array, path) as IEnumerable; + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(new[] { "1", "2" }); + } + + [TestMethod] + public void SelectionDictionaryKeyResolution_CorrectSetup_Success() + { + var r = new Resolver(); + var array = new[] + { + new Dictionary { { "Key", "1" } }, + new Dictionary { { "Key", "2" } } + }; + var path = "[][Key]"; + + var result = r.ResolveSafe(array, path) as IEnumerable; + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(new[] { "1", "2" }); + } + + [TestMethod] + public void SelectionArrayIndexResolution_CorrectSetup_Success() + { + var r = new Resolver(); + var array = new[] + { + new[] { "1", "2" }, + new[] { "3", "4" } + }; + var path = "[][1]"; + + var result = r.ResolveSafe(array, path) as IEnumerable; + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(new[] { "2", "4" }); + } + + [TestMethod] + public void SelectionFlattening_CorrectSetup_Success() + { + var r = new Resolver(); + var array = new object[] + { + 1, + new[] { 2, 3 } + }; + var path = "[][]"; + + var result = r.ResolveSafe(array, path) as IEnumerable; + result.Should().NotBeNull(); + result.Should().BeEquivalentTo(new[] { 1, 2, 3 }); + } + + [TestMethod] + public void SinglePropertyResolution_NoPathElementTypeForPath_FailWithNoApplicablePathElementType() + { + var r = new Resolver(); + var o = new { Property = "1" }; + var path = "Property^%#"; + + r.Invoking(re => re.ResolveSafe(o, path)).Should().Throw(); + } + + [TestMethod] + public void SinglePropertyResolution_NonExistingProperty_FailWithPropertyCouldNotBeFound() + { + var r = new Resolver(); + var o = new { Property = "1" }; + var path = "NonExistingProperty"; + + r.Invoking(re => re.ResolveSafe(o, path)).Should().Throw(); + } + + [TestMethod] + public void ArrayIndexResolution_IndexHigher_FailWithIndexTooHigh() + { + var r = new Resolver(); + var array = new[] { "1", "2" }; + var path = "[3]"; + + r.Invoking(re => re.ResolveSafe(array, path)).Should().Throw(); + } + + [TestMethod] + public void ArrayIndexResolution_IndexLower_FailWithNoApplicablePathElementType() + { + var r = new Resolver(); + var array = new[] { "1", "2" }; + var path = "[-2]"; + + r.Invoking(re => re.ResolveSafe(array, path)).Should().Throw(); + } + + [TestMethod] + public void DictionaryKeyResolution_KeyNotExisting_FailWithKeyNotExisting() + { + var r = new Resolver(); + var dictionary = new Dictionary { { "Key", "Value" } }; + var path = "[NonExistingKey]"; + + r.Invoking(re => re.ResolveSafe(dictionary, path)).Should().Throw(); + } + + [TestMethod] + public void IndexerResolution_Int_Success() + { + var target = new IntIndexerClassNoIEnumerable("Test"); + var resolver = new Resolver(); + var path = "[123456]"; + + var res = resolver.ResolveSafe(target, path); + res.Should().Be("Test123456"); + } + + [TestMethod] + public void IndexerResolution_String_Success() + { + var target = new StringIndexerClassNoIEnumerable("Test"); + var resolver = new Resolver(); + var path = "[abc]"; + + var res = resolver.ResolveSafe(target, path); + res.Should().Be("Testabc"); + } + } +} diff --git a/test/Pather.CSharp.UnitTests/ResolverTests.cs b/test/Pather.CSharp.UnitTests/ResolveTests.cs similarity index 95% rename from test/Pather.CSharp.UnitTests/ResolverTests.cs rename to test/Pather.CSharp.UnitTests/ResolveTests.cs index d642206..e3d56e6 100644 --- a/test/Pather.CSharp.UnitTests/ResolverTests.cs +++ b/test/Pather.CSharp.UnitTests/ResolveTests.cs @@ -10,7 +10,7 @@ namespace Pather.CSharp.UnitTests { [TestClass] - public class ResolverTests + public class ResolveTests { [TestMethod] public void SinglePropertyResolution_CorrectSetup_Success() @@ -48,6 +48,16 @@ public void MultiplePropertyResolution_CorrectSetup_Success() result.Should().Be(value); } + [TestMethod] + public void MultiplePropertyResolution_Null_ShouldThrow() + { + var r = new Resolver(); + var o = new { Property1 = (string)null }; + var path = "Property1.Property2"; + + r.Invoking(re => re.Resolve(o, path)).Should().Throw(); + } + [TestMethod] public void DictionaryKeyResolutionWithProperty_CorrectSetup_Success() { From ea8f9020ebce67abbbaab5b108f2f697f65cb1da Mon Sep 17 00:00:00 2001 From: domysee Date: Fri, 23 Feb 2018 14:52:14 +0100 Subject: [PATCH 4/4] Update Version to 2.1.0 --- src/Pather.CSharp/Pather.CSharp.csproj | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Pather.CSharp/Pather.CSharp.csproj b/src/Pather.CSharp/Pather.CSharp.csproj index 759a616..e57c47c 100644 --- a/src/Pather.CSharp/Pather.CSharp.csproj +++ b/src/Pather.CSharp/Pather.CSharp.csproj @@ -14,8 +14,9 @@ https://github.com/Domysee/Pather.CSharp/blob/master/LICENSE git https://github.com/Domysee/Pather.CSharp - 2.0.4 + 2.1.0 True + 2.1.0.0