Skip to content

Commit

Permalink
Add failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
joeriddles committed Jan 8, 2024
1 parent ab114e8 commit f1715bb
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
internal class Pair
{
public DateTimeOffset DateTimeOffset { get; init; }
public DateTime DateTime { get; init; }

public Pair(DateTimeOffset dateTimeOffset, DateTime dateTime)
{
DateTimeOffset = dateTimeOffset;
DateTime = dateTime;
}
}

internal class Program
{
static void Main(string[] args)
{
List<Pair> list = new(){ new(DateTimeOffset.Now, DateTime.Now) };
_ = list.Where(pair => pair.DateTimeOffset < pair.DateTime);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.CodeAnalysis;
using System;
using System.IO;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestHelper;
Expand Down Expand Up @@ -40,7 +42,7 @@ static void Main(string[] args)
}

[TestMethod]
public void UsageOfImplicitConversionInComparison_ProducesWarningMessage()
public void UsageOfImplicitConversionInComparison_DateTimeToDateTimeOffset_ProducesWarningMessage()
{
string source = @"
using System;
Expand All @@ -53,15 +55,42 @@ internal class Program
static void Main(string[] args)
{
DateTime first = DateTime.Now;
DateTimeOffset second = DateTimeOffset.Now;
_ = first < second
}
}
}";

Thread.Sleep(10);
VerifyCSharpDiagnostic(source,
new DiagnosticResult
{
Id = "INTL0202",
Severity = DiagnosticSeverity.Warning,
Message = "Using the symbol 'DateTimeOffset.implicit operator DateTimeOffset(DateTime)' can result in unpredictable behavior",
Locations =
new[] {
new DiagnosticResultLocation("Test0.cs", 13, 17)
}
});

DateTimeOffset second = DateTimeOffset.Now;
}

if (first < second)
{
Console.WriteLine(""Time has passed..."");
}
[TestMethod]
public void UsageOfImplicitConversionInComparison_DateTimeOffsetToDateTime_ProducesWarningMessage()
{
string source = @"
using System;
using System.Threading;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
DateTimeOffset first = DateTimeOffset.Now;
DateTime second = DateTime.Now;
_ = first < second
}
}
}";
Expand All @@ -74,12 +103,70 @@ static void Main(string[] args)
Message = "Using the symbol 'DateTimeOffset.implicit operator DateTimeOffset(DateTime)' can result in unpredictable behavior",
Locations =
new[] {
new DiagnosticResultLocation("Test0.cs", 17, 17)
new DiagnosticResultLocation("Test0.cs", 13, 25)
}
});

}

[TestMethod]
public void UsageOfImplicitConversionInComparison_NullableDateTimeOffsetToDateTime_ProducesWarningMessage()
{
string source = @"
using System;
using System.Threading;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
DateTimeOffset? first = DateTimeOffset.Now;
DateTime second = DateTime.Now;
_ = first < second
}
}
}";

VerifyCSharpDiagnostic(
source,
new DiagnosticResult
{
Id = "INTL0202",
Severity = DiagnosticSeverity.Warning,
Message = "Using the symbol 'DateTimeOffset.implicit operator DateTimeOffset(DateTime)' can result in unpredictable behavior",
Locations = new[] {
new DiagnosticResultLocation("Test0.cs", 13, 25)
}
}
);

}

[TestMethod]
public void UsageOfImplicitConversion_InLinq_ProducesWarningMessage()
{
string dir = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName;
string path = Path.Combine(dir, "Data", "DateTime_ImplicitConversion_InLinq.cs");
string source = File.ReadAllText(path);

VerifyCSharpDiagnostic(
source,
new DiagnosticResult
{
Id = "INTL0202",
Severity = DiagnosticSeverity.Warning,
Message = "Using the symbol 'DateTimeOffset.implicit operator DateTimeOffset(DateTime)' can result in unpredictable behavior",
Locations =
new[] {
new DiagnosticResultLocation("Test0.cs", 17, 25)
}
}
);
}


[TestMethod]
public void UsageOfExplicitConversion_ProducesNothing()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<NoWarn>CA2007,CA1815,CA1303,CA1707,CA1305</NoWarn>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Data\**" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4-beta1.22559.1">
<PrivateAssets>all</PrivateAssets>
Expand Down

0 comments on commit f1715bb

Please sign in to comment.