Skip to content

Commit

Permalink
S3257: Add UTs for Alias any type
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioaversa committed Sep 28, 2023
1 parent 09d04b5 commit baed000
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,30 @@ public void RedundantDeclaration_CSharp10_CodeFix_ExplicitDelegate() =>
.WithOptions(ParseOptionsHelper.FromCSharp10)
.VerifyCodeFix();

[TestMethod]
public void RedundantDeclaration_CSharp12() =>
builder.AddPaths("RedundantDeclaration.CSharp12.cs")
.WithOptions(ParseOptionsHelper.FromCSharp12)
.Verify();

[TestMethod]
public void RedundantDeclaration_CSharp12_CodeFix_ArraySize() =>
codeFixBuilder.AddPaths("RedundantDeclaration.CSharp12.cs")
.WithCodeFix<RedundantDeclarationCodeFix>()
.WithCodeFixTitle(RedundantDeclarationCodeFix.TitleRedundantArraySize)
.WithCodeFixedPaths("RedundantDeclaration.CSharp12.ArraySize.Fixed.cs")
.WithOptions(ParseOptionsHelper.FromCSharp12)
.VerifyCodeFix();

[TestMethod]
public void RedundantDeclaration_CSharp12_CodeFix_LambdaParameterType() =>
codeFixBuilder.AddPaths("RedundantDeclaration.CSharp12.cs")
.WithCodeFix<RedundantDeclarationCodeFix>()
.WithCodeFixTitle(RedundantDeclarationCodeFix.TitleRedundantLambdaParameterType)
.WithCodeFixedPaths("RedundantDeclaration.CSharp12.LambdaParameterType.Fixed.cs")
.WithOptions(ParseOptionsHelper.FromCSharp12)
.VerifyCodeFix();

#endif

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;

namespace AliasAnyType
{
using Int = int;
using IntArray = int[];
using IntNullable = int?;
using Point = (int x, int y);
using ThreeInts = int[3]; // Error [CS0270]: Array size cannot be specified in a variable declaration
using IntToIntFunc = Func<int, int>;

class AClass
{
void AliasWithArraySize()
{
IntArray a1 = new Int[] { 1, 2, 3 }; // Fixed
}

void AliasWithUnnecessaryType()
{
IntToIntFunc f = (Int i) => i; // Fixed
}

void AliasWithInitializer()
{
IntArray a1 = new IntArray { }; // Error [CS8386]: Invalid object creation
var a2 = new IntArray { }; // Error [CS8386]: Invalid object creation
int[] a3 = new IntArray(); // Error [CS8386]: Invalid object creation
}

void AliasWithEmptyParamsList()
{
IntArray a1 = new IntArray(); // Error [CS8386]: Invalid object creation
var a2 = new IntArray(); // Error [CS8386]: Invalid object creation
int[] a3 = new IntArray(); // Error [CS8386]: Invalid object creation
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;

namespace AliasAnyType
{
using Int = int;
using IntArray = int[];
using IntNullable = int?;
using Point = (int x, int y);
using ThreeInts = int[3]; // Error [CS0270]: Array size cannot be specified in a variable declaration
using IntToIntFunc = Func<int, int>;

class AClass
{
void AliasWithArraySize()
{
IntArray a1 = new Int[3] { 1, 2, 3 }; // Fixed
}

void AliasWithUnnecessaryType()
{
IntToIntFunc f = (i) => i; // Fixed
}

void AliasWithInitializer()
{
IntArray a1 = new IntArray { }; // Error [CS8386]: Invalid object creation
var a2 = new IntArray { }; // Error [CS8386]: Invalid object creation
int[] a3 = new IntArray(); // Error [CS8386]: Invalid object creation
}

void AliasWithEmptyParamsList()
{
IntArray a1 = new IntArray(); // Error [CS8386]: Invalid object creation
var a2 = new IntArray(); // Error [CS8386]: Invalid object creation
int[] a3 = new IntArray(); // Error [CS8386]: Invalid object creation
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;

namespace AliasAnyType
{
using Int = int;
using IntArray = int[];
using IntNullable = int?;
using Point = (int x, int y);
using ThreeInts = int[3]; // Error [CS0270]: Array size cannot be specified in a variable declaration
using IntToIntFunc = Func<int, int>;

class AClass
{
void AliasWithArraySize()
{
IntArray a1 = new Int[3] { 1, 2, 3 }; // Noncompliant
}

void AliasWithUnnecessaryType()
{
IntToIntFunc f = (Int i) => i; // Noncompliant {{Remove the type specification; it is redundant.}}
}

void AliasWithInitializer()
{
IntArray a1 = new IntArray { }; // Error [CS8386]: Invalid object creation
var a2 = new IntArray { }; // Error [CS8386]: Invalid object creation
int[] a3 = new IntArray(); // Error [CS8386]: Invalid object creation
}

void AliasWithEmptyParamsList()
{
IntArray a1 = new IntArray(); // Error [CS8386]: Invalid object creation
var a2 = new IntArray(); // Error [CS8386]: Invalid object creation
int[] a3 = new IntArray(); // Error [CS8386]: Invalid object creation
}
}
}

0 comments on commit baed000

Please sign in to comment.