Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code improvements #2

Merged
merged 7 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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<AnalyzerAndFixerTest>>
{
private readonly PathCompilationSyntax _pathCompilationSyntax;
private readonly BrConfiguration _brConfiguration;
private readonly WorkspaceFixture<AnalyzerAndFixerTest> _fixture;
private readonly Mock<IPrint> _printMock;

public AnalyzerAndFixerTest()
public AnalyzerAndFixerTest(WorkspaceFixture<AnalyzerAndFixerTest> fixture)
{
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();

_fixture = fixture;
_printMock = new Mock<IPrint>();
}

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

// Act
var directoryNode = analyzer.LoadThreeFiles().Last();
analyzerAndFixer.TryMatchRule(directoryNode, _brConfiguration.Rules);
analyzerAndFixer.TryMatchRule(directoryNode, _fixture.WorkspaceSyntax.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(_fixture.WorkspaceSyntax.BrConfiguration, _printMock.Object);
var analyzerAndFixer = new AnalyzerAndFixer(_printMock.Object);

// Act
var directoryNode = analyzer.LoadThreeFiles().Last();
analyzerAndFixer.TryMatchRule(directoryNode, _brConfiguration.Rules);
analyzerAndFixer.TryMatchRule(directoryNode, _fixture.WorkspaceSyntax.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);
}
}
}
100 changes: 45 additions & 55 deletions src/ByReplace.Test/Analyzers/AnalyzerRunnerTest.cs
Original file line number Diff line number Diff line change
@@ -1,77 +1,67 @@
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<AnalyzerRunnerTest>>
{
private readonly PathCompilationSyntax _pathCompilationSyntax;
private readonly BrConfiguration _brConfiguration;
private readonly WorkspaceFixture<AnalyzerRunnerTest> _fixture;
private readonly Mock<IPrint> _printMock;

public AnalyzerRunnerTest()
public AnalyzerRunnerTest(WorkspaceFixture<AnalyzerRunnerTest> fixture)
{
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()"));

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

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

var objFolder = FolderSyntax.FolderDeclaration("obj")
.AddParent(rootFolder)
.AddMembers(
FileSyntax.FileDeclaration("obj1.txt", "ITest = new Test()"),
FileSyntax.FileDeclaration("obj2.txt", "ITest = new Test()"));
_fixture = fixture;
_printMock = new Mock<IPrint>();

_pathCompilationSyntax = PathFactory
.Compile(nameof(AnalyzerRunnerTest))
.AddMembers(controllerFolder, binFolder, objFolder)
.AddBrConfiguration(configContent)
fixture.ClearPrevious();

_fixture.WorkspaceSyntax = new WorkspaceSyntax(nameof(AnalyzerRunnerTest))
.BRContent(c =>
{
c.AddPath("")
.AddSkip("obj", ".bin")
.AddRules(
ruleOne => ruleOne
.WithName("RuleTest")
.WithExtensions(".cs", ".txt")
.WithSkips("**\\Controllers\\*", "bin\\bin1.txt", "obj\\obj2.txt")
.WithReplacement(BrContentFactory.Replacement("Test", "Test2")));
})
.Folder(folderStructure =>
{
folderStructure
.AddFile(FileSyntax.FileDeclaration("RootFile1.cs", "ITest = new Test()"))
.AddFile(FileSyntax.FileDeclaration("RootFile2.cs", "ITest = new Test()"))
.AddFolder("Controllers", c =>
{
c.AddFile(FileSyntax.FileDeclaration("Controller1.cs", "ITest2 = new Test()"));
c.AddFile(FileSyntax.FileDeclaration("Controller2.cs", "ITest2 = new Test()"));
})
.AddFolder("bin", c =>
{
c.AddFile(FileSyntax.FileDeclaration("bin1.txt", "ITest = new Test()"));
c.AddFile(FileSyntax.FileDeclaration("bin2.txt", "ITest = new Test()"));
})
.AddFolder("obj", c =>
{
c.AddFile(FileSyntax.FileDeclaration("obj1.txt", "ITest = new Test()"));
c.AddFile(FileSyntax.FileDeclaration("obj2.txt", "ITest = new Test()"));
});
})
.Create();

_brConfiguration = BrConfigurationBuilder
.Create()
.SetPath($"./{_pathCompilationSyntax.InternalIdentifier}")
.SetConfigPath($"./{_pathCompilationSyntax.InternalIdentifier}")
.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(_fixture.WorkspaceSyntax.BrConfiguration, _printMock.Object);
var analyzerRunner = new AnalyzerRunner(_fixture.WorkspaceSyntax.BrConfiguration, _printMock.Object);

// Act
var directoryNodes = analyzer.LoadThreeFiles();
Expand Down Expand Up @@ -114,8 +104,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(_fixture.WorkspaceSyntax.BrConfiguration, _printMock.Object);
var analyzerRunner = new AnalyzerRunner(_fixture.WorkspaceSyntax.BrConfiguration, _printMock.Object);

// Act
var directoryNodes = analyzer.LoadThreeFiles();
Expand Down
46 changes: 20 additions & 26 deletions src/ByReplace.Test/Analyzers/AnalyzerTest.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,42 @@
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<AnalyzerTest>>
{
private readonly PathCompilationSyntax _pathCompilationSyntax;
private readonly BrConfiguration _brConfiguration;
private readonly WorkspaceFixture<AnalyzerTest> _fixture;
private readonly Mock<IPrint> _printMock;

public AnalyzerTest()
public AnalyzerTest(WorkspaceFixture<AnalyzerTest> fixture)
{
var rootFolder = FolderSyntax
.FolderDeclaration("RootFolder")
.AddMembers(FileSyntax.FileDeclaration("FileOne.cs", "ITest = new Test()"));

var firstLevel = FolderSyntax.FolderDeclaration("FirstLevel")
.AddParent(rootFolder)
.AddMembers(FileSyntax.FileDeclaration("FileSecond.cs", "ITest2 = new Test()"));

_pathCompilationSyntax = PathFactory
.Compile(nameof(AnalyzerTest))
.AddMembers(firstLevel)
.Create();
_fixture = fixture;
_printMock = new Mock<IPrint>();

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

_printMock = new Mock<IPrint>();
_fixture.WorkspaceSyntax = new WorkspaceSyntax(nameof(AnalyzerTest))
.Folder(folderStructure =>
{
folderStructure
.AddFile(FileSyntax.FileDeclaration("FileOne.cs", "ITest = new Test()"))
.AddFolder("FirstLevel", c =>
{
c.AddFile(FileSyntax.FileDeclaration("FileSecond.cs", "ITest2 = new Test()"));
});
})
.Create();
}

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

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

// Act
var directoryNodes = analyzer.LoadThreeFiles();
Expand Down
Loading
Loading