Skip to content

Commit

Permalink
Some code clean up (#243)
Browse files Browse the repository at this point in the history
* Some code clean up

* Enabled centralized package management

Updated NuGets
Fixed issue with NUnit test generator.

* Role back compiler NuGets

Also disabling dependabot for them. We need the old version for net6
  • Loading branch information
Keboo committed Jan 8, 2024
1 parent bf635a0 commit 09ba0b5
Show file tree
Hide file tree
Showing 18 changed files with 84 additions and 79 deletions.
3 changes: 3 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ updates:
directory: "/" # Location of package manifests
schedule:
interval: "weekly"
ignore:
- dependency-name: "Microsoft.CodeAnalysis.Common"
- dependency-name: "Microsoft.CodeAnalysis.CSharp"

- package-ecosystem: "github-actions" # See documentation for possible values
directory: "/" # Location of package manifests
Expand Down
21 changes: 21 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="coverlet.collector" Version="6.0.0"/>
<PackageVersion Include="Microsoft.CodeAnalysis" Version="4.3.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.MSTest" Version="1.1.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageVersion Include="Moq" Version="4.20.70" />
<PackageVersion Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageVersion Include="MSTest.TestFramework" Version="3.1.1" />
<PackageVersion Include="NonBlocking" Version="2.1.2" />
<PackageVersion Include="NUnit" Version="4.0.1" />
<PackageVersion Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageVersion Include="xunit" Version="2.6.5" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.6" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="MSTest.TestAdapter" />
<PackageReference Include="MSTest.TestFramework" />
<PackageReference Include="coverlet.collector">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="NUnit" />
<PackageReference Include="NUnit3TestAdapter" />
<PackageReference Include="coverlet.collector" >
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.5" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="coverlet.collector">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
12 changes: 8 additions & 4 deletions Generators/CombineGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,14 @@ private MemberDeclarationSyntax Combine(int count)
"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(" ") })
"types will be implemented by the same instance.",
}.SelectMany(text => new[] { XmlNewLine(NewLine), XmlText($" {text}") })
.Concat(new[] { XmlNewLine(NewLine), XmlText(" ") })
.ToArray()
),
XmlText($"{Environment.NewLine} "));
XmlText($"{NewLine} "));

//A new line that will respect the checked out state of auto.crlf
private const string NewLine = @"
";
}
4 changes: 3 additions & 1 deletion Generators/Generators.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<IsPackable>false</IsPackable>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>

</PropertyGroup>

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

</Project>
2 changes: 1 addition & 1 deletion Moq.AutoMock.Tests/DescribeCreateInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void It_creates_mock_objects_for_internal_ctor_parameters()
{
var mocker = new AutoMocker();
var instance = mocker.CreateInstance<WithServiceInternal>(true);
Assert.IsNotNull(Mock.Get(instance.Service));
Assert.IsNotNull(Mock.Get(instance.Service!));
}

[TestMethod]
Expand Down
2 changes: 1 addition & 1 deletion Moq.AutoMock.Tests/DescribeSetups.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Moq.AutoMock.Tests.Util;

namespace Moq.AutoMock.Tests;


[TestClass]
public class DescribeSetups
{
Expand Down
8 changes: 4 additions & 4 deletions Moq.AutoMock.Tests/Moq.AutoMock.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Moq" version="4.18.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
<PackageReference Include="Moq" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="MSTest.TestAdapter" />
<PackageReference Include="MSTest.TestFramework" />
<ProjectReference Include="..\Moq.AutoMock\Moq.AutoMock.csproj" />
</ItemGroup>
</Project>
28 changes: 14 additions & 14 deletions Moq.AutoMock.Tests/Util/CustomDefaultValueProvider.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
namespace Moq.AutoMock.Tests.Util;
internal class CustomDefaultValueProvider : DefaultValueProvider
{
public Dictionary<Type, object?> DefaultValues { get; } = new();

protected override object? GetDefaultValue(Type type, Mock mock)
{
if (DefaultValues.TryGetValue(type, out object? value))
{
return value;
}
return null;
}
}
namespace Moq.AutoMock.Tests.Util;
internal class CustomDefaultValueProvider : DefaultValueProvider
{
public Dictionary<Type, object> DefaultValues { get; } = new();

protected override object GetDefaultValue(Type type, Mock mock)
{
if (DefaultValues.TryGetValue(type, out object? value))
{
return value;
}
throw new NotImplementedException();
}
}
1 change: 1 addition & 0 deletions Moq.AutoMock.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.editorconfig = .editorconfig
Directory.Build.props = Directory.Build.props
Directory.Build.targets = Directory.Build.targets
Directory.Packages.props = Directory.Packages.props
global.json = global.json
LICENSE.md = LICENSE.md
README.md = README.md
Expand Down
13 changes: 5 additions & 8 deletions Moq.AutoMock/AutoMocker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
using Moq.AutoMock.Resolvers;
using Moq.Language;
using Moq.Language.Flow;
#if NET45
using Array = Moq.AutoMock.Resolvers.Array;
#endif

namespace Moq.AutoMock;

Expand Down Expand Up @@ -257,7 +254,7 @@ internal object CreateInstanceInternal(Type type, ObjectGraphContext context)
/// </summary>
/// <typeparam name="T">The instance that you want to build</typeparam>
/// <returns>An instance with virtual and abstract members mocked</returns>
public T CreateSelfMock<T>() where T : class?
public T CreateSelfMock<T>() where T : class
=> CreateSelfMock<T>(false);

/// <summary>
Expand All @@ -269,7 +266,7 @@ public T CreateSelfMock<T>() where T : class?
/// <typeparam name="T">The instance that you want to build</typeparam>
/// <param name="enablePrivate">When true, non-public constructors will also be used to create mocks.</param>
/// <returns>An instance with virtual and abstract members mocked</returns>
public T CreateSelfMock<T>(bool enablePrivate) where T : class?
public T CreateSelfMock<T>(bool enablePrivate) where T : class
=> CreateSelfMock<T>(enablePrivate, MockBehavior, DefaultValue, null, CallBase);

/// <summary>
Expand All @@ -281,7 +278,7 @@ public T CreateSelfMock<T>(bool enablePrivate) where T : class?
/// <typeparam name="T">The instance that you want to build</typeparam>
/// <param name="enablePrivate">When true, non-public constructors will also be used to create mocks.</param>
/// <param name="mockBehavior">Sets the Behavior property on the created Mock.</param>
/// <param name="defaultValue">Sets the DefaultValue propert on the created Mock.</param>
/// <param name="defaultValue">Sets the DefaultValue property on the created Mock.</param>
/// <param name="defaultValueProvider">The instance that will be used to produce default return values for unexpected invocations.</param>
/// <param name="callBase">Sets the CallBase property on the created Mock.</param>
/// <returns>An instance with virtual and abstract members mocked</returns>
Expand All @@ -291,7 +288,7 @@ public T CreateSelfMock<T>(
DefaultValue? defaultValue = null,
DefaultValueProvider? defaultValueProvider = null,
bool? callBase = null)
where T : class?
where T : class
{
return BuildSelfMock<T>(enablePrivate, mockBehavior ?? MockBehavior, defaultValue ?? DefaultValue, defaultValueProvider ?? DefaultValueProvider, callBase ?? CallBase).Object;
}
Expand Down Expand Up @@ -428,7 +425,7 @@ public object WithSelfMock(
}

private Mock<T> BuildSelfMock<T>(bool enablePrivate, MockBehavior mockBehavior, DefaultValue defaultValue, DefaultValueProvider? defaultValueProvider, bool callBase)
where T : class?
where T : class
{
var context = new ObjectGraphContext(enablePrivate);
return CreateMock(typeof(T), mockBehavior, defaultValue, defaultValueProvider, callBase, context) is Mock<T> mock
Expand Down
10 changes: 3 additions & 7 deletions Moq.AutoMock/Moq.AutoMock.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,18 @@


<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0">
<PackageReference Include="Microsoft.SourceLink.GitHub">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="NonBlocking" Version="2.1.2" />
<PackageReference Include="Moq" />
<PackageReference Include="NonBlocking" />
<ProjectReference Include="../Generators/Generators.csproj">
<OutputItemType>Analyzer</OutputItemType>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
<ProjectReference Include="..\Moq.AutoMocker.TestGenerator\Moq.AutoMocker.TestGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

<ItemGroup Condition="$(TargetFramework) == 'net461'">
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>

<ItemGroup>
<None Include="..\LICENSE" Pack="true" PackagePath="" />
<Compile Include="..\System.Diagnostics.CodeAnalysis.cs" />
Expand Down
20 changes: 0 additions & 20 deletions Moq.AutoMock/Resolvers/Array.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.MSTest" Version="1.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="MSTest.TestAdapter" />
<PackageReference Include="MSTest.TestFramework" />
<PackageReference Include="Microsoft.CodeAnalysis" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.MSTest" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<IsPackable>false</IsPackable>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>

<ItemGroup>
Expand All @@ -11,6 +12,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" PrivateAssets="all" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion Moq.AutoMocker.TestGenerator/UnitTestSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void Execute(GeneratorExecutionContext context)
break;
case TargetTestingFramework.NUnit:
builder.AppendLine($" System.ArgumentNullException ex = global::NUnit.Framework.Assert.Throws<System.ArgumentNullException>(() => {constructorInvocation});");
builder.AppendLine($" global::NUnit.Framework.Assert.AreEqual(\"{test.NullParameterName}\", ex.ParamName);");
builder.AppendLine($" global::NUnit.Framework.Assert.That(\"{test.NullParameterName}\" == ex.ParamName);");
break;
}

Expand Down

0 comments on commit 09ba0b5

Please sign in to comment.