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

Adding Caller for generating unit test #356

Merged
merged 5 commits into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion BHoM_UI/BHoM_UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@
<Reference Include="System.Xml">
<Private>False</Private>
</Reference>
<Reference Include="Test_oM">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\ProgramData\BHoM\Assemblies\Test_oM.dll</HintPath>
</Reference>
<Reference Include="Versioning_Engine">
<HintPath>C:\ProgramData\BHoM\Assemblies\Versioning_Engine.dll</HintPath>
<Private>False</Private>
Expand Down Expand Up @@ -185,6 +189,7 @@
<Compile Include="Components\oM\CreateObject.cs" />
<Compile Include="Components\oM\CreateDictionary.cs" />
<Compile Include="Components\UI\RunCode.cs" />
<Compile Include="Components\UI\UnitTest.cs" />
<Compile Include="Global\Initialisation.cs" />
<Compile Include="Global\GlobalSearch.cs" />
<Compile Include="Global\SearchMenu.cs" />
Expand Down Expand Up @@ -247,6 +252,7 @@
<Content Include="Properties\Resources\RunCode.png" />
<Content Include="Properties\Resources\ToJson.png" />
<Content Include="Properties\Resources\Type.png" />
<None Include="Properties\Resources\UnitTest.png" />
<Content Include="Properties\Resources\UpdateProperty.png" />
<Content Include="Properties\Resources\VBH_Script.png" />
<Content Include="Properties\Resources\VS_Script.png" />
Expand Down Expand Up @@ -286,4 +292,4 @@ xcopy "$(TargetDir)ICSharpCode.AvalonEdit.dll" "C:\ProgramData\BHoM\Assemblies"
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
138 changes: 138 additions & 0 deletions BHoM_UI/Components/UI/UnitTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2021, 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.CodeDom.Compiler;
using BH.Engine.Reflection;
using System.Windows.Forms;
using System.Windows.Controls;
using BH.oM.Test.UnitTests;
using BH.oM.UI;
using BH.oM.Reflection.Interface;

namespace BH.UI.Base.Components
{
public class UnitTestCaller : Caller
{
/*************************************/
/**** Properties ****/
/*************************************/

public override System.Drawing.Bitmap Icon_24x24 { get; protected set; } = Properties.Resources.UnitTest;

public override Guid Id { get; protected set; } = new Guid("dd7ad577-8e27-42e0-ad72-f70e87fc7492");

public override string Name { get; protected set; } = "UnitTest";

public override string Category { get; protected set; } = "UI";

public override string Description { get; protected set; } = "Creates unit tests based on the inputs from the selected method.";


/*************************************/
/**** Constructors ****/
/*************************************/

public UnitTestCaller() : base()
{
SetPossibleItems(Engine.UI.Query.EngineItems());
}


/*************************************/
/**** Override Methods ****/
/*************************************/

protected override void SetOutputs(MethodBase method)
{
if (method == null)
OutputParams = new List<ParamInfo>();

OutputParams = new List<ParamInfo>();
OutputParams.Add(new ParamInfo
{
Name = "UnitTest",
DataType = typeof(UnitTest),
Description = "UnitTest for the method " + method.Name +".",
Kind = ParamKind.Output,
IsRequired = true
});
}

/*************************************/

protected override void SetComponentDetails(MethodBase method)
{
if (method == null)
return;

// Set component name
if (method is MethodInfo)
Name = "UT:" + method.Name;
else if (method is ConstructorInfo)
Name = "UT:" + method.DeclaringType.Name;
else
Name = "UT:UnknownMethod";

// Set description
Description = "Creates a UnitTest for the method " + method.Name + ".";
}

/*************************************/

public override object Run(List<object> inputs)
{
object returnValue = base.Run(inputs);

if (m_CompiledFunc != null)
return new UnitTest() { Method = m_OriginalItem as MethodBase, Data = new List<TestData>() { new TestData(inputs, SeparateOutputs(returnValue)) } };
else
return null;
}

/*************************************/

private List<object> SeparateOutputs(object returnObject)
{
List<object> returnObjects = new List<object>();
IOutput output = returnObject as IOutput;

if (output == null)
returnObjects.Add(returnObject);
else
{
for (int i = 0; i < output.OutputCount(); i++)
{
returnObjects.Add(output.IItem(i));
}
}

return returnObjects;
}
}
}

12 changes: 12 additions & 0 deletions BHoM_UI/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions BHoM_UI/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,7 @@
<data name="RunCode" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>resources\runcode.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="UnitTest" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>Resources\UnitTest.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
</root>
Binary file added BHoM_UI/Properties/Resources/UnitTest.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.