Skip to content

Commit

Permalink
Merge pull request #68 from Ashampoo/develop
Browse files Browse the repository at this point in the history
fix: get correct format provider
  • Loading branch information
tjorvenK authored Feb 28, 2024
2 parents 4128e5d + bb61be5 commit 89745f0
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 4 deletions.
6 changes: 6 additions & 0 deletions Ashampoo-Translation-Systems.sln
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ashampoo.Translation.System
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ashampoo.Translation.Systems.Formats.JavaProperties.Test", "Ashampoo.Translation.Systems.Formats.JavaProperties.Test\Ashampoo.Translation.Systems.Formats.JavaProperties.Test.csproj", "{B31DF60F-D7D6-4D9A-BE2A-3A7089E27118}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ashampoo.Translation.Systems.Formats.Tests", "Ashampoo.Translation.Systems.Formats.Tests\Ashampoo.Translation.Systems.Formats.Tests.csproj", "{AB03EE65-2741-4FB4-9056-0239A824FD6C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -76,5 +78,9 @@ Global
{B31DF60F-D7D6-4D9A-BE2A-3A7089E27118}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B31DF60F-D7D6-4D9A-BE2A-3A7089E27118}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B31DF60F-D7D6-4D9A-BE2A-3A7089E27118}.Release|Any CPU.Build.0 = Release|Any CPU
{AB03EE65-2741-4FB4-9056-0239A824FD6C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AB03EE65-2741-4FB4-9056-0239A824FD6C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AB03EE65-2741-4FB4-9056-0239A824FD6C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AB03EE65-2741-4FB4-9056-0239A824FD6C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/>
<PackageReference Include="xunit" Version="2.4.2"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\src\Ashampoo.Translation.Systems.Formats\src\Ashampoo.Translation.Systems.Formats.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using Ashampoo.Translation.Systems.Formats.Abstractions;
using Ashampoo.Translation.Systems.Formats.AshLang;
using Ashampoo.Translation.Systems.Formats.Gengo;
using Ashampoo.Translation.Systems.Formats.JavaProperties;
using Ashampoo.Translation.Systems.Formats.Json;
using Ashampoo.Translation.Systems.Formats.NLang;
using Ashampoo.Translation.Systems.Formats.PO;
using Ashampoo.Translation.Systems.Formats.ResX;
using Ashampoo.Translation.Systems.Formats.TsProj;
using FluentAssertions;

namespace Ashampoo.Translation.Systems.Formats.Tests;

public sealed class DefaultFormatFactoryTests
{
public static IEnumerable<object[]> GetTestData()
{
yield return [new AshLangFormat(), typeof(AshLangFormatProvider)];
yield return [new GengoFormat(), typeof(GengoFormatProvider)];
yield return [new JavaPropertiesFormat(), typeof(JavaPropertiesFormatProvider)];
yield return [new JsonFormat(), typeof(JsonFormatProvider)];
yield return [new NLangFormat(), typeof(NLangFormatProvider)];
yield return [new POFormat(), typeof(POFormatProvider)];
yield return [new ResXFormat(), typeof(ResXFormatProvider)];
yield return [new TsProjFormat(), typeof(TsProjFormatProvider)];
}


[Theory]
[MemberData(nameof(GetTestData))]
public void GetFormatProvider_FromFormat(IFormat format, Type providerType)
{
IEnumerable<IFormatProvider<IFormat>> providers =
[
new AshLangFormatProvider(), new GengoFormatProvider(), new JavaPropertiesFormatProvider(),
new JsonFormatProvider(), new NLangFormatProvider(), new POFormatProvider(), new ResXFormatProvider(),
new TsProjFormatProvider()
];

var formatFactory = new DefaultFormatFactory(providers);
var formatProvider = formatFactory.GetFormatProvider(format);
formatProvider.Should().BeOfType(providerType);
}


[Theory]
[InlineData("ashlang", typeof(AshLangFormatProvider))]
[InlineData("gengo", typeof(GengoFormatProvider))]
[InlineData("javaProperties", typeof(JavaPropertiesFormatProvider))]
[InlineData("json", typeof(JsonFormatProvider))]
[InlineData("nlang", typeof(NLangFormatProvider))]
[InlineData("po", typeof(POFormatProvider))]
[InlineData("resx", typeof(ResXFormatProvider))]
[InlineData("tsproj", typeof(TsProjFormatProvider))]
public void GetFormatProvider_FromId(string formatId, Type providerType)
{
IEnumerable<IFormatProvider<IFormat>> providers =
[
new AshLangFormatProvider(), new GengoFormatProvider(), new JavaPropertiesFormatProvider(),
new JsonFormatProvider(), new NLangFormatProvider(), new POFormatProvider(), new ResXFormatProvider(),
new TsProjFormatProvider()
];

var formatFactory = new DefaultFormatFactory(providers);
var formatProvider = formatFactory.GetFormatProvider(formatId);
formatProvider.Should().BeOfType(providerType);
}

}
1 change: 1 addition & 0 deletions Ashampoo.Translation.Systems.Formats.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Ashampoo.Translation.Systems.Formats.Abstractions;
public class DefaultFormatFactory : IFormatFactory
{
private readonly Dictionary<string, IFormatProvider<IFormat>> _formatProviders;

/// <summary>
/// Initializes a new instance of the <see cref="DefaultFormatFactory"/> class.
/// </summary>
Expand All @@ -28,13 +28,15 @@ public IFormat CreateFormat(string formatId)
/// <inheritdoc />
public IFormatProvider<T> GetFormatProvider<T>(T format) where T : class, IFormat
{
return TryGetFormatProvider(format) ?? throw new Exception("Format provider not found"); // TODO: throw specific exception
return TryGetFormatProvider(format) ??
throw new Exception("Format provider not found"); // TODO: throw specific exception
}

/// <inheritdoc />
public IFormatProvider<IFormat> GetFormatProvider(string formatId)
{
return TryGetFormatProvider(formatId) ?? throw new Exception("Format provider not found"); // TODO: throw specific exception
return TryGetFormatProvider(formatId) ??
throw new Exception("Format provider not found"); // TODO: throw specific exception
}

/// <inheritdoc />
Expand All @@ -50,7 +52,7 @@ public IFormatProvider<IFormat> GetFormatProvider(string formatId)
/// <inheritdoc />
public IFormatProvider<T>? TryGetFormatProvider<T>(T format) where T : class, IFormat
{
return _formatProviders.Values.OfType<IFormatProvider<T>>().FirstOrDefault();
return _formatProviders.Values.OfType<IFormatProvider<T>>().FirstOrDefault(p => p.SupportsFormat(format));
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,15 @@ public interface IFormatProvider<out T> where T : class, IFormat
/// </summary>
/// <returns></returns>
IFormatBuilder<T> GetFormatBuilder();

/// <summary>
/// Checks if the format provider supports the given format.
/// </summary>
/// <param name="format">
/// The format to check.
/// </param>
/// <returns>
/// True if the format provider supports the given format, otherwise false.
/// </returns>
bool SupportsFormat(IFormat format) => format is T;
}

0 comments on commit 89745f0

Please sign in to comment.