@@ -213,7 +213,7 @@ class MyClass
213213 }
214214
215215 [ TestMethod ]
216- public void ImplementEqualsAndGetHashCode_ImplementsEquals_HasField ( )
216+ public void ImplementEqualsAndGetHashCode_ClassImplementsEquals_HasField ( )
217217 {
218218 var original = @"
219219namespace ConsoleApplication1
@@ -237,5 +237,315 @@ public override bool Equals(object obj)
237237
238238 VerifyDiagnostic ( original ) ;
239239 }
240+
241+ [ TestMethod ]
242+ public void ImplementEqualsAndGetHashCode_StructDoesNotImplementEither_HasField ( )
243+ {
244+ var original = @"
245+ namespace ConsoleApplication1
246+ {
247+ struct MyStruct
248+ {
249+ string _foo;
250+ }
251+ }" ;
252+
253+ var result = @"
254+ namespace ConsoleApplication1
255+ {
256+ struct MyStruct
257+ {
258+ string _foo;
259+
260+ public override bool Equals(object obj)
261+ {
262+ if (obj == null || typeof(MyStruct) != obj.GetType())
263+ {
264+ return false;
265+ }
266+
267+ var value = (MyStruct)obj;
268+ return _foo == value._foo;
269+ }
270+
271+ public override int GetHashCode()
272+ {
273+ return _foo.GetHashCode();
274+ }
275+ }
276+ }" ;
277+
278+ VerifyDiagnostic ( original , string . Format ( ImplementEqualsAndGetHashCodeAnalyzer . Rule . MessageFormat . ToString ( ) , "MyStruct" ) ) ;
279+ VerifyFix ( original , result ) ;
280+ }
281+
282+ [ TestMethod ]
283+ public void ImplementEqualsAndGetHashCode_StructDoesNotImplementEither_HasProperty ( )
284+ {
285+ var original = @"
286+ namespace ConsoleApplication1
287+ {
288+ struct MyStruct
289+ {
290+ string Foo { get; set; }
291+ }
292+ }" ;
293+
294+ var result = @"
295+ namespace ConsoleApplication1
296+ {
297+ struct MyStruct
298+ {
299+ string Foo { get; set; }
300+
301+ public override bool Equals(object obj)
302+ {
303+ if (obj == null || typeof(MyStruct) != obj.GetType())
304+ {
305+ return false;
306+ }
307+
308+ var value = (MyStruct)obj;
309+ return Foo == value.Foo;
310+ }
311+
312+ public override int GetHashCode()
313+ {
314+ return Foo.GetHashCode();
315+ }
316+ }
317+ }" ;
318+
319+ VerifyDiagnostic ( original , string . Format ( ImplementEqualsAndGetHashCodeAnalyzer . Rule . MessageFormat . ToString ( ) , "MyStruct" ) ) ;
320+ VerifyFix ( original , result ) ;
321+ }
322+
323+ [ TestMethod ]
324+ public void ImplementEqualsAndGetHashCode_StructDoesNotImplementEither_HasMultipleFieldsAndProperties ( )
325+ {
326+ var original = @"
327+ namespace ConsoleApplication1
328+ {
329+ struct MyStruct
330+ {
331+ string _foo;
332+ string _bar;
333+
334+ string Foo { get; set; }
335+ string Bar { get; set; }
336+ }
337+ }" ;
338+
339+ var result = @"
340+ namespace ConsoleApplication1
341+ {
342+ struct MyStruct
343+ {
344+ string _foo;
345+ string _bar;
346+
347+ string Foo { get; set; }
348+ string Bar { get; set; }
349+
350+ public override bool Equals(object obj)
351+ {
352+ if (obj == null || typeof(MyStruct) != obj.GetType())
353+ {
354+ return false;
355+ }
356+
357+ var value = (MyStruct)obj;
358+ return _foo == value._foo && _bar == value._bar && Foo == value.Foo && Bar == value.Bar;
359+ }
360+
361+ public override int GetHashCode()
362+ {
363+ return _foo.GetHashCode() ^ _bar.GetHashCode() ^ Foo.GetHashCode() ^ Bar.GetHashCode();
364+ }
365+ }
366+ }" ;
367+
368+ VerifyDiagnostic ( original , string . Format ( ImplementEqualsAndGetHashCodeAnalyzer . Rule . MessageFormat . ToString ( ) , "MyStruct" ) ) ;
369+ VerifyFix ( original , result ) ;
370+ }
371+
372+ [ TestMethod ]
373+ public void ImplementEqualsAndGetHashCode_StructDoesNotImplementEither_HasSetOnlyProperty ( )
374+ {
375+ var original = @"
376+ namespace ConsoleApplication1
377+ {
378+ struct MyStruct
379+ {
380+ string _foo;
381+ string _bar;
382+
383+ string Foo { get; set; }
384+ string Bar
385+ {
386+ set { _bar = value; }
387+ }
388+ }
389+ }" ;
390+
391+ var result = @"
392+ namespace ConsoleApplication1
393+ {
394+ struct MyStruct
395+ {
396+ string _foo;
397+ string _bar;
398+
399+ string Foo { get; set; }
400+ string Bar
401+ {
402+ set { _bar = value; }
403+ }
404+
405+ public override bool Equals(object obj)
406+ {
407+ if (obj == null || typeof(MyStruct) != obj.GetType())
408+ {
409+ return false;
410+ }
411+
412+ var value = (MyStruct)obj;
413+ return _foo == value._foo && _bar == value._bar && Foo == value.Foo;
414+ }
415+
416+ public override int GetHashCode()
417+ {
418+ return _foo.GetHashCode() ^ _bar.GetHashCode() ^ Foo.GetHashCode();
419+ }
420+ }
421+ }" ;
422+
423+ VerifyDiagnostic ( original , string . Format ( ImplementEqualsAndGetHashCodeAnalyzer . Rule . MessageFormat . ToString ( ) , "MyStruct" ) ) ;
424+ VerifyFix ( original , result ) ;
425+ }
426+
427+ [ TestMethod ]
428+ public void ImplementEqualsAndGetHashCode_StructDoesNotImplementEither_DoesNotHaveFieldOrProperty ( )
429+ {
430+ var original = @"
431+ namespace ConsoleApplication1
432+ {
433+ struct MyStruct
434+ {
435+ }
436+ }" ;
437+
438+ VerifyDiagnostic ( original ) ;
439+ }
440+
441+ [ TestMethod ]
442+ public void ImplementEqualsAndGetHashCode_StructImplementsEquals_HasField ( )
443+ {
444+ var original = @"
445+ namespace ConsoleApplication1
446+ {
447+ struct MyStruct
448+ {
449+ string _foo;
450+
451+ public override bool Equals(object obj)
452+ {
453+ if (obj == null || typeof(MyStruct) != obj.GetType())
454+ {
455+ return false;
456+ }
457+
458+ var value = (MyStruct)obj;
459+ return _foo == value._foo;
460+ }
461+ }
462+ }" ;
463+
464+ VerifyDiagnostic ( original ) ;
465+ }
466+
467+ [ TestMethod ]
468+ public void ImplementEqualsAndGetHashCode_ClassDoesNotImplementEither_HasChainedFields ( )
469+ {
470+ var original = @"
471+ namespace ConsoleApplication1
472+ {
473+ class MyClass
474+ {
475+ string _foo = ""test"", _bar = ""test"";
476+ }
477+ }" ;
478+
479+ var result = @"
480+ namespace ConsoleApplication1
481+ {
482+ class MyClass
483+ {
484+ string _foo = ""test"", _bar = ""test"";
485+
486+ public override bool Equals(object obj)
487+ {
488+ if (obj == null || typeof(MyClass) != obj.GetType())
489+ {
490+ return false;
491+ }
492+
493+ var value = (MyClass)obj;
494+ return _foo == value._foo && _bar == value._bar;
495+ }
496+
497+ public override int GetHashCode()
498+ {
499+ return _foo.GetHashCode() ^ _bar.GetHashCode();
500+ }
501+ }
502+ }" ;
503+
504+ VerifyDiagnostic ( original , string . Format ( ImplementEqualsAndGetHashCodeAnalyzer . Rule . MessageFormat . ToString ( ) , "MyClass" ) ) ;
505+ VerifyFix ( original , result ) ;
506+ }
507+
508+ [ TestMethod ]
509+ public void ImplementEqualsAndGetHashCode_ClassDoesNotImplementEither_HasStaticFields ( )
510+ {
511+ var original = @"
512+ namespace ConsoleApplication1
513+ {
514+ class MyClass
515+ {
516+ string _foo = ""test"";
517+ static string _bar = ""test"";
518+ }
519+ }" ;
520+
521+ var result = @"
522+ namespace ConsoleApplication1
523+ {
524+ class MyClass
525+ {
526+ string _foo = ""test"";
527+ static string _bar = ""test"";
528+
529+ public override bool Equals(object obj)
530+ {
531+ if (obj == null || typeof(MyClass) != obj.GetType())
532+ {
533+ return false;
534+ }
535+
536+ var value = (MyClass)obj;
537+ return _foo == value._foo;
538+ }
539+
540+ public override int GetHashCode()
541+ {
542+ return _foo.GetHashCode();
543+ }
544+ }
545+ }" ;
546+
547+ VerifyDiagnostic ( original , string . Format ( ImplementEqualsAndGetHashCodeAnalyzer . Rule . MessageFormat . ToString ( ) , "MyClass" ) ) ;
548+ VerifyFix ( original , result ) ;
549+ }
240550 }
241551}
0 commit comments