diff --git a/src/ByReplace.Test/Analyzers/DocumentFixTest.cs b/src/ByReplace.Test/Analyzers/DocumentFixTest.cs index cd1e4ff..ecff9b9 100644 --- a/src/ByReplace.Test/Analyzers/DocumentFixTest.cs +++ b/src/ByReplace.Test/Analyzers/DocumentFixTest.cs @@ -56,7 +56,7 @@ public DocumentFixTest() } [Fact] - public async Task ApplyAsync_WhenPassAllRules_ShoulApplyTheRulesInAllFilesAsync() + public async Task ApplyAsync_WhenPassAllRules_ShouldApplyTheRulesInAllFilesAsync() { // Arrange var analyzer = new Analyzer(_brConfiguration, _printMock.Object); @@ -75,7 +75,7 @@ public async Task ApplyAsync_WhenPassAllRules_ShoulApplyTheRulesInAllFilesAsync( Assert.Contains("var test = new Test()", fileContents); _printMock.Verify(x => x.Information("Initializing fixing."), Times.Once); _printMock.Verify(x => x.Information("Processing file [Cyan]RootFile1.cs"), Times.Once); - _printMock.Verify(x => x.Information("Appling rule [Cyan]RuleTest 1/1 on file [Cyan]RootFile1.cs."), Times.Once); + _printMock.Verify(x => x.Information("Applying rule [Cyan]RuleTest 1/1 on file [Cyan]RootFile1.cs."), Times.Once); } [Fact] @@ -106,6 +106,6 @@ public async Task ApplyAsync_WhenPassOnlyOneRule_ShoulApplyTheRuleInAllFiles() _printMock.Verify(x => x.Information("Initializing fixing."), Times.Once); _printMock.Verify(x => x.Information("Processing file [Cyan]RootFile1.cs"), Times.Once); - _printMock.Verify(x => x.Information("Appling rule [Cyan]RuleTest 1/1 on file [Cyan]RootFile1.cs."), Times.Once); + _printMock.Verify(x => x.Information("Applying rule [Cyan]RuleTest 1/1 on file [Cyan]RootFile1.cs."), Times.Once); } } diff --git a/src/ByReplace.Test/Commands/Rule/ListRules/ListRulesCommandTest.cs b/src/ByReplace.Test/Commands/Rule/ListRules/ListRulesCommandTest.cs index 0951910..4f8d76e 100644 --- a/src/ByReplace.Test/Commands/Rule/ListRules/ListRulesCommandTest.cs +++ b/src/ByReplace.Test/Commands/Rule/ListRules/ListRulesCommandTest.cs @@ -61,6 +61,6 @@ public async Task Execute_WhenPrintTheBoxWithTheRules_ShouldValidadeIfBoxWasPrin await command.ExecuteAsync(It.IsAny()); // Assert - _printBoxMock.Verify(c => c.CreateBoxAndPrint(builder)); + _printBoxMock.Verify(c => c.CreateBoxAndPrint(builder), Times.Once); } } diff --git a/src/ByReplace.Test/Commands/Rule/OpenRule/OpenRuleCommandTest.cs b/src/ByReplace.Test/Commands/Rule/OpenRule/OpenRuleCommandTest.cs index 3e1b242..d66d112 100644 --- a/src/ByReplace.Test/Commands/Rule/OpenRule/OpenRuleCommandTest.cs +++ b/src/ByReplace.Test/Commands/Rule/OpenRule/OpenRuleCommandTest.cs @@ -1,6 +1,90 @@ -namespace ByReplace.Test.Commands.Rule.OpenRule +using ByReplace.Builders; +using ByReplace.Commands.Rule.ListRules; +using ByReplace.Commands.Rule.OpenRule; +using ByReplace.Models; +using ByReplace.Printers; +using ByReplace.Test.Analyzers; +using ByReplace.Test.Common.ConfigMock; +using ByReplace.Test.Common.FolderMock; +using Moq; +using System.Data; +using Xunit; + +namespace ByReplace.Test.Commands.Rule.OpenRule; + +public class OpenRuleCommandTest { - internal class OpenRuleCommandTest + private readonly PathCompilationSyntax _pathCompilationSyntax; + private readonly BrConfiguration _brConfiguration; + private readonly Mock _printMock; + private readonly Mock _printBoxMock; + + public OpenRuleCommandTest() { + _printMock = new Mock(); + _printBoxMock = new Mock(); + + var configContent = BrContentFactory + .CreateDefault() + .AddConfig(BrContentFactory.ConfigNoPathDeclaration("obj", ".bin")) + .AddRules( + BrContentFactory + .Rule("RuleOne") + .WithExtensions(".cs", ".txt") + .WithSkips("**\\Controllers\\*", "bin\\bin1.txt", "obj\\obj2.txt") + .WithReplacement(BrContentFactory.Replacement("OldText", "NewText")), + BrContentFactory + .Rule("RuleTwo") + .WithExtensions(".cs") + .WithSkips("**\\Controllers\\*", "bin\\bin1.txt", "obj\\obj2.txt") + .WithReplacement(BrContentFactory.Replacement("MyOldText", "MyNewText")) + ) + .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(); + } + + [Fact] + public async Task Execute_WhenNotFindTheRuleOnRulesConfiguration_ShouldValidateTheLogThatShowRuleWasNotFind() + { + // Arrange + var command = new OpenRuleCommand(_brConfiguration, "NotConfiguratedRule", _printMock.Object, _printBoxMock.Object); + + // Act + await command.ExecuteAsync(It.IsAny()); + + // Assert + _printMock.Verify(c => c.Warning("Rule named NotConfiguratedRule was not found on brconfig file")); + } + + [Fact] + public async Task Execute_WhenFindTheRuleOnRulesConfiguration_ShouldValidateIfThePrintBoxWasCalled() + { + // Arrange + var expectedRule = _brConfiguration.Rules.Last(); + var expectedPrintBox = new RuleBox(expectedRule); + var command = new OpenRuleCommand(_brConfiguration, "RuleTwo", _printMock.Object, _printBoxMock.Object); + + // Act + await command.ExecuteAsync(It.IsAny()); + + // Assert + _printBoxMock.Verify(c => c.CreateBoxAndPrint(expectedPrintBox), Times.Once); } } diff --git a/src/ByReplace.Test/Commands/Rule/OpenRule/PrintRuleBuilderTest.cs b/src/ByReplace.Test/Commands/Rule/OpenRule/PrintRuleBuilderTest.cs deleted file mode 100644 index 0f8a8f9..0000000 --- a/src/ByReplace.Test/Commands/Rule/OpenRule/PrintRuleBuilderTest.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace ByReplace.Test.Commands.Rule.OpenRule -{ - internal class PrintRuleBuilderTest - { - } -} diff --git a/src/ByReplace.Test/Commands/Rule/OpenRule/RuleBoxTest.cs b/src/ByReplace.Test/Commands/Rule/OpenRule/RuleBoxTest.cs new file mode 100644 index 0000000..6ed6ef6 --- /dev/null +++ b/src/ByReplace.Test/Commands/Rule/OpenRule/RuleBoxTest.cs @@ -0,0 +1,108 @@ +using ByReplace.Builders; +using ByReplace.Commands.Rule.ListRules; +using ByReplace.Commands.Rule.OpenRule; +using ByReplace.Models; +using ByReplace.Printers; +using ByReplace.Test.Analyzers; +using ByReplace.Test.Common.ConfigMock; +using ByReplace.Test.Common.FolderMock; +using Moq; +using System.Collections.Immutable; +using Xunit; + +namespace ByReplace.Test.Commands.Rule.OpenRule; + +public class RuleBoxTest +{ + private readonly PathCompilationSyntax _pathCompilationSyntax; + private readonly BrConfiguration _brConfiguration; + private readonly Mock _printMock; + + public RuleBoxTest() + { + _printMock = new Mock(); + + var configContent = BrContentFactory + .CreateDefault() + .AddConfig(BrContentFactory.ConfigNoPathDeclaration("obj", ".bin")) + .AddRules(BrContentFactory + .Rule("RuleOne") + .WithExtensions(".cs", ".txt") + .WithSkips("**\\Controllers\\*", "bin\\bin1.txt", "obj\\obj2.txt") + .WithReplacement(BrContentFactory.Replacement("OldText", "NewText")), + BrContentFactory + .Rule("RuleTwo") + .WithExtensions(".cs") + .WithSkips("**\\Controllers\\*", "bin\\bin1.txt", "obj\\obj2.txt") + .WithReplacement(BrContentFactory.Replacement("MyOldText", "MyNewText"))) + .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(); + } + + [Fact] + public void RuleBox_WhenInstantiate_ShouldValidateTheBoxConfiguration() + { + // Arrange & Act + var ruleBox = new RuleBox(_brConfiguration.Rules.First()); + + // Assert + Assert.Equal(100, ruleBox.Width); + Assert.Equal(5 * 2, ruleBox.Height); + Assert.Equal("Rule", ruleBox.BoxName); + } + + [Fact] + public void RuleBox_WhenInstantiate_ShouldValidateIfTwoObjectWithTheSameParametersAreEquals() + { + // Arrange + var ruleBoxFirst = new RuleBox(_brConfiguration.Rules.First()); + var ruleBoxSecond = new RuleBox(_brConfiguration.Rules.First()); + + // Act + var isEquals = ruleBoxFirst.Equals(ruleBoxSecond); + var isEqualsLikeObject = ruleBoxFirst.Equals((object)ruleBoxSecond); + var hasTheSameHashcode = ruleBoxFirst.GetHashCode() == ruleBoxSecond.GetHashCode(); + + // Assert + Assert.Equal(ruleBoxSecond, ruleBoxFirst); + Assert.True(isEquals); + Assert.True(isEqualsLikeObject); + Assert.True(hasTheSameHashcode); + } + + [Fact] + public void RuleBox_WhenInstantiate_ShouldValidateIfTwoObjectWithTheSameParametersAreNotEquals() + { + // Arrange + var ruleBoxFirst = new RuleBox(_brConfiguration.Rules.First()); + var ruleBoxSecond = new RuleBox(_brConfiguration.Rules.Last()); + + // Act + var isEquals = ruleBoxFirst.Equals(ruleBoxSecond); + var isEqualsLikeObject = ruleBoxFirst.Equals((object)ruleBoxSecond); + var hasTheSameHashcode = ruleBoxFirst.GetHashCode() == ruleBoxSecond.GetHashCode(); + + // Assert + Assert.NotEqual(ruleBoxSecond, ruleBoxFirst); + Assert.False(isEquals); + Assert.False(isEqualsLikeObject); + Assert.False(hasTheSameHashcode); + } +} diff --git a/src/ByReplace.Test/Commands/Version/PrintBRVersionCommandTest.cs b/src/ByReplace.Test/Commands/Version/VersionCommandTest.cs similarity index 57% rename from src/ByReplace.Test/Commands/Version/PrintBRVersionCommandTest.cs rename to src/ByReplace.Test/Commands/Version/VersionCommandTest.cs index 7e62405..f6fbc4c 100644 --- a/src/ByReplace.Test/Commands/Version/PrintBRVersionCommandTest.cs +++ b/src/ByReplace.Test/Commands/Version/VersionCommandTest.cs @@ -1,6 +1,6 @@ namespace ByReplace.Test.Commands.Version { - internal class PrintBRVersionCommandTest + internal class VersionCommandTest { } } diff --git a/src/ByReplace/Commands/Rule/OpenRule/OpenRuleCommand.cs b/src/ByReplace/Commands/Rule/OpenRule/OpenRuleCommand.cs index 0fc95ad..d790684 100644 --- a/src/ByReplace/Commands/Rule/OpenRule/OpenRuleCommand.cs +++ b/src/ByReplace/Commands/Rule/OpenRule/OpenRuleCommand.cs @@ -1,23 +1,27 @@ -namespace ByReplace.Commands.Rule.OpenRule; +using ByReplace.Printers; + +namespace ByReplace.Commands.Rule.OpenRule; internal class OpenRuleCommand : ICommand { private readonly BrConfiguration configuration; private readonly string ruleName; private readonly IPrint print; + private readonly IPrintBox printBox; - public OpenRuleCommand(BrConfiguration configuration, string ruleName, IPrint print) + public OpenRuleCommand(BrConfiguration configuration, string ruleName, IPrint print, IPrintBox printBox) { this.configuration = configuration; this.ruleName = ruleName; this.print = print; + this.printBox = printBox; } public ValueTask ExecuteAsync(CancellationToken cancellationToken = default) { Models.Rule rule = configuration .Rules - .Where(c => c.Name.Equals(ruleName.Trim(), StringComparison.InvariantCulture)) + .Where(c => c.Name.Equals(ruleName.Trim(), StringComparison.CurrentCultureIgnoreCase)) .FirstOrDefault(); if (rule is null) @@ -27,10 +31,9 @@ public ValueTask ExecuteAsync(CancellationToken cancellationToken = default) return ValueTask.CompletedTask; } - PrintRuleBuilder builder = new PrintRuleBuilder(rule); + RuleBox builder = new RuleBox(rule); - PrintBox printer = new PrintBox(); - printer.CreateBoxAndPrint(builder); + printBox.CreateBoxAndPrint(builder); return ValueTask.CompletedTask; } diff --git a/src/ByReplace/Commands/Rule/OpenRule/RuleBox.cs b/src/ByReplace/Commands/Rule/OpenRule/RuleBox.cs index dd21e9b..35bffca 100644 --- a/src/ByReplace/Commands/Rule/OpenRule/RuleBox.cs +++ b/src/ByReplace/Commands/Rule/OpenRule/RuleBox.cs @@ -1,10 +1,12 @@ -namespace ByReplace.Commands.Rule.OpenRule; +using ByReplace.Commands.Rule.ListRules; -internal class PrintRuleBuilder : IBox +namespace ByReplace.Commands.Rule.OpenRule; + +internal sealed class RuleBox : IBox, IEquatable, IEqualityComparer { private readonly Models.Rule _rule; - public PrintRuleBuilder(Models.Rule rule) + public RuleBox(Models.Rule rule) { _rule = rule; @@ -31,4 +33,34 @@ public string GetValuesToPrint() return sb.ToString(); } + + public bool Equals(RuleBox other) + { + return Width == other.Width && + Height == other.Height && + BoxName == other.BoxName && + _rule == other._rule; + } + + public override bool Equals(object obj) + { + RuleBox other = (RuleBox)obj; + + return other.Equals(this); + } + + public bool Equals(RuleBox x, RuleBox y) + { + return x.Equals(y); + } + + public override int GetHashCode() + { + return HashCode.Combine(Width, Height, BoxName, _rule); + } + + public int GetHashCode([DisallowNull] RuleBox obj) + { + return HashCode.Combine(obj.Width, obj.Height, obj.BoxName, obj._rule); + } } \ No newline at end of file diff --git a/src/ByReplace/Commands/Version/PrintBRVersionCommand.cs b/src/ByReplace/Commands/Version/VersionCommand.cs similarity index 84% rename from src/ByReplace/Commands/Version/PrintBRVersionCommand.cs rename to src/ByReplace/Commands/Version/VersionCommand.cs index 86bb1da..12d52f7 100644 --- a/src/ByReplace/Commands/Version/PrintBRVersionCommand.cs +++ b/src/ByReplace/Commands/Version/VersionCommand.cs @@ -1,6 +1,6 @@ namespace ByReplace.Commands.Version; -internal class PrintBRVersionCommand : ICommand +internal class VersionCommand : ICommand { public async ValueTask ExecuteAsync(CancellationToken cancellationToken) { diff --git a/src/ByReplace/Program.cs b/src/ByReplace/Program.cs index 27c9691..7ab7a85 100644 --- a/src/ByReplace/Program.cs +++ b/src/ByReplace/Program.cs @@ -43,7 +43,7 @@ CompositeCommand compositeCommand = new CompositeCommand(new ICommand[] { new PrintLogoCommand(print), - new PrintBRVersionCommand(), + new VersionCommand(), new ApplyRuleCommand(configuration, applyRuleParameters, print), new TimerFinishCommand(print) }); @@ -65,7 +65,7 @@ CompositeCommand compositeCommand = new CompositeCommand(new ICommand[] { new PrintLogoCommand(print), - new PrintBRVersionCommand(), + new VersionCommand(), new ApplyRulesCommand(configuration, print), new TimerFinishCommand(print) }); @@ -94,7 +94,7 @@ CompositeCommand compositeCommand = new CompositeCommand(new ICommand[] { new PrintLogoCommand(print), - new PrintBRVersionCommand(), + new VersionCommand(), new ListRulesCommand(configuration, printBox), new TimerFinishCommand(print) }); @@ -102,7 +102,7 @@ await compositeCommand.ExecuteAsync(token); }); - rule.AddCommand("open-rule", async (OpenRuleParameter openRuleParameter, IPrint print) => + rule.AddCommand("open-rule", async (OpenRuleParameter openRuleParameter, IPrint print, IPrintBox printBox) => { //Print rule in config file CancellationTokenSource source = new CancellationTokenSource(); @@ -116,8 +116,8 @@ CompositeCommand compositeCommand = new CompositeCommand(new ICommand[] { new PrintLogoCommand(print), - new PrintBRVersionCommand(), - new OpenRuleCommand(configuration, openRuleParameter.Name , print), + new VersionCommand(), + new OpenRuleCommand(configuration, openRuleParameter.Name, print, printBox), new TimerFinishCommand(print) });