-
-
Notifications
You must be signed in to change notification settings - Fork 55
Using resolvers for all behavior #125
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
bd2205d
WIP
Keboo 915fe33
Cleaning up Array population
Keboo cbb44cc
All test are passing
Keboo 4275c06
Cleaning up code
Keboo a03a706
Work on improving code coverage numbers
Keboo f222bdf
More code coverage
Keboo 4cb8c23
Adding more verify tests
Keboo 097a2e6
More verify tests
Keboo 53f74df
Finish verify tests
Keboo 11cb639
More tests
Keboo 169b452
Replacing tab with spaces
Keboo c0d7e16
Removing commented code
Keboo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Linq; | ||
| using System.Reflection; | ||
|
|
||
| /// <summary> | ||
| /// Based on https://gist.github.com/Keboo/d2f7f470d66fc4ff7d978ccb107b07cf | ||
| /// </summary> | ||
| namespace Moq.AutoMock.Tests | ||
| { | ||
| public interface IConstructorTest | ||
| { | ||
| void Run(); | ||
| } | ||
|
|
||
| [ExcludeFromCodeCoverage] | ||
| public static class ConstructorTest | ||
| { | ||
| private class Test : IConstructorTest | ||
| { | ||
| public Dictionary<Type, object?> SpecifiedValues { get; } | ||
| = new Dictionary<Type, object?>(); | ||
|
|
||
| private Type TargetType { get; } | ||
|
|
||
| public Test(Type targetType) | ||
| { | ||
| TargetType = targetType; | ||
| } | ||
|
|
||
| public Test(Test original) | ||
| { | ||
| TargetType = original.TargetType; | ||
| foreach (KeyValuePair<Type, object?> kvp in original.SpecifiedValues) | ||
| { | ||
| SpecifiedValues[kvp.Key] = kvp.Value; | ||
| } | ||
| } | ||
|
|
||
| public void Run() | ||
| { | ||
| foreach (ConstructorInfo constructor in TargetType.GetConstructors()) | ||
| { | ||
| ParameterInfo[] parameters = constructor.GetParameters(); | ||
|
|
||
| object?[] parameterValues = parameters | ||
| .Select(p => p.ParameterType) | ||
| .Select(t => | ||
| { | ||
| if (SpecifiedValues.TryGetValue(t, out object? value)) | ||
| { | ||
| return value; | ||
| } | ||
| Mock mock = (Mock)Activator.CreateInstance(typeof(Mock<>).MakeGenericType(t))!; | ||
| return mock.Object; | ||
| }) | ||
| .ToArray(); | ||
|
|
||
| for (int i = 0; i < parameters.Length; i++) | ||
| { | ||
| object?[] values = parameterValues.ToArray(); | ||
| values[i] = null; | ||
|
|
||
| if (parameters[i].HasDefaultValue && parameters[i].DefaultValue is null) | ||
| { | ||
| //NB: no exception thrown | ||
| constructor.Invoke(values); | ||
| } | ||
| else | ||
| { | ||
| string parameterDisplay = $"'{parameters[i].Name}' ({parameters[i].ParameterType.Name})"; | ||
| TargetInvocationException ex = Assert.ThrowsException<TargetInvocationException>(new Action(() => | ||
| { | ||
| object? rv = constructor.Invoke(values); | ||
| throw new Exception($"Expected {nameof(ArgumentNullException)} for null parameter {parameterDisplay} but no exception was thrown"); | ||
| })); | ||
| if (ex.InnerException is ArgumentNullException argumentNullException) | ||
| { | ||
| Assert.AreEqual(parameters[i].Name, argumentNullException.ParamName); | ||
| } | ||
| else | ||
| { | ||
| throw new Exception($"Thrown argument for {parameterDisplay} was '{ex.InnerException?.GetType().Name}' not {nameof(ArgumentNullException)}.", ex.InnerException); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static IConstructorTest Use<T>(this IConstructorTest test, T value) | ||
| { | ||
| if (test is Test internalTest) | ||
| { | ||
| var newTest = new Test(internalTest); | ||
| newTest.SpecifiedValues.Add(typeof(T), value); | ||
| return newTest; | ||
| } | ||
| else | ||
| { | ||
| throw new ArgumentException("Argument not expected type", nameof(test)); | ||
| } | ||
| } | ||
|
|
||
| public static IConstructorTest BuildArgumentNullExceptionsTest<T>() | ||
| => new Test(typeof(T)); | ||
|
|
||
| public static void AssertArgumentNullExceptions<T>() | ||
| => BuildArgumentNullExceptionsTest<T>().Run(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.