Skip to content

Commit 26e2118

Browse files
committed
- Add .github files.
- Add unit tests for chain class.
1 parent 54329e9 commit 26e2118

File tree

7 files changed

+136
-1
lines changed

7 files changed

+136
-1
lines changed

.github/FUNDING.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github: CodingFlow

.github/dependabot.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "nuget"
4+
directory: "/"
5+
schedule:
6+
interval: "weekly"

.github/workflows/pull-request.yml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: pull-request-unit-tests
2+
run-name: Unit tests
3+
on: [push]
4+
jobs:
5+
build:
6+
name: Unit tests
7+
runs-on: ubuntu-latest
8+
strategy:
9+
matrix:
10+
dotnet-version: ["8.0.x"]
11+
12+
steps:
13+
- uses: actions/checkout@v3
14+
- name: Setup .NET Core SDK ${{ matrix.dotnet-version }}
15+
uses: actions/setup-dotnet@v3
16+
with:
17+
dotnet-version: ${{ matrix.dotnet-version }}
18+
- name: Install dependencies
19+
run: dotnet restore
20+
- name: Build
21+
run: dotnet build --configuration Release --no-restore
22+
- name: Test
23+
run: dotnet test --no-restore --verbosity normal

LightChain.UnitTests/ChainTests.cs

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using NSubstitute;
2+
using NSubstitute.ReceivedExtensions;
3+
4+
namespace LightChain.UnitTests;
5+
6+
public class ChainTests
7+
{
8+
private List<IProcessor<Input, string>> processors;
9+
private Chain<IProcessor<Input, string>, Input, string> chain;
10+
11+
[SetUp]
12+
public void Setup() {
13+
processors = new List<IProcessor<Input, string>> {
14+
Substitute.For<IProcessor<Input, string>>(),
15+
Substitute.For<IProcessor<Input, string>>()
16+
};
17+
chain = new Chain<IProcessor<Input, string>, Input, string>(processors);
18+
}
19+
20+
[Test]
21+
public void Processor_First_Called_Successfully() {
22+
processors.First().Condition(null).ReturnsForAnyArgs(true);
23+
processors.First().Process(null).ReturnsForAnyArgs("some value");
24+
25+
var input = new Input
26+
{
27+
Value = "some value"
28+
};
29+
30+
var result = chain.Run(input);
31+
32+
Assert.That(result, Is.EqualTo("some value"));
33+
34+
processors.First().Received(1).Condition(input);
35+
processors.First().Received(1).Process(input);
36+
}
37+
38+
[Test]
39+
public void Processor_Second_Called_Successfully() {
40+
processors.First().Condition(null).ReturnsForAnyArgs(false);
41+
processors.First().Process(null).ReturnsForAnyArgs("first value");
42+
43+
processors.ElementAt(1).Condition(null).ReturnsForAnyArgs(true);
44+
processors.ElementAt(1).Process(null).ReturnsForAnyArgs("second value");
45+
46+
var input = new Input
47+
{
48+
Value = "some value"
49+
};
50+
51+
var result = chain.Run(input);
52+
53+
Assert.That(result, Is.EqualTo("second value"));
54+
55+
processors.First().Received(1).Condition(input);
56+
processors.First().DidNotReceive().Process(Arg.Any<Input>());
57+
58+
processors.ElementAt(1).Received(1).Condition(input);
59+
processors.ElementAt(1).Received(1).Process(input);
60+
}
61+
62+
public class Input
63+
{
64+
public string Value { get; set; }
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
<IsTestProject>true</IsTestProject>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="coverlet.collector" Version="6.0.0" />
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
15+
<PackageReference Include="NSubstitute" Version="5.1.0" />
16+
<PackageReference Include="NSubstitute.Analyzers.CSharp" Version="1.0.17">
17+
<PrivateAssets>all</PrivateAssets>
18+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
19+
</PackageReference>
20+
<PackageReference Include="NUnit" Version="3.14.0" />
21+
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
22+
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<ProjectReference Include="..\LightChain\LightChain.csproj" />
27+
</ItemGroup>
28+
29+
<ItemGroup>
30+
<Using Include="NUnit.Framework" />
31+
</ItemGroup>
32+
33+
</Project>

LightChain.sln

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionItems", "SolutionIt
1111
EndProject
1212
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LightChain", "LightChain\LightChain.csproj", "{F8234EC4-E3F3-4810-8F43-C049F0111E57}"
1313
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LightChain.UnitTests", "LightChain.UnitTests\LightChain.UnitTests.csproj", "{51E06062-8E93-4849-BCC0-4CCFF756256A}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
2123
{F8234EC4-E3F3-4810-8F43-C049F0111E57}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{F8234EC4-E3F3-4810-8F43-C049F0111E57}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{F8234EC4-E3F3-4810-8F43-C049F0111E57}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{51E06062-8E93-4849-BCC0-4CCFF756256A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{51E06062-8E93-4849-BCC0-4CCFF756256A}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{51E06062-8E93-4849-BCC0-4CCFF756256A}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{51E06062-8E93-4849-BCC0-4CCFF756256A}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE

LightChain/IChain.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ public interface IChain<TInput, TOutput>
1313
/// </summary>
1414
/// <param name="input">Input passed to processors.</param>
1515
/// <returns>Output of processor able to handle input.</returns>
16-
TOutput Run(TInput input);
16+
public TOutput Run(TInput input);
1717
}

0 commit comments

Comments
 (0)