Commit 69017a94 authored by Nathan Bean's avatar Nathan Bean
Browse files

Added support for functional validation

parent c74ed005
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Helpers\**" />
<EmbeddedResource Remove="Helpers\**" />
<None Remove="Helpers\**" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.3.2" />
......@@ -20,4 +27,8 @@
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.CodeRefactoring.Testing.MSTest" Version="1.0.1-beta1.*" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CodeAnalyzers\CodeAnalyzers.csproj" />
</ItemGroup>
</Project>
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading.Tasks;
using CodeAnalyzers.Test;
using KSU.CS.Pendant.CodeAnalyzers;
/*
namespace KSU.CS.Pendant.CodeAnalyzers.Test
{
[TestClass]
......@@ -41,3 +41,4 @@ namespace KSU.CS.Pendant.CodeAnalyzers.Test
}
}
}
*/
using Microsoft.CodeAnalysis;
using System;
namespace Foobar
namespace CodeAnalyzers.Test.Helpers
{
/// <summary>
/// Location where the diagnostic appears, as determined by path, line number, and column number.
......
......@@ -7,7 +7,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace KSU.CS.Pendant.CodeAnalyzers.Test.Helpers
namespace CodeAnalyzers.Test.Helpers
{
/// <summary>
/// Superclass of all Unit Tests for DiagnosticAnalyzers
......@@ -20,7 +20,7 @@ namespace KSU.CS.Pendant.CodeAnalyzers.Test.Helpers
/// </summary>
protected virtual DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()
{
return new CommentsAnalyzer();
return null;
}
/// <summary>
......
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading.Tasks;
using VerifyCS = CodeAnalyzers.Test.CSharpAnalyzerVerifier<
KSU.CS.Pendant.CodeAnalysis.StyleAnalyzers.NamingConventionsAnalyzer>;
namespace KSU.CS.Pendant.CodeAnalysis.Test
{
[TestClass]
public class NamingConventionsAnalyzerUnitTests
{
[TestMethod]
public async Task ValidNamespaceNamesPass()
{
var test = @"
namespace Valid.Example.OfANamespace
{
class TestClass
{
}
}
";
await VerifyCS.VerifyAnalyzerAsync(test);
}
[TestMethod]
public async Task NamespaceFirstSegmentMustBePascalCase()
{
var test = @"
namespace {|#0:invalid.Example|}
{
class TestClass
{
int Foo()
{
var f = 5;
return f;
}
}
}
";
var expected = VerifyCS.Diagnostic("NAM0001").WithLocation(0).WithArguments("invalid.Example", "invalid");
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}
[TestMethod]
public async Task LastNamespaceSegmentsMustBePascalCase()
{
var test = @"
namespace {|#0:Invalid.example|}
{
class TestClass
{
}
}
";
var expected = VerifyCS.Diagnostic("NAM0001").WithLocation(0).WithArguments("Invalid.example", "example");
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}
[TestMethod]
public async Task ClassNamesMustBePascalCase()
{
var test = @"
namespace Example
{
public class {|#0:testClass|}
{
}
}
";
var expected = VerifyCS.Diagnostic("NAM0002").WithLocation(0).WithArguments("testClass");
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}
[TestMethod]
public async Task ClassesShouldNotBeNamedLikeInterfaces()
{
var test = @"
namespace Example
{
public class {|#0:ITestClass|}
{
}
}
";
var expected = VerifyCS.Diagnostic("NAM0003").WithLocation(0).WithArguments("ITestClass", 'T');
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}
[TestMethod]
public async Task StructNamesMustBePascalCase()
{
var test = @"
namespace Example
{
public struct {|#0:testStruct|}
{
}
}
";
var expected = VerifyCS.Diagnostic("NAM0004").WithLocation(0).WithArguments("testStruct");
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}
[TestMethod]
public async Task StructsShouldNotBeNamedLikeInterfaces()
{
var test = @"
namespace Example
{
public struct {|#0:ITestStruct|}
{
}
}
";
var expected = VerifyCS.Diagnostic("NAM0005").WithLocation(0).WithArguments("ITestStruct", 'T');
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}
[TestMethod]
public async Task EnumNamesMustBePascalCase()
{
var test = @"
namespace Example
{
public enum {|#0:testEnum|}
{
One, Two, Three
}
}
";
var expected = VerifyCS.Diagnostic("NAM0006").WithLocation(0).WithArguments("testEnum");
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}
[TestMethod]
public async Task EnumsShouldNotBeNamedLikeInterfaces()
{
var test = @"
namespace Example
{
public enum {|#0:ITestEnum|}
{
One, Two, Three
}
}
";
var expected = VerifyCS.Diagnostic("NAM0007").WithLocation(0).WithArguments("ITestEnum", 'T');
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}
[TestMethod]
public async Task InterfacesShouldStartWithI()
{
var test = @"
namespace Example
{
public interface {|#0:TestInterface|}
{
}
}
";
var expected = VerifyCS.Diagnostic("NAM0008").WithLocation(0).WithArguments("TestInterface");
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}
[TestMethod]
public async Task InterfacesShouldUsePascalCase()
{
var test = @"
namespace Example
{
public interface {|#0:ItestInterface|}
{
}
}
";
var expected = VerifyCS.Diagnostic("NAM0009").WithLocation(0).WithArguments("ItestInterface", 'I');
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}
[TestMethod]
public async Task MethodsShouldUsePascalCase()
{
var test = @"
namespace Example
{
public class TestClass
{
public void {|#0:doSomething|}() {}
}
}
";
var expected = VerifyCS.Diagnostic("NAM0010").WithLocation(0).WithArguments("doSomething");
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}
[TestMethod]
public async Task PropertiesShouldUsePascalCase()
{
var test = @"
namespace Example
{
public class TestClass
{
public string {|#0:info|} {get; set;}
}
}
";
var expected = VerifyCS.Diagnostic("NAM0011").WithLocation(0).WithArguments("info");
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}
[TestMethod]
public async Task ParametersShouldUseCamelCase()
{
var test = @"
namespace Example
{
public class TestClass
{
public int TestMethod(int {|#0:Num|})
{
return Num + 5;
}
}
}
";
var expected = VerifyCS.Diagnostic("NAM0012").WithLocation(0).WithArguments("Num");
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}
[TestMethod]
public async Task PublicConstFieldsShouldUsePascalCase()
{
var test = @"
namespace Example
{
public class TestClass
{
public const double {|#0:pi|} = 3.14;
}
}
";
var expected = VerifyCS.Diagnostic("NAM0013").WithLocation(0).WithArguments("pi");
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}
[TestMethod]
public async Task PrivateFieldsShouldBePrefixedWithUnderscore()
{
var test = @"
namespace Example
{
public class TestClass
{
private double {|#0:pi|} = 3.14;
}
}
";
var expected = VerifyCS.Diagnostic("NAM0014").WithLocation(0).WithArguments("pi");
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}
[TestMethod]
public async Task ProtectedFieldsShouldBePrefixedWithUnderscore()
{
var test = @"
namespace Example
{
public class TestClass
{
protected double {|#0:pi|} = 3.14;
}
}
";
var expected = VerifyCS.Diagnostic("NAM0014").WithLocation(0).WithArguments("pi");
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}
[TestMethod]
public async Task PrivateFieldsShouldUseCamelCase()
{
var test = @"
namespace Example
{
public class TestClass
{
private double {|#0:_Pi|} = 3.14;
}
}
";
var expected = VerifyCS.Diagnostic("NAM0015").WithLocation(0).WithArguments("_Pi");
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}
[TestMethod]
public async Task ProtectedFieldsShouldUseCamelCase()
{
var test = @"
namespace Example
{
public class TestClass
{
private double {|#0:_Pi|} = 3.14;
}
}
";
var expected = VerifyCS.Diagnostic("NAM0015").WithLocation(0).WithArguments("_Pi");
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}
[TestMethod]
public async Task LocalVariablesShouldUseCamelCase()
{
var test = @"
namespace Example
{
public class TestClass
{
private void DoSomething()
{
int {|#0:Local|} = 5;
}
}
}
";
var expected = VerifyCS.Diagnostic("NAM0016").WithLocation(0).WithArguments("Local");
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}
[TestMethod]
public async Task MultipleLocalVariablesShouldUseCamelCase()
{
var test = @"
namespace Example
{
public class TestClass
{
private void DoSomething()
{
int num1 = 0, num2 = 4, {|#0:Num3|} = 5;
}
}
}
";
var expected = VerifyCS.Diagnostic("NAM0016").WithLocation(0).WithArguments("Num3");
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}
}
}
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading.Tasks;
using VerifyCS = CodeAnalyzers.Test.CSharpAnalyzerVerifier<
KSU.CS.Pendant.CodeAnalysis.DesignAnalyzers.PropertySelfReferenceAnalyzer>;
namespace KSU.CS.Pendant.CodeAnalysis.Test
{
[TestClass]
public class PropertySelfReferenceAnalyzerUnitTest
{
//No diagnostics expected to show up
[TestMethod]
public async Task NotFoundInAutoProperty()
{
var test = @"
namespace ConsoleApplication1
{
class TestClass
{
public int Number { get; set; } = 0;
}
}
";
await VerifyCS.VerifyAnalyzerAsync(test);
}
//No diagnostics expected to show up
[TestMethod]
public async Task NotFoundInProperPrivateBackingVariableProperty()
{
var test = @"
namespace ConsoleApplication1
{
class TestClass
{
private int _number = 0;
public int Number
{
get { return _number; }
set { _number = value; }
}
}
}
";
await VerifyCS.VerifyAnalyzerAsync(test);
}
//No diagnostics expected to show up
[TestMethod]
public async Task NotFoundWithSharedTypeIdentifier()
{
var test = @"
namespace ConsoleApplication1
{
enum Stuff { One, Two, Three }
class TestClass
{
public Stuff Stuff
{
get
{
Stuff stuff = Stuff.One;
return stuff;
}
}
}
}
";
await VerifyCS.VerifyAnalyzerAsync(test);
}
//Diagnostic triggered in getter
[TestMethod]
public async Task SelfReferenceInGetter()
{
var test = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApplication1
{
class TestClass
{
public int {|#0:Number|}
{
get { return {|#1:Number|}; }
}
}
}";
var expected = VerifyCS.Diagnostic("FAL0001").WithLocation(0).WithLocation(1).WithArguments("Number", "get");
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}
//Diagnostic triggered in setter
[TestMethod]
public async Task SelfReferenceInSetter()
{
var test = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApplication1
{
class TestClass
{
public int {|#0:Number|}
{
set { {|#1:Number|} = value; }
}
}
}";
var expected = VerifyCS.Diagnostic("FAL0001").WithLocation(0).WithLocation(1).WithArguments("Number", "set");
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}
// Diagnostic triggered with lambda get syntax
[TestMethod]
public async Task SelfReferenceInLambdaProperty()
{
var test = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace ConsoleApplication1
{
class TestClass
{
public int {|#0:Number|}
{
get => {|#1:Number|};
}
}
}";
var expected = VerifyCS.Diagnostic("FAL0001").WithLocation(0).WithLocation(1).WithArguments("Number", "get");
await VerifyCS.VerifyAnalyzerAsync(test, expected);
}
// Diagnostic triggered with lambda property syntax
[TestMethod]
public async Task SelfReferenceInLambdaGetter()
{
var test = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;