Unit test hinzugefügt
This commit is contained in:
parent
d7e6dcc3b2
commit
fed5a3b443
18
InjectorTest/HelperTest.cs
Normal file
18
InjectorTest/HelperTest.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using SmallInjectorDemo;
|
||||
using Xunit;
|
||||
|
||||
namespace InjectorTest
|
||||
{
|
||||
public class HelperTest
|
||||
{
|
||||
[Fact]
|
||||
public void Test()
|
||||
{
|
||||
const string expected = "[-] HelperTest.Test Id: 0";
|
||||
var actual = Helper.WriteMethodString(new TestClock(), 0);
|
||||
Assert.Equal(expected, actual);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
22
InjectorTest/InjectorTest.csproj
Normal file
22
InjectorTest/InjectorTest.csproj
Normal file
@ -0,0 +1,22 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp2.1</TargetFramework>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.0" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SmallInjectorDemo\SmallInjectorDemo.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
9
InjectorTest/TestClock.cs
Normal file
9
InjectorTest/TestClock.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using SmallInjectorDemo;
|
||||
|
||||
namespace InjectorTest
|
||||
{
|
||||
public class TestClock : IClock
|
||||
{
|
||||
public string CurrentDateTime => "-";
|
||||
}
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
using System;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace SmallInjectorDemo
|
||||
{
|
||||
/// <summary>
|
||||
/// Static helper class for generating random numbers.
|
||||
/// </summary>
|
||||
public static class RandomHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Return cryptographic stable random integer.
|
||||
/// </summary>
|
||||
/// <param name="min">Lower border of result.</param>
|
||||
/// <param name="max">Upper border of result.</param>
|
||||
/// <returns>Random integer.</returns>
|
||||
public static int NewRandomInteger(int min, int max)
|
||||
{
|
||||
// The random number provider.
|
||||
using (var rand = new RNGCryptoServiceProvider())
|
||||
{
|
||||
uint scale = uint.MaxValue;
|
||||
while (scale == uint.MaxValue)
|
||||
{
|
||||
// Get four random bytes.
|
||||
var four_bytes = new byte[4];
|
||||
rand.GetBytes(four_bytes);
|
||||
|
||||
// Convert that into an uint.
|
||||
scale = BitConverter.ToUInt32(four_bytes, 0);
|
||||
}
|
||||
|
||||
// Add min to the scaled difference between max and min.
|
||||
return (int)(min + (max - min) *
|
||||
(scale / (double)uint.MaxValue));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 15
|
||||
VisualStudioVersion = 15.0.26124.0
|
||||
MinimumVisualStudioVersion = 15.0.26124.0
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmallInjectorDemo", "SmallInjectorDemo.csproj", "{620CC001-7DF9-4233-AFC2-187FD9144835}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmallInjectorDemo", "SmallInjectorDemo\SmallInjectorDemo.csproj", "{620CC001-7DF9-4233-AFC2-187FD9144835}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InjectorTest", "InjectorTest\InjectorTest.csproj", "{0648782E-EB73-4326-9471-CE386C1FBC4D}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@ -27,6 +29,18 @@ Global
|
||||
{620CC001-7DF9-4233-AFC2-187FD9144835}.Release|x64.Build.0 = Release|Any CPU
|
||||
{620CC001-7DF9-4233-AFC2-187FD9144835}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{620CC001-7DF9-4233-AFC2-187FD9144835}.Release|x86.Build.0 = Release|Any CPU
|
||||
{0648782E-EB73-4326-9471-CE386C1FBC4D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{0648782E-EB73-4326-9471-CE386C1FBC4D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{0648782E-EB73-4326-9471-CE386C1FBC4D}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{0648782E-EB73-4326-9471-CE386C1FBC4D}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{0648782E-EB73-4326-9471-CE386C1FBC4D}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{0648782E-EB73-4326-9471-CE386C1FBC4D}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{0648782E-EB73-4326-9471-CE386C1FBC4D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{0648782E-EB73-4326-9471-CE386C1FBC4D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{0648782E-EB73-4326-9471-CE386C1FBC4D}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{0648782E-EB73-4326-9471-CE386C1FBC4D}.Release|x64.Build.0 = Release|Any CPU
|
||||
{0648782E-EB73-4326-9471-CE386C1FBC4D}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{0648782E-EB73-4326-9471-CE386C1FBC4D}.Release|x86.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -25,31 +25,31 @@ namespace SmallInjectorDemo
|
||||
while (scale == uint.MaxValue)
|
||||
{
|
||||
// Get four random bytes.
|
||||
var four_bytes = new byte[4];
|
||||
rand.GetBytes(four_bytes);
|
||||
var fourBytes = new byte[4];
|
||||
rand.GetBytes(fourBytes);
|
||||
|
||||
// Convert that into an uint.
|
||||
scale = BitConverter.ToUInt32(four_bytes, 0);
|
||||
scale = BitConverter.ToUInt32(fourBytes, 0);
|
||||
}
|
||||
|
||||
// Add min to the scaled difference between max and min.
|
||||
return (int) (min + (max - min) * (scale / (double) uint.MaxValue));
|
||||
return (int)(min + (max - min) * (scale / (double)uint.MaxValue));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Write method string.
|
||||
/// </summary>
|
||||
/// <param name="clock">The current clock</param>
|
||||
/// <param name="id">id of the object</param>
|
||||
/// <param name="memberName">member name</param>
|
||||
/// <param name="filepath">file path</param>
|
||||
/// <returns></returns>
|
||||
public static string WriteMethodString(int id, [CallerMemberName] string memberName = null, [CallerFilePath] string filepath = null)
|
||||
public static string WriteMethodString(IClock clock, int id, [CallerMemberName] string memberName = null, [CallerFilePath] string filepath = null)
|
||||
{
|
||||
var classname = Path.GetFileNameWithoutExtension(filepath);
|
||||
return $"{classname}.{memberName?.TrimStart('.')}".PadRight(30) + $"Id: {id}";
|
||||
return $"[{clock.CurrentDateTime}] {classname}.{memberName?.TrimStart('.')}".PadRight(30) + $"Id: {id}";
|
||||
}
|
||||
}
|
||||
}
|
13
SmallInjectorDemo/IClock.cs
Normal file
13
SmallInjectorDemo/IClock.cs
Normal file
@ -0,0 +1,13 @@
|
||||
namespace SmallInjectorDemo
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface of the clock.
|
||||
/// </summary>
|
||||
public interface IClock
|
||||
{
|
||||
/// <summary>
|
||||
/// Current Date-Time.
|
||||
/// </summary>
|
||||
string CurrentDateTime { get; }
|
||||
}
|
||||
}
|
@ -8,6 +8,8 @@ namespace SmallInjectorDemo
|
||||
{
|
||||
Console.WriteLine("Small dependency injection example");
|
||||
var injector = new SmallInjector();
|
||||
Console.WriteLine("Register " + nameof(SystemClock));
|
||||
injector.RegisterType<SystemClock, IClock>(true);
|
||||
Console.WriteLine("Register " + nameof(ServiceOne));
|
||||
injector.RegisterType<ServiceOne, IServiceOne>(true);
|
||||
Console.WriteLine("Register " + nameof(ServiceTwo));
|
||||
@ -27,7 +29,7 @@ namespace SmallInjectorDemo
|
||||
useful2.TestTheServices();
|
||||
Console.WriteLine();
|
||||
useful3.TestTheServices();
|
||||
//Console.ReadLine();
|
||||
Console.ReadLine();
|
||||
}
|
||||
}
|
||||
}
|
@ -7,6 +7,7 @@ namespace SmallInjectorDemo
|
||||
/// </summary>
|
||||
public class ServiceConsumer
|
||||
{
|
||||
private readonly IClock _clock;
|
||||
private readonly IServiceOne _service1;
|
||||
private readonly IServiceTwo _service2;
|
||||
private readonly int _id;
|
||||
@ -14,14 +15,16 @@ namespace SmallInjectorDemo
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="ServiceConsumer"/>.
|
||||
/// </summary>
|
||||
/// <param name="clock">The current clock</param>
|
||||
/// <param name="service1">injected service one.</param>
|
||||
/// <param name="service2">injected service two.</param>
|
||||
public ServiceConsumer(IServiceOne service1, IServiceTwo service2)
|
||||
public ServiceConsumer(IClock clock, IServiceOne service1, IServiceTwo service2)
|
||||
{
|
||||
_clock = clock;
|
||||
_service1 = service1;
|
||||
_service2 = service2;
|
||||
_id = Helper.NewRandomInteger(10, 99);
|
||||
Console.WriteLine(Helper.WriteMethodString(_id));
|
||||
Console.WriteLine(Helper.WriteMethodString(clock, _id));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -35,7 +38,7 @@ namespace SmallInjectorDemo
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => Helper.WriteMethodString(_id);
|
||||
public override string ToString() => Helper.WriteMethodString(_clock, _id);
|
||||
|
||||
}
|
||||
}
|
@ -8,19 +8,21 @@ namespace SmallInjectorDemo
|
||||
/// </summary>
|
||||
public class ServiceOne : IServiceOne
|
||||
{
|
||||
private readonly IClock _clock;
|
||||
private readonly int _id;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="ServiceOne"/>.
|
||||
/// </summary>
|
||||
public ServiceOne()
|
||||
public ServiceOne(IClock clock)
|
||||
{
|
||||
_clock = clock;
|
||||
_id = Helper.NewRandomInteger(10, 99);
|
||||
Console.WriteLine(Helper.WriteMethodString(_id));
|
||||
Console.WriteLine(Helper.WriteMethodString(clock, _id));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => Helper.WriteMethodString(_id);
|
||||
public override string ToString() => Helper.WriteMethodString(_clock, _id);
|
||||
}
|
||||
|
||||
/// <summary>
|
@ -8,19 +8,21 @@ namespace SmallInjectorDemo
|
||||
/// </summary>
|
||||
public class ServiceTwo : IServiceTwo
|
||||
{
|
||||
private readonly IClock _clock;
|
||||
private readonly int _id;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="ServiceTwo"/>.
|
||||
/// </summary>
|
||||
public ServiceTwo()
|
||||
public ServiceTwo(IClock clock)
|
||||
{
|
||||
_clock = clock;
|
||||
_id = Helper.NewRandomInteger(10, 99);
|
||||
Console.WriteLine(Helper.WriteMethodString(_id));
|
||||
Console.WriteLine(Helper.WriteMethodString(clock, _id));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => Helper.WriteMethodString(_id);
|
||||
public override string ToString() => Helper.WriteMethodString(_clock, _id);
|
||||
}
|
||||
|
||||
/// <summary>
|
11
SmallInjectorDemo/SystemClock.cs
Normal file
11
SmallInjectorDemo/SystemClock.cs
Normal file
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
|
||||
namespace SmallInjectorDemo
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public class SystemClock : IClock
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public string CurrentDateTime => DateTime.Now.ToString("O");
|
||||
}
|
||||
}
|
@ -20,7 +20,7 @@ namespace SmallInjectorDemo
|
||||
{
|
||||
_service1 = service1;
|
||||
_service2 = service2;
|
||||
_rand = RandomHelper.NewRandomInteger(10, 99);
|
||||
_rand = Helper.NewRandomInteger(10, 99);
|
||||
Console.WriteLine(nameof(UsefulClass) + ".ctor\tId: " + _rand);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user