-
Notifications
You must be signed in to change notification settings - Fork 20
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
Review Design/Implementation Handling Ref-Like Types #170
Comments
Related issue: the generated delegates used the hash code of the method's signature to differentiate methods that have the same name but different signatures. This changes every time a test is run that generates these delegates. This doesn't break the mock itself, but the code diffs in the test will fail every time as the number changes. May be worth to see if there is a way to create a name that is unique for that delegate that will generate a consistent, repeatable name. |
One thing that may be coming in the future is the ability to pass ref structs to generics. Details can be found here. It's unclear if it'll show up in C# 13, or 14, or....never (?), but it was mentioned in recent C# design notes. If this would go through, it may make this relatively trivial, but....in the meantime, having specialized types for |
Did some experimental work, seems like I can create some core types for public delegate Span<T> SpanReturnValue<T>();
public delegate bool SpanArgumentEvalution<T>(Span<T> @value);
[Serializable]
public sealed class SpanArgument<T>
: Argument
{
private readonly SpanArgumentEvalution<T>? evaluation;
private readonly ValidationState validation;
internal SpanArgument() => this.validation = ValidationState.None;
internal SpanArgument(SpanArgumentEvalution<T> @evaluation)
{
this.evaluation = @evaluation;
this.validation = ValidationState.Evaluation;
}
public bool IsValid(Span<T> @value) =>
this.validation switch
{
ValidationState.None => true,
ValidationState.Evaluation => this.evaluation!(@value),
_ => throw new global::System.NotSupportedException("Invalid validation state."),
};
}
public delegate ReadOnlySpan<T> ReadOnlySpanReturnValue<T>();
public delegate bool ReadOnlySpanArgumentEvalution<T>(ReadOnlySpan<T> @value);
[Serializable]
public sealed class ReadOnlySpanArgument<T>
: Argument
{
private readonly ReadOnlySpanArgumentEvalution<T>? evaluation;
private readonly ValidationState validation;
internal ReadOnlySpanArgument() => this.validation = ValidationState.None;
internal ReadOnlySpanArgument(ReadOnlySpanArgumentEvalution<T> @evaluation)
{
this.evaluation = @evaluation;
this.validation = ValidationState.Evaluation;
}
public bool IsValid(Span<T> @value) =>
this.validation switch
{
ValidationState.None => true,
ValidationState.Evaluation => this.evaluation!(@value),
_ => throw new global::System.NotSupportedException("Invalid validation state."),
};
} Also, need to remind myself to write a test with a ref struct that isn't one of the span types. |
FWIW it looks like var myStructArg = new RefStructArgument<MyStruct>();
myStructArg.IsValid(new MyStruct());
var spanIntArg = new RefStructArgument<Span<int>>();
spanIntArg.IsValid([1, 2, 3]);
public ref struct MyStruct { }
public delegate T RefStructReturnValue<T>()
where T : allows ref struct;
public delegate bool RefStructArgumentEvaluation<T>(T @value)
where T : allows ref struct;
[Serializable]
public sealed class RefStructArgument<T>
: Argument
where T : allows ref struct
{
private readonly RefStructArgumentEvaluation<T>? evaluation;
private readonly ValidationState validation;
internal RefStructArgument() => this.validation = ValidationState.None;
internal RefStructArgument(RefStructArgumentEvaluation<T> @evaluation)
{
this.evaluation = @evaluation;
this.validation = ValidationState.Evaluation;
}
public bool IsValid(T @value) =>
this.validation switch
{
ValidationState.None => true,
ValidationState.Evaluation => this.evaluation!(@value),
_ => throw new global::System.NotSupportedException("Invalid validation state."),
};
} Will definitely need to experiment with this first to ensure this could work before committing to this. Probably look at the gen-d code that handles a |
Actually....what I just realized is that all I need to do is add |
Remember to add a test in |
With #167, I thought I addressed ref-like types, and....the tests in
RefStructTests
show that things do work. But it feels inconsistent. Some tests require the return value to be returned through a custom delegate. Others don't do this.I need to figure out what truly needs to be done with ref-like types in mocks and ensure things are consistent and correct.
The text was updated successfully, but these errors were encountered: