Skip to content

Commit

Permalink
Melhorias em reuso de código
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-iel committed May 30, 2024
1 parent 3caa86d commit f5404b1
Show file tree
Hide file tree
Showing 20 changed files with 230 additions and 292 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ StrykerOutput
*/StrykerOutput/*
*/StrykerOutput/

*/coverage.opencover.xml
coverage.opencover.xml

src/**/nupkg/*
*/src/*/nupkg/*
.user
Expand Down
75 changes: 22 additions & 53 deletions src/ByReplace.Test/Analyzers/AnalyzerAndFixerTest.cs
Original file line number Diff line number Diff line change
@@ -1,95 +1,64 @@
using ByReplace.Analyzers;
using ByReplace.Builders;
using ByReplace.Models;
using ByReplace.Printers;
using ByReplace.Test.Common.ConfigMock;
using ByReplace.Test.Common.FolderMock;
using ByReplace.Test.ClassFixture;
using Moq;
using Xunit;

namespace ByReplace.Test.Analyzers;

public class AnalyzerAndFixerTest
public class AnalyzerAndFixerTest : IClassFixture<WorkspaceFixture>
{
private readonly PathCompilationSyntax _pathCompilationSyntax;
private readonly BrConfiguration _brConfiguration;
private readonly WorkspaceFixture _workspace;
private readonly Mock<IPrint> _printMock;

public AnalyzerAndFixerTest()
public AnalyzerAndFixerTest(WorkspaceFixture workspace)
{
var configContent = BrContentFactory
.CreateDefault()
.AddConfig(BrContentFactory.ConfigNoPathDeclaration("obj", ".bin"))
.AddRules(BrContentFactory
.Rule("RuleTest")
.WithExtensions(".cs", ".txt")
.WithSkips("**\\Controllers\\*", "bin\\bin1.txt", "obj\\obj2.txt")
.WithReplacement(BrContentFactory.Replacement("Test", "Test2")))
.Compile();

var rootFolder = FolderSyntax
.FolderDeclaration("RootFolder")
.AddMembers(
FileSyntax.FileDeclaration("RootFile1.cs", "ITest = new Test()"),
FileSyntax.FileDeclaration("RootFile2.cs", "ITest = new Test()"));

_pathCompilationSyntax = PathFactory
.Compile(nameof(AnalyzerAndFixerTest))
.AddMembers(rootFolder)
.AddBrConfiguration(configContent)
.Create();

_brConfiguration = BrConfigurationBuilder
.Create()
.SetPath($"./{_pathCompilationSyntax.InternalIdentifier}")
.SetConfigPath($"./{_pathCompilationSyntax.InternalIdentifier}")
.Build();

_workspace = workspace;
_printMock = new Mock<IPrint>();
}

[Fact]
public void TryMatchRule_MapTheFilesThatMatchToRule_ShouldReturnFilesThatMatch()
{
// Arrange
var analyzer = new Analyzer(_brConfiguration, _printMock.Object);
var analyzer = new Analyzer(_workspace.BrConfiguration, _printMock.Object);
var analyzerAndFixer = new AnalyzerAndFixer(_printMock.Object);

// Act
var directoryNode = analyzer.LoadThreeFiles().Last();
analyzerAndFixer.TryMatchRule(directoryNode, _brConfiguration.Rules);
analyzerAndFixer.TryMatchRule(directoryNode, _workspace.BrConfiguration.Rules);

// Assert
Assert.Equal(2, analyzerAndFixer.Count);

Assert.Collection(analyzerAndFixer,
entry =>
{
Assert.Equal("RootFile1.cs", entry.Key.Name);
Assert.Equal(".cs", entry.Key.Extension);
Assert.Collection(entry.Value, rule => Assert.Equal("RuleTest", rule.Name));
},
entry =>
{
Assert.Equal("RootFile2.cs", entry.Key.Name);
Assert.Equal(".cs", entry.Key.Extension);
Assert.Collection(entry.Value, rule => Assert.Equal("RuleTest", rule.Name));
});
entry =>
{
Assert.Equal("RootFile1.cs", entry.Key.Name);
Assert.Equal(".cs", entry.Key.Extension);
Assert.Collection(entry.Value, rule => Assert.Equal("RuleTest", rule.Name));
},
entry =>
{
Assert.Equal("RootFile2.cs", entry.Key.Name);
Assert.Equal(".cs", entry.Key.Extension);
Assert.Collection(entry.Value, rule => Assert.Equal("RuleTest", rule.Name));
});
}

[Fact]
public void TryMatchRule_WhenMapTheFilesThatMatchToRule_ShouldValidateLogWasCalled()
{
// Arrange
var analyzer = new Analyzer(_brConfiguration, _printMock.Object);
var analyzer = new Analyzer(_workspace.BrConfiguration, _printMock.Object);
var analyzerAndFixer = new AnalyzerAndFixer(_printMock.Object);

// Act
var directoryNode = analyzer.LoadThreeFiles().Last();
analyzerAndFixer.TryMatchRule(directoryNode, _brConfiguration.Rules);
analyzerAndFixer.TryMatchRule(directoryNode, _workspace.BrConfiguration.Rules);

// Assert
_printMock.Verify(x => x.Information("[Cyan]1 rules in total match the file [Cyan]RootFile1.cs."), Times.Once);
_printMock.Verify(x => x.Information("[Cyan]1 rules in total match the file [Cyan]RootFile2.cs."), Times.Once);
}
}
}
44 changes: 23 additions & 21 deletions src/ByReplace.Test/Analyzers/AnalyzerRunnerTest.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
using ByReplace.Analyzers;
using ByReplace.Builders;
using ByReplace.Models;
using ByReplace.Printers;
using ByReplace.Test.ClassFixture;
using ByReplace.Test.Common.ConfigMock;
using ByReplace.Test.Common.FolderMock;
using Moq;
using Xunit;

namespace ByReplace.Test.Analyzers;

public class AnalyzerRunnerTest
public class AnalyzerRunnerTest : IClassFixture<WorkspaceFixture>
{
private readonly PathCompilationSyntax _pathCompilationSyntax;
private readonly BrConfiguration _brConfiguration;
private readonly WorkspaceFixture _workspace;
private readonly Mock<IPrint> _printMock;

public AnalyzerRunnerTest()
public AnalyzerRunnerTest(WorkspaceFixture workspace)
{
var configContent = BrContentFactory
_workspace = workspace;
_printMock = new Mock<IPrint>();

workspace.ClearPrevious();

_workspace.ConfigContent = BrContentFactory
.CreateDefault()
.AddConfig(BrContentFactory.ConfigNoPathDeclaration("obj", ".bin"))
.AddRules(BrContentFactory
Expand All @@ -29,49 +33,47 @@ public AnalyzerRunnerTest()

var rootFolder = FolderSyntax
.FolderDeclaration("RootFolder")
.AddMembers(
.AddFiles(
FileSyntax.FileDeclaration("RootFile1.cs", "ITest = new Test()"),
FileSyntax.FileDeclaration("RootFile2.cs", "ITest = new Test()"));

var controllerFolder = FolderSyntax.FolderDeclaration("Controllers")
.AddParent(rootFolder)
.AddMembers(
.AddFiles(
FileSyntax.FileDeclaration("Controller1.cs", "ITest2 = new Test()"),
FileSyntax.FileDeclaration("Controller2.cs", "ITest2 = new Test()"));

var binFolder = FolderSyntax.FolderDeclaration("bin")
.AddParent(rootFolder)
.AddMembers(
.AddFiles(
FileSyntax.FileDeclaration("bin1.txt", "ITest = new Test()"),
FileSyntax.FileDeclaration("bin2.txt", "ITest = new Test()"));

var objFolder = FolderSyntax.FolderDeclaration("obj")
.AddParent(rootFolder)
.AddMembers(
.AddFiles(
FileSyntax.FileDeclaration("obj1.txt", "ITest = new Test()"),
FileSyntax.FileDeclaration("obj2.txt", "ITest = new Test()"));

_pathCompilationSyntax = PathFactory
_workspace.WorkspaceSyntax = WorkspaceFactory
.Compile(nameof(AnalyzerRunnerTest))
.AddMembers(controllerFolder, binFolder, objFolder)
.AddBrConfiguration(configContent)
.AddBrConfiguration(_workspace.ConfigContent)
.Create();

_brConfiguration = BrConfigurationBuilder
_workspace.BrConfiguration = BrConfigurationBuilder
.Create()
.SetPath($"./{_pathCompilationSyntax.InternalIdentifier}")
.SetConfigPath($"./{_pathCompilationSyntax.InternalIdentifier}")
.SetPath($"./{_workspace.WorkspaceSyntax.Identifier}")
.SetConfigPath($"./{_workspace.WorkspaceSyntax.Identifier}")
.Build();

_printMock = new Mock<IPrint>();
}

[Fact]
public void RunAnalysis_MapAllRulesThatMatchToFileInSourceTree_ShouldReturnRulesThatMatch()
{
// Arrange
var analyzer = new Analyzer(_brConfiguration, _printMock.Object);
var analyzerRunner = new AnalyzerRunner(_brConfiguration, _printMock.Object);
var analyzer = new Analyzer(_workspace.BrConfiguration, _printMock.Object);
var analyzerRunner = new AnalyzerRunner(_workspace.BrConfiguration, _printMock.Object);

// Act
var directoryNodes = analyzer.LoadThreeFiles();
Expand Down Expand Up @@ -114,8 +116,8 @@ public void RunAnalysis_MapAllRulesThatMatchToFileInSourceTree_ShouldReturnRules
public void RunAnalysis_WhenPrintLogInformation_ShouldValidateLogWasCalled()
{
// Arrange
var analyzer = new Analyzer(_brConfiguration, _printMock.Object);
var analyzerRunner = new AnalyzerRunner(_brConfiguration, _printMock.Object);
var analyzer = new Analyzer(_workspace.BrConfiguration, _printMock.Object);
var analyzerRunner = new AnalyzerRunner(_workspace.BrConfiguration, _printMock.Object);

// Act
var directoryNodes = analyzer.LoadThreeFiles();
Expand Down
28 changes: 15 additions & 13 deletions src/ByReplace.Test/Analyzers/AnalyzerTest.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
using ByReplace.Analyzers;
using ByReplace.Builders;
using ByReplace.Models;
using ByReplace.Printers;
using ByReplace.Test.ClassFixture;
using ByReplace.Test.Common.FolderMock;
using Moq;
using Xunit;

namespace ByReplace.Test.Analyzers;

public class AnalyzerTest
public class AnalyzerTest : IClassFixture<WorkspaceFixture>
{
private readonly PathCompilationSyntax _pathCompilationSyntax;
private readonly BrConfiguration _brConfiguration;
private readonly WorkspaceFixture _workspace;
private readonly Mock<IPrint> _printMock;

public AnalyzerTest()
public AnalyzerTest(WorkspaceFixture workspace)
{
_workspace = workspace;
_printMock = new Mock<IPrint>();

_workspace.ClearPrevious();

var rootFolder = FolderSyntax
.FolderDeclaration("RootFolder")
.AddMembers(FileSyntax.FileDeclaration("FileOne.cs", "ITest = new Test()"));
Expand All @@ -24,25 +28,23 @@ public AnalyzerTest()
.AddParent(rootFolder)
.AddMembers(FileSyntax.FileDeclaration("FileSecond.cs", "ITest2 = new Test()"));

_pathCompilationSyntax = PathFactory
_workspace.WorkspaceSyntax = WorkspaceFactory
.Compile(nameof(AnalyzerTest))
.AddMembers(firstLevel)
.Create();

_brConfiguration = BrConfigurationBuilder
_workspace.BrConfiguration = BrConfigurationBuilder
.Create()
.SetPath($"./{_pathCompilationSyntax.InternalIdentifier}")
.SetConfigPath($"./{_pathCompilationSyntax.InternalIdentifier}")
.SetPath($"./{_workspace.WorkspaceSyntax.Identifier}")
.SetConfigPath($"./{_workspace.WorkspaceSyntax.Identifier}")
.Build();

_printMock = new Mock<IPrint>();
}

[Fact]
public void LoadThreeFiles_MapAllSourceThreeOfDirectory_ShouldReturnSourceFileThree()
{
// Arrange
var analyzer = new Analyzer(_brConfiguration, _printMock.Object);
var analyzer = new Analyzer(_workspace.BrConfiguration, _printMock.Object);

// Act
var directoryNodes = analyzer.LoadThreeFiles();
Expand All @@ -59,7 +61,7 @@ public void LoadThreeFiles_MapAllSourceThreeOfDirectory_ShouldReturnSourceFileTh
public void LoadThreeFiles_WhenPrintLogInformation_ShouldValidateLogWasCalled()
{
// Arrange
var analyzer = new Analyzer(_brConfiguration, _printMock.Object);
var analyzer = new Analyzer(_workspace.BrConfiguration, _printMock.Object);

// Act
var directoryNodes = analyzer.LoadThreeFiles();
Expand Down
36 changes: 18 additions & 18 deletions src/ByReplace.Test/Analyzers/DocumentFixTest.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
using ByReplace.Analyzers;
using ByReplace.Builders;
using ByReplace.Models;
using ByReplace.Printers;
using ByReplace.Test.ClassFixture;
using ByReplace.Test.Common.ConfigMock;
using ByReplace.Test.Common.FolderMock;
using Moq;
using Xunit;

namespace ByReplace.Test.Analyzers;

public class DocumentFixTest
public class DocumentFixTest : IClassFixture<WorkspaceFixture>
{
private readonly PathCompilationSyntax _pathCompilationSyntax;
private readonly BrConfiguration _brConfiguration;
private readonly WorkspaceFixture _workspace;
private readonly Mock<IPrint> _printMock;

public DocumentFixTest()
public DocumentFixTest(WorkspaceFixture workspace)
{
_workspace = workspace;
_printMock = new Mock<IPrint>();

var configContent = BrContentFactory
.CreateDefault()
.AddConfig(BrContentFactory.ConfigNoPathDeclaration("obj", ".bin"))
Expand All @@ -34,33 +36,31 @@ public DocumentFixTest()

var rootFolder = FolderSyntax
.FolderDeclaration("RootFolder")
.AddMembers(
.AddFiles(
FileSyntax.FileDeclaration("RootFile1.cs", "var test = new Test2()"),
FileSyntax.FileDeclaration("RootFile1.txt", "var test = new Test2()"));

_pathCompilationSyntax = PathFactory
_workspace.WorkspaceSyntax = WorkspaceFactory
.Compile(nameof(DocumentFixTest))
.AddMembers(rootFolder)
.AddBrConfiguration(configContent)
.Create();
.Create();

_brConfiguration = BrConfigurationBuilder
.Create()
.SetPath($"./{_pathCompilationSyntax.InternalIdentifier}")
.SetConfigPath($"./{_pathCompilationSyntax.InternalIdentifier}")
_workspace.BrConfiguration = BrConfigurationBuilder
.Create()
.SetPath($"./{_workspace.WorkspaceSyntax.Identifier}")
.SetConfigPath($"./{_workspace.WorkspaceSyntax.Identifier}")
.Build();

_printMock = new Mock<IPrint>();
}

[Fact]
public async Task ApplyAsync_WhenPassAllRules_ShouldApplyTheRulesInAllFilesAsync()
{
// Arrange
var analyzer = new Analyzer(_brConfiguration, _printMock.Object);
var analyzer = new Analyzer(_workspace.BrConfiguration, _printMock.Object);
var analyzerAndFixer = new AnalyzerAndFixer(_printMock.Object);
var directoryNode = analyzer.LoadThreeFiles().Last();
analyzerAndFixer.TryMatchRule(directoryNode, _brConfiguration.Rules);
analyzerAndFixer.TryMatchRule(directoryNode, _workspace.BrConfiguration.Rules);
var documentFix = new DocumentFix(analyzerAndFixer, _printMock.Object);

// Act
Expand All @@ -80,10 +80,10 @@ public async Task ApplyAsync_WhenPassAllRules_ShouldApplyTheRulesInAllFilesAsync
public async Task ApplyAsync_WhenPassOnlyOneRule_ShouldApplyTheRuleInAllFiles()
{
// Arrange
var analyzer = new Analyzer(_brConfiguration, _printMock.Object);
var analyzer = new Analyzer(_workspace.BrConfiguration, _printMock.Object);
var analyzerAndFixer = new AnalyzerAndFixer(_printMock.Object);
var directoryNode = analyzer.LoadThreeFiles().Last();
analyzerAndFixer.TryMatchRule(directoryNode, _brConfiguration.Rules);
analyzerAndFixer.TryMatchRule(directoryNode, _workspace.BrConfiguration.Rules);
var documentFix = new DocumentFix(analyzerAndFixer, _printMock.Object);

// Act
Expand Down
Loading

0 comments on commit f5404b1

Please sign in to comment.