Skip to content

Commit

Permalink
Cleanup ISymbolExtensionsTest TestInput
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-mikula-sonarsource committed Apr 30, 2024
1 parent 1b8159d commit 9fe2a0e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 69 deletions.
102 changes: 45 additions & 57 deletions analyzers/tests/SonarAnalyzer.Test/Extensions/ISymbolExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,63 +35,47 @@ namespace SonarAnalyzer.Test.Extensions;
[TestClass]
public class ISymbolExtensionsTest
{
internal const string TestInput = """
namespace NS
private const string TestInput = """
public interface IInterface
{
using System;
using System.Collections.Generic;
using System.Reflection;
using PropertyBag = System.Collections.Generic.Dictionary<string, object>;

public class Base
{
public class Nested
{
public class NestedMore
{}
}
int Property2 { get; set; }
void Method3();
}

public abstract class Base
{
public virtual void Method1() { }
protected virtual void Method2() { }
public abstract int Property { get; set; }

public void Method4(){}
}
private class Derived1 : Base
{
}

public class Derived1 : Base
{
public override int Property { get; set; }
}
public class Derived2 : Base, IInterface
{
private int PrivateProperty { get; set; }
private protected int PrivateProtectedProperty { get; set; }
protected int ProtectedProperty { get; set; }
protected internal int ProtectedInternalProperty { get; set; }
internal int InternalProperty { get; set; }
}

public abstract class Derived2 : Base, IInterface
{
public override int Property { get; set; }
public int Property2 { get; set; }
public void Method3(){}

public abstract void Method5();
public void EventHandler(object o, System.EventArgs args){}
}
public interface IInterface
{
int Property2 { get; set; }
void Method3();
void Method4<T, V>(List<T> param1, List<int> param2, List<V> param3, IList<int> param4);
}
public class AssemblyLoad
{
public AssemblyLoad()
{
AppDomain.CurrentDomain.AssemblyResolve += LoadAnyVersion;
}
Assembly LoadAnyVersion(object sender, ResolveEventArgs args) => null;
}
}
""";

private SnippetCompiler testCode;
private SnippetCompiler testSnippet;

[TestInitialize]
public void Compile() =>
testCode = new SnippetCompiler(TestInput, ignoreErrors: true, language: AnalyzerLanguage.CSharp); // FIXME: Why ignore errors?
testSnippet = new SnippetCompiler(TestInput);

[TestMethod]
public void GetDescendantNodes_ForNullSourceTree_ReturnsEmpty_VB() =>
Expand Down Expand Up @@ -284,49 +268,53 @@ public void IsPrimaryConstructor_CS(string code, bool hasPrimaryConstructor)
[TestMethod]
public void Symbol_IsPublicApi()
{
ISymbolExtensionsCommon.IsPubliclyAccessible(testCode.GetMethodSymbol("Base.Method1")).Should().BeTrue();
ISymbolExtensionsCommon.IsPubliclyAccessible(testCode.GetMethodSymbol("Base.Method2")).Should().BeTrue();
ISymbolExtensionsCommon.IsPubliclyAccessible(testCode.GetPropertySymbol("Base.Property")).Should().BeTrue();
ISymbolExtensionsCommon.IsPubliclyAccessible(testCode.GetPropertySymbol("IInterface.Property2")).Should().BeTrue();
ISymbolExtensionsCommon.IsPubliclyAccessible(testCode.GetPropertySymbol("Derived1.Property")).Should().BeFalse();
ISymbolExtensionsCommon.IsPubliclyAccessible(testSnippet.GetMethodSymbol("Base.Method1")).Should().BeTrue();
ISymbolExtensionsCommon.IsPubliclyAccessible(testSnippet.GetMethodSymbol("Base.Method2")).Should().BeTrue();
ISymbolExtensionsCommon.IsPubliclyAccessible(testSnippet.GetPropertySymbol("Base.Property")).Should().BeTrue();
ISymbolExtensionsCommon.IsPubliclyAccessible(testSnippet.GetPropertySymbol("IInterface.Property2")).Should().BeTrue();
ISymbolExtensionsCommon.IsPubliclyAccessible(testSnippet.GetPropertySymbol("Derived1.PrivateProperty")).Should().BeFalse();
ISymbolExtensionsCommon.IsPubliclyAccessible(testSnippet.GetPropertySymbol("Derived1.PrivateProtectedProperty")).Should().BeFalse();
ISymbolExtensionsCommon.IsPubliclyAccessible(testSnippet.GetPropertySymbol("Derived1.ProtectedProperty")).Should().BeTrue();
ISymbolExtensionsCommon.IsPubliclyAccessible(testSnippet.GetPropertySymbol("Derived1.ProtectedInternalProperty")).Should().BeTrue();
ISymbolExtensionsCommon.IsPubliclyAccessible(testSnippet.GetPropertySymbol("Derived1.InternalProperty")).Should().BeFalse();
}

[TestMethod]
public void Symbol_IsInterfaceImplementationOrMemberOverride()
{
ISymbolExtensionsCommon.GetInterfaceMember(testCode.GetMethodSymbol("Base.Method1")).Should().BeNull();
ISymbolExtensionsCommon.GetOverriddenMember(testCode.GetMethodSymbol("Base.Method1")).Should().BeNull();
ISymbolExtensionsCommon.GetOverriddenMember(testCode.GetPropertySymbol("Derived2.Property")).Should().NotBeNull();
ISymbolExtensionsCommon.GetInterfaceMember(testCode.GetPropertySymbol("Derived2.Property2")).Should().NotBeNull();
ISymbolExtensionsCommon.GetInterfaceMember(testCode.GetMethodSymbol("Derived2.Method3")).Should().NotBeNull();
ISymbolExtensionsCommon.GetInterfaceMember(testSnippet.GetMethodSymbol("Base.Method1")).Should().BeNull();
ISymbolExtensionsCommon.GetOverriddenMember(testSnippet.GetMethodSymbol("Base.Method1")).Should().BeNull();
ISymbolExtensionsCommon.GetOverriddenMember(testSnippet.GetPropertySymbol("Derived2.Property")).Should().NotBeNull();
ISymbolExtensionsCommon.GetInterfaceMember(testSnippet.GetPropertySymbol("Derived2.Property2")).Should().NotBeNull();
ISymbolExtensionsCommon.GetInterfaceMember(testSnippet.GetMethodSymbol("Derived2.Method3")).Should().NotBeNull();
}

[TestMethod]
public void Symbol_TryGetOverriddenOrInterfaceMember()
{
var actualOverriddenMethod = ISymbolExtensionsCommon.GetOverriddenMember(testCode.GetMethodSymbol("Base.Method1"));
var actualOverriddenMethod = ISymbolExtensionsCommon.GetOverriddenMember(testSnippet.GetMethodSymbol("Base.Method1"));
actualOverriddenMethod.Should().BeNull();

var expectedOverriddenProperty = testCode.GetPropertySymbol("Base.Property");
var propertySymbol = testCode.GetPropertySymbol("Derived2.Property");
var expectedOverriddenProperty = testSnippet.GetPropertySymbol("Base.Property");
var propertySymbol = testSnippet.GetPropertySymbol("Derived2.Property");

var actualOverriddenProperty = ISymbolExtensionsCommon.GetOverriddenMember(propertySymbol);
actualOverriddenProperty.Should().NotBeNull();
actualOverriddenProperty.Should().Be(expectedOverriddenProperty);

var expectedOverriddenMethod = testCode.GetMethodSymbol("IInterface.Method3");
actualOverriddenMethod = ISymbolExtensionsCommon.GetInterfaceMember(testCode.GetMethodSymbol("Derived2.Method3"));
var expectedOverriddenMethod = testSnippet.GetMethodSymbol("IInterface.Method3");
actualOverriddenMethod = ISymbolExtensionsCommon.GetInterfaceMember(testSnippet.GetMethodSymbol("Derived2.Method3"));
actualOverriddenMethod.Should().NotBeNull();
actualOverriddenMethod.Should().Be(expectedOverriddenMethod);
}

[TestMethod]
public void Symbol_IsChangeable()
{
ISymbolExtensionsCommon.IsChangeable(testCode.GetMethodSymbol("Base.Method1")).Should().BeFalse();
ISymbolExtensionsCommon.IsChangeable(testCode.GetMethodSymbol("Base.Method4")).Should().BeTrue();
ISymbolExtensionsCommon.IsChangeable(testCode.GetMethodSymbol("Derived2.Method5")).Should().BeFalse();
ISymbolExtensionsCommon.IsChangeable(testCode.GetMethodSymbol("Derived2.Method3")).Should().BeFalse();
ISymbolExtensionsCommon.IsChangeable(testSnippet.GetMethodSymbol("Base.Method1")).Should().BeFalse();
ISymbolExtensionsCommon.IsChangeable(testSnippet.GetMethodSymbol("Base.Method4")).Should().BeTrue();
ISymbolExtensionsCommon.IsChangeable(testSnippet.GetMethodSymbol("Derived2.Method5")).Should().BeFalse();
ISymbolExtensionsCommon.IsChangeable(testSnippet.GetMethodSymbol("Derived2.Method3")).Should().BeFalse();
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,37 @@ namespace SonarAnalyzer.Test.Extensions;
[TestClass]
public class ITypeSymbolExtensionsTest
{
private const string TestInput = """
namespace NS
{
using System;
using PropertyBag = System.Collections.Generic.Dictionary<string, object>;

public abstract class Base
{
public class Nested
{
public class NestedMore { }
}
}
public class Derived1 : Base { }
public class Derived2 : Base, IInterface { }
public interface IInterface { }
}
""";

private ClassDeclarationSyntax baseClassDeclaration;
private ClassDeclarationSyntax derivedClassDeclaration1;
private ClassDeclarationSyntax derivedClassDeclaration2;
private SyntaxNode root;
private SemanticModel semanticModel;
private SemanticModel model;

[TestInitialize]
public void Compile()
{
using var workspace = new AdhocWorkspace();
var document = workspace.CurrentSolution.AddProject("foo", "foo.dll", LanguageNames.CSharp).AddDocument("test", ISymbolExtensionsTest.TestInput);
var compilation = document.Project.GetCompilationAsync().Result;
var tree = compilation.SyntaxTrees.First();
semanticModel = compilation.GetSemanticModel(tree);

root = tree.GetRoot();
var snippet = new SnippetCompiler(TestInput);
root = snippet.SyntaxTree.GetRoot();
model = snippet.SemanticModel;
baseClassDeclaration = root.DescendantNodes().OfType<ClassDeclarationSyntax>().First(x => x.Identifier.ValueText == "Base");
derivedClassDeclaration1 = root.DescendantNodes().OfType<ClassDeclarationSyntax>().First(x => x.Identifier.ValueText == "Derived1");
derivedClassDeclaration2 = root.DescendantNodes().OfType<ClassDeclarationSyntax>().First(x => x.Identifier.ValueText == "Derived2");
Expand All @@ -62,8 +77,8 @@ public void Type_DerivesOrImplementsAny()
var baseType = new KnownType("NS.Base");
var interfaceType = new KnownType("NS.IInterface");

var derived1Type = semanticModel.GetDeclaredSymbol(derivedClassDeclaration1) as INamedTypeSymbol;
var derived2Type = semanticModel.GetDeclaredSymbol(derivedClassDeclaration2) as INamedTypeSymbol;
var derived1Type = model.GetDeclaredSymbol(derivedClassDeclaration1) as INamedTypeSymbol;
var derived2Type = model.GetDeclaredSymbol(derivedClassDeclaration2) as INamedTypeSymbol;

derived2Type.DerivesOrImplements(interfaceType).Should().BeTrue();
derived1Type.DerivesOrImplements(interfaceType).Should().BeFalse();
Expand All @@ -78,7 +93,7 @@ public void Type_Is()
var baseKnownType = new KnownType("NS.Base");
var baseKnownTypes = ImmutableArray.Create(new[] { baseKnownType });

var baseType = semanticModel.GetDeclaredSymbol(baseClassDeclaration) as INamedTypeSymbol;
var baseType = model.GetDeclaredSymbol(baseClassDeclaration) as INamedTypeSymbol;

baseType.Is(baseKnownType).Should().BeTrue();
baseType.IsAny(baseKnownTypes).Should().BeTrue();
Expand All @@ -88,7 +103,7 @@ public void Type_Is()
public void Type_GetSymbolType_Alias()
{
var aliasUsing = root.DescendantNodesAndSelf().OfType<UsingDirectiveSyntax>().FirstOrDefault(x => x.Alias is not null);
var symbol = semanticModel.GetDeclaredSymbol(aliasUsing);
var symbol = model.GetDeclaredSymbol(aliasUsing);
var type = symbol.GetSymbolType();
symbol.ToString().Should().Be("PropertyBag");
type.ToString().Should().Be("System.Collections.Generic.Dictionary<string, object>");
Expand Down

0 comments on commit 9fe2a0e

Please sign in to comment.