-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e8e4de
commit f63f00a
Showing
3 changed files
with
60 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 42 additions & 73 deletions
115
....tests/Analyzers/X2000/AssertEmptyShouldNotBeUsedForCollectionDoesNotContainCheckTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,144 +1,113 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using Verify = CSharpVerifier<Xunit.Analyzers.AssertEmptyShouldNotBeUsedForCollectionDoesNotContainCheck>; | ||
|
||
|
||
|
||
public class AssertEmptyShouldNotBeUsedForCollectionDoesNotContainCheckTests | ||
{ | ||
public static TheoryData<string> GetEnumerables(string typeName) | ||
{ | ||
return new TheoryData<string>() | ||
{ | ||
$"new System.Collections.Generic.List<{typeName}>()", | ||
$"new System.Collections.Generic.HashSet<{typeName}>()", | ||
$"new System.Collections.ObjectModel.Collection<{typeName}>()", | ||
$"new {typeName}[0]", | ||
$"System.Linq.Enumerable.Empty<{typeName}>()" | ||
}; | ||
} | ||
|
||
public static TheoryData<string> GetSampleStrings() | ||
{ | ||
return new TheoryData<string>() | ||
{ | ||
String.Empty, | ||
"123", | ||
@"abc\n\t\\\""" | ||
}; | ||
} | ||
public static TheoryData<string, string> GetEnumerables( | ||
string typeName, | ||
string comparison) => | ||
new() | ||
{ | ||
{ $"new System.Collections.Generic.List<{typeName}>()", comparison }, | ||
{ $"new System.Collections.Generic.HashSet<{typeName}>()", comparison }, | ||
{ $"new System.Collections.ObjectModel.Collection<{typeName}>()", comparison }, | ||
{ $"new {typeName}[0]", comparison }, | ||
{ $"System.Linq.Enumerable.Empty<{typeName}>()", comparison }, | ||
}; | ||
|
||
[Theory] | ||
[MemberData(nameof(GetEnumerables), "int")] | ||
public async Task FindsWarningForIntEnumerableDoesNotContainCheckWithEmpty(string collection) | ||
[MemberData(nameof(GetEnumerables), "int", "")] | ||
[MemberData(nameof(GetEnumerables), "string", "")] | ||
public async Task Containers_WithoutWhereClause_DoesNotTrigger( | ||
string collection, | ||
string _) | ||
{ | ||
var source = $@" | ||
using System.Linq; | ||
class TestClass | ||
{{ | ||
void TestMethod() | ||
{{ | ||
[|Xunit.Assert.Empty({collection}.Where(f => f > 0))|]; | ||
Xunit.Assert.Empty({collection}); | ||
}} | ||
}}"; | ||
await Verify.VerifyAnalyzer(source); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(GetEnumerables), "string")] | ||
public async Task FindsWarningForStringEnumerableDoesNotContainCheckWithEmpty(string collection) | ||
[MemberData(nameof(GetEnumerables), "int", "f > 0")] | ||
[MemberData(nameof(GetEnumerables), "string", "f.Length > 0")] | ||
public async Task Containers_WithWhereClause_Triggers( | ||
string collection, | ||
string comparison) | ||
{ | ||
var source = $@" | ||
using System.Linq; | ||
class TestClass | ||
{{ | ||
void TestMethod() | ||
{{ | ||
[|Xunit.Assert.Empty({collection}.Where(f => f.Length > 0))|]; | ||
[|Xunit.Assert.Empty({collection}.Where(f => {comparison}))|]; | ||
}} | ||
}}"; | ||
await Verify.VerifyAnalyzer(source); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(GetSampleStrings))] | ||
public async Task FindsWarningForStringDoesNotContainCheckWithEmpty(string sampleString) | ||
{ | ||
var source = $@" | ||
using System.Linq; | ||
class TestClass | ||
{{ | ||
void TestMethod() | ||
{{ | ||
[|Xunit.Assert.Empty(""{sampleString}"".Where(f => f > 0))|]; | ||
}} | ||
}}"; | ||
await Verify.VerifyAnalyzer(source); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(GetEnumerables), "int")] | ||
public async Task DoesNotFindWarningForIntEnumerableDoesNotContainCheckWithEmptyWithIndex(string collection) | ||
[MemberData(nameof(GetEnumerables), "int", "f > 0")] | ||
[MemberData(nameof(GetEnumerables), "string", "f.Length > 0")] | ||
public async Task Containers_WithWhereClauseWithIndex_DoesNotTrigger( | ||
string collection, | ||
string comparison) | ||
{ | ||
var source = $@" | ||
using System.Linq; | ||
class TestClass | ||
{{ | ||
void TestMethod() | ||
{{ | ||
Xunit.Assert.Empty({collection}.Where((f, i) => f > 0 && i > 0)); | ||
Xunit.Assert.Empty({collection}.Where((f, i) => {comparison} && i > 0)); | ||
}} | ||
}}"; | ||
await Verify.VerifyAnalyzer(source); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(GetEnumerables), "string")] | ||
public async Task DoesNotFindWarningForStringEnumerableDoesNotContainCheckWithEmptyWithIndex(string collection) | ||
[MemberData(nameof(GetEnumerables), "int", "f > 0")] | ||
[MemberData(nameof(GetEnumerables), "string", "f.Length > 0")] | ||
public async Task DoesNotFindWarningForEnumurableEmptyCheckWithChainedLinq( | ||
string collection, | ||
string comparison) | ||
{ | ||
var source = $@" | ||
using System.Linq; | ||
class TestClass | ||
{{ | ||
void TestMethod() | ||
{{ | ||
Xunit.Assert.Empty({collection}.Where((f, i) => f.Length > 0 && i > 0)); | ||
Xunit.Assert.Empty({collection}.Where(f => {comparison}).Select(f => f)); | ||
}} | ||
}}"; | ||
await Verify.VerifyAnalyzer(source); | ||
} | ||
|
||
public static TheoryData<string> GetSampleStrings() => | ||
new(string.Empty, "123", @"abc\n\t\\\"""); | ||
|
||
[Theory] | ||
[MemberData(nameof(GetEnumerables), "int")] | ||
[MemberData(nameof(GetEnumerables), "string")] | ||
public async Task DoesNotFindWarningForEnumerableEmptyCheckWithoutLinq(string collection) | ||
[MemberData(nameof(GetSampleStrings))] | ||
public async Task Strings_WithWhereClause_DoesNotTrigger(string sampleString) | ||
{ | ||
var source = $@" | ||
using System.Linq; | ||
class TestClass | ||
{{ | ||
void TestMethod() | ||
{{ | ||
Xunit.Assert.Empty({collection}); | ||
[|Xunit.Assert.Empty(""{sampleString}"".Where(f => f > 0))|]; | ||
}} | ||
}}"; | ||
await Verify.VerifyAnalyzer(source); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(GetEnumerables), "int")] | ||
[MemberData(nameof(GetEnumerables), "string")] | ||
public async Task DoesNotFindWarningForEnumurableEmptyCheckWithChainedLinq(string collection) | ||
{ | ||
var source = $@" | ||
using System.Linq; | ||
class TestClass | ||
{{ | ||
void TestMethod() | ||
{{ | ||
Xunit.Assert.Empty({collection}.Where(f => f.ToString().Length > 0).Select(f => f)); | ||
}} | ||
}}"; | ||
await Verify.VerifyAnalyzer(source); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters