Skip to content

Commit

Permalink
Include Mutation testing
Browse files Browse the repository at this point in the history
  • Loading branch information
fangdatto committed Aug 5, 2024
1 parent 71e5486 commit b42fe65
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,39 @@ jobs:
only-summary: 'false'
fail-on-error: true

Mutation-Testing:

env:
BUILD_CONFIG: 'Release'
SOLUTION: 'Solution/UnitTest.sln'

runs-on: ubuntu-latest
permissions:
issues: write
contents: write
actions: write
checks: write

steps:
- uses: actions/checkout@v2

- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: 6.x

- name: stryker-mutator .NET
uses: stryker-mutator/github-action@v0.0.1
with:
testProject: "Solution/TestPalindrome/" # required
breakAt: "75" # Optional

- name: Html-Report
uses: actions/upload-artifact@v3
with:
name: html-report
path: ${{github.workspace}}/Solution/StrykerOutput/**/**/*.html

Release-Build:

env:
Expand Down
20 changes: 20 additions & 0 deletions Solution/Palindrome/Palindrome.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace us.fatehi.palindrome
{
public class Palindrome
{
public bool IsPalindrome(string word)
{
// Exit early for trivial cases, such as null,
// empty string, and a word with a single letter
if (word == null || word.Length < 2)
{
return true;
}

var firstChar = word[0];
var lastChar = word[^1];
var center = word[1..^1];
return (firstChar == lastChar) && IsPalindrome(center);
}
}
}
9 changes: 9 additions & 0 deletions Solution/Palindrome/Palindrome.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

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

</Project>
24 changes: 24 additions & 0 deletions Solution/TestPalindrome/BetterPalindromeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using NUnit.Framework;
using us.fatehi.palindrome;

namespace us.fatehi.palindrome.test
{

[TestFixture]
public class BetterPalindromeTest
{
private readonly Palindrome _p = new Palindrome();

[Test]
public void BoundaryConditionHappy()
{
Assert.That(_p.IsPalindrome("oo"));
}

[Test]
public void BoundaryConditionNegative()
{
Assert.That(_p.IsPalindrome("ah"), Is.False);
}
}
}
18 changes: 18 additions & 0 deletions Solution/TestPalindrome/MorePalindromeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using NUnit.Framework;
using us.fatehi.palindrome;

namespace us.fatehi.palindrome.test
{

[TestFixture]
public class MorePalindromeTest
{
private readonly Palindrome _p = new Palindrome();

[Test]
public void AlmostPalindrome()
{
Assert.That(_p.IsPalindrome("polyp"), Is.False);
}
}
}
48 changes: 48 additions & 0 deletions Solution/TestPalindrome/PalindromeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using NUnit.Framework;
using us.fatehi.palindrome;

namespace us.fatehi.palindrome.test
{

[TestFixture]
public class PalindromeTest
{
private readonly Palindrome _p = new Palindrome();

[Test]
public void EdgeCaseNull()
{
Assert.That(_p.IsPalindrome(null));

Check warning on line 15 in Solution/TestPalindrome/PalindromeTest.cs

View workflow job for this annotation

GitHub Actions / Examples-Test

Cannot convert null literal to non-nullable reference type.
}

[Test]
public void EdgeCaseEmptyString()
{
Assert.That(_p.IsPalindrome(""));
}

[Test]
public void HappyPathSingleLetter()
{
Assert.That(_p.IsPalindrome("a"));
}

[Test]
public void HappyPathEvenLetters()
{
Assert.That(_p.IsPalindrome("noon"));
}

[Test]
public void HappyPathOddLetters()
{
Assert.That(_p.IsPalindrome("racecar"));
}

[Test]
public void NegativeCase()
{
Assert.That(_p.IsPalindrome("burning"), Is.False);
}
}
}
28 changes: 28 additions & 0 deletions Solution/TestPalindrome/TestPalindrome.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.2.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
</ItemGroup>

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

<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>

</Project>
12 changes: 12 additions & 0 deletions Solution/UnitTest.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SocketDemo", "SocketDemo\So
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFrameworkCore.MySQL", "EntityFrameworkCore.MySQL\EntityFrameworkCore.MySQL.csproj", "{2897BFC4-CEE8-4BA4-94A9-023470652462}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Palindrome", "Palindrome\Palindrome.csproj", "{F7AAC371-BACF-4DE6-994B-49DBFFDA0915}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestPalindrome", "TestPalindrome\TestPalindrome.csproj", "{00BD75DE-F945-417D-8B43-6FE16A0F0BE5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -39,6 +43,14 @@ Global
{2897BFC4-CEE8-4BA4-94A9-023470652462}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2897BFC4-CEE8-4BA4-94A9-023470652462}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2897BFC4-CEE8-4BA4-94A9-023470652462}.Release|Any CPU.Build.0 = Release|Any CPU
{F7AAC371-BACF-4DE6-994B-49DBFFDA0915}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F7AAC371-BACF-4DE6-994B-49DBFFDA0915}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F7AAC371-BACF-4DE6-994B-49DBFFDA0915}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F7AAC371-BACF-4DE6-994B-49DBFFDA0915}.Release|Any CPU.Build.0 = Release|Any CPU
{00BD75DE-F945-417D-8B43-6FE16A0F0BE5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{00BD75DE-F945-417D-8B43-6FE16A0F0BE5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{00BD75DE-F945-417D-8B43-6FE16A0F0BE5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{00BD75DE-F945-417D-8B43-6FE16A0F0BE5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit b42fe65

Please sign in to comment.