Skip to content

Commit 3302e91

Browse files
committed
fix error with full namespace
1 parent edbe644 commit 3302e91

10 files changed

Lines changed: 210 additions & 27 deletions

File tree

Scratch/Program.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Threading;
3+
using Tocsoft.DateTimeAbstractions;
4+
5+
namespace Scratch
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
12+
Console.WriteLine("Real time");
13+
for (var i = 0; i < 10; i++)
14+
{
15+
logTime();
16+
}
17+
Console.WriteLine();
18+
Console.WriteLine("Pinned");
19+
using (Clock.Pin(new DateTime(2000, 01, 01)))
20+
{
21+
for (var i = 0; i < 10; i++)
22+
{
23+
logTime();
24+
}
25+
}
26+
Console.WriteLine();
27+
28+
Console.WriteLine("Real time");
29+
for (var i = 0; i < 10; i++)
30+
{
31+
logTime();
32+
}
33+
Console.WriteLine();
34+
35+
Console.ReadLine();
36+
}
37+
38+
private static void logTime()
39+
{
40+
Console.WriteLine(Clock.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
41+
Thread.Sleep(100);
42+
43+
}
44+
}
45+
}

Scratch/Scratch.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.0</TargetFramework>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<PackageReference Include="Tocsoft.DateTimeAbstractions" Version="1.0.1" />
9+
</ItemGroup>
10+
</Project>

Scratch/nuget.config

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<packageSources>
4+
<clear />
5+
<add key="nuget.org" value="../Tocsoft.DateTimeAbstractions/bin/debug" />
6+
</packageSources>
7+
</configuration>

Tocsoft.Clock.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.27130.2027
4+
VisualStudioVersion = 15.0.27130.2026
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tocsoft.DateTimeAbstractions.Tests", "Tocsoft.DateTimeAbstractions.Tests\Tocsoft.DateTimeAbstractions.Tests.csproj", "{F59B2FEE-7E98-4DE5-8BBA-3D628FB2F615}"
77
EndProject
@@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tocsoft.DateTimeAbstraction
1111
EndProject
1212
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tocsoft.DateTimeAbstractions.Analyzer.Test", "Tocsoft.DateTimeAbstractions.Analyzer.Test\Tocsoft.DateTimeAbstractions.Analyzer.Test.csproj", "{52919D50-B2EB-4BEB-9F67-E55250AACB84}"
1313
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Scratch", "Scratch\Scratch.csproj", "{F1BA6FB7-F8EA-47F9-BF9C-1259E612B7E8}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Any CPU = Debug|Any CPU
@@ -33,6 +35,10 @@ Global
3335
{52919D50-B2EB-4BEB-9F67-E55250AACB84}.Debug|Any CPU.Build.0 = Debug|Any CPU
3436
{52919D50-B2EB-4BEB-9F67-E55250AACB84}.Release|Any CPU.ActiveCfg = Release|Any CPU
3537
{52919D50-B2EB-4BEB-9F67-E55250AACB84}.Release|Any CPU.Build.0 = Release|Any CPU
38+
{F1BA6FB7-F8EA-47F9-BF9C-1259E612B7E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39+
{F1BA6FB7-F8EA-47F9-BF9C-1259E612B7E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
40+
{F1BA6FB7-F8EA-47F9-BF9C-1259E612B7E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
41+
{F1BA6FB7-F8EA-47F9-BF9C-1259E612B7E8}.Release|Any CPU.Build.0 = Release|Any CPU
3642
EndGlobalSection
3743
GlobalSection(SolutionProperties) = preSolution
3844
HideSolutionNode = FALSE

Tocsoft.DateTimeAbstractions.Analyzer.Test/Helpers/DiagnosticResult.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace TestHelper
88
/// </summary>
99
public struct DiagnosticResultLocation
1010
{
11-
public DiagnosticResultLocation(string path, int line, int column)
11+
public DiagnosticResultLocation(string path, int line, int column, int length = -1)
1212
{
1313
if (line < -1)
1414
{
@@ -23,11 +23,13 @@ public DiagnosticResultLocation(string path, int line, int column)
2323
this.Path = path;
2424
this.Line = line;
2525
this.Column = column;
26+
this.Length = length;
2627
}
2728

2829
public string Path { get; }
2930
public int Line { get; }
3031
public int Column { get; }
32+
public int Length { get; }
3133
}
3234

3335
/// <summary>

Tocsoft.DateTimeAbstractions.Analyzer.Test/TocsoftDateTimeAbstractionsAnalyzerUnitTests.cs

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,36 @@ public static class Clock { public static DateTime Now { get; set; } }
5454
VerifyCSharpDiagnostic(test, expected);
5555
}
5656

57+
//No diagnostics expected to show up
58+
[TestMethod]
59+
public void TestMethodAnalyzerExpectedForSystemDatetime()
60+
{
61+
var test = @"
62+
namespace Tocsoft.DateTimeAbstractions
63+
{
64+
class TypeName
65+
{
66+
public TypeName(){
67+
System.DateTime time = System.DateTime.Now;
68+
}
69+
}
70+
71+
public static class Clock { public static DateTime Now { get; set; } }
72+
}";
73+
var expected = new DiagnosticResult
74+
{
75+
Id = DateTimeUsageAnalyzer.DiagnosticId,
76+
Message = "Do not call DateTime.Now",
77+
Severity = DiagnosticSeverity.Warning,
78+
Locations =
79+
new[] {
80+
new DiagnosticResultLocation("Test0.cs", 7, 40, 19)
81+
}
82+
};
83+
84+
VerifyCSharpDiagnostic(test, expected);
85+
}
86+
5787

5888

5989
//No diagnostics expected to show up
@@ -101,7 +131,9 @@ public TypeName(){
101131
public void DateTimeUtcNowMappsToClockUtcNow()
102132
{
103133
var test = @"
134+
using Tocsoft.DateTimeAbstractions;
104135
using System;
136+
105137
namespace TestApplication
106138
{
107139
class TypeName
@@ -122,8 +154,8 @@ public static class Clock { public static DateTime UtcNow { get; set; } }
122154
};
123155

124156
var fixtest = @"
125-
using System;
126157
using Tocsoft.DateTimeAbstractions;
158+
using System;
127159
128160
namespace TestApplication
129161
{
@@ -136,6 +168,82 @@ public TypeName(){
136168
}";
137169
VerifyCSharpFix(test, fixtest);
138170
}
171+
[TestMethod]
172+
public void DateTimeUtcNowMappsToClockUtcNowDotHour()
173+
{
174+
var test = @"
175+
using System;
176+
namespace TestApplication
177+
{
178+
class TypeName
179+
{
180+
public TypeName(){
181+
DateTime time = DateTime.UtcNow.Hour;
182+
}
183+
}
184+
}";
185+
AdditionalCodeFiles = new[] {
186+
@"
187+
using System;
188+
189+
namespace Tocsoft.DateTimeAbstractions
190+
{
191+
public static class Clock { public static DateTime UtcNow { get; set; } }
192+
}"
193+
};
194+
195+
var fixtest = @"
196+
using System;
197+
using Tocsoft.DateTimeAbstractions;
198+
199+
namespace TestApplication
200+
{
201+
class TypeName
202+
{
203+
public TypeName(){
204+
DateTime time = Clock.UtcNow.Hour;
205+
}
206+
}
207+
}";
208+
VerifyCSharpFix(test, fixtest);
209+
}
210+
211+
[TestMethod]
212+
public void SystemDateTimeUtcNowMappsToClockUtcNow()
213+
{
214+
var test = @"
215+
namespace TestApplication
216+
{
217+
class TypeName
218+
{
219+
public TypeName(){
220+
System.DateTime time = System.DateTime.UtcNow;
221+
}
222+
}
223+
}";
224+
AdditionalCodeFiles = new[] {
225+
@"
226+
using System;
227+
228+
namespace Tocsoft.DateTimeAbstractions
229+
{
230+
public static class Clock { public static DateTime UtcNow { get; set; } }
231+
}"
232+
};
233+
234+
var fixtest = @"using Tocsoft.DateTimeAbstractions;
235+
236+
namespace TestApplication
237+
{
238+
class TypeName
239+
{
240+
public TypeName(){
241+
System.DateTime time = Clock.UtcNow;
242+
}
243+
}
244+
}";
245+
VerifyCSharpFix(test, fixtest);
246+
}
139247

140248
protected override CodeFixProvider GetCSharpCodeFixProvider()
141249
{

Tocsoft.DateTimeAbstractions.Analyzer.Test/Verifiers/DiagnosticVerifier.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ private static void VerifyDiagnosticLocation(DiagnosticAnalyzer analyzer, Diagno
188188
expected.Path, actualSpan.Path, FormatDiagnostics(analyzer, diagnostic)));
189189

190190
var actualLinePosition = actualSpan.StartLinePosition;
191+
var actualEndLinePosition = actualSpan.EndLinePosition;
191192

192193
// Only check line position if there is an actual line in the real diagnostic
193194
if (actualLinePosition.Line > 0)
@@ -210,6 +211,17 @@ private static void VerifyDiagnosticLocation(DiagnosticAnalyzer analyzer, Diagno
210211
expected.Column, actualLinePosition.Character + 1, FormatDiagnostics(analyzer, diagnostic)));
211212
}
212213
}
214+
// Only check column position if there is an actual column position in the real diagnostic
215+
if (actualEndLinePosition.Character > 0 & expected.Length > 0)
216+
{
217+
var endColumn = expected.Column + expected.Length;
218+
if (actualEndLinePosition.Character + 1 != endColumn)
219+
{
220+
Assert.IsTrue(false,
221+
string.Format("Expected diagnostic to end at column \"{0}\" was actually at column \"{1}\"\r\n\r\nDiagnostic:\r\n {2}\r\n",
222+
endColumn, actualEndLinePosition.Character + 1, FormatDiagnostics(analyzer, diagnostic)));
223+
}
224+
}
213225
}
214226
#endregion
215227

Tocsoft.DateTimeAbstractions.Analyzer/DateTimeUsageCodeFixProvider.cs

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,9 @@ private static async Task<SyntaxNode> ReplaceMemberCall(CodeFixContext context,
6969
var diagnostic = context.Diagnostics.First();
7070
var diagnosticSpan = diagnostic.Location.SourceSpan;
7171

72-
var memberAccess =
73-
root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf()
74-
.OfType<MemberAccessExpressionSyntax>().First();
72+
var memberAccess = root.FindNode(diagnostic.Location.SourceSpan) as MemberAccessExpressionSyntax;
7573

76-
var operation = (IPropertyReferenceOperation)model.GetOperation(memberAccess);
77-
var property = operation.Property.Name;
74+
var propertyName = memberAccess.Name.ToString();
7875

7976
var expression = SyntaxFactory.MemberAccessExpression(
8077
SyntaxKind.SimpleMemberAccessExpression,
@@ -86,7 +83,7 @@ private static async Task<SyntaxNode> ReplaceMemberCall(CodeFixContext context,
8683
SyntaxFactory.IdentifierName("DateTimeAbstractions")),
8784
SyntaxFactory.IdentifierName("Clock"))
8885
.WithAdditionalAnnotations(Simplifier.Annotation),
89-
SyntaxFactory.IdentifierName(property));
86+
SyntaxFactory.IdentifierName(propertyName));
9087

9188
root = root.ReplaceNode(memberAccess, expression);
9289
return root;
@@ -96,28 +93,22 @@ private static SyntaxNode ApplyUsings(SyntaxNode root)
9693
{
9794
var compilation =
9895
root as CompilationUnitSyntax;
99-
100-
var abstractionsUsingStatement =
101-
SyntaxFactory.UsingDirective(
102-
SyntaxFactory.QualifiedName(
103-
SyntaxFactory.IdentifierName("Tocsoft"),
104-
SyntaxFactory.IdentifierName("DateTimeAbstractions")));
105-
106-
if (null == compilation)
107-
{
108-
root =
109-
root.InsertNodesBefore(
110-
root.ChildNodes().First(),
111-
new[] { abstractionsUsingStatement });
96+
if (compilation == null) {
97+
return root;
11298
}
113-
else if (compilation.Usings.All(u => u.Name.GetText().ToString() != "Tocsoft.DateTimeAbstractions"))
99+
100+
if (compilation.Usings.Any(x => x.Name.GetText().ToString() == "Tocsoft.DateTimeAbstractions"))
114101
{
115-
root =
116-
root.InsertNodesAfter(compilation.Usings.Last(),
117-
new[] { abstractionsUsingStatement });
102+
return root;
118103
}
119104

120-
return root;
105+
var abstractionsUsingStatement =
106+
SyntaxFactory.UsingDirective(
107+
SyntaxFactory.QualifiedName(
108+
SyntaxFactory.IdentifierName("Tocsoft"),
109+
SyntaxFactory.IdentifierName("DateTimeAbstractions")));
110+
111+
return compilation.AddUsings(abstractionsUsingStatement);
121112
}
122113
}
123114
}

Tocsoft.DateTimeAbstractions/Clock.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public void Dispose()
6565
}
6666

6767
public static DateTime Now => CurrentProvider.Now();
68+
public static DateTime Today => CurrentProvider.Now().Date;
6869
public static DateTime UtcNow => CurrentProvider.UtcNow();
6970
}
7071
}

Tocsoft.DateTimeAbstractions/Tocsoft.DateTimeAbstractions.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
<PropertyGroup>
1919
<AnlyzersOutputDir>..\Tocsoft.DateTimeAbstractions.Analyzer\bin\$(Configuration)\netstandard1.3</AnlyzersOutputDir>
20+
<Version>1.0.1</Version>
2021
</PropertyGroup>
2122
<ItemGroup>
2223
<None Include="$(AnlyzersOutputDir)\tools\*.ps1" Pack="true" PackagePath="tools" Visible="false" />

0 commit comments

Comments
 (0)