Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Both tests don't work. When a struct is turned into a class, they start to work
using System;
using CodeCop.Core;
using CodeCop.Core.Fluent;
using NUnit.Framework;
namespace CodeCop.Tests
{
public class CodeCopStructBugTests
{
public unsafe struct WithPtr
{
private readonly long* _ptr;
public WithPtr(long* ptr)
{
_ptr = ptr;
}
public long Read()
{
return *_ptr;
}
}
private long _return;
public CodeCopStructBugTests()
{
Cop.AsFluent();
typeof(WithPtr).GetMethod("Read").Override(c =>
{
// is this a problem? How is Sender passed for structs? Is it captured elsewhere, outside of the stack?
Console.WriteLine(c.Sender);
return _return;
});
Cop.Intercept();
}
[Test]
public unsafe void When_intercepting_a_struct_with_invalid_pointer()
{
const long @return = 43509847534;
_return = @return;
var withPtr = new WithPtr((long*)new IntPtr(3));
Assert.AreEqual(@return, withPtr.Read());
}
[Test]
public unsafe void When_intercepting_a_struct_with_valid_pointer()
{
const long @return = 9873453;
long l = 0;
_return = @return;
var withPtr = new WithPtr(&l);
Assert.AreEqual(@return, withPtr.Read());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.