-
-
Notifications
You must be signed in to change notification settings - Fork 0
fix: support params and optional parameters
#520
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -169,14 +169,25 @@ namespace Mockolate; | |||||||||||||||||||||||||||||||||||||||||||||||
| .Select(constructor => constructor.Parameters)) | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| constructorIndex++; | ||||||||||||||||||||||||||||||||||||||||||||||||
| sb.Append("\t\t\t\telse if (constructorParameters.Parameters.Length == ") | ||||||||||||||||||||||||||||||||||||||||||||||||
| .Append(constructorParameters.Count); | ||||||||||||||||||||||||||||||||||||||||||||||||
| int requiredParameters = constructorParameters.Count(c => !c.HasExplicitDefaultValue); | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (requiredParameters < constructorParameters.Count) | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| sb.Append("\t\t\t\telse if (constructorParameters.Parameters.Length >= ") | ||||||||||||||||||||||||||||||||||||||||||||||||
| .Append(requiredParameters); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| sb.Append("\t\t\t\telse if (constructorParameters.Parameters.Length == ") | ||||||||||||||||||||||||||||||||||||||||||||||||
| .Append(constructorParameters.Count); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| int constructorParameterIndex = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
| foreach (MethodParameter parameter in constructorParameters) | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| sb.AppendLine().Append("\t\t\t\t && TryCast(constructorParameters.Parameters[") | ||||||||||||||||||||||||||||||||||||||||||||||||
| sb.AppendLine().Append("\t\t\t\t && TryCast(constructorParameters.Parameters, ") | ||||||||||||||||||||||||||||||||||||||||||||||||
| .Append(constructorParameterIndex++) | ||||||||||||||||||||||||||||||||||||||||||||||||
| .Append("], mockBehavior, out ").Append(parameter.Type.Fullname).Append(" c") | ||||||||||||||||||||||||||||||||||||||||||||||||
| .Append(parameter.HasExplicitDefaultValue ? $", {parameter.ExplicitDefaultValue}" : "") | ||||||||||||||||||||||||||||||||||||||||||||||||
| .Append(", mockBehavior, out ").Append(parameter.Type.Fullname).Append(" c") | ||||||||||||||||||||||||||||||||||||||||||||||||
| .Append(constructorIndex) | ||||||||||||||||||||||||||||||||||||||||||||||||
| .Append('p') | ||||||||||||||||||||||||||||||||||||||||||||||||
| .Append(constructorParameterIndex).Append(")"); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -201,13 +212,13 @@ namespace Mockolate; | |||||||||||||||||||||||||||||||||||||||||||||||
| sb.Append("\t\t\t\t\t\t\tsetup.Invoke(setupTarget);").AppendLine(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| sb.Append("\t\t\t\t\t\t}").AppendLine(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| sb.Append("\t\t\t\t\t}").AppendLine(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| sb.Append("\t\t\t\t\t_value = new global::Mockolate.Generated.MockFor").Append(mock.Name).Append("("); | ||||||||||||||||||||||||||||||||||||||||||||||||
| sb.Append("\t\t\t\t\t_value = new global::Mockolate.Generated.MockFor").Append(mock.Name).Append("(mockRegistration"); | ||||||||||||||||||||||||||||||||||||||||||||||||
| for (int i = 1; i <= constructorParameters.Count; i++) | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| sb.Append('c').Append(constructorIndex).Append('p').Append(i).Append(", "); | ||||||||||||||||||||||||||||||||||||||||||||||||
| sb.Append(", ").Append('c').Append(constructorIndex).Append('p').Append(i); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| sb.Append("mockRegistration);").AppendLine(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| sb.Append(");").AppendLine(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| sb.Append("\t\t\t\t}").AppendLine(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -284,8 +295,9 @@ namespace Mockolate; | |||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| sb.Append(""" | ||||||||||||||||||||||||||||||||||||||||||||||||
| private static bool TryCast<TValue>(object? value, global::Mockolate.MockBehavior behavior, out TValue result) | ||||||||||||||||||||||||||||||||||||||||||||||||
| private static bool TryCast<TValue>(object?[] values, int index, global::Mockolate.MockBehavior behavior, out TValue result) | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| var value = values[index]; | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (value is TValue typedValue) | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| result = typedValue; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -295,6 +307,17 @@ private static bool TryCast<TValue>(object? value, global::Mockolate.MockBehavio | |||||||||||||||||||||||||||||||||||||||||||||||
| result = default!; | ||||||||||||||||||||||||||||||||||||||||||||||||
| return value is null; | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| private static bool TryCast<TValue>(object?[] values, int index, TValue defaultValue, global::Mockolate.MockBehavior behavior, out TValue result) | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (values.Length > index && values[index] is TValue typedValue) | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| result = typedValue; | ||||||||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| result = defaultValue; | ||||||||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
vbreuss marked this conversation as resolved.
Comment on lines
+313
to
+320
|
||||||||||||||||||||||||||||||||||||||||||||||||
| if (values.Length > index && values[index] is TValue typedValue) | |
| { | |
| result = typedValue; | |
| return true; | |
| } | |
| result = defaultValue; | |
| return true; | |
| if (values.Length <= index) | |
| { | |
| result = defaultValue; | |
| return true; | |
| } | |
| var value = values[index]; | |
| if (value is TValue typedValue) | |
| { | |
| result = typedValue; | |
| return true; | |
| } | |
| result = default!; | |
| return value is null; |
Uh oh!
There was an error while loading. Please reload this page.