Skip to content

Commit

Permalink
feat: decoupled business tests
Browse files Browse the repository at this point in the history
  • Loading branch information
psarras committed Oct 28, 2024
1 parent d469781 commit d77dce1
Show file tree
Hide file tree
Showing 24 changed files with 631 additions and 199 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
/.vs
/.venv
packages
bin/
obj/
AdSecGH/bin
AdSecGH/obj
AdSecGHConverters/bin
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ repos:

- id: dotnet-build
name: Build
entry: msbuild /p:AppxBundlePlatforms="x64" /p:AppxBundle=Always /p:UapAppxPackageBuildMode=StoreUpload /m /nr:false
entry: msbuild /p:AppxBundlePlatforms="x64" /p:AppxBundle=Always /p:UapAppxPackageBuildMode=StoreUpload /m /nr:false AdSecGH.sln
language: system
pass_filenames: false
stages: [pre-push]
Expand Down
22 changes: 22 additions & 0 deletions AdSecCore.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}") = "AdSecCore", "AdSecCore\AdSecCore.csproj", "{2A3194AF-5B84-47DF-8E36-6819C2CF6E97}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdSecCoreTests", "AdSecCoreTests\AdSecCoreTests.csproj", "{2407A2E8-B608-454F-A6A9-EABDFFFF48FD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2A3194AF-5B84-47DF-8E36-6819C2CF6E97}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2A3194AF-5B84-47DF-8E36-6819C2CF6E97}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2A3194AF-5B84-47DF-8E36-6819C2CF6E97}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2A3194AF-5B84-47DF-8E36-6819C2CF6E97}.Release|Any CPU.Build.0 = Release|Any CPU
{2407A2E8-B608-454F-A6A9-EABDFFFF48FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2407A2E8-B608-454F-A6A9-EABDFFFF48FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2407A2E8-B608-454F-A6A9-EABDFFFF48FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2407A2E8-B608-454F-A6A9-EABDFFFF48FD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
4 changes: 4 additions & 0 deletions AdSecCore.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=a0644a1b_002D75bf_002D4493_002Db209_002D201de422018a/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from Solution" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;Solution /&gt;&#xD;
&lt;/SessionState&gt;</s:String></wpf:ResourceDictionary>
9 changes: 9 additions & 0 deletions AdSecCore/AdSecCore.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>
<TargetFrameworks>net48;net7.0</TargetFrameworks>
</PropertyGroup>

</Project>
38 changes: 38 additions & 0 deletions AdSecCore/DoubleComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;

namespace AdSecGHCore.Helpers {
public class DoubleComparer : IEqualityComparer<double> {
private readonly double _epsilon = 0.01; //default accuracy in %
private readonly bool _margin;

public DoubleComparer(double epsilon = 0.01, bool useEpsilonAsMargin = false) {
_epsilon = epsilon;
_margin = useEpsilonAsMargin;
}

public bool Equals(double x, double y) {
x = Math.Round(x, 6);
y = Math.Round(y, 6);

if (x == y) {
return true;
}

if (_margin) {
if (Math.Abs(x - y) < _epsilon) {
return true;

Check warning on line 24 in AdSecCore/DoubleComparer.cs

View check run for this annotation

Codecov / codecov/patch

AdSecCore/DoubleComparer.cs#L24

Added line #L24 was not covered by tests
}
} else {
double error = Math.Abs(x - y) / (x + y) * 0.5;
return error < _epsilon;
}

return false;

Check warning on line 31 in AdSecCore/DoubleComparer.cs

View check run for this annotation

Codecov / codecov/patch

AdSecCore/DoubleComparer.cs#L31

Added line #L31 was not covered by tests
}

public int GetHashCode(double obj) {
return obj.GetHashCode();

Check warning on line 35 in AdSecCore/DoubleComparer.cs

View check run for this annotation

Codecov / codecov/patch

AdSecCore/DoubleComparer.cs#L35

Added line #L35 was not covered by tests
}
}
}
60 changes: 60 additions & 0 deletions AdSecCore/Result.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;

namespace AdSecGHCore.Helpers {

internal class Result {

internal static double RoundToSignificantDigits(double d, int digits) {
if (d == 0.0) {
return 0.0;

Check warning on line 10 in AdSecCore/Result.cs

View check run for this annotation

Codecov / codecov/patch

AdSecCore/Result.cs#L10

Added line #L10 was not covered by tests
}

double leftSideNumbers = Math.Floor(Math.Log10(Math.Abs(d))) + 1;
double scale = Math.Pow(10, leftSideNumbers);
double result = scale * Math.Round(d / scale, digits, MidpointRounding.AwayFromZero);

Check warning on line 15 in AdSecCore/Result.cs

View check run for this annotation

Codecov / codecov/patch

AdSecCore/Result.cs#L13-L15

Added lines #L13 - L15 were not covered by tests

// Clean possible precision error.
if ((int)leftSideNumbers >= digits) {
return Math.Round(result, 0, MidpointRounding.AwayFromZero);

Check warning on line 19 in AdSecCore/Result.cs

View check run for this annotation

Codecov / codecov/patch

AdSecCore/Result.cs#L19

Added line #L19 was not covered by tests
}

if (Math.Abs(digits - (int)leftSideNumbers) > 15) {
return 0.0;

Check warning on line 23 in AdSecCore/Result.cs

View check run for this annotation

Codecov / codecov/patch

AdSecCore/Result.cs#L23

Added line #L23 was not covered by tests
}

return Math.Round(result, digits - (int)leftSideNumbers, MidpointRounding.AwayFromZero);

Check warning on line 26 in AdSecCore/Result.cs

View check run for this annotation

Codecov / codecov/patch

AdSecCore/Result.cs#L26

Added line #L26 was not covered by tests
}

internal static List<double> SmartRounder(double max, double min) {
// find the biggest abs value of max and min
double val = Math.Max(Math.Abs(max), Math.Abs(min));

Check warning on line 31 in AdSecCore/Result.cs

View check run for this annotation

Codecov / codecov/patch

AdSecCore/Result.cs#L31

Added line #L31 was not covered by tests

// round that with 4 significant digits
double scale = RoundToSignificantDigits(val, 4);

Check warning on line 34 in AdSecCore/Result.cs

View check run for this annotation

Codecov / codecov/patch

AdSecCore/Result.cs#L34

Added line #L34 was not covered by tests

// list to hold output values
var roundedvals = new List<double>();

Check warning on line 37 in AdSecCore/Result.cs

View check run for this annotation

Codecov / codecov/patch

AdSecCore/Result.cs#L37

Added line #L37 was not covered by tests

// do max
if (max == 0) {
roundedvals.Add(0);

Check warning on line 41 in AdSecCore/Result.cs

View check run for this annotation

Codecov / codecov/patch

AdSecCore/Result.cs#L41

Added line #L41 was not covered by tests
} else {
double tempmax = scale * Math.Round(max / scale, 4);
tempmax = Math.Ceiling(tempmax * 1000) / 1000;
roundedvals.Add(tempmax);

Check warning on line 45 in AdSecCore/Result.cs

View check run for this annotation

Codecov / codecov/patch

AdSecCore/Result.cs#L43-L45

Added lines #L43 - L45 were not covered by tests
}

// do min
if (min == 0) {
roundedvals.Add(0);

Check warning on line 50 in AdSecCore/Result.cs

View check run for this annotation

Codecov / codecov/patch

AdSecCore/Result.cs#L50

Added line #L50 was not covered by tests
} else {
double tempmin = scale * Math.Round(min / scale, 4);
tempmin = Math.Floor(tempmin * 1000) / 1000;
roundedvals.Add(tempmin);

Check warning on line 54 in AdSecCore/Result.cs

View check run for this annotation

Codecov / codecov/patch

AdSecCore/Result.cs#L52-L54

Added lines #L52 - L54 were not covered by tests
}

return roundedvals;

Check warning on line 57 in AdSecCore/Result.cs

View check run for this annotation

Codecov / codecov/patch

AdSecCore/Result.cs#L57

Added line #L57 was not covered by tests
}
}
}
31 changes: 31 additions & 0 deletions AdSecCoreTests/AdSecCoreTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

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

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

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.extensibility.core" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<Using Include="Xunit"/>
</ItemGroup>

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

</Project>
16 changes: 16 additions & 0 deletions AdSecCoreTests/DoubleComparerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using AdSecGHCore.Helpers;

namespace AdSecCoreTests {
public class DoubleComparerTests {
[Fact]
public void ValuesDifferLessThanEpsilonShouldBeConsideredEqual() {
Assert.Equal(10.0, 10.01, new DoubleComparer());
}

[Fact]
public void ValuesLargerThanEpsilonShouldBeConsideredEqual() {
int epsilon = 1;
Assert.NotEqual(10.0, 10.0 + (epsilon * 2), new DoubleComparer(epsilon, true));
}
}
}
12 changes: 12 additions & 0 deletions AdSecGH.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AdSecGHConverters", "AdSecG
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AdSecGHConverterTests", "AdSecGHConverterTests\AdSecGHConverterTests.csproj", "{885C7D18-8B54-4430-A84B-8FD37CF7AD27}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdSecCore", "AdSecCore\AdSecCore.csproj", "{D7A31820-EC4F-42EC-863C-51BB3647A145}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdSecCoreTests", "AdSecCoreTests\AdSecCoreTests.csproj", "{DF259C79-A298-44B9-B702-573D4206A3C2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Expand All @@ -39,6 +43,14 @@ Global
{885C7D18-8B54-4430-A84B-8FD37CF7AD27}.Debug|x64.Build.0 = Debug|x64
{885C7D18-8B54-4430-A84B-8FD37CF7AD27}.Release|x64.ActiveCfg = Release|x64
{885C7D18-8B54-4430-A84B-8FD37CF7AD27}.Release|x64.Build.0 = Release|x64
{D7A31820-EC4F-42EC-863C-51BB3647A145}.Debug|x64.ActiveCfg = Debug|Any CPU
{D7A31820-EC4F-42EC-863C-51BB3647A145}.Debug|x64.Build.0 = Debug|Any CPU
{D7A31820-EC4F-42EC-863C-51BB3647A145}.Release|x64.ActiveCfg = Release|Any CPU
{D7A31820-EC4F-42EC-863C-51BB3647A145}.Release|x64.Build.0 = Release|Any CPU
{DF259C79-A298-44B9-B702-573D4206A3C2}.Debug|x64.ActiveCfg = Debug|Any CPU
{DF259C79-A298-44B9-B702-573D4206A3C2}.Debug|x64.Build.0 = Debug|Any CPU
{DF259C79-A298-44B9-B702-573D4206A3C2}.Release|x64.ActiveCfg = Release|Any CPU
{DF259C79-A298-44B9-B702-573D4206A3C2}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
4 changes: 4 additions & 0 deletions AdSecGH.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=dcccd273_002Dab94_002D4b0c_002D8563_002D940157ce25e3/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from Solution" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;Solution /&gt;&#xD;
&lt;/SessionState&gt;</s:String></wpf:ResourceDictionary>
3 changes: 3 additions & 0 deletions AdSecGH/AdSecGH.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@
<ItemGroup>
<InternalsVisibleTo Include="AdSecGHTests"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AdSecCore\AdSecCore.csproj"/>
</ItemGroup>
<PropertyGroup Condition="$(Configuration) == 'Debug' AND $([MSBuild]::IsOSPlatform(Windows))">
<StartProgram>C:\Program Files\Rhino 7\System\Rhino.exe</StartProgram>
<StartArguments></StartArguments>
Expand Down
Loading

0 comments on commit d77dce1

Please sign in to comment.