Skip to content

Commit

Permalink
More test, working generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Keboo committed Jan 25, 2024
1 parent 11f7627 commit 8cbe45f
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ public partial class ControllerStringTests
partial void AutoMockerTestSetup(Moq.AutoMock.AutoMocker mocker, string testName)
{
mocker.Use<string>("");
mocker.Use<int?>(42);
mocker.Use<int>(24);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@
public class ControllerString
{
public ControllerString(
string name,
string name,
int years,
string? nullableName,
string? testName = null,
string foo = null!,
int? i = null)
int? age = null)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
NullableName = nullableName;
TestName = testName;
ArgumentNullException.ThrowIfNull(name);
}

public string Name { get; } = "";
public string? NullableName { get; }
public string? TestName { get; }
}
105 changes: 105 additions & 0 deletions Moq.AutoMocker.TestGenerator.Tests/TestGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,111 @@ partial class ControllerTests
}.RunAsync();
}

[TestMethod]
public async Task Generation_ParameterWithValueType_DoesNotGenerateTest()
{
var code = @"
using Moq.AutoMock;
using System.Threading;
namespace TestNamespace;
[ConstructorTests(typeof(Controller), TestGenerationBehavior.IgnoreNullableParameters)]
public partial class ControllerTests
{
}
public class Controller
{
public Controller(int years)
{ }
}
";
string expected = @"namespace TestNamespace
{
partial class ControllerTests
{
partial void AutoMockerTestSetup(Moq.AutoMock.AutoMocker mocker, string testName);
}
}
";

await new VerifyCS.Test
{
TestCode = code,
TestState =
{
GeneratedSources =
{
GetSourceFile(expected, "ControllerTests.g.cs")
}
}

}.RunAsync();
}

[TestMethod]
public async Task Generation_ParametersTypesWithValueTypeBetweenReferenceTypes_OnlyGeneratesTestsForReferenceType()
{
var code = @"
#nullable enable
using Moq.AutoMock;
using System.Threading;
namespace TestNamespace;
[ConstructorTests(typeof(Controller), TestGenerationBehavior.IgnoreNullableParameters)]
public partial class ControllerTests
{
}
public class Controller
{
public Controller(
string name,
int years,
string? nullableName)
{ }
}
";
string expected = @"namespace TestNamespace
{
partial class ControllerTests
{
partial void AutoMockerTestSetup(Moq.AutoMock.AutoMocker mocker, string testName);
partial void ControllerConstructor_WithNullstring_ThrowsArgumentNullExceptionSetup(Moq.AutoMock.AutoMocker mocker);
public void ControllerConstructor_WithNullstring_ThrowsArgumentNullException()
{
Moq.AutoMock.AutoMocker mocker = new Moq.AutoMock.AutoMocker();
AutoMockerTestSetup(mocker, ""ControllerConstructor_WithNullstring_ThrowsArgumentNullException"");
ControllerConstructor_WithNullstring_ThrowsArgumentNullExceptionSetup(mocker);
var years = mocker.Get<int>();
var nullableName = mocker.Get<string>();
}
}
}
";

await new VerifyCS.Test
{
TestCode = code,
TestState =
{
GeneratedSources =
{
GetSourceFile(expected, "ControllerTests.g.cs")
}
}

}.RunAsync();
}

private static (string FileName, SourceText SourceText) GetSourceFile(string content, string fileName)
{
return (Path.Combine("Moq.AutoMocker.TestGenerator", "Moq.AutoMocker.TestGenerator.UnitTestSourceGenerator", fileName), SourceText.From(content, Encoding.UTF8));
Expand Down
3 changes: 3 additions & 0 deletions Moq.AutoMocker.TestGenerator/GeneratorTargetClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ public Parameter(IParameterSymbol symbol)
{
IsNullable = true;
}

IsValueType = symbol.Type.IsValueType;
}
private IParameterSymbol Symbol { get; }

public bool IsValueType { get; }
public bool IsNullable { get; }
public string Name => Symbol.Name;
public string ParameterType => Symbol.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
Expand Down
1 change: 0 additions & 1 deletion Moq.AutoMocker.TestGenerator/SyntaxReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
int nullIndex = 0;
foreach (IParameterSymbol parameter in ctor.Parameters)
{
if (parameter.Type.IsValueType) continue;
sut.NullConstructorParameterTests.Add(new NullConstructorParameterTest()
{
Parameters = parameters,
Expand Down
4 changes: 4 additions & 0 deletions Moq.AutoMocker.TestGenerator/UnitTestSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public void Execute(GeneratorExecutionContext context)

foreach (NullConstructorParameterTest test in testClass.Sut?.NullConstructorParameterTests ?? Enumerable.Empty<NullConstructorParameterTest>())
{
if (test.Parameters?[test.NullParameterIndex].IsValueType == true)
{
continue;
}
// If SkipNullableParameters is true and the parameter is a nullable reference type, skip it
if (testClass.SkipNullableParameters && test.Parameters?[test.NullParameterIndex].IsNullable == true)
{
Expand Down

0 comments on commit 8cbe45f

Please sign in to comment.