Skip to content

Commit

Permalink
Add tests for AnalyzersTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-iel committed Dec 18, 2023
1 parent a57274d commit cad50f1
Show file tree
Hide file tree
Showing 20 changed files with 363 additions and 39 deletions.
11 changes: 11 additions & 0 deletions src/ByReplace.Test/Analyzers/AnalyzerRunnerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ByReplace.Test.Analyzers;

internal class AnalyzerRunnerTest
{
}
11 changes: 11 additions & 0 deletions src/ByReplace.Test/Analyzers/AnalyzersAndFixersTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ByReplace.Test.Analyzers;

internal class AnalyzersAndFixersTest
{
}
69 changes: 69 additions & 0 deletions src/ByReplace.Test/Analyzers/AnalyzersTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using ByReplace.Analyzers;
using ByReplace.Builders;
using ByReplace.Models;
using ByReplace.Printers;
using ByReplace.Test.Common.FolderMock;
using Moq;
using Xunit;

namespace ByReplace.Test.Analyzers;

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

public AnalyzersTest()
{
var levelOne = FolderSyntax
.FolderDeclaration("OneLevel")
.AddMenbers(FileSyntax.FileDeclaration("FileOne.cs", "ITest = new Test()"));

var SecondOne = FolderSyntax.FolderDeclaration("SecondLevel")
.AddParent(levelOne)
.AddMenbers(FileSyntax.FileDeclaration("FileSecond.cs", "ITest2 = new Test()"));

_pathCompilationSyntax = PathFactory
.Compile(nameof(AnalyzersTest))
.AddFolders(SecondOne)
.CreateThreeFolder();

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

_printMock = new Mock<IPrint>();
}

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

// Act
var directoryNodes = analyzer.LoadThreeFiles();

// Assert
Assert.Equal(3, directoryNodes.Count);
Assert.Single(directoryNodes[0].Files);
Assert.Single(directoryNodes[1].Files);
Assert.Single(directoryNodes[2].Files);
}

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

// Act
var directoryNodes = analyzer.LoadThreeFiles();

// Assert
_printMock.Verify(x => x.Information("Identifying folder three files."), Times.Once);
}
}
11 changes: 11 additions & 0 deletions src/ByReplace.Test/Analyzers/DocumentFixTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ByReplace.Test.Analyzers;

internal class DocumentFixTest
{
}
27 changes: 22 additions & 5 deletions src/ByReplace.Test/ByReplace.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -13,9 +13,12 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
<PackageReference Include="xunit" Version="2.6.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand All @@ -25,4 +28,18 @@
</PackageReference>
</ItemGroup>

<ItemGroup>
<Folder Include="Common\Builders\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ByReplace\ByReplace.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="brconfig.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions src/ByReplace.Test/Common/ConfigMock/ConfigFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace ByReplace.Test.Common.ConfigMock;

public class ConfigFactory
{

}
30 changes: 30 additions & 0 deletions src/ByReplace.Test/Common/FolderMock/FileSyntax.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace ByReplace.Test.Common.FolderMock;

internal sealed class FileSyntax
{
public FileSyntax(string name)
{
Name = name;
}

public FileSyntax(string name, string content)
{
Name = name;
Content = content;
}

public string Name { get; }
public string Content { get; }

public string Extension => System.IO.Path.GetExtension(Name);

public static FileSyntax FileDeclaration(string name)
{
return new FileSyntax(name);
}

public static FileSyntax FileDeclaration(string name, string content)
{
return new FileSyntax(name, content);
}
}
57 changes: 57 additions & 0 deletions src/ByReplace.Test/Common/FolderMock/FolderSyntax.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
namespace ByReplace.Test.Common.FolderMock;

internal sealed class FolderSyntax
{
public FolderSyntax(string name)
{
Name = name;
Files = new List<FileSyntax>();
}

public FolderSyntax(string name, FolderSyntax parent)
{
Name = name;
Parent = parent;
Files = new List<FileSyntax>();
}

public FolderSyntax(string name, FolderSyntax parent, List<FileSyntax> files)
{
Name = name;
Parent = parent;
Files = files;
}

public FolderSyntax Parent { get; private set; }

public string Name { get; private set; }

public List<FileSyntax> Files { get; private set; }

public static FolderSyntax FolderDeclaration(string name)
{
return new FolderSyntax(name);
}

public static FolderSyntax FolderDeclaration(string name, FolderSyntax parent)
{
return new FolderSyntax(name, parent);
}

public static FolderSyntax FolderDeclaration(string name, FolderSyntax parent, List<FileSyntax> files)
{
return new FolderSyntax(name, parent, files);
}

public FolderSyntax AddMenbers(FileSyntax fileSyntax)
{
this.Files.Add(fileSyntax);
return this;
}

public FolderSyntax AddParent(FolderSyntax parent)
{
this.Parent = parent;
return this;
}
}
82 changes: 82 additions & 0 deletions src/ByReplace.Test/Common/FolderMock/PathCompilationSyntax.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System.Runtime.InteropServices;
namespace ByReplace.Test.Common.FolderMock;

internal sealed class PathCompilationSyntax
{
public List<FolderSyntax> Folders = new List<FolderSyntax>();

public string InternalIdentifier { get; private set; }

public PathCompilationSyntax()
{
InternalIdentifier = Guid.NewGuid().ToString();
}

public PathCompilationSyntax(string testCase)
{
InternalIdentifier = $"{testCase}_{Guid.NewGuid()}";
}

public PathCompilationSyntax AddFolders(params FolderSyntax[] foldersSyntax)
{
this.Folders.AddRange(foldersSyntax);

return this;
}

public PathCompilationSyntax AddFolder(FolderSyntax folderSyntax)
{
this.Folders.Add(folderSyntax);

return this;
}

public PathCompilationSyntax AddFolder(string name)
{
this.Folders.Add(new FolderSyntax(name));

return this;
}

public PathCompilationSyntax CreateThreeFolder()
{
foreach (ref var folder in CollectionsMarshal.AsSpan(Folders))
{
CreateThreeFolder(folder);
}

//TODO: Código temporário até o ConfigFactory ser implementado.
File.Copy("./brconfig.json", $"./{InternalIdentifier}/brconfig.json", true);

return this;
}

public PathCompilationSyntax CreateThreeFolder(FolderSyntax folderSyntax)
{
if (folderSyntax is null)
{
return this;
}

if (folderSyntax.Parent is not null)
{
CreateThreeFolder(folderSyntax.Parent);
}

var dirPath = folderSyntax.Parent is not null
? $"./{InternalIdentifier}/{folderSyntax.Parent.Name}/{folderSyntax.Name}"
: $"./{InternalIdentifier}/{folderSyntax.Name}";

if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}

foreach (ref var file in CollectionsMarshal.AsSpan(folderSyntax.Files))
{
File.WriteAllText($"{dirPath}/{file.Name}", file.Content);
}

return this;
}
}
7 changes: 7 additions & 0 deletions src/ByReplace.Test/Common/FolderMock/PathFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ByReplace.Test.Common.FolderMock;

internal sealed class PathFactory
{
public static PathCompilationSyntax Compile() => new PathCompilationSyntax();
public static PathCompilationSyntax Compile(string testCase) => new PathCompilationSyntax(testCase);
}
16 changes: 16 additions & 0 deletions src/ByReplace.Test/brconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"Path": "C:\\Users\\iel_1\\Documents\\TestLieu",
"SkipDirectories": [ "bin", ".vs", "object", "git" ],
"Rules": [
{
"Name": "RemoveServiceBus",
"Description": "Substitui a implementação da mensageria para AwsEventBroker",
"Skip": [ "Startup.cs", "Program.cs", "**/Controllers/*" ],
"Extensions": [ ".cs", ".json" ],
"Replacement": {
"Old": [ "this._eventBus", "this.eventBus" ],
"New": "this._awsEventBroker"
}
}
]
}
6 changes: 4 additions & 2 deletions src/ByReplace/Analyzers/Analyzer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace ByReplace.Analyzers;
[assembly: InternalsVisibleTo("ByReplace.Test")]

namespace ByReplace.Analyzers;

internal class Analyzer
{
Expand All @@ -13,7 +15,7 @@ public Analyzer(BrConfiguration brConfiguration, IPrint print)

internal ImmutableList<DirectoryNode> LoadThreeFiles()
{
print.Information($"Identifying folder three files.");
print.Information("Identifying folder three files.");

DirectoryThree directoryThree = new DirectoryThree(brConfiguration.Path);
directoryThree.MapThreeSources();
Expand Down
6 changes: 4 additions & 2 deletions src/ByReplace/Analyzers/AnalyzerRunner.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace ByReplace.Analyzers;
[assembly: InternalsVisibleTo("ByReplace.Test")]

namespace ByReplace.Analyzers;

internal class AnalyzerRunner
{
Expand All @@ -13,7 +15,7 @@ internal AnalyzerRunner(BrConfiguration brConfiguration, IPrint print)

internal AnalyzersAndFixers RunAnalysis(ImmutableList<DirectoryNode> directoryThree, Analyses diagnostic)
{
print.Information($"Identifying folder three files.");
print.Information("Identifying rules that has matches.");

AnalyzersAndFixers analyzersAndFixers = new AnalyzersAndFixers(this.print);

Expand Down
4 changes: 3 additions & 1 deletion src/ByReplace/Analyzers/AnalyzersAndFixers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace ByReplace.Analyzers;
[assembly: InternalsVisibleTo("ByReplace.Test")]

namespace ByReplace.Analyzers;

internal class AnalyzersAndFixers : Dictionary<FileMapper, List<Rule>>
{
Expand Down
Loading

0 comments on commit cad50f1

Please sign in to comment.