Skip to content

Commit

Permalink
Fix issue 77 and add tests (#78)
Browse files Browse the repository at this point in the history
* Fix issue 77 and add tests

* Remove trailing whitespace
  • Loading branch information
pakdev authored Jun 7, 2023
1 parent 0e2c2f5 commit 7db4984
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private static string EnsureUniqueMemberName(
var suffix = 2;
var name = baseName;

while (!symbols.Any(x => string.Equals(x.Name, name, StringComparison.Ordinal)))
while (symbols.Any(x => string.Equals(x.Name, name, StringComparison.Ordinal)))
{
name = baseName + suffix.ToString(CultureInfo.InvariantCulture);
suffix++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,34 @@

namespace NationalInstruments.Analyzers.UnitTests
{
public sealed class FieldsCamelCasedWithUnderscoreAnalyzerTests : NIDiagnosticAnalyzerTests<FieldsCamelCasedWithUnderscoreAnalyzer>
public sealed class FieldsCamelCasedWithUnderscoreAnalyzerTests : NIDiagnosticAnalyzerWithCodeFixTests<FieldsCamelCasedWithUnderscoreAnalyzer, FieldsCamelCasedWithUnderscoreCodeFixProvider>
{
private const string NoLeadingUnderscoreCodeMarkup = @"
class Foo
{
private int <|name|>;
}";

private const string ExtraLeadingUnderscoreCodeMarkup = @"
class Foo
{
private int <|__name|>;
}";

private const string UnderscoreUppercaseLetterCodeMarkup = @"
class Foo
{
private int <|_Name|>;
}";

private const string FixedCode = @"
class Foo
{
private int _name;
}";

[Fact]
public void NI1001_PublicField_NoDiagnostic()
public void PublicField_Verify_NoDiagnostic()
{
var test = new TestFile(@"
class Foo
Expand All @@ -21,7 +45,7 @@ class Foo
}

[Fact]
public void NI1001_InternalField_NoDiagnostic()
public void InternalField_Verify_NoDiagnostic()
{
var test = new TestFile(@"
class Foo
Expand All @@ -33,7 +57,7 @@ class Foo
}

[Fact]
public void NI1001_ProtectedField_NoDiagnostic()
public void ProtectedField_Verify_NoDiagnostic()
{
var test = new TestFile(@"
class Foo
Expand All @@ -45,7 +69,7 @@ class Foo
}

[Fact]
public void NI1001_PropertyImplicitBackingField_NoDiagnostic()
public void PropertyImplicitBackingField_Verify_NoDiagnostic()
{
var test = new TestFile(@"
class Foo
Expand All @@ -57,7 +81,7 @@ class Foo
}

[Fact]
public void NI1001_PrivateConst_NoDiagnostic()
public void PrivateConst_Verify_NoDiagnostic()
{
var test = new TestFile(@"
class Foo
Expand All @@ -69,7 +93,7 @@ class Foo
}

[Fact]
public void NI1001_PrivateReadonlyField_NoDiagnostic()
public void PrivateReadonlyField_Verify_NoDiagnostic()
{
var test = new TestFile(@"
class Foo
Expand All @@ -81,7 +105,7 @@ class Foo
}

[Fact]
public void NI1001_UnderscoreAndBeginsLowercase_NoDiagnostic()
public void UnderscoreAndBeginsLowercase_Verify_NoDiagnostic()
{
var test = new TestFile(@"
class Foo
Expand All @@ -93,7 +117,7 @@ class Foo
}

[Fact]
public void NI1001_UnderscoreOnly_NoDiagnostic()
public void UnderscoreOnly_Verify_NoDiagnostic()
{
var test = new TestFile(@"
class Foo
Expand All @@ -105,7 +129,7 @@ class Foo
}

[Fact]
public void NI1001_InternalUnderscore_NoDiagnostic()
public void InternalUnderscore_Verify_NoDiagnostic()
{
var test = new TestFile(@"
class Foo
Expand All @@ -117,7 +141,7 @@ class Foo
}

[Fact]
public void NI1001_TrailingUnderscore_NoDiagnostic()
public void TrailingUnderscore_Verify_NoDiagnostic()
{
var test = new TestFile(@"
class Foo
Expand All @@ -129,43 +153,67 @@ class Foo
}

[Fact]
public void NI1001_NoLeadingUnderscore_Diagnostic()
public void NoLeadingUnderscore_Verify_Diagnostic()
{
var test = new TestFile(@"
class Foo
{
private int name;
}");
var test = new AutoTestFile(
NoLeadingUnderscoreCodeMarkup,
GetNI1001FieldsCamelCasedWithUnderscoreRule());

VerifyDiagnostics(test, GetNI1001ResultAt(4, 17, "name"));
VerifyDiagnostics(test);
}

[Fact]
public void NI1001_ExtraLeadingUnderscore_Diagnostic()
public void NoLeadingUnderscore_ApplyFix_FixedWithNoDiagnostic()
{
var test = new TestFile(@"
class Foo
{
private int __name;
}");
var testBeforeFix = new AutoTestFile(NoLeadingUnderscoreCodeMarkup);
var testAfterFix = new TestFile(FixedCode);

VerifyDiagnostics(test, GetNI1001ResultAt(4, 17, "__name"));
VerifyFix(testBeforeFix, testAfterFix);
VerifyDiagnostics(testAfterFix);
}

[Fact]
public void NI1001_UnderscoreUppercaseLetter_Diagnostic()
public void ExtraLeadingUnderscore_Verify_Diagnostic()
{
var test = new TestFile(@"
class Foo
{
private int _Name;
}");
var test = new AutoTestFile(
ExtraLeadingUnderscoreCodeMarkup,
GetNI1001FieldsCamelCasedWithUnderscoreRule());

VerifyDiagnostics(test);
}

[Fact]
public void ExtraLeadingUnderscore_ApplyFix_FixedWithNoDiagnostic()
{
var testBeforeFix = new AutoTestFile(ExtraLeadingUnderscoreCodeMarkup);
var testAfterFix = new TestFile(FixedCode);

VerifyFix(testBeforeFix, testAfterFix);
VerifyDiagnostics(testAfterFix);
}

VerifyDiagnostics(test, GetNI1001ResultAt(4, 17, "_Name"));
[Fact]
public void UnderscoreUppercaseLetter_Verify_Diagnostic()
{
var test = new AutoTestFile(
UnderscoreUppercaseLetterCodeMarkup,
GetNI1001FieldsCamelCasedWithUnderscoreRule());

VerifyDiagnostics(test);
}

[Fact]
public void NI1001_UnderscoreNumeral_NoDiagnostic()
public void UnderscoreUppercaseLetter_ApplyFix_FixedWithNoDiagnostic()
{
var testBeforeFix = new AutoTestFile(UnderscoreUppercaseLetterCodeMarkup);
var testAfterFix = new TestFile(FixedCode);

VerifyFix(testBeforeFix, testAfterFix);
VerifyDiagnostics(testAfterFix);
}

[Fact]
public void UnderscoreNumeral_Verify_NoDiagnostic()
{
var test = new TestFile(@"
class Foo
Expand All @@ -176,9 +224,7 @@ class Foo
VerifyDiagnostics(test);
}

private DiagnosticResult GetNI1001ResultAt(int line, int column, string fieldName)
{
return GetResultAt(line, column, FieldsCamelCasedWithUnderscoreAnalyzer.Rule, fieldName);
}
private Rule GetNI1001FieldsCamelCasedWithUnderscoreRule()
=> new Rule(FieldsCamelCasedWithUnderscoreAnalyzer.Rule);
}
}

0 comments on commit 7db4984

Please sign in to comment.