Skip to content

Commit

Permalink
Moq -> NSubstitute
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpalme committed Apr 2, 2024
1 parent 5788b50 commit 7790b7b
Show file tree
Hide file tree
Showing 19 changed files with 149 additions and 141 deletions.
8 changes: 4 additions & 4 deletions src/ReportGenerator.Core.Test/Issues/Issue235_Cobertura.cs
@@ -1,6 +1,6 @@
using System.Linq;
using System.Xml.Linq;
using Moq;
using NSubstitute;
using Palmmedia.ReportGenerator.Core.Parser;
using Palmmedia.ReportGenerator.Core.Parser.Filtering;
using Xunit;
Expand Down Expand Up @@ -30,12 +30,12 @@ public class Issue235_Cobertura
[Fact]
public void NestedClassWithoutParentIsPresent()
{
var filterMock = new Mock<IFilter>();
filterMock.Setup(f => f.IsElementIncludedInReport(It.IsAny<string>())).Returns(true);
var filter = Substitute.For<IFilter>();
filter.IsElementIncludedInReport(Arg.Any<string>()).Returns(true);

var report = XDocument.Parse(Report);

var parserResult = new CoberturaParser(filterMock.Object, filterMock.Object, filterMock.Object).Parse(report.Root);
var parserResult = new CoberturaParser(filter, filter, filter).Parse(report.Root);

Assert.Equal("OutwardPaymentDocumentProcessing.HttpStart", parserResult.Assemblies.First().Classes.First().Name);
}
Expand Down
53 changes: 31 additions & 22 deletions src/ReportGenerator.Core.Test/Parser/Analysis/CodeFileTest.cs
@@ -1,6 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using Moq;
using NSubstitute;
using Palmmedia.ReportGenerator.Core.Parser.Analysis;
using Palmmedia.ReportGenerator.Core.Parser.FileReading;
using Xunit;
Expand Down Expand Up @@ -267,24 +267,24 @@ public void AnalyzeFile_NonExistingFile_AnalysisIsReturned()
[Fact]
public void AnalyzeFile_AdditionFileReaderNoError_RegularFileReaderIgnored()
{
var additionalFileReaderMock = new Mock<IFileReader>();
string error = null;
additionalFileReaderMock.Setup(f => f.LoadFile(It.IsAny<string>(), out error))
var additionalFileReader = Substitute.For<IFileReader>();
string errorArg = Arg.Any<string>();
additionalFileReader.LoadFile(Arg.Any<string>(), out errorArg)
.Returns(new[] { "Test" });

var fileReaderMock = new Mock<IFileReader>();
var fileReader = Substitute.For<IFileReader>();

var sut = new CodeFile("C:\\temp\\Other.cs", new int[] { -2, -1, 0, 1 }, new LineVisitStatus[] { LineVisitStatus.NotCoverable, LineVisitStatus.NotCoverable, LineVisitStatus.NotCovered, LineVisitStatus.Covered }, additionalFileReaderMock.Object);
var sut = new CodeFile("C:\\temp\\Other.cs", new int[] { -2, -1, 0, 1 }, new LineVisitStatus[] { LineVisitStatus.NotCoverable, LineVisitStatus.NotCoverable, LineVisitStatus.NotCovered, LineVisitStatus.Covered }, additionalFileReader);

Assert.Null(sut.TotalLines);

var fileAnalysis = sut.AnalyzeFile(fileReaderMock.Object);
var fileAnalysis = sut.AnalyzeFile(fileReader);

Assert.NotNull(fileAnalysis);
Assert.Null(fileAnalysis.Error);

additionalFileReaderMock.Verify(f => f.LoadFile(It.IsAny<string>(), out error), Times.Once);
fileReaderMock.Verify(f => f.LoadFile(It.IsAny<string>(), out error), Times.Never);
additionalFileReader.Received(1).LoadFile(Arg.Any<string>(), out errorArg);
fileReader.DidNotReceive().LoadFile(Arg.Any<string>(), out errorArg);
}

/// <summary>
Expand All @@ -293,29 +293,38 @@ public void AnalyzeFile_AdditionFileReaderNoError_RegularFileReaderIgnored()
[Fact]
public void AnalyzeFile_AdditionFileReaderReturnsError_RegularFileReaderUsed()
{
var additionalFileReaderMock = new Mock<IFileReader>();
string error = "Some error";
additionalFileReaderMock.Setup(f => f.LoadFile(It.IsAny<string>(), out error))
.Returns((string[])null);

var fileReaderMock = new Mock<IFileReader>();
fileReaderMock.Setup(f => f.LoadFile(It.IsAny<string>(), out error))
.Returns(new[] { "Test" });

var sut = new CodeFile("C:\\temp\\Other.cs", new int[] { -2, -1, 0, 1 }, new LineVisitStatus[] { LineVisitStatus.NotCoverable, LineVisitStatus.NotCoverable, LineVisitStatus.NotCovered, LineVisitStatus.Covered }, additionalFileReaderMock.Object);
var additionalFileReader = Substitute.For<IFileReader>();
string errorArg = Arg.Any<string>();
string errorOut = "Some error";
additionalFileReader.LoadFile(Arg.Any<string>(), out errorArg)
.Returns(x =>
{
x[1] = errorOut;
return null;
});

var fileReader = Substitute.For<IFileReader>();
fileReader.LoadFile(Arg.Any<string>(), out errorArg)
.Returns(x =>
{
x[1] = errorOut;
return new[] { "Test" };
});

var sut = new CodeFile("C:\\temp\\Other.cs", new int[] { -2, -1, 0, 1 }, new LineVisitStatus[] { LineVisitStatus.NotCoverable, LineVisitStatus.NotCoverable, LineVisitStatus.NotCovered, LineVisitStatus.Covered }, additionalFileReader);

Assert.Null(sut.TotalLines);

var fileAnalysis = sut.AnalyzeFile(fileReaderMock.Object);
var fileAnalysis = sut.AnalyzeFile(fileReader);

Assert.NotNull(fileAnalysis);
Assert.NotNull(fileAnalysis.Error);
Assert.Equal(fileAnalysis.Path, fileAnalysis.Path);
Assert.Equal(4, sut.TotalLines);
Assert.Equal(4, fileAnalysis.Lines.Count());

additionalFileReaderMock.Verify(f => f.LoadFile(It.IsAny<string>(), out error), Times.Once);
fileReaderMock.Verify(f => f.LoadFile(It.IsAny<string>(), out error), Times.Once);
additionalFileReader.Received(1).LoadFile(Arg.Any<string>(), out errorArg);
fileReader.Received(1).LoadFile(Arg.Any<string>(), out errorArg);
}

/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions src/ReportGenerator.Core.Test/Parser/CloverParserTest.cs
Expand Up @@ -2,7 +2,7 @@
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Moq;
using NSubstitute;
using Palmmedia.ReportGenerator.Core.Parser;
using Palmmedia.ReportGenerator.Core.Parser.Analysis;
using Palmmedia.ReportGenerator.Core.Parser.FileReading;
Expand All @@ -25,12 +25,12 @@ public class CloverParserTest

public CloverParserTest()
{
var filterMock = new Mock<IFilter>();
filterMock.Setup(f => f.IsElementIncludedInReport(It.IsAny<string>())).Returns(true);
var filter = Substitute.For<IFilter>();
filter.IsElementIncludedInReport(Arg.Any<string>()).Returns(true);

var report = XDocument.Load(FilePath1);
new CloverReportPreprocessor(new[] { "C:\\temp" }).Execute(report);
this.parserResult = new CloverParser(filterMock.Object, filterMock.Object, filterMock.Object).Parse(report);
this.parserResult = new CloverParser(filter, filter, filter).Parse(report);
}

/// <summary>
Expand Down
20 changes: 10 additions & 10 deletions src/ReportGenerator.Core.Test/Parser/CoberturaParserTest.cs
Expand Up @@ -2,7 +2,7 @@
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Moq;
using NSubstitute;
using Palmmedia.ReportGenerator.Core.Parser;
using Palmmedia.ReportGenerator.Core.Parser.Analysis;
using Palmmedia.ReportGenerator.Core.Parser.FileReading;
Expand All @@ -19,7 +19,7 @@ namespace Palmmedia.ReportGenerator.Core.Test.Parser
[Collection("FileManager")]
public class CoberturaParserTest
{
private static readonly string FilePathJavaReport = Path.Combine(FileManager.GetJavaReportDirectory(), "Cobertura2.1.1.xml");
private static readonly string FilePathJavaReport = Path.Combine(FileManager.GetJavaReportDirectory(), "Cobertura2.1.1.xml");
private static readonly string FilePathCSharpReport = Path.Combine(FileManager.GetCSharpReportDirectory(), "Cobertura_coverlet.xml");

private readonly ParserResult javaParserResult;
Expand Down Expand Up @@ -156,11 +156,11 @@ public void MethodMetricsTest()
/// A test for MethodMetrics
/// </summary>
[Theory]
[InlineData("Test", "Test.AbstractClass", "C:\\temp\\AbstractClass.cs", ".ctor()", 1, 1, 100, 100, 1)]
[InlineData("Test", "Test.AbstractClass_SampleImpl1", "C:\\temp\\AbstractClass.cs", "Method1()", 3, 1, 0, 100, 2)]
[InlineData("Test", "Test.PartialClass", "C:\\temp\\PartialClass.cs", "set_SomeProperty(System.Int32)", 4, 2, 66.66, 50, 2.15)]
[InlineData("Test", "Test.Program", "C:\\temp\\Program.cs", "Main(System.String[])", 4, 1, 89.65, 100, 1.00)]
[InlineData("Test", "Test.TestClass", "C:\\temp\\TestClass.cs", "SampleFunction()", 5, 4, 80, 50, 4.13)]
[InlineData("Test", "Test.AbstractClass", "C:\\temp\\AbstractClass.cs", ".ctor()", 1, 1, 100, 100, 1)]
[InlineData("Test", "Test.AbstractClass_SampleImpl1", "C:\\temp\\AbstractClass.cs", "Method1()", 3, 1, 0, 100, 2)]
[InlineData("Test", "Test.PartialClass", "C:\\temp\\PartialClass.cs", "set_SomeProperty(System.Int32)", 4, 2, 66.66, 50, 2.15)]
[InlineData("Test", "Test.Program", "C:\\temp\\Program.cs", "Main(System.String[])", 4, 1, 89.65, 100, 1.00)]
[InlineData("Test", "Test.TestClass", "C:\\temp\\TestClass.cs", "SampleFunction()", 5, 4, 80, 50, 4.13)]
public void MethodMetricsTest_2(string assemblyName, string className, string filePath, string methodName, int expectedMethodMetrics, double expectedComplexity, double expectedLineCoverage, double expectedBranchCoverage, double expectedCrapScore)
{
var methodMetrics = csharpParserResult
Expand Down Expand Up @@ -219,12 +219,12 @@ public void CodeElementsTest()

private static ParserResult ParseReport(string filePath)
{
var filterMock = new Mock<IFilter>();
filterMock.Setup(f => f.IsElementIncludedInReport(It.IsAny<string>())).Returns(true);
var filter = Substitute.For<IFilter>();
filter.IsElementIncludedInReport(Arg.Any<string>()).Returns(true);

var report = XDocument.Load(filePath);
new CoberturaReportPreprocessor().Execute(report);
return new CoberturaParser(filterMock.Object, filterMock.Object, filterMock.Object).Parse(report.Root);
return new CoberturaParser(filter, filter, filter).Parse(report.Root);
}
}
}

0 comments on commit 7790b7b

Please sign in to comment.