Skip to content

Commit

Permalink
Fix culture issues (#391)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Aug 9, 2021
1 parent 1f0132e commit d9e3efe
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 32 deletions.
2 changes: 1 addition & 1 deletion docs/build-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Use a [on_failure build step](https://www.appveyor.com/docs/build-configuration/
on_failure:
- ps: Get-ChildItem *.received.* -recurse | % { Push-AppveyorArtifact $_.FullName -FileName $_.Name }
```
<sup><a href='/src/appveyor.yml#L25-L28' title='Snippet source file'>snippet source</a> | <a href='#snippet-appveyorartifacts' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/appveyor.yml#L31-L34' title='Snippet source file'>snippet source</a> | <a href='#snippet-appveyorartifacts' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

See also [Pushing artifacts from scripts](https://www.appveyor.com/docs/packaging-artifacts/#pushing-artifacts-from-scripts).
Expand Down
4 changes: 2 additions & 2 deletions docs/http-recording.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static async Task<int> MethodThatDoesHttpCalls()
return jsonResult.Length + xmlResult.Length;
}
```
<sup><a href='/src/Verify.Tests/Tests.cs#L99-L129' title='Snippet source file'>snippet source</a> | <a href='#snippet-httprecording' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Tests.cs#L118-L148' title='Snippet source file'>snippet source</a> | <a href='#snippet-httprecording' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

The requests/response pairs will be appended to the verified file.
Expand Down Expand Up @@ -171,7 +171,7 @@ public async Task TestHttpRecordingExplicit()
});
}
```
<sup><a href='/src/Verify.Tests/Tests.cs#L131-L158' title='Snippet source file'>snippet source</a> | <a href='#snippet-httprecordingexplicit' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Tests.cs#L150-L177' title='Snippet source file'>snippet source</a> | <a href='#snippet-httprecordingexplicit' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Results in the following:
Expand Down
4 changes: 2 additions & 2 deletions docs/logging-recording.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ClassThatUsesLogging
}
}
```
<sup><a href='/src/Verify.Tests/Tests.cs#L223-L257' title='Snippet source file'>snippet source</a> | <a href='#snippet-loggerrecording' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Tests.cs#L242-L276' title='Snippet source file'>snippet source</a> | <a href='#snippet-loggerrecording' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Results in:
Expand Down Expand Up @@ -119,7 +119,7 @@ class ClassThatUsesTypedLogging
}
}
```
<sup><a href='/src/Verify.Tests/Tests.cs#L168-L198' title='Snippet source file'>snippet source</a> | <a href='#snippet-loggerrecordingtyped' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Tests.cs#L187-L217' title='Snippet source file'>snippet source</a> | <a href='#snippet-loggerrecordingtyped' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Results in:
Expand Down
25 changes: 13 additions & 12 deletions docs/serializer-settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ var settings = new JsonSerializerSettings
{
Formatting = Formatting.Indented,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore
DefaultValueHandling = DefaultValueHandling.Ignore,
Culture = CultureInfo.InvariantCulture
};
```
<sup><a href='/src/Verify/Serialization/SerializationSettings.cs#L126-L135' title='Snippet source file'>snippet source</a> | <a href='#snippet-defaultserialization' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify/Serialization/SerializationSettings.cs#L127-L137' title='Snippet source file'>snippet source</a> | <a href='#snippet-defaultserialization' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down Expand Up @@ -1057,13 +1058,13 @@ The default mapping is:
```cs
{typeof(string), (target, _) => (string) target},
{typeof(StringBuilder), (target, _) => ((StringBuilder) target).ToString()},
{typeof(bool), (target, _) => ((bool) target).ToString()},
{typeof(short), (target, _) => ((short) target).ToString()},
{typeof(ushort), (target, _) => ((ushort) target).ToString()},
{typeof(int), (target, _) => ((int) target).ToString()},
{typeof(uint), (target, _) => ((uint) target).ToString()},
{typeof(long), (target, _) => ((long) target).ToString()},
{typeof(ulong), (target, _) => ((ulong) target).ToString()},
{typeof(bool), (target, _) => ((bool) target).ToString(CultureInfo.InvariantCulture)},
{typeof(short), (target, _) => ((short) target).ToString(CultureInfo.InvariantCulture)},
{typeof(ushort), (target, _) => ((ushort) target).ToString(CultureInfo.InvariantCulture)},
{typeof(int), (target, _) => ((int) target).ToString(CultureInfo.InvariantCulture)},
{typeof(uint), (target, _) => ((uint) target).ToString(CultureInfo.InvariantCulture)},
{typeof(long), (target, _) => ((long) target).ToString(CultureInfo.InvariantCulture)},
{typeof(ulong), (target, _) => ((ulong) target).ToString(CultureInfo.InvariantCulture)},
{typeof(decimal), (target, _) => ((decimal) target).ToString(CultureInfo.InvariantCulture)},
#if NET5_0_OR_GREATER
{typeof(Half), (target, settings) => ((Half) target).ToString(CultureInfo.InvariantCulture)},
Expand All @@ -1073,14 +1074,14 @@ The default mapping is:
typeof(DateOnly), (target, _) =>
{
var date = (DateOnly) target;
return date.ToString("yyyy-MM-dd");
return date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
}
},
{
typeof(TimeOnly), (target, _) =>
{
var time = (TimeOnly) target;
return time.ToString("h:mm tt");
return time.ToString("h:mm tt", CultureInfo.InvariantCulture);
}
},
#endif
Expand All @@ -1098,7 +1099,7 @@ The default mapping is:
typeof(DateTimeOffset), (target, _) =>
{
var dateTimeOffset = (DateTimeOffset) target;
return dateTimeOffset.ToString("yyyy-MM-ddTHH:mm:ss.FFFFFFFz");
return dateTimeOffset.ToString("yyyy-MM-ddTHH:mm:ss.FFFFFFFz", CultureInfo.InvariantCulture);
}
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1000.9999
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1000.9999
19 changes: 19 additions & 0 deletions src/Verify.Tests/Tests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using VerifyTests;
Expand Down Expand Up @@ -32,6 +34,23 @@ public Task ReplaceInvalidParamChar(string value)
.UseParameters(Path.GetInvalidPathChars().First());
}

[Theory]
[InlineData(1000.9999d)]
public async Task LocalizedParam(decimal value)
{
var culture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("de-DE");
try
{
await Verifier.Verify(value)
.UseParameters(value);
}
finally
{
Thread.CurrentThread.CurrentCulture = culture;
}
}

[Fact]
public async Task HttpResponseNested()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Verify/Counters/CounterContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ public static CounterContext Current
}
}

static void Start()
internal static void Start()
{
local.Value = new();
}

static void Stop()
internal static void Stop()
{
local.Value = null;
}
Expand Down
18 changes: 18 additions & 0 deletions src/Verify/Naming/ParameterBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.Text;
using System.Threading;
using VerifyTests;

static class ParameterBuilder
{
public static string Concat(MethodInfo method, IReadOnlyList<object?> parameterValues)
{
var thread = Thread.CurrentThread;
var culture = thread.CurrentCulture;
thread.CurrentCulture = CultureInfo.InvariantCulture;
try
{
return Inner(method, parameterValues);
}
finally
{
thread.CurrentCulture = culture;
}
}

static string Inner(MethodInfo method, IReadOnlyList<object?> parameterValues)
{
var builder = new StringBuilder();
var parameters = method.GetParameters();

for (var index = 0; index < parameters.Length; index++)
{
var parameter = parameters[index];
Expand Down
2 changes: 1 addition & 1 deletion src/Verify/Serialization/Converters/TimeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class TimeConverter :
JsonSerializer serializer,
IReadOnlyDictionary<string, object> context)
{
writer.WriteValue(value.ToString("h:mm tt"));
writer.WriteValue(value.ToString("h:mm tt", serializer.Culture));
}
}
#endif
6 changes: 4 additions & 2 deletions src/Verify/Serialization/SerializationSettings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using Newtonsoft.Json;
Expand Down Expand Up @@ -121,15 +122,16 @@ public void DontScrubNumericIds()
scrubNumericIds = false;
}

public JsonSerializerSettings BuildSettings()
JsonSerializerSettings BuildSettings()
{
#region defaultSerialization

var settings = new JsonSerializerSettings
{
Formatting = Formatting.Indented,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore
DefaultValueHandling = DefaultValueHandling.Ignore,
Culture = CultureInfo.InvariantCulture
};

#endregion
Expand Down
20 changes: 10 additions & 10 deletions src/Verify/Serialization/VerifierSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ public static partial class VerifierSettings

{typeof(string), (target, _) => (string) target},
{typeof(StringBuilder), (target, _) => ((StringBuilder) target).ToString()},
{typeof(bool), (target, _) => ((bool) target).ToString()},
{typeof(short), (target, _) => ((short) target).ToString()},
{typeof(ushort), (target, _) => ((ushort) target).ToString()},
{typeof(int), (target, _) => ((int) target).ToString()},
{typeof(uint), (target, _) => ((uint) target).ToString()},
{typeof(long), (target, _) => ((long) target).ToString()},
{typeof(ulong), (target, _) => ((ulong) target).ToString()},
{typeof(bool), (target, _) => ((bool) target).ToString(CultureInfo.InvariantCulture)},
{typeof(short), (target, _) => ((short) target).ToString(CultureInfo.InvariantCulture)},
{typeof(ushort), (target, _) => ((ushort) target).ToString(CultureInfo.InvariantCulture)},
{typeof(int), (target, _) => ((int) target).ToString(CultureInfo.InvariantCulture)},
{typeof(uint), (target, _) => ((uint) target).ToString(CultureInfo.InvariantCulture)},
{typeof(long), (target, _) => ((long) target).ToString(CultureInfo.InvariantCulture)},
{typeof(ulong), (target, _) => ((ulong) target).ToString(CultureInfo.InvariantCulture)},
{typeof(decimal), (target, _) => ((decimal) target).ToString(CultureInfo.InvariantCulture)},
#if NET5_0_OR_GREATER
{typeof(Half), (target, settings) => ((Half) target).ToString(CultureInfo.InvariantCulture)},
Expand All @@ -80,14 +80,14 @@ public static partial class VerifierSettings
typeof(DateOnly), (target, _) =>
{
var date = (DateOnly) target;
return date.ToString("yyyy-MM-dd");
return date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
}
},
{
typeof(TimeOnly), (target, _) =>
{
var time = (TimeOnly) target;
return time.ToString("h:mm tt");
return time.ToString("h:mm tt", CultureInfo.InvariantCulture);
}
},
#endif
Expand All @@ -105,7 +105,7 @@ public static partial class VerifierSettings
typeof(DateTimeOffset), (target, _) =>
{
var dateTimeOffset = (DateTimeOffset) target;
return dateTimeOffset.ToString("yyyy-MM-ddTHH:mm:ss.FFFFFFFz");
return dateTimeOffset.ToString("yyyy-MM-ddTHH:mm:ss.FFFFFFFz", CultureInfo.InvariantCulture);
}
},
{
Expand Down
6 changes: 6 additions & 0 deletions src/appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ build_script:
}
- dotnet build src --configuration Release
- dotnet test src --configuration Release --no-build --no-restore

- pwsh: |
if ($isWindows) {
Set-Culture -CultureInfo de-DE
dotnet test src --configuration Release --no-build --no-restore
}
# begin-snippet: AppVeyorArtifacts
on_failure:
- ps: Get-ChildItem *.received.* -recurse | % { Push-AppveyorArtifact $_.FullName -FileName $_.Name }
Expand Down

0 comments on commit d9e3efe

Please sign in to comment.