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

Conversation

IsakNaslundBh
Copy link
Contributor

@IsakNaslundBh IsakNaslundBh commented Jan 15, 2021

Issues addressed by this PR

Closes #355

Adding a complete new component that can be used to generate a unit test.

The component lets you pick the method you want, and then transforms into giving the inputs corresponding to that method. Instead of just running the method, though, the component returns a UnitTest object with the method from the component as well as the linked input-output data.

The component is currently returning one UnitTest object per run of the method, which then you should be able to convert into one simply after BHoM/Test_Toolkit#316 has been closed out.

The menu for the method will need some work, but that is quite deep into the code and used by a lot of other things (not something that is simply overrideable as far as I can see @adecler ), so all methods that are not create methods can be found under "NonBHoMObjects".

UnitTest

Have not made an icon yet (and do not have time right now) but wanted to get this pushed for initial feedback and review.

Test files

Changelog

Additional comments

Requires BHoM/Grasshopper_UI#590 to be run in GH. I have not added support in any other UI right now (although I think it should work in them as well with similar addition)

@FraserGreenroyd
Copy link
Contributor

This is timely @IsakNaslundBh as @JohannaOlin1 and I are preparing some Unit Tests for some Environment Engine work. We've started looking into this PR today, will continue to do so early next week and provide detailed feedback prior to merging 😄

Copy link
Member

@adecler adecler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment below plus missing icon.

Otherwise, looks good to me.

Comment on lines 105 to 141
public override object Run(List<object> inputs)
{
if (m_CompiledFunc != null)
{
object returnValue = null;
MethodInfo method = m_OriginalItem as MethodInfo;
try
{
returnValue = m_CompiledFunc(inputs.ToArray());
}
catch (InvalidCastException e)
{
MethodInfo originalMethod = m_OriginalItem as MethodInfo;
if (originalMethod != null && originalMethod.IsGenericMethod)
{
// Try to update the generic method to fit the input types
method = Engine.Reflection.Compute.MakeGenericFromInputs(originalMethod, inputs.Select(x => x.GetType()).ToList());
m_CompiledFunc = method.ToFunc();
returnValue = m_CompiledFunc(inputs.ToArray());
}
else
throw e;
}

return new UnitTest() { Method = method, Data = new List<TestData>() { new TestData(inputs, SeparateOutputs(returnValue)) } };
}
else if (InputParams.Count <= 0)
{
BH.Engine.Reflection.Compute.RecordWarning("This is a magic component. Right click on it and <Select a method>");
return null;
}
else
{
BH.Engine.Reflection.Compute.RecordError("The component is not linked to a method.");
return null;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As requested previously, call the base method instead of just copy pasting the whole thing. This creates duplicates in the code that make it difficult to maintain.

Here's an example of what you could do instead:

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

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed simpler. That case does not capture the case with generic methods though, but maybe that is something to be resolved at the run side of the unit test rather than here.

Will update.

adecler
adecler previously approved these changes Jan 19, 2021
@IsakNaslundBh
Copy link
Contributor Author

@BHoMBot check code copyright

@bhombot-ci
Copy link

bhombot-ci bot commented Jan 19, 2021

@IsakNaslundBh to confirm, check-code-compliance task is now queued.

@IsakNaslundBh
Copy link
Contributor Author

@BHoMBot check installer

@bhombot-ci
Copy link

bhombot-ci bot commented Jan 19, 2021

@IsakNaslundBh to confirm, check-installer task is now queued.

@IsakNaslundBh
Copy link
Contributor Author

@BHoMBot check copyright

@bhombot-ci
Copy link

bhombot-ci bot commented Jan 19, 2021

@IsakNaslundBh to confirm, check-copyright-compliance task is now queued.

JohannaOlin1
JohannaOlin1 previously approved these changes Jan 19, 2021
Copy link
Contributor

@JohannaOlin1 JohannaOlin1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worked for me when added unit test for MapRegions

Copy link
Contributor

@FraserGreenroyd FraserGreenroyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Icon looks good to me @JohannaOlin1 - see screenshot of it rendered on my monitor below.

@IsakNaslundBh see compliance fix needed - hopefully an easy commit? 😉

image

BHoM_UI/BHoM_UI.csproj Outdated Show resolved Hide resolved
@FraserGreenroyd FraserGreenroyd dismissed stale reviews from JohannaOlin1 via c95b9c6 January 19, 2021 17:55
Copy link
Contributor

@FraserGreenroyd FraserGreenroyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IsakNaslundBh noticed you left so took liberty of committing fix to allow @adecler a final review and merge if he's happy 😄 but this LGTM in testing with the other PR!

@FraserGreenroyd
Copy link
Contributor

@BHoMBot check installer

@bhombot-ci
Copy link

bhombot-ci bot commented Jan 19, 2021

@FraserGreenroyd to confirm, check-installer task is now queued.

@FraserGreenroyd
Copy link
Contributor

@BHoMBot check copyright compliance

@bhombot-ci
Copy link

bhombot-ci bot commented Jan 19, 2021

@FraserGreenroyd to confirm, check-copyright-compliance task is now queued.

@adecler
Copy link
Member

adecler commented Jan 20, 2021

The icon colour was not correct (notice colour coding for each section of icon) so fixed it myself.
I took the liberty to do a minor iteration on it while I was at it.

image

@adecler
Copy link
Member

adecler commented Jan 20, 2021

/azp run BHoM_UI.CheckCore

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@adecler
Copy link
Member

adecler commented Jan 20, 2021

@BHoMBot check copyright

@bhombot-ci
Copy link

bhombot-ci bot commented Jan 20, 2021

@adecler to confirm, check-copyright-compliance task is now queued.

@adecler
Copy link
Member

adecler commented Jan 20, 2021

@BHoMBot check installer

@bhombot-ci
Copy link

bhombot-ci bot commented Jan 20, 2021

@adecler to confirm, check-installer task is now queued.

@IsakNaslundBh
Copy link
Contributor Author

Like the Icon! Thanks @JohannaOlin1 and @adecler !

Tested this quickly again, and works fine for me! Can not approve the latest tweaks, being the author, but I am happy!

@adecler adecler merged commit f65e0dd into master Jan 20, 2021
@adecler adecler deleted the BHoM_UI-#355-AddCallerToGenerateUnitTest branch January 20, 2021 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature New capability or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BHoM_UI: Add method to generate a UnitTest from a component
4 participants