Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MyNUnit #8

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.x'
dotnet-version: '7.x'
- name: Build
run: for dir in */; do cd $dir; for sln in *.sln; do dotnet build $sln; cd ..; done; done
shell: bash
Expand Down
5 changes: 5 additions & 0 deletions MyNUnit/MyNUnit.Tests/Exceptions/TestException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace MyNUnit.Tests.Exceptions;

public class TestException : Exception
{
}
23 changes: 23 additions & 0 deletions MyNUnit/MyNUnit.Tests/MyNUnit.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MyNUnit\MyNUnit.csproj" />
</ItemGroup>

</Project>
53 changes: 53 additions & 0 deletions MyNUnit/MyNUnit.Tests/MyNUnitTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using MyNUnit.Internal;
using MyNUnit.Tests.TestFiles;

namespace MyNUnit.Tests;

public class MyNUnitTests
{
[Test]
public void TestAbstractClass()
{
var testType = new TestType(typeof(AbstractClassExample));
testType.Run();
Assert.That(testType.Status, Is.EqualTo(TestTypeStatus.AbstractType));
}

[TestCase(typeof(BeforeExample))]
[TestCase(typeof(BeforeClassExample))]
[TestCase(typeof(AfterExample))]
[TestCase(typeof(AfterClassExample))]
public void TestAssistantMethods(Type classExample)
{
var testType = new TestType(classExample);
testType.Run();
var actualStatus = testType.TestUnitCollection.First().Status;
Assert.That(actualStatus, Is.EqualTo(TestUnitStatus.Succeed));
}

[Test]
public void TestMethods()
{
var testType = new TestType(typeof(TestCasesExample));
testType.Run();

foreach (var testUnit in testType.TestUnitCollection)
{
Assert.That(testUnit.Status, Is.EqualTo(GetExpectedStatus(testUnit)));
}
}

private TestUnitStatus GetExpectedStatus(TestUnit testUnit) =>
testUnit switch
{
{ Method.Name: "Success" } => TestUnitStatus.Succeed,
{ Method.Name: "Ignore" } => TestUnitStatus.Ignored,
{ Method.Name: "ExpectedException" } => TestUnitStatus.CaughtExpectedException,
{ Method.Name: "UnexpectedException" } => TestUnitStatus.TestFailed,
{ Method.Name: "NonVoidReturnType" } => TestUnitStatus.NonVoidMethod,
{ Method.Name: "HasArguments" } => TestUnitStatus.MethodHasArguments,
{ Method.Name: "PrivateMethod" } => TestUnitStatus.NonPublicMethod,
_ => throw new ArgumentOutOfRangeException(nameof(testUnit), testUnit, null)
};

}
7 changes: 7 additions & 0 deletions MyNUnit/MyNUnit.Tests/TestFiles/AbstractClassExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace MyNUnit.Tests.TestFiles;

public abstract class AbstractClassExample
{
[Test]
public abstract void AbstractMethod();
}
19 changes: 19 additions & 0 deletions MyNUnit/MyNUnit.Tests/TestFiles/AfterClassExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using MyNUnit.Attributes;

namespace MyNUnit.Tests.TestFiles;

public class AfterClassExample
{
public static int StaticVariable = 100;

[Attributes.Test]
public void Test()
{
}

[AfterClass]
public static void AfterClass()
{
StaticVariable -= 50;
}
}
19 changes: 19 additions & 0 deletions MyNUnit/MyNUnit.Tests/TestFiles/AfterExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using MyNUnit.Attributes;

namespace MyNUnit.Tests.TestFiles;

public class AfterExample
{
public int Field = 100;

[Attributes.Test]
public void Test()
{
}

[After]
public void After()
{
Field -= 50;
}
}
19 changes: 19 additions & 0 deletions MyNUnit/MyNUnit.Tests/TestFiles/BeforeClassExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using MyNUnit.Attributes;

namespace MyNUnit.Tests.TestFiles;

public class BeforeClassExample
{
public static int StaticVariable;

[BeforeClass]
public static void BeforeClass()
{
StaticVariable = 50;
}

[Attributes.Test]
public void Test()
{
}
}
19 changes: 19 additions & 0 deletions MyNUnit/MyNUnit.Tests/TestFiles/BeforeExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using MyNUnit.Attributes;

namespace MyNUnit.Tests.TestFiles;

public class BeforeExample
{
public int Field = 100;

[Before]
public void Before()
{
Field -= 50;
}

[Attributes.Test]
public void Test()
{
}
}
62 changes: 62 additions & 0 deletions MyNUnit/MyNUnit.Tests/TestFiles/TestCasesExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
namespace MyNUnit.Tests.TestFiles;

using Attributes;
using Exceptions;

public class TestCasesExample
{
[Test]
public void Success()
{
var number = 0;

for (var i = 0; i < 100; i++)
{
number += i;
}
}

[Test("Ignore")]
public void Ignore()
{
throw new TestException();
}

[Test(typeof(TestException))]
public void ExpectedException()
{
throw new TestException();
}

[Test]
public void UnexpectedException()
{
throw new TestException();
}

[Test]
public int NonVoidReturnType()
{
return 50;
}

[Test]
public void HasArguments(int number)
{
for (var i = 0; i < 100; i++)
{
number += i;
}
}

[Test]
private void PrivateMethod()
{
var number = 0;

for (var i = 0; i < 100; i++)
{
number += i;
}
}
}
1 change: 1 addition & 0 deletions MyNUnit/MyNUnit.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using NUnit.Framework;
22 changes: 22 additions & 0 deletions MyNUnit/MyNUnit.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyNUnit", "MyNUnit\MyNUnit.csproj", "{8D23FC1D-2F35-401B-8DB7-651FBEB6C961}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyNUnit.Tests", "MyNUnit.Tests\MyNUnit.Tests.csproj", "{C519FA39-7BF9-4E5B-990B-B4738F3CC4C4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{8D23FC1D-2F35-401B-8DB7-651FBEB6C961}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8D23FC1D-2F35-401B-8DB7-651FBEB6C961}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8D23FC1D-2F35-401B-8DB7-651FBEB6C961}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8D23FC1D-2F35-401B-8DB7-651FBEB6C961}.Release|Any CPU.Build.0 = Release|Any CPU
{C519FA39-7BF9-4E5B-990B-B4738F3CC4C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C519FA39-7BF9-4E5B-990B-B4738F3CC4C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C519FA39-7BF9-4E5B-990B-B4738F3CC4C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C519FA39-7BF9-4E5B-990B-B4738F3CC4C4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
9 changes: 9 additions & 0 deletions MyNUnit/MyNUnit/Attributes/AfterAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace MyNUnit.Attributes;

/// <summary>
/// Identifies a method that is called every time after executing <see cref="TestAttribute"/> method.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class AfterAttribute : MyNUnitAttribute
{
}
9 changes: 9 additions & 0 deletions MyNUnit/MyNUnit/Attributes/AfterClassAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace MyNUnit.Attributes;

/// <summary>
/// Identifies a static method that is called once after executing all <see cref="TestAttribute"/> methods.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class AfterClassAttribute : MyNUnitAttribute
{
}
9 changes: 9 additions & 0 deletions MyNUnit/MyNUnit/Attributes/BeforeAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace MyNUnit.Attributes;

/// <summary>
/// Identifies a method that is called every time prior to executing <see cref="TestAttribute"/> methods.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class BeforeAttribute : MyNUnitAttribute
{
}
9 changes: 9 additions & 0 deletions MyNUnit/MyNUnit/Attributes/BeforeClassAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace MyNUnit.Attributes;

/// <summary>
/// Identifies a static method that is called once prior to executing all <see cref="MyNUnitAttribute"/> methods.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class BeforeClassAttribute : MyNUnitAttribute
{
}
15 changes: 15 additions & 0 deletions MyNUnit/MyNUnit/Attributes/MyNUnitAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace MyNUnit.Attributes;

/// <summary>
/// Base attribute for all MyNUnit attributes.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public abstract class MyNUnitAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="MyNUnitAttribute"/> class.
/// </summary>
public MyNUnitAttribute()
{
}
}
50 changes: 50 additions & 0 deletions MyNUnit/MyNUnit/Attributes/TestAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
namespace MyNUnit.Attributes;

using Optional;

/// <summary>
/// Marks the method as a test.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class TestAttribute : MyNUnitAttribute
{
/// <summary>
/// Initializes a new instance of the <see cref="TestAttribute"/> class.
/// </summary>
public TestAttribute()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="TestAttribute"/> class.
/// </summary>
/// <param name="ignore">Reason of ignoring the test method.</param>
public TestAttribute(string ignore) => Ignore = ignore.Some();

/// <summary>
/// Initializes a new instance of the <see cref="TestAttribute"/> class.
/// </summary>
/// <param name="expected">Type of expected exception.</param>
public TestAttribute(Type expected) => Expected = expected.Some();

/// <summary>
/// Initializes a new instance of the <see cref="TestAttribute"/> class.
/// </summary>
/// <param name="expected">Type of expected exception.</param>
/// <param name="ignore">Reason of ignoring test method.</param>
public TestAttribute(Type expected, string ignore)
{
Expected = expected.Some();
Ignore = ignore.Some();
}

/// <summary>
/// Gets reason of ignoring test method.
/// </summary>
public Option<string> Ignore { get; } = Option.None<string>();

/// <summary>
/// Gets type of expected exception.
/// </summary>
public Option<Type> Expected { get; } = Option.None<Type>();
}
Loading