Skip to content

Commit

Permalink
Adding editorconfig (#132)
Browse files Browse the repository at this point in the history
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
Keboo authored Dec 14, 2021
1 parent b9eb041 commit a6733b5
Show file tree
Hide file tree
Showing 82 changed files with 3,544 additions and 3,259 deletions.
365 changes: 365 additions & 0 deletions .editorconfig

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .github/workflows/dotnetcore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 5.0.100
dotnet-version: 6.0.100

# Run unit tests
- name: Test
Expand Down
3 changes: 2 additions & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<Project>

<PropertyGroup>
<LangVersion>9.0</LangVersion>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
<EnforceCodeStyleInBuild Condition=" '$(BuildingForLiveUnitTesting)' == '' ">true</EnforceCodeStyleInBuild>
<TreatWarningsAsErrors Condition=" '$(BuildingForLiveUnitTesting)' == '' ">true</TreatWarningsAsErrors>
</PropertyGroup>

Expand Down
89 changes: 44 additions & 45 deletions Generators/CombineGenerator.cs
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} "));
}
2 changes: 1 addition & 1 deletion Generators/Generators.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.0.1" PrivateAssets="all" />
</ItemGroup>

</Project>
111 changes: 55 additions & 56 deletions Moq.AutoMock.Tests/ConstructorSelectorTests.cs
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);
}
}
Loading

0 comments on commit a6733b5

Please sign in to comment.