@@ -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;
104135using System;
136+
105137namespace 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;
126157using Tocsoft.DateTimeAbstractions;
158+ using System;
127159
128160namespace 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 {
0 commit comments