Skip to content

Commit

Permalink
adicionar testes
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-iel committed May 2, 2024
1 parent 5e2e8a1 commit c2bc4d9
Show file tree
Hide file tree
Showing 26 changed files with 333 additions and 61 deletions.
3 changes: 0 additions & 3 deletions src/ByReplace.Test/ByReplace.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
<ItemGroup>
<Folder Include="Common\Builders\" />
<Folder Include="Exceptions\" />
<Folder Include="Mappers\" />
<Folder Include="Matches\" />
<Folder Include="Models\" />
<Folder Include="Printers\" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using ByReplace.Commands.Apply.Rule;
using ByReplace.Models;
using ByReplace.Printers;
using ByReplace.Test.Analyzers;
using ByReplace.Test.Common.ConfigMock;
using ByReplace.Test.Common.FolderMock;
using Moq;
Expand Down Expand Up @@ -37,7 +36,7 @@ public ApplyRuleCommandTest()
FileSyntax.FileDeclaration("RootFile2.cs", "ITest = new Test()"));

_pathCompilationSyntax = PathFactory
.Compile(nameof(AnalyzerAndFixerTest))
.Compile(nameof(ApplyRuleCommandTest))
.AddMembers(rootFolder)
.AddBrConfiguration(configContent)
.Create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using ByReplace.Commands.Apply.Rules;
using ByReplace.Models;
using ByReplace.Printers;
using ByReplace.Test.Analyzers;
using ByReplace.Test.Common.ConfigMock;
using ByReplace.Test.Common.FolderMock;
using Moq;
Expand Down Expand Up @@ -36,7 +35,7 @@ public ApplyRulesCommandTEst()
FileSyntax.FileDeclaration("RootFile1.cs", "ITest = new Test()"),
FileSyntax.FileDeclaration("RootFile2.cs", "ITest = new Test()"));
_pathCompilationSyntax = PathFactory
.Compile(nameof(AnalyzerAndFixerTest))
.Compile(nameof(ApplyRulesCommandTEst))
.AddMembers(rootFolder)
.AddBrConfiguration(configContent)
.Create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using ByReplace.Commands.Rule.ListRules;
using ByReplace.Models;
using ByReplace.Printers;
using ByReplace.Test.Analyzers;
using ByReplace.Test.Common.ConfigMock;
using ByReplace.Test.Common.FolderMock;
using Moq;
Expand Down Expand Up @@ -37,7 +36,7 @@ public ListRulesCommandTest()
FileSyntax.FileDeclaration("RootFile2.cs", "ITest = new Test()"));

_pathCompilationSyntax = PathFactory
.Compile(nameof(AnalyzerAndFixerTest))
.Compile(nameof(ListRulesCommandTest))
.AddMembers(rootFolder)
.AddBrConfiguration(configContent)
.Create();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using ByReplace.Commands.Apply.Rule;
using ByReplace.Commands.Rule.ListRules;
using ByReplace.Commands.Rule.ListRules;
using ByReplace.Test.Common.Helpers;
using Cocona;
using Xunit;
Expand Down
5 changes: 2 additions & 3 deletions src/ByReplace.Test/Commands/Rule/ListRules/RulesBoxTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using ByReplace.Builders;
using ByReplace.Models;
using ByReplace.Printers;
using ByReplace.Test.Analyzers;
using ByReplace.Test.Common.ConfigMock;
using ByReplace.Test.Common.FolderMock;
using Moq;
Expand Down Expand Up @@ -38,7 +37,7 @@ public RulesBoxTest()
FileSyntax.FileDeclaration("RootFile2.cs", "ITest = new Test()"));

_pathCompilationSyntax = PathFactory
.Compile(nameof(AnalyzerAndFixerTest))
.Compile(nameof(RulesBoxTest))
.AddMembers(rootFolder)
.AddBrConfiguration(configContent)
.Create();
Expand Down Expand Up @@ -86,7 +85,7 @@ public void RulesBox_WhenInstantiate_ShouldValidateIfTwoObjectWithTheSameParamet
{
// Arrange
var rulesBoxFirst = new RulesBox(_brConfiguration.Rules);
var rulesBoxSecond = new RulesBox(ImmutableList<Models.Rule>.Empty);
var rulesBoxSecond = new RulesBox(ImmutableList<ByReplace.Models.Rule>.Empty);

// Act
var isEquals = rulesBoxFirst.Equals(rulesBoxSecond);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
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;
Expand Down Expand Up @@ -48,7 +45,7 @@ public OpenRuleCommandTest()
FileSyntax.FileDeclaration("RootFile2.cs", "ITest = new Test()"));

_pathCompilationSyntax = PathFactory
.Compile(nameof(AnalyzerAndFixerTest))
.Compile(nameof(OpenRuleCommandTest))
.AddMembers(rootFolder)
.AddBrConfiguration(configContent)
.Create();
Expand Down
5 changes: 1 addition & 4 deletions src/ByReplace.Test/Commands/Rule/OpenRule/RuleBoxTest.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
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;
Expand Down Expand Up @@ -44,7 +41,7 @@ public RuleBoxTest()
FileSyntax.FileDeclaration("RootFile2.cs", "ITest = new Test()"));

_pathCompilationSyntax = PathFactory
.Compile(nameof(AnalyzerAndFixerTest))
.Compile(nameof(RuleBoxTest))
.AddMembers(rootFolder)
.AddBrConfiguration(configContent)
.Create();
Expand Down
27 changes: 25 additions & 2 deletions src/ByReplace.Test/Commands/TimerFinish/TimerFinishCommandTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
namespace ByReplace.Test.Commands.TimerFinish
using ByReplace.Commands.TimerFinish;
using ByReplace.Printers;
using Moq;
using Xunit;

namespace ByReplace.Test.Commands.TimerFinish;

public class TimerFinishCommandTest
{
internal class TimerFinishCommandTest
private readonly Mock<IPrint> _printMock;

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

[Fact]
public async Task ExecuteAsync_ShouldCallPrintTimerOnce_WhenExecutionFinishes()
{
// Arrange
var command = new TimerFinishCommand(_printMock.Object);

// Act
await command.ExecuteAsync();

// Assert
_printMock.Verify(c => c.Timer(), Times.Once);
}
}
28 changes: 26 additions & 2 deletions src/ByReplace.Test/Commands/Version/NugetVersionTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
namespace ByReplace.Test.Commands.Version
using ByReplace.Commands.TimerFinish;
using ByReplace.Printers;
using Moq;
using Xunit;

namespace ByReplace.Test.Commands.Version
{
internal class NugetVersionTest
public class NugetVersionTest
{
public readonly Mock<IPrint> _printMock;

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

[Fact]
public async Task Execute_WhenCalledTimerToPrintTimeOfExecution_ShoulVerifyIfTimerWasCalledOnce()
{
// Arrange
var command = new TimerFinishCommand(_printMock.Object);

// Act
await command.ExecuteAsync(It.IsAny<CancellationToken>());

// Assert
_printMock.Verify(c => c.Timer(), Times.Once);
}
}
}
96 changes: 96 additions & 0 deletions src/ByReplace.Test/Mappers/DirectoryThreeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using ByReplace.Builders;
using ByReplace.Mappers;
using ByReplace.Models;
using ByReplace.Printers;
using ByReplace.Test.Common.ConfigMock;
using ByReplace.Test.Common.FolderMock;
using Moq;
using Xunit;

namespace ByReplace.Test.Mappers;

public class DirectoryThreeTest
{
private readonly PathCompilationSyntax _pathCompilationSyntax;
private readonly BrConfiguration _brConfiguration;
private readonly Mock<IPrint> _printMock;

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

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(DirectoryThreeTest))
.AddMembers(rootFolder)
.AddBrConfiguration(configContent)
.Create();

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

[Fact]
public void MapThreeSources_WhenStartTheSourceMap_ShouldReturnTheThreeFile()
{
// Arrange
var dirThree = new DirectoryThree(_printMock.Object);

// Act
var nodes = dirThree.MapThreeSources(_brConfiguration.Path);

// Assert
Assert.Equal(2, nodes.Count);
Assert.Collection(nodes,
entry =>
{
Assert.Single(entry.Files);
Assert.NotEmpty(entry.Parent);
Assert.NotEmpty(entry.Path);
Assert.Collection(entry.Files,
entry =>
{
Assert.Equal(".json", entry.Extension);
Assert.EndsWith("brconfig.json", entry.FullName);
Assert.Equal("brconfig.json", entry.Name);
});
},
entry =>
{
Assert.Equal(2, entry.Files.Count);
Assert.NotEmpty(entry.Parent);
Assert.NotEmpty(entry.Path);
Assert.Collection(entry.Files,
entry =>
{
Assert.Equal(".cs", entry.Extension);
Assert.EndsWith("RootFile1.cs", entry.FullName);
Assert.Equal("RootFile1.cs", entry.Name);
},
entry =>
{
Assert.Equal(".cs", entry.Extension);
Assert.EndsWith("RootFile2.cs", entry.FullName);
Assert.Equal("RootFile2.cs", entry.Name);
});
});
}
}
39 changes: 39 additions & 0 deletions src/ByReplace.Test/Matches/ExtensionMatchTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using ByReplace.Matches;
using Xunit;

namespace ByReplace.Test.Matches;

public class ExtensionMatchTest
{
[Theory]
[InlineData(".cs", ".cs", ".text", ".py")]
[InlineData(".text", ".cs", ".text", ".py")]
[InlineData(".py", ".cs", ".text", ".py")]
public void HasMatch_WhenTryMatchTheAllowExtensionFromFile_ShouldMatch(string extension, params string[] extensions)
{
// Arrange
var match = new ExtensionMatch(extension, extensions);

// Act
var hasMatch = match.HasMatch;

// Assert
Assert.True(hasMatch);
}

[Theory]
[InlineData(".cs", ".text", ".py")]
[InlineData(".text", ".cs", ".py")]
[InlineData(".py", ".cs", ".text")]
public void HasMatch_WhenTryMatchTheDenyExtensionFromFile_ShouldNotMatch(string extension, params string[] extensions)
{
// Arrange
var match = new ExtensionMatch(extension, extensions);

// Act
var hasMatch = match.HasMatch;

// Assert
Assert.False(hasMatch);
}
}
53 changes: 53 additions & 0 deletions src/ByReplace.Test/Matches/SkipMatchTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using ByReplace.Mappers;
using ByReplace.Matches;
using Xunit;

namespace ByReplace.Test.Matches;

public class SkipMatchTest
{
[Fact]
public void HasMatch_WhenTryMatchTheFileAndItNotMatchToAnyRuleToSkip_ShouldReturnFalse()
{
// Arrange
var fileMapperMock = new FileMapper(Guid.NewGuid(), "testFile.txt", "c://byreplace//testFile.txt", ".txt");

var match = new SkipMatch("c://byreplace//", fileMapperMock, new string[] { "Startup.cs", "Program.cs", "**/Controllers/*" });

// Act
var hasMatch = match.HasMatch;

// Assert
Assert.False(hasMatch);
}

[Fact]
public void HasMatch_WhenTryMatchSomeFileByDirectoryPath_ShouldReturnTrue()
{
// Arrange
var fileMapperMock = new FileMapper(Guid.NewGuid(), "testFile.txt", "c://byreplace//testFile.txt", ".txt");

var match = new SkipMatch("c://byreplace//", fileMapperMock, new string[] { "**/byreplace/*" });

// Act
var hasMatch = match.HasMatch;

// Assert
Assert.True(hasMatch);
}

[Fact]
public void HasMatch_WhenTryMatchSomeFileByDirectoryWithFileNamePath_ShouldReturnTrue()
{
// Arrange
var fileMapperMock = new FileMapper(Guid.NewGuid(), "testFile.txt", "c://byreplace//testFile.txt", ".txt");

var match = new SkipMatch("c://byreplace//", fileMapperMock, ["//byreplace//testFile.txt"]);

// Act
var hasMatch = match.HasMatch;

// Assert
Assert.True(hasMatch);
}
}
Loading

0 comments on commit c2bc4d9

Please sign in to comment.