Skip to content

Commit c9506cf

Browse files
committed
Add accessor list tests
1 parent e2e7ed3 commit c9506cf

1 file changed

Lines changed: 258 additions & 0 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/ReadabilityRules/SA1137UnitTests.cs

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,264 @@ void TypeWithMultipleConstraints2<T1, T2, T3>()
373373
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
374374
}
375375

376+
[Fact]
377+
public async Task TestPropertyAccessorListAsync()
378+
{
379+
string testCode = @"
380+
using System;
381+
382+
class Container
383+
{
384+
int Property1
385+
{
386+
[My]
387+
get;
388+
389+
set;
390+
}
391+
392+
int Property2
393+
{
394+
[My]
395+
get;
396+
397+
set;
398+
}
399+
400+
int Property3
401+
{
402+
[My] get;
403+
404+
set;
405+
}
406+
}
407+
408+
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
409+
class MyAttribute : Attribute { }
410+
";
411+
string fixedCode = @"
412+
using System;
413+
414+
class Container
415+
{
416+
int Property1
417+
{
418+
[My]
419+
get;
420+
421+
set;
422+
}
423+
424+
int Property2
425+
{
426+
[My]
427+
get;
428+
429+
set;
430+
}
431+
432+
int Property3
433+
{
434+
[My] get;
435+
436+
set;
437+
}
438+
}
439+
440+
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
441+
class MyAttribute : Attribute { }
442+
";
443+
444+
DiagnosticResult[] expected =
445+
{
446+
this.CSharpDiagnostic().WithLocation(8, 1),
447+
this.CSharpDiagnostic().WithLocation(11, 1),
448+
this.CSharpDiagnostic().WithLocation(16, 1),
449+
this.CSharpDiagnostic().WithLocation(19, 1),
450+
this.CSharpDiagnostic().WithLocation(24, 1),
451+
};
452+
453+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
454+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
455+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
456+
}
457+
458+
[Fact]
459+
public async Task TestIndexerAccessorListAsync()
460+
{
461+
string testCode = @"
462+
using System;
463+
464+
interface IContainer1
465+
{
466+
int this[int arg]
467+
{
468+
[My]
469+
get;
470+
471+
set;
472+
}
473+
}
474+
475+
interface IContainer2
476+
{
477+
int this[int arg]
478+
{
479+
[My]
480+
get;
481+
482+
set;
483+
}
484+
}
485+
486+
interface IContainer3
487+
{
488+
int this[int arg]
489+
{
490+
[My] get;
491+
492+
set;
493+
}
494+
}
495+
496+
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
497+
class MyAttribute : Attribute { }
498+
";
499+
string fixedCode = @"
500+
using System;
501+
502+
interface IContainer1
503+
{
504+
int this[int arg]
505+
{
506+
[My]
507+
get;
508+
509+
set;
510+
}
511+
}
512+
513+
interface IContainer2
514+
{
515+
int this[int arg]
516+
{
517+
[My]
518+
get;
519+
520+
set;
521+
}
522+
}
523+
524+
interface IContainer3
525+
{
526+
int this[int arg]
527+
{
528+
[My] get;
529+
530+
set;
531+
}
532+
}
533+
534+
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
535+
class MyAttribute : Attribute { }
536+
";
537+
538+
DiagnosticResult[] expected =
539+
{
540+
this.CSharpDiagnostic().WithLocation(8, 1),
541+
this.CSharpDiagnostic().WithLocation(11, 1),
542+
this.CSharpDiagnostic().WithLocation(19, 1),
543+
this.CSharpDiagnostic().WithLocation(22, 1),
544+
this.CSharpDiagnostic().WithLocation(30, 1),
545+
};
546+
547+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
548+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
549+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
550+
}
551+
552+
[Fact]
553+
public async Task TestEventAccessorListAsync()
554+
{
555+
string testCode = @"
556+
using System;
557+
558+
class Container
559+
{
560+
event EventHandler Event1
561+
{
562+
[My]
563+
add { }
564+
565+
remove { }
566+
}
567+
568+
event EventHandler Event2
569+
{
570+
[My]
571+
add { }
572+
573+
remove { }
574+
}
575+
576+
event EventHandler Event3
577+
{
578+
[My] add { }
579+
580+
remove { }
581+
}
582+
}
583+
584+
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
585+
class MyAttribute : Attribute { }
586+
";
587+
string fixedCode = @"
588+
using System;
589+
590+
class Container
591+
{
592+
event EventHandler Event1
593+
{
594+
[My]
595+
add { }
596+
597+
remove { }
598+
}
599+
600+
event EventHandler Event2
601+
{
602+
[My]
603+
add { }
604+
605+
remove { }
606+
}
607+
608+
event EventHandler Event3
609+
{
610+
[My] add { }
611+
612+
remove { }
613+
}
614+
}
615+
616+
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
617+
class MyAttribute : Attribute { }
618+
";
619+
620+
DiagnosticResult[] expected =
621+
{
622+
this.CSharpDiagnostic().WithLocation(8, 1),
623+
this.CSharpDiagnostic().WithLocation(11, 1),
624+
this.CSharpDiagnostic().WithLocation(16, 1),
625+
this.CSharpDiagnostic().WithLocation(19, 1),
626+
this.CSharpDiagnostic().WithLocation(24, 1),
627+
};
628+
629+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
630+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
631+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
632+
}
633+
376634
[Fact]
377635
public async Task TestValidVariableDeclarationAsync()
378636
{

0 commit comments

Comments
 (0)