Skip to content

Commit

Permalink
Use IFormatProvider in StringLocalizerMockup #237
Browse files Browse the repository at this point in the history
  • Loading branch information
Spacelord-XaN committed Feb 4, 2024
1 parent fe55269 commit 2e9c0f7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task ByIdWithAmount_ShouldReturnTransactionWithIdAndChangeShouldBeA
await Db.SaveChangesAsync();

// Act
TransactionEntity result = await Sut.GetAsync(toGet.Id);
TransactionEntity result = await Sut.GetAsync(toGet.Id, amountGiven);

// Assert
using (new AssertionScope())
Expand Down
16 changes: 14 additions & 2 deletions test/BraunauMobil.VeloBasar.Tests/Mockups/StringLocalizerMock.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Microsoft.Extensions.Localization;
using System.Text;

namespace BraunauMobil.VeloBasar.Tests.Mockups;

public class StringLocalizerMock
public class StringLocalizerMock(IFormatProvider formatProvider)
: IStringLocalizer<SharedResources>

{
Expand All @@ -23,7 +24,18 @@ public LocalizedString this[string name]
ArgumentNullException.ThrowIfNull(name);
ArgumentNullException.ThrowIfNull(arguments);

string value = $"{name}_{string.Join('_', arguments)}";
StringBuilder argsString = new();
foreach (object argument in arguments)
{
string argString = string.Format(formatProvider, "{0}", argument);
if (argsString.Length > 0)
{
argsString.Append('_');
}
argsString.Append(argString);
}

string value = $"{name}_{argsString}";
return new(name, value);
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/BraunauMobil.VeloBasar.Tests/X.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static class X

public static IFormatProvider FormatProvider { get; } = CultureInfo.GetCultureInfo("de-AT");

public static IStringLocalizer<SharedResources> StringLocalizer { get; } = new StringLocalizerMock();
public static IStringLocalizer<SharedResources> StringLocalizer { get; } = new StringLocalizerMock(FormatProvider);

public static Action<ServiceDescriptor> CreateInspector<TServiceType, TImplementationType>(ServiceLifetime lifetime)
=> item =>
Expand Down

0 comments on commit 2e9c0f7

Please sign in to comment.