Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of custom-scalar and applicable to variable
- Loading branch information
Cédric L. Charlier
committed
Nov 4, 2019
1 parent
0ada10a
commit ea0e7ad
Showing
22 changed files
with
315 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Microsoft.CSharp; | ||
using NBi.Core.Assemblies; | ||
using System; | ||
using System.CodeDom.Compiler; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace NBi.Core.Scalar.Resolver | ||
{ | ||
class CustomScalarResolver<T> : AbstractCustomFactory<IScalarResolver>, IScalarResolver<T> | ||
{ | ||
private CustomScalarResolverArgs Args {get;} | ||
|
||
public CustomScalarResolver(CustomScalarResolverArgs args) | ||
=> Args = args; | ||
|
||
protected override string CustomKind => "custom evaluation of a scalar"; | ||
|
||
public T Execute() | ||
{ | ||
var instance = Instantiate(Args); | ||
var value = instance.Execute(); | ||
return (T)Convert.ChangeType(value, typeof(T)); | ||
} | ||
|
||
object IResolver.Execute() => Execute(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using NBi.Core.Assemblies; | ||
using NBi.Core.Query; | ||
using NBi.Core.Query.Resolver; | ||
using NBi.Core.ResultSet.Resolver; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace NBi.Core.Scalar.Resolver | ||
{ | ||
public class CustomScalarResolverArgs : IScalarResolverArgs, ICustomArgs | ||
{ | ||
public IScalarResolver<string> AssemblyPath { get; } | ||
|
||
public IScalarResolver<string> TypeName { get; } | ||
|
||
public IReadOnlyDictionary<string, IScalarResolver> Parameters { get; } | ||
|
||
public CustomScalarResolverArgs(IScalarResolver<string> assemblyPath, IScalarResolver<string> typeName, IDictionary<string, IScalarResolver> parameters) | ||
=> (AssemblyPath, TypeName, Parameters) = (assemblyPath, typeName, new ReadOnlyDictionary<string, IScalarResolver>(parameters)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
NBi.Testing.Core/Scalar/Resolver/CustomScalarResolverTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using Moq; | ||
using NBi.Core.Scalar.Resolver; | ||
using NBi.Testing.Core.Scalar.Resolver.Resources; | ||
using NUnit.Framework; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace NBi.Testing.Core.Scalar.Resolver | ||
{ | ||
public class CustomScalarResolverTest | ||
{ | ||
[Test] | ||
public void Execute_TypeWithoutParam_CorrectEvaluation() | ||
{ | ||
var args = new CustomScalarResolverArgs | ||
( | ||
new LiteralScalarResolver<string>(GetType().Assembly.Location), | ||
new LiteralScalarResolver<string>($"{typeof(MyCustomClass).Namespace}.{typeof(MyCustomClass).Name}"), | ||
new Dictionary<string, IScalarResolver>() | ||
); | ||
var resolver = new CustomScalarResolver<string>(args); | ||
Assert.That(resolver.Execute(), Is.EqualTo("myValue")); | ||
} | ||
|
||
[Test] | ||
public void Execute_TypeWithParam_CorrectEvaluation() | ||
{ | ||
var args = new CustomScalarResolverArgs | ||
( | ||
new LiteralScalarResolver<string>(GetType().Assembly.Location), | ||
new LiteralScalarResolver<string>($"{typeof(MyCustomClassWithParams).Namespace}.{typeof(MyCustomClassWithParams).Name}"), | ||
new Dictionary<string, IScalarResolver>() { { "foo", new LiteralScalarResolver<int>(5) }, { "bar", new LiteralScalarResolver<DateTime>(new DateTime(2019, 1, 1)) } } | ||
); | ||
var resolver = new CustomScalarResolver<DateTime>(args); | ||
var output = resolver.Execute(); | ||
Assert.That(output, Is.EqualTo(new DateTime(2019,1,6))); | ||
} | ||
|
||
//[Test] | ||
//public void Execute_TwoCalls_OneExecution() | ||
//{ | ||
// var factory = new CustomScalarFactory(); | ||
// var mock = new Mock<ICustomScalarEvaluation>(); | ||
// mock.SetupSequence(x => x.Execute()).Returns(true).Returns(false); | ||
|
||
// var customEvaluation = factory.Instantiate(mock.Object); | ||
// Assert.That(customEvaluation.Execute(), Is.True); | ||
// Assert.That(customEvaluation.Execute(), Is.True); | ||
// mock.Verify(x => x.Execute(), Times.Once); | ||
//} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
NBi.Testing.Core/Scalar/Resolver/Resources/MyCustomClass.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using NBi.Core.Scalar.Resolver; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace NBi.Testing.Core.Scalar.Resolver.Resources | ||
{ | ||
public class MyCustomClass : IScalarResolver | ||
{ | ||
public object Execute() => "myValue"; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
NBi.Testing.Core/Scalar/Resolver/Resources/MyCustomClassWithParams.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using NBi.Core.Scalar.Resolver; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace NBi.Testing.Core.Scalar.Resolver.Resources | ||
{ | ||
public class MyCustomClassWithParams : IScalarResolver | ||
{ | ||
private int Foo { get; } | ||
private DateTime Bar { get; } | ||
|
||
public MyCustomClassWithParams(DateTime bar, int foo) | ||
=> (Bar, Foo) = (bar, foo); | ||
|
||
public object Execute() => Bar.AddDays(Foo); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
NBi.Testing/Acceptance/Resources/CustomVariableDaysBetween.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using NBi.Core.Scalar.Resolver; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace NBi.Testing.Acceptance.Resources | ||
{ | ||
public class CustomVariableDaysBetween : IScalarResolver | ||
{ | ||
private DateTime From { get; } | ||
private DateTime To { get; } | ||
|
||
public CustomVariableDaysBetween(DateTime from, DateTime to) | ||
=> (From, To) = (from, to); | ||
|
||
public object Execute() => To.Subtract(From).TotalDays; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.