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

[cDAC] Draft CodeVersionsTest refactor #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

max-charlamb
Copy link
Owner

No description provided.

private IReadOnlyCollection<MockCodeBlockStart> _codeBlocks;
private Mock<IRuntimeTypeSystem> mockRuntimeTypeSystem = new(MockBehavior.Strict);
private Mock<IExecutionManager> mockExecutionManager = new(MockBehavior.Strict);
private Mock<ILoader> mockLoader = new(MockBehavior.Strict);
Copy link
Owner Author

Choose a reason for hiding this comment

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

MockBehavior.Strict isn't required, but I wanted to make sure I was capturing all the calls.

ulong addressLowBits = (ulong)address & ((ulong)_target.PointerSize - 1);

// this is not quite accurate on 32 bit architectures, but it's good enough for testing
ulong addressLowBits = (ulong)address & 7;
Copy link
Owner Author

Choose a reason for hiding this comment

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

Should be (ulong)address & ((ulong)_target.PointerSize - 1);, however when we set this up the target isn't created. I guess we could create the target first and pass it in.

Choose a reason for hiding this comment

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

You should be able to get the pointer size off of the TargetTestHelpers for MockDescriptor.CodeVersions, which we create before doing this.

@@ -276,6 +157,7 @@ internal static Target CreateTarget(
[ClassData(typeof(MockTarget.StdArch))]
public void GetNativeCodeVersion_Null(MockTarget.Architecture arch)
{
mockExecutionManager.Setup(e => e.GetCodeBlockHandle(TargetCodePointer.Null)).Returns(() => null);
Copy link
Owner Author

Choose a reason for hiding this comment

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

Only required with MockBehavior.Strict. Otherwise this test relies on the mock returning null when there is no setup. I find this a bit confusing.


TargetCodePointer IExecutionManager.GetStartAddress(CodeBlockHandle codeInfoHandle) => FindCodeBlock(codeInfoHandle.Address)?.StartAddress ?? TargetCodePointer.Null;
TargetPointer IExecutionManager.GetMethodDesc(CodeBlockHandle codeInfoHandle) => FindCodeBlock(codeInfoHandle.Address)?.MethodDescAddress ?? TargetPointer.Null;
private void AddCodeBlock(MockCodeBlockStart block)

Choose a reason for hiding this comment

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

Do you think MockCodeBlockStart and friends are still helpful? Versus having these method just take in the address, length, method desc, etc. as separate parameters.

Copy link
Owner Author

Choose a reason for hiding this comment

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

I think they are still helpful to encapsulate state related to a mock. I think if we removed them, it would make tests with multiple MethodDescs confusing due to keeping track of multiple similar fields.

internal class MockExecutionManager : IExecutionManager
{
private IReadOnlyCollection<MockCodeBlockStart> _codeBlocks;
private Mock<IRuntimeTypeSystem> mockRuntimeTypeSystem = new(MockBehavior.Strict);

Choose a reason for hiding this comment

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

I tend to find it easier to reason about tests when everything is contained in the test method (explicitly sets up what it needs / no state outside the method, don't have to know that each test method actually gets a new test class instance).

Maybe try something like having the test cases explicitly create the mock(s) they need and turn the Add* methods into extension methods for Mock<...>?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Happy to make this change. I settled on having a MockExtensions class which adds the helpers as extensions on the correct generic implementation of Mock<...>.

@max-charlamb max-charlamb changed the base branch from md-gccover to main December 16, 2024 17:48
@max-charlamb max-charlamb changed the base branch from main to md-dynamic December 16, 2024 17:49
@max-charlamb max-charlamb changed the base branch from md-dynamic to main December 16, 2024 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants