Skip to content

Commit 0e1df41

Browse files
authored
Changed snapshot name extensions to culture info invariant and date time format ISO8601 (#78)
Fixes #77
1 parent 3f8c588 commit 0e1df41

File tree

4 files changed

+115
-6
lines changed

4 files changed

+115
-6
lines changed

.editorconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ trim_trailing_whitespace = false
1414
# CSharp code style settings:
1515
[*.cs]root = true
1616

17+
# C# or VB files
18+
[*.{cs,vb}]
19+
guidelines = 80, 100
20+
1721
[*]
1822
charset = utf-8
1923
end_of_line = lf
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Threading;
4+
using Xunit;
5+
6+
namespace Snapshooter
7+
{
8+
public class SnapshotNameExtensionTests
9+
{
10+
[Theory]
11+
[InlineData("en-US")]
12+
[InlineData("en-GB")]
13+
[InlineData("de-DE")]
14+
[InlineData("fr-FR")]
15+
[InlineData("it-IT")]
16+
[InlineData("ru-RU")]
17+
[InlineData("it-CH")]
18+
[InlineData("de-AT")]
19+
[InlineData("en-AU")]
20+
[InlineData("de-LU")]
21+
[InlineData("en-NZ")]
22+
[InlineData("es-ES")]
23+
[InlineData("es-CR")]
24+
public void ToParamsString_InDifferentCultures_CreatedNameExtensionsSuccessfully(
25+
string cultureName)
26+
{
27+
// arrange
28+
string nameExtension = string.Empty;
29+
30+
var thread = new Thread(new ThreadStart(() =>
31+
{
32+
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(cultureName);
33+
34+
var snapshotNameExtension = SnapshotNameExtension.Create(
35+
"bool", true,
36+
"char", 'b',
37+
"byte", (byte)255,
38+
"sbyte", (sbyte)127,
39+
"float", 789.6655f,
40+
"double", 45.987d,
41+
"decimal", 7834.99000944m,
42+
"integer", -159485,
43+
"long", 9223372036854775807,
44+
"guid", Guid.Parse("{CA9C1C14-839A-4ED2-9763-01FB68FDF49D}"),
45+
"enum", Importance.Regular,
46+
"dateTime", DateTime.ParseExact(
47+
"12.01.2009 17:05:34", "dd.MM.yyyy HH:mm:ss", CultureInfo.CurrentCulture),
48+
"dateTimeOffset", DateTimeOffset.ParseExact(
49+
"10/21/2020 18:34 +05:00", "MM/dd/yyyy HH:mm zzz", CultureInfo.CurrentCulture)
50+
);
51+
52+
nameExtension = snapshotNameExtension.ToParamsString();
53+
}));
54+
55+
// act
56+
thread.Start();
57+
thread.Join();
58+
59+
// assert
60+
Assert.Equal(
61+
"_bool_True" +
62+
"_char_b" +
63+
"_byte_255" +
64+
"_sbyte_127" +
65+
"_float_789.6655" +
66+
"_double_45.987" +
67+
"_decimal_7834.99000944" +
68+
"_integer_-159485" +
69+
"_long_9223372036854775807" +
70+
"_guid_ca9c1c14-839a-4ed2-9763-01fb68fdf49d" +
71+
"_enum_Regular" +
72+
"_dateTime_2009-01-12T17:05:34Z" +
73+
"_dateTimeOffset_2020-10-21T18:34:00Z",
74+
nameExtension);
75+
}
76+
77+
private enum Importance
78+
{
79+
Regular
80+
}
81+
}
82+
}

src/Snapshooter/Extensions/MethodBaseExtension.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Reflection;
1+
using System.Globalization;
2+
using System.Reflection;
23

34
namespace Snapshooter.Extensions
45
{
@@ -15,7 +16,8 @@ public static class MethodBaseExtension
1516
public static string ToName(this MethodBase methodBase)
1617
{
1718
var fullName = string.Concat(
18-
methodBase.ReflectedType.Name, ".", methodBase.Name);
19+
methodBase.ReflectedType.Name.ToString(CultureInfo.InvariantCulture), ".",
20+
methodBase.Name.ToString(CultureInfo.InvariantCulture));
1921

2022
return fullName;
2123
}

src/Snapshooter/SnapshotNameExtension.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Linq;
25

36
namespace Snapshooter
47
{
@@ -69,13 +72,31 @@ public static SnapshotNameExtension Create(params object[] snapshotNameExtension
6972
/// </summary>
7073
/// <returns>The snapshot name extension text.</returns>
7174
public string ToParamsString()
72-
{
73-
string extensionName = string.Join("_", _snapshotNameExtensions);
75+
{
76+
string extensionName = string.Join("_",
77+
_snapshotNameExtensions.Select(obj => { return ConvertParameter(obj); }));
78+
7479
if (!string.IsNullOrWhiteSpace(extensionName))
7580
{
7681
extensionName = string.Concat("_", extensionName);
7782
}
78-
return extensionName;
83+
84+
return extensionName;
85+
}
86+
87+
private static string ConvertParameter(object obj)
88+
{
89+
if (obj is DateTime dateTime)
90+
{
91+
return dateTime.ToString("yyyy-MM-ddTHH:mm:ssZ");
92+
}
93+
94+
if (obj is DateTimeOffset dateTimeOffset)
95+
{
96+
return dateTimeOffset.ToString("yyyy-MM-ddTHH:mm:ssZ");
97+
}
98+
99+
return Convert.ToString(obj, CultureInfo.InvariantCulture);
79100
}
80101
}
81102
}

0 commit comments

Comments
 (0)