Skip to content

Commit

Permalink
Reformat most of helpers code using C# 11 new features.
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegRa committed Dec 20, 2022
1 parent f1251db commit ee7b572
Show file tree
Hide file tree
Showing 24 changed files with 580 additions and 595 deletions.
18 changes: 9 additions & 9 deletions DateTimeOnly.Tests/ArgumentValidationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ namespace System.Tests
{
public sealed class ArgumentValidationTests
{
private const String InputArgumentName = "s";
private const string InputArgumentName = "s";

private const String StyleArgumentName = "style";
private const string StyleArgumentName = "style";

private const String FormatArgumentName = "format";
private const string FormatArgumentName = "format";

private static readonly DateOnly DateOnlyValue = new ();

private static readonly String DateOnlyStringValue = DateOnlyValue.ToString();
private static readonly string DateOnlyStringValue = DateOnlyValue.ToString();

private static readonly TimeOnly TimeOnlyValue = new ();

private static readonly String TimeOnlyStringValue = TimeOnlyValue.ToString();
private static readonly string TimeOnlyStringValue = TimeOnlyValue.ToString();

#pragma warning disable CS8625
private static readonly String NullString = null;
private static readonly string NullString = null;

private static readonly String[] NullStringArray = null;
private static readonly string[] NullStringArray = null;
#pragma warning restore CS8625

[Fact]
Expand Down Expand Up @@ -91,7 +91,7 @@ public void DateOnlyParseExactValidationWorkedTest()
() => DateOnly.ParseExact(string.Empty, NullStringArray, CultureInfo.InvariantCulture)).ParamName);

Assert.Throws<FormatException>(() => DateOnly.ParseExact(
string.Empty, new []{ String.Empty }, CultureInfo.InvariantCulture));
string.Empty, new []{ string.Empty }, CultureInfo.InvariantCulture));
}

[Fact]
Expand Down Expand Up @@ -209,7 +209,7 @@ public void TimeOnlyParseExactValidationWorkedTest()
() => TimeOnly.ParseExact(string.Empty, NullStringArray, CultureInfo.InvariantCulture)).ParamName);

Assert.Throws<FormatException>(() => TimeOnly.ParseExact(
string.Empty, new []{ String.Empty }, CultureInfo.InvariantCulture));
string.Empty, new []{ string.Empty }, CultureInfo.InvariantCulture));
}

[Fact]
Expand Down
2 changes: 2 additions & 0 deletions DateTimeOnly.Tests/DateOnlyTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// ReSharper disable All

using System.Collections.Generic;
using System.Globalization;
using Xunit;
Expand Down
14 changes: 7 additions & 7 deletions DateTimeOnly.Tests/FastStrictParsingLogicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public static void DateTimeMinValueDateOnlyParseWorkedTest() =>
public static void DateTimeMinValueTimeOnlyParseWorkedTest() =>
RunInFastParsingLogicContext(TimeOnlyParseWorked,DateTime.MinValue, DateOnlyFormat);

private static void RunInFastParsingLogicContext(Action<DateTime, String> testMethod, DateTime dt, String format)
private static void RunInFastParsingLogicContext(Action<DateTime, string> testMethod, DateTime dt, string format)
{
const String appContextSwitchName = "Portable.System.DateTimeOnly.UseFastParsingLogic";
const string appContextSwitchName = "Portable.System.DateTimeOnly.UseFastParsingLogic";

AppContext.SetSwitch(appContextSwitchName, true);
try
Expand All @@ -53,7 +53,7 @@ private static void RunInFastParsingLogicContext(Action<DateTime, String> testMe
}
}

private static void TimeOnlyParseWorked(DateTime dt, String format)
private static void TimeOnlyParseWorked(DateTime dt, string format)
{
var formatted = dt.ToString(format);
Assert.Equal(TimeOnly.FromDateTime(dt), TimeOnly.Parse(formatted));
Expand All @@ -62,10 +62,10 @@ private static void TimeOnlyParseWorked(DateTime dt, String format)
Assert.True(TimeOnly.TryParse(formatted.AsSpan(), out _));
}

private static void DateOnlyParseWorked(DateTime dt, String format) =>
private static void DateOnlyParseWorked(DateTime dt, string format) =>
DateOnlyParseWorked(dt, format, dt);

private static void DateOnlyParseWorked(DateTime dt, String format, DateTime @default)
private static void DateOnlyParseWorked(DateTime dt, string format, DateTime @default)
{
var formatted = dt.ToString(format);
Assert.Equal(DateOnly.FromDateTime(@default), DateOnly.Parse(formatted));
Expand All @@ -74,7 +74,7 @@ private static void DateOnlyParseWorked(DateTime dt, String format, DateTime @de
Assert.True(DateOnly.TryParse(formatted.AsSpan(), out _));
}

private static void TimeOnlyParseFailed(DateTime dt, String format)
private static void TimeOnlyParseFailed(DateTime dt, string format)
{
var formatted = dt.ToString(format);
Assert.Throws<FormatException>(() => TimeOnly.Parse(formatted));
Expand All @@ -83,7 +83,7 @@ private static void TimeOnlyParseFailed(DateTime dt, String format)
Assert.False(TimeOnly.TryParse(formatted.AsSpan(), out _));
}

private static void DateOnlyParseFailed(DateTime dt, String format)
private static void DateOnlyParseFailed(DateTime dt, string format)
{
var formatted = dt.ToString(format);
Assert.Throws<FormatException>(() => DateOnly.Parse(formatted));
Expand Down
5 changes: 3 additions & 2 deletions DateTimeOnly.Tests/Helpers/ConditionalFact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ namespace System.Tests
public sealed class ConditionalFactAttribute : FactAttribute
{
public ConditionalFactAttribute(
String conditionalMemberName) =>
string conditionalMemberName) =>
ConditionalMemberName = conditionalMemberName;

// ReSharper disable once MemberCanBePrivate.Global
public String ConditionalMemberName { get; }
// ReSharper disable once UnusedAutoPropertyAccessor.Global
public string ConditionalMemberName { get; }
}
}
16 changes: 8 additions & 8 deletions DateTimeOnly.Tests/Helpers/ConditionalFactDiscoverer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ public override IEnumerable<IXunitTestCase> Discover(
{
IEnumerable<IXunitTestCase> testCases = base.Discover(discoveryOptions, testMethod, factAttribute);

if (factAttribute.GetConstructorArguments().SingleOrDefault() is String conditionMemberName)
if (factAttribute.GetConstructorArguments().SingleOrDefault() is not string conditionMemberName)
{
var typeInfo = testMethod.Method?.ToRuntimeMethod().DeclaringType?.GetTypeInfo();
var methodInfo = typeInfo?.GetDeclaredProperty(conditionMemberName)?.GetMethod;
if (methodInfo?.Invoke(null, null) is false)
{
return testCases.Select(_ => new SkippedTestCase(_, conditionMemberName));
}
return testCases;
}

return testCases;
var typeInfo = testMethod.Method?.ToRuntimeMethod().DeclaringType?.GetTypeInfo();
var methodInfo = typeInfo?.GetDeclaredProperty(conditionMemberName)?.GetMethod;

return methodInfo?.Invoke(null, null) is false
? testCases.Select(_ => new SkippedTestCase(_, conditionMemberName))
: testCases;
}
}
}
8 changes: 4 additions & 4 deletions DateTimeOnly.Tests/Helpers/DateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ namespace System.Tests
{
internal static class DateTimeExtensions
{
internal static System.DateTime Create(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond) =>
new System.DateTime(year, month, day, hour, minute, second, millisecond)
internal static DateTime Create(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond) =>
new DateTime(year, month, day, hour, minute, second, millisecond)
.Add(new TimeSpan(microsecond * Diagnostics.TimeSpan.TicksPerMicrosecond));

/// <summary>
/// The microseconds component, expressed as a value between 0 and 999.
/// </summary>
internal static int Microsecond(this System.DateTime dateTime) => (int)((dateTime.Ticks / Diagnostics.TimeSpan.TicksPerMicrosecond) % 1000);
internal static int Microsecond(this DateTime dateTime) => (int)(dateTime.Ticks / Diagnostics.TimeSpan.TicksPerMicrosecond % 1000);

/// <summary>
/// The nanoseconds component, expressed as a value between 0 and 900 (in increments of 100 nanoseconds).
/// </summary>
internal static int Nanosecond(this System.DateTime dateTime) => (int)(dateTime.Ticks % Diagnostics.TimeSpan.TicksPerMicrosecond) * 100;
internal static int Nanosecond(this DateTime dateTime) => (int)(dateTime.Ticks % Diagnostics.TimeSpan.TicksPerMicrosecond) * 100;
}
}
25 changes: 12 additions & 13 deletions DateTimeOnly.Tests/Helpers/SkippedTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,29 @@ internal sealed class SkippedTestCase : LongLivedMarshalByRefObject, IXunitTestC
{
private readonly IXunitTestCase _testCase;

private readonly String _skippedReason;

// ReSharper disable once UnusedMember.Global
public SkippedTestCase()
{
_skippedReason = String.Empty;
SkipReason = string.Empty;
_testCase = this;
}

internal SkippedTestCase(
IXunitTestCase testCase, String skippedReason)
IXunitTestCase testCase, string skippedReason)
{
_skippedReason = skippedReason;
SkipReason = skippedReason;
_testCase = testCase;
}

public Int32 Timeout => _testCase.Timeout;
public string SkipReason { get; }

public String SkipReason => _skippedReason;
public int Timeout => _testCase.Timeout;

public String UniqueID => _testCase.UniqueID;
public string UniqueID => _testCase.UniqueID;

public IMethodInfo Method => _testCase.Method;

public String DisplayName => _testCase.DisplayName;
public string DisplayName => _testCase.DisplayName;

public Exception InitializationException => _testCase.InitializationException;

Expand All @@ -46,20 +45,20 @@ public ISourceInformation SourceInformation

public ITestMethod TestMethod => _testCase.TestMethod;

public Dictionary<String, List<String>> Traits => _testCase.Traits;
public Dictionary<string, List<string>> Traits => _testCase.Traits;

public Object[] TestMethodArguments => _testCase.TestMethodArguments;
public object[] TestMethodArguments => _testCase.TestMethodArguments;

public void Deserialize(IXunitSerializationInfo info) => _testCase.Deserialize(info);

public Task<RunSummary> RunAsync(
IMessageSink diagnosticMessageSink,
IMessageBus messageBus,
Object[] constructorArguments,
object[] constructorArguments,
ExceptionAggregator aggregator,
CancellationTokenSource cancellationTokenSource) =>
new XunitTestCaseRunner(
this, DisplayName, _skippedReason, constructorArguments,
this, DisplayName, SkipReason, constructorArguments,
TestMethodArguments, messageBus, aggregator, cancellationTokenSource).RunAsync();

public void Serialize(IXunitSerializationInfo info) => _testCase.Serialize(info);
Expand Down
2 changes: 2 additions & 0 deletions DateTimeOnly.Tests/TimeOnlyTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// ReSharper disable All

using System.Collections.Generic;
using System.Globalization;
using System.Runtime.CompilerServices;
Expand Down
3 changes: 3 additions & 0 deletions DateTimeOnly.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/BUILTIN_TYPE_REFERENCE_FOR_MEMBER_ACCESS_STYLE/@EntryValue">UseKeyword</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/BUILTIN_TYPE_REFERENCE_STYLE/@EntryValue">UseKeyword</s:String>
<s:Boolean x:Key="/Default/UserDictionary/Words/=dtfi/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Formattable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Parsable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Roundtrippable/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
8 changes: 5 additions & 3 deletions DateTimeOnly/DateOnly.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// ReSharper disable All

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
Expand Down Expand Up @@ -421,7 +423,7 @@ private static ParseFailureKind TryParseInternal(ReadOnlySpan<char> s, IFormatPr
}

DateTimeResult dtResult = default;
dtResult.Init(s);
DateTimeResult.Init(s);

if (!DateTimeParse.TryParse(s, DateTimeFormatInfo.GetInstance(provider), style, ref dtResult))
{
Expand Down Expand Up @@ -488,7 +490,7 @@ private static ParseFailureKind TryParseExactInternal(ReadOnlySpan<char> s, Read
}

DateTimeResult dtResult = default;
dtResult.Init(s);
DateTimeResult.Init(s);

if (!DateTimeParse.TryParseExact(s, format, DateTimeFormatInfo.GetInstance(provider), style, ref dtResult))
{
Expand Down Expand Up @@ -569,7 +571,7 @@ private static ParseFailureKind TryParseExactInternal(ReadOnlySpan<char> s, stri
// Create a new result each time to ensure the runs are independent. Carry through
// flags from the caller and return the result.
DateTimeResult dtResult = default;
dtResult.Init(s);
DateTimeResult.Init(s);
if (DateTimeParse.TryParseExact(s, format.AsSpan(), dtfiToUse, style, ref dtResult) && ((dtResult.flags & ParseFlagsDateMask) == 0))
{
result = new DateOnly(DayNumberFromDateTime(dtResult.parsedDate));
Expand Down
Loading

0 comments on commit ee7b572

Please sign in to comment.