public void MethodExecuteTest() { string sourceCode = @"class Bar { public void Foo() {} }"; var semanticModel = CompileAndGetSymanticModel(sourceCode); var syntaxTree = semanticModel.SyntaxTree; var fooMethodSyntax = syntaxTree.GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>().ToList()[0]; var fooMethodSymbol = semanticModel.GetDeclaredSymbol(fooMethodSyntax); var methodSymbolInfo = new MethodQueryData(fooMethodSymbol); Assert.AreEqual("Foo", methodSymbolInfo.MethodName); }
public void HasAttributeAppliedTest() { string sourceCode = @"using System; class Bar { [Obsolete] public void Foo() { } }"; var semanticModel = CompileAndGetSymanticModel(sourceCode); var syntaxTree = semanticModel.SyntaxTree; var fooMethodSyntax = syntaxTree.GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>().ToList()[0]; var fooMethodSymbol = semanticModel.GetDeclaredSymbol(fooMethodSyntax); var methodSymbolInfo = new MethodQueryData(fooMethodSymbol); Assert.True(methodSymbolInfo.HasAttributeApplied(typeof(ObsoleteAttribute))); }
public void HasParameterTypeTest() { string sourceCode = @"using System; class Bar { public int Foo(int a) { return a+1; } }"; var semanticModel = CompileAndGetSymanticModel(sourceCode); var syntaxTree = semanticModel.SyntaxTree; var fooMethodSyntax = syntaxTree.GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>().ToList()[0]; var fooMethodSymbol = semanticModel.GetDeclaredSymbol(fooMethodSyntax); var methodSymbolInfo = new MethodQueryData(fooMethodSymbol); Assert.True(methodSymbolInfo.HasParameterType(typeof(int))); Assert.False(methodSymbolInfo.HasParameterType(typeof(string))); }
public void HasModifierTest() { string sourceCode = @"using System; class Bar { public virtual int Foo(params int[] a) { return 0; } }"; var semanticModel = CompileAndGetSymanticModel(sourceCode); var syntaxTree = semanticModel.SyntaxTree; var fooMethodSyntax = syntaxTree.GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>().ToList()[0]; var fooMethodSymbol = semanticModel.GetDeclaredSymbol(fooMethodSyntax); var methodSymbolInfo = new MethodQueryData(fooMethodSymbol); Assert.IsTrue(methodSymbolInfo.HasModifier(MethodModifier.Virtual)); Assert.IsFalse(methodSymbolInfo.HasModifier(MethodModifier.Protected)); }
public void MethodReturningVoidTest() { string sourceCode = @"class Bar { public void Foo() { } }"; var semanticModel = CompileAndGetSymanticModel(sourceCode); var syntaxTree = semanticModel.SyntaxTree; var fooMethodSyntax = syntaxTree.GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>().ToList()[0]; var fooMethodSymbol = semanticModel.GetDeclaredSymbol(fooMethodSyntax); var methodSymbolInfo = new MethodQueryData(fooMethodSymbol); Assert.True(methodSymbolInfo.ReturnsType(typeof(void))); }