-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updating LangVersion to 10 Converted everything to file scoped namespaces Ran dotnet format to clean up the source Bumping GH Action to 6.0 SDK Test project to net6 an nuget updates
- Loading branch information
Showing
82 changed files
with
3,544 additions
and
3,259 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 |
---|---|---|
@@ -1,62 +1,61 @@ | ||
using Microsoft.CodeAnalysis; | ||
using System; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using System; | ||
using System.Linq; | ||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; | ||
using static Microsoft.CodeAnalysis.CSharp.SyntaxKind; | ||
|
||
namespace Generators | ||
namespace Generators; | ||
|
||
[Generator] | ||
public class CombineGenerator : ISourceGenerator | ||
{ | ||
[Generator] | ||
public class CombineGenerator : ISourceGenerator | ||
public void Initialize(GeneratorInitializationContext context) | ||
{ | ||
public void Initialize(GeneratorInitializationContext context) | ||
{ | ||
} | ||
} | ||
|
||
public void Execute(GeneratorExecutionContext context) | ||
{ | ||
var sourceCode = CompilationUnit() | ||
.WithMembers(SingletonList<MemberDeclarationSyntax>(NamespaceDeclaration(QualifiedName(IdentifierName("Moq"), IdentifierName("AutoMock"))) | ||
.WithMembers(SingletonList<MemberDeclarationSyntax>(ClassDeclaration("AutoMocker") | ||
.WithModifiers(TokenList(Token(PartialKeyword))) | ||
.WithMembers(List(Enumerable.Range(1, 10).Select(Combine))))))) | ||
.NormalizeWhitespace() | ||
.ToFullString(); | ||
public void Execute(GeneratorExecutionContext context) | ||
{ | ||
var sourceCode = CompilationUnit() | ||
.WithMembers(SingletonList<MemberDeclarationSyntax>(NamespaceDeclaration(QualifiedName(IdentifierName("Moq"), IdentifierName("AutoMock"))) | ||
.WithMembers(SingletonList<MemberDeclarationSyntax>(ClassDeclaration("AutoMocker") | ||
.WithModifiers(TokenList(Token(PartialKeyword))) | ||
.WithMembers(List(Enumerable.Range(1, 10).Select(Combine))))))) | ||
.NormalizeWhitespace() | ||
.ToFullString(); | ||
|
||
context.AddSource(nameof(CombineGenerator), sourceCode); | ||
} | ||
context.AddSource(nameof(CombineGenerator), sourceCode); | ||
} | ||
|
||
private MemberDeclarationSyntax Combine(int count) | ||
{ | ||
return MethodDeclaration(PredefinedType(Token(VoidKeyword)), "Combine") | ||
.WithModifiers(TokenList(Token(TriviaList(Trivia(Documentation)), PublicKeyword, TriviaList()))) | ||
.WithTypeParameterList(TypeParameterList(SeparatedList(Enumerable.Range(0, count + 1).Select(type)))) | ||
.WithExpressionBody(ArrowExpressionClause( | ||
InvocationExpression(IdentifierName("Combine")) | ||
.WithArgumentList(ArgumentList(SeparatedList(Enumerable.Range(0, count + 1).Select(argument)))))) | ||
.WithSemicolonToken(Token(SemicolonToken)) | ||
.WithTrailingTrivia(LineFeed); | ||
private MemberDeclarationSyntax Combine(int count) | ||
{ | ||
return MethodDeclaration(PredefinedType(Token(VoidKeyword)), "Combine") | ||
.WithModifiers(TokenList(Token(TriviaList(Trivia(Documentation)), PublicKeyword, TriviaList()))) | ||
.WithTypeParameterList(TypeParameterList(SeparatedList(Enumerable.Range(0, count + 1).Select(type)))) | ||
.WithExpressionBody(ArrowExpressionClause( | ||
InvocationExpression(IdentifierName("Combine")) | ||
.WithArgumentList(ArgumentList(SeparatedList(Enumerable.Range(0, count + 1).Select(argument)))))) | ||
.WithSemicolonToken(Token(SemicolonToken)) | ||
.WithTrailingTrivia(LineFeed); | ||
|
||
static string identifier(int index) => index is 0 ? "TService" : $"TAsWellAs{index}"; | ||
static TypeParameterSyntax type(int index) => TypeParameter(identifier(index)); | ||
static ArgumentSyntax argument(int index) => Argument(TypeOfExpression(IdentifierName(identifier(index)))); | ||
} | ||
static string identifier(int index) => index is 0 ? "TService" : $"TAsWellAs{index}"; | ||
static TypeParameterSyntax type(int index) => TypeParameter(identifier(index)); | ||
static ArgumentSyntax argument(int index) => Argument(TypeOfExpression(IdentifierName(identifier(index)))); | ||
} | ||
|
||
private DocumentationCommentTriviaSyntax Documentation { get; } = DocumentationComment( | ||
XmlText(" "), | ||
XmlSummaryElement( | ||
new[] | ||
{ | ||
private DocumentationCommentTriviaSyntax Documentation { get; } = DocumentationComment( | ||
XmlText(" "), | ||
XmlSummaryElement( | ||
new[] | ||
{ | ||
"Combines all given types so that they are mocked by the same", | ||
@"mock. Some IoC containers call this ""Forwarding"" one type to", | ||
"other interfaces. In the end, this just means that all given", | ||
"types will be implemnted by the same instance.", | ||
}.SelectMany(text => new[] { XmlNewLine(Environment.NewLine), XmlText($" {text}") }) | ||
.Concat(new[] { XmlNewLine(Environment.NewLine), XmlText(" ") }) | ||
.ToArray() | ||
), | ||
XmlText($"{Environment.NewLine} ")); | ||
} | ||
}.SelectMany(text => new[] { XmlNewLine(Environment.NewLine), XmlText($" {text}") }) | ||
.Concat(new[] { XmlNewLine(Environment.NewLine), XmlText(" ") }) | ||
.ToArray() | ||
), | ||
XmlText($"{Environment.NewLine} ")); | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,70 +1,69 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System; | ||
using System.Reflection; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Moq.AutoMock.Tests.Util; | ||
|
||
namespace Moq.AutoMock.Tests | ||
namespace Moq.AutoMock.Tests; | ||
|
||
[TestClass] | ||
public class ConstructorSelectorTests | ||
{ | ||
[TestClass] | ||
public class ConstructorSelectorTests | ||
{ | ||
private const BindingFlags DefaultBindingFlags = BindingFlags.Instance | BindingFlags.Public; | ||
private const BindingFlags DefaultBindingFlags = BindingFlags.Instance | BindingFlags.Public; | ||
|
||
[TestMethod] | ||
public void It_chooses_the_ctor_with_arguments() | ||
{ | ||
var ctor = typeof(WithDefaultAndSingleParameter).SelectCtor(Array.Empty<Type>(), DefaultBindingFlags); | ||
Assert.AreEqual(1, ctor.GetParameters().Length); | ||
} | ||
[TestMethod] | ||
public void It_chooses_the_ctor_with_arguments() | ||
{ | ||
var ctor = typeof(WithDefaultAndSingleParameter).SelectCtor(Array.Empty<Type>(), DefaultBindingFlags); | ||
Assert.AreEqual(1, ctor.GetParameters().Length); | ||
} | ||
|
||
[TestMethod] | ||
public void It_chooses_the_ctor_with_the_most_arguments() | ||
{ | ||
var ctor = typeof(With3Parameters).SelectCtor(Array.Empty<Type>(), DefaultBindingFlags); | ||
Assert.AreEqual(2, ctor.GetParameters().Length); | ||
} | ||
[TestMethod] | ||
public void It_chooses_the_ctor_with_the_most_arguments() | ||
{ | ||
var ctor = typeof(With3Parameters).SelectCtor(Array.Empty<Type>(), DefaultBindingFlags); | ||
Assert.AreEqual(2, ctor.GetParameters().Length); | ||
} | ||
|
||
[TestMethod] | ||
public void It_chooses_the_ctor_with_the_most_arguments_when_arguments_are_arrays() | ||
{ | ||
var ctor = typeof(WithArrayParameter).SelectCtor(Array.Empty<Type>(), DefaultBindingFlags); | ||
Assert.AreEqual(1, ctor.GetParameters().Length); | ||
} | ||
[TestMethod] | ||
public void It_chooses_the_ctor_with_the_most_arguments_when_arguments_are_arrays() | ||
{ | ||
var ctor = typeof(WithArrayParameter).SelectCtor(Array.Empty<Type>(), DefaultBindingFlags); | ||
Assert.AreEqual(1, ctor.GetParameters().Length); | ||
} | ||
|
||
[TestMethod] | ||
public void It_wont_select_if_an_argument_is_sealed_and_only_one_constructor() | ||
{ | ||
Assert.ThrowsException<ArgumentException>( | ||
() => typeof(WithSealedParameter2).SelectCtor(Array.Empty<Type>(), DefaultBindingFlags)); | ||
} | ||
[TestMethod] | ||
public void It_wont_select_if_an_argument_is_sealed_and_only_one_constructor() | ||
{ | ||
Assert.ThrowsException<ArgumentException>( | ||
() => typeof(WithSealedParameter2).SelectCtor(Array.Empty<Type>(), DefaultBindingFlags)); | ||
} | ||
|
||
[TestMethod] | ||
public void It_wont_select_if_an_argument_is_sealed_and_not_array() | ||
{ | ||
var ctor = typeof(WithSealedParameter).SelectCtor(Array.Empty<Type>(), DefaultBindingFlags); | ||
Assert.AreEqual(0, ctor.GetParameters().Length); | ||
} | ||
[TestMethod] | ||
public void It_wont_select_if_an_argument_is_sealed_and_not_array() | ||
{ | ||
var ctor = typeof(WithSealedParameter).SelectCtor(Array.Empty<Type>(), DefaultBindingFlags); | ||
Assert.AreEqual(0, ctor.GetParameters().Length); | ||
} | ||
|
||
[TestMethod] | ||
public void It_will_select_if_an_argument_is_sealed_and_supplied() | ||
{ | ||
var ctor = typeof(WithSealedParameter).SelectCtor(new [] { typeof(string) }, DefaultBindingFlags); | ||
Assert.AreEqual(1, ctor.GetParameters().Length); | ||
} | ||
[TestMethod] | ||
public void It_will_select_if_an_argument_is_sealed_and_supplied() | ||
{ | ||
var ctor = typeof(WithSealedParameter).SelectCtor(new[] { typeof(string) }, DefaultBindingFlags); | ||
Assert.AreEqual(1, ctor.GetParameters().Length); | ||
} | ||
|
||
[TestMethod] | ||
public void It_will_select_a_private_ctor_when_specified() | ||
{ | ||
const BindingFlags privateBindingFlags = DefaultBindingFlags | BindingFlags.NonPublic; | ||
var ctor = typeof(WithPrivateConstructor).SelectCtor(Array.Empty<Type>(), privateBindingFlags); | ||
Assert.AreEqual(2, ctor.GetParameters().Length); | ||
} | ||
[TestMethod] | ||
public void It_will_select_a_private_ctor_when_specified() | ||
{ | ||
const BindingFlags privateBindingFlags = DefaultBindingFlags | BindingFlags.NonPublic; | ||
var ctor = typeof(WithPrivateConstructor).SelectCtor(Array.Empty<Type>(), privateBindingFlags); | ||
Assert.AreEqual(2, ctor.GetParameters().Length); | ||
} | ||
|
||
[TestMethod] | ||
public void It_will_always_allow_empty_private_constructor() | ||
{ | ||
var ctor = typeof(ProtectedConstructor).SelectCtor(Array.Empty<Type>(), DefaultBindingFlags); | ||
Assert.AreEqual(0, ctor.GetParameters().Length); | ||
} | ||
[TestMethod] | ||
public void It_will_always_allow_empty_private_constructor() | ||
{ | ||
var ctor = typeof(ProtectedConstructor).SelectCtor(Array.Empty<Type>(), DefaultBindingFlags); | ||
Assert.AreEqual(0, ctor.GetParameters().Length); | ||
} | ||
} |
Oops, something went wrong.