Skip to content

Commit

Permalink
adicionando testes
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-iel committed May 2, 2024
1 parent 4e3acb3 commit 5e2e8a1
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 29 deletions.
6 changes: 3 additions & 3 deletions src/ByReplace.Test/Analyzers/DocumentFixTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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]
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ public async Task Execute_WhenPrintTheBoxWithTheRules_ShouldValidadeIfBoxWasPrin
await command.ExecuteAsync(It.IsAny<CancellationToken>());

// Assert
_printBoxMock.Verify(c => c.CreateBoxAndPrint(builder));
_printBoxMock.Verify(c => c.CreateBoxAndPrint(builder), Times.Once);
}
}
88 changes: 86 additions & 2 deletions src/ByReplace.Test/Commands/Rule/OpenRule/OpenRuleCommandTest.cs
Original file line number Diff line number Diff line change
@@ -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<IPrint> _printMock;
private readonly Mock<IPrintBox> _printBoxMock;

public OpenRuleCommandTest()
{
_printMock = new Mock<IPrint>();
_printBoxMock = new Mock<IPrintBox>();

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<CancellationToken>());

// 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<CancellationToken>());

// Assert
_printBoxMock.Verify(c => c.CreateBoxAndPrint(expectedPrintBox), Times.Once);
}
}

This file was deleted.

108 changes: 108 additions & 0 deletions src/ByReplace.Test/Commands/Rule/OpenRule/RuleBoxTest.cs
Original file line number Diff line number Diff line change
@@ -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<IPrint> _printMock;

public RuleBoxTest()
{
_printMock = new Mock<IPrint>();

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);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace ByReplace.Test.Commands.Version
{
internal class PrintBRVersionCommandTest
internal class VersionCommandTest
{
}
}
15 changes: 9 additions & 6 deletions src/ByReplace/Commands/Rule/OpenRule/OpenRuleCommand.cs
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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;
}
Expand Down
38 changes: 35 additions & 3 deletions src/ByReplace/Commands/Rule/OpenRule/RuleBox.cs
Original file line number Diff line number Diff line change
@@ -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<RuleBox>, IEqualityComparer<RuleBox>
{
private readonly Models.Rule _rule;

public PrintRuleBuilder(Models.Rule rule)
public RuleBox(Models.Rule rule)
{
_rule = rule;

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace ByReplace.Commands.Version;

internal class PrintBRVersionCommand : ICommand
internal class VersionCommand : ICommand
{
public async ValueTask ExecuteAsync(CancellationToken cancellationToken)
{
Expand Down
Loading

0 comments on commit 5e2e8a1

Please sign in to comment.