Skip to content

BHoM_Engine: TryRunExtensionMethod made more sensitive to inputs #3425

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

Merged
Merged
12 changes: 10 additions & 2 deletions .ci/unit-tests/BHoM_Engine_Tests.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Versioning_Engine", "..\..\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Base_Engine_Tests", "Base_Engine_Tests\Base_Engine_Tests.csproj", "{CC1F950E-D22F-443E-BB63-D6C0B013E3E7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serialiser_Engine_Tests", "Serialiser_Engine_Tests\Serialiser_Engine_Tests.csproj", "{9D02A8E2-D414-4633-9487-75A779E483A7}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serialiser_Engine_Tests", "Serialiser_Engine_Tests\Serialiser_Engine_Tests.csproj", "{9D02A8E2-D414-4633-9487-75A779E483A7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Structure_Engine_Tests", "Structure_Engine_Tests\Structure_Engine_Tests.csproj", "{FCC23B54-9A9E-4DA0-AB91-B4D5A739A9B6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Structure_Engine_Tests", "Structure_Engine_Tests\Structure_Engine_Tests.csproj", "{FCC23B54-9A9E-4DA0-AB91-B4D5A739A9B6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestHelper_Engine", "TestHelper_Engine\TestHelper_Engine.csproj", "{04990536-063C-446B-8043-E3850617CF0A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -68,6 +70,12 @@ Global
{FCC23B54-9A9E-4DA0-AB91-B4D5A739A9B6}.Release|Any CPU.Build.0 = Release|Any CPU
{FCC23B54-9A9E-4DA0-AB91-B4D5A739A9B6}.Test|Any CPU.ActiveCfg = Debug|Any CPU
{FCC23B54-9A9E-4DA0-AB91-B4D5A739A9B6}.Test|Any CPU.Build.0 = Debug|Any CPU
{04990536-063C-446B-8043-E3850617CF0A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{04990536-063C-446B-8043-E3850617CF0A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{04990536-063C-446B-8043-E3850617CF0A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{04990536-063C-446B-8043-E3850617CF0A}.Release|Any CPU.Build.0 = Release|Any CPU
{04990536-063C-446B-8043-E3850617CF0A}.Test|Any CPU.ActiveCfg = Debug|Any CPU
{04990536-063C-446B-8043-E3850617CF0A}.Test|Any CPU.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
5 changes: 3 additions & 2 deletions .ci/unit-tests/Base_Engine_Tests/Base_Engine_Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<FileVersion>8.0.0.0</FileVersion>
Expand All @@ -22,14 +22,15 @@
<PackageReference Include="AutoBogus" Version="2.13.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<PackageReference Include="Shouldly" Version="4.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\BHoM_Engine\BHoM_Engine.csproj" />
<ProjectReference Include="..\TestHelper_Engine\TestHelper_Engine.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
157 changes: 157 additions & 0 deletions .ci/unit-tests/Base_Engine_Tests/Compute/TryRunExtensionMethod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2024, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using BH.oM.Structure.Elements;
using NUnit.Framework;

namespace BH.Tests.Engine.Base.Compute
{
public class TryRunExtensionMethodTests
{
[Test]
public void TryRunExtensionMethod1()
{
Bar bar = new Bar();
double dbl = 123;
string expected = "BH.Engine.TestHelper.Compute.ExtensionMethodToCallHelper(BH.oM.Structure.Elements.Bar, System.Double, System.Double, System.Double)";
string result = BH.Engine.TestHelper.Compute.IExtensionMethodToCallHelper(bar, dbl, dbl, dbl);
Assert.AreEqual(result, expected);
}

[Test]
public void TryRunExtensionMethod2()
{
Bar bar = new Bar();
double dbl = 123;
string expected = "BH.Engine.TestHelper.Compute.ExtensionMethodToCallHelper(BH.oM.Structure.Elements.Bar, BH.oM.Structure.Elements.Bar, System.Double, System.Double)";
string result = BH.Engine.TestHelper.Compute.IExtensionMethodToCallHelper(bar, bar, dbl, dbl);
Assert.AreEqual(result, expected);
}

[Test]
public void TryRunExtensionMethod3()
{
Bar bar = new Bar();
double dbl = 123;
string expected = "BH.Engine.TestHelper.Compute.ExtensionMethodToCallHelper(BH.oM.Structure.Elements.Bar, System.Object, System.Object, System.Object)";
string result = BH.Engine.TestHelper.Compute.IExtensionMethodToCallHelper(bar, bar, bar, dbl);
Assert.AreEqual(result, expected);
}

[Test]
public void TryRunExtensionMethod4()
{
Bar bar = new Bar();
string expected = "BH.Engine.TestHelper.Compute.ExtensionMethodToCallHelper(BH.oM.Structure.Elements.Bar, System.Object, System.Object, System.Object)";
string result = BH.Engine.TestHelper.Compute.IExtensionMethodToCallHelper(bar, bar, bar, bar);
Assert.AreEqual(result, expected);
}

[Test]
public void TryRunExtensionMethod5()
{
Bar bar = new Bar();
Panel panel = new Panel();
double dbl = 123;
string expected = "BH.Engine.TestHelper.Compute.ExtensionMethodToCallHelper(BH.oM.Structure.Elements.Bar, System.Object, BH.oM.Structure.Elements.Panel, System.Object)";
string result = BH.Engine.TestHelper.Compute.IExtensionMethodToCallHelper(bar, bar, panel, dbl);
Assert.AreEqual(result, expected);
}

[Test]
public void TryRunExtensionMethod6()
{
Panel panel = new Panel();
double dbl = 123;
string expected = "BH.Engine.TestHelper.Compute.ExtensionMethodToCallHelper(BH.oM.Dimensional.IElement2D, System.Double, System.Double, System.Double)";
string result = BH.Engine.TestHelper.Compute.IExtensionMethodToCallHelper(panel, dbl, dbl, dbl);
Assert.AreEqual(result, expected);
}

[Test]
public void TryRunExtensionMethod7()
{
Bar bar = new Bar();
Panel panel = new Panel();
string expected = "BH.Engine.TestHelper.Compute.ExtensionMethodToCallHelper(BH.oM.Structure.Elements.Bar, System.Object, BH.oM.Structure.Elements.Panel, System.Object)";
string result = BH.Engine.TestHelper.Compute.IExtensionMethodToCallHelper(bar, null, panel, null);
Assert.AreEqual(result, expected);
}

[Test]
public void TryRunExtensionMethod8()
{
Panel panel = new Panel();
double dbl = 123;
string expected = null;
string result = BH.Engine.TestHelper.Compute.IExtensionMethodToCallHelper(panel, panel, panel, dbl);
Assert.AreEqual(result, expected);
}

[Test]
public void TryRunExtensionMethod9()
{
Bar bar = new Bar();
double dbl = 123;
string expected = null;
string result = BH.Engine.TestHelper.Compute.IExtensionMethodToCallHelper(bar, dbl, null, null);
Assert.AreEqual(result, expected);
}

[Test]
public void TryRunExtensionMethod10()
{
Bar bar = new Bar();
string expected = null;
string result = BH.Engine.TestHelper.Compute.IExtensionMethodToCallHelper(bar, null, null, bar);
Assert.AreEqual(result, expected);
}

[Test]
public void TryRunExtensionMethod11()
{
Bar bar = new Bar();
string expected = null;
string result = BH.Engine.TestHelper.Compute.IExtensionMethodToCallHelper(bar, null, null, null);
Assert.AreEqual(result, expected);
}

[Test]
public void TryRunExtensionMethod12()
{
Bar bar = new Bar();
double dbl = 123;
string expected = null;
string result = BH.Engine.TestHelper.Compute.IExtensionMethodToCallHelper(null, dbl, dbl, dbl);
Assert.AreEqual(result, expected);
}

[Test]
public void TryRunExtensionMethod13()
{
Bar bar = new Bar();
string expected = null;
string result = BH.Engine.TestHelper.Compute.IExtensionMethodToCallHelper(null, null, null, null);
Assert.AreEqual(result, expected);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<FileVersion>8.0.0.0</FileVersion>
Expand All @@ -22,7 +22,7 @@
<PackageReference Include="AutoBogus" Version="2.13.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.3.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.5.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<PackageReference Include="Shouldly" Version="4.1.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using BH.Engine.Versioning;
using BH.oM.Dimensional;
using BH.oM.Structure.Elements;
using System.Reflection;

namespace BH.Engine.TestHelper
{
public static partial class Compute
{
public static string ExtensionMethodToCallHelper(this Bar a, double b, double c, double d)
{
return MethodBase.GetCurrentMethod().VersioningKey();
}

public static string ExtensionMethodToCallHelper(this Bar a, Bar b, double c, double d)
{
return MethodBase.GetCurrentMethod().VersioningKey();
}

public static string ExtensionMethodToCallHelper(this Bar a, object b, double c, double d)
{
return MethodBase.GetCurrentMethod().VersioningKey();
}

public static string ExtensionMethodToCallHelper(this Bar a, object b, object c, object d)
{
return MethodBase.GetCurrentMethod().VersioningKey();
}

public static string ExtensionMethodToCallHelper(this Bar a, object b, Panel c, object d)
{
return MethodBase.GetCurrentMethod().VersioningKey();
}

public static string ExtensionMethodToCallHelper(this IElement2D a, double b, double c, double d)
{
return MethodBase.GetCurrentMethod().VersioningKey();
}

public static string ExtensionMethodToCallHelper()
{
return MethodBase.GetCurrentMethod().VersioningKey();
}

public static string IExtensionMethodToCallHelper(this object a, object b, object c, object d)
{
object result;
if (!BH.Engine.Base.Compute.TryRunExtensionMethod(a, nameof(ExtensionMethodToCallHelper), new object[] { b, c, d }, out result))
{
BH.Engine.Base.Compute.RecordError("Extension method not found.");
return null;
}
else
return (string)result;
}
}
}
67 changes: 67 additions & 0 deletions .ci/unit-tests/TestHelper_Engine/TestHelper_Engine.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<Description>https://github.com/BHoM/BHoM_Engine</Description>
<Version>5.0.0</Version>
<Authors>BHoM</Authors>
<Copyright>Copyright © https://github.com/BHoM</Copyright>
<RootNamespace>BH.Engine.TestHelper</RootNamespace>
<FileVersion>8.0.0.0</FileVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath>..\Build\</OutputPath>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\BHoM_Engine\BHoM_Engine.csproj" />
<ProjectReference Include="..\..\..\Spatial_Engine\Spatial_Engine.csproj" />
<ProjectReference Include="..\..\..\Structure_Engine\Structure_Engine.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="Analytical_oM">
<HintPath>$(ProgramData)\BHoM\Assemblies\Analytical_oM.dll</HintPath>
<Private>false</Private>
<SpecificVersion>false</SpecificVersion>
</Reference>
<Reference Include="BHoM">
<HintPath>$(ProgramData)\BHoM\Assemblies\BHoM.dll</HintPath>
<Private>false</Private>
<SpecificVersion>false</SpecificVersion>
</Reference>
<Reference Include="Data_oM">
<HintPath>$(ProgramData)\BHoM\Assemblies\Data_oM.dll</HintPath>
<Private>false</Private>
<SpecificVersion>false</SpecificVersion>
</Reference>
<Reference Include="Dimensional_oM">
<HintPath>$(ProgramData)\BHoM\Assemblies\Dimensional_oM.dll</HintPath>
<Private>false</Private>
<SpecificVersion>false</SpecificVersion>
</Reference>
<Reference Include="Geometry_oM">
<HintPath>$(ProgramData)\BHoM\Assemblies\Geometry_oM.dll</HintPath>
<Private>false</Private>
<SpecificVersion>false</SpecificVersion>
</Reference>
<Reference Include="Matter_oM">
<HintPath>$(ProgramData)\BHoM\Assemblies\Matter_oM.dll</HintPath>
<Private>false</Private>
<SpecificVersion>false</SpecificVersion>
</Reference>
<Reference Include="Quantities_oM">
<HintPath>$(ProgramData)\BHoM\Assemblies\Quantities_oM.dll</HintPath>
<Private>false</Private>
<SpecificVersion>false</SpecificVersion>
</Reference>
<Reference Include="Spatial_oM">
<HintPath>$(ProgramData)\BHoM\Assemblies\Spatial_oM.dll</HintPath>
<Private>false</Private>
<SpecificVersion>false</SpecificVersion>
</Reference>
<Reference Include="Structure_oM">
<HintPath>$(ProgramData)\BHoM\Assemblies\Structure_oM.dll</HintPath>
<Private>false</Private>
<SpecificVersion>false</SpecificVersion>
</Reference>
</ItemGroup>
</Project>
Loading