Example #1
0
        public void ScriptNamespaceRetrievedOnClassMethodArgumentType()
        {
            var tree = CSharpSyntaxTree.ParseText(@"
            namespace Namespace1 {
                [ScriptNamespace(""OverridenNamespace"")]
                public class Class1 { }
                public class Class2 {
                    public void MyMethod(Class1 parameter) { }
                }
            }
            ");

            // Loading MSCoreLib
            var semanticModel = (CSharpCompilation.Create("TestAssembly")
                                 .AddReferences(
                                     MetadataReference.CreateFromFile(
                                         typeof(object).Assembly.Location))
                                 .AddSyntaxTrees(tree).AddScriptNamespaceReference().GetSemanticModel(tree));

            var node = new NodeLocator(tree).LocateFirst(typeof(MethodDeclarationSyntax));

            Assert.IsNotNull(node);

            var methodDeclarationNode = node as MethodDeclarationSyntax;

            Assert.IsNotNull(methodDeclarationNode);

            var parameters = new MethodDeclaration(methodDeclarationNode, semanticModel).Parameters;

            Assert.AreEqual(1, parameters.Count(), "1 argument expected");
            var typeReference = parameters.ElementAt(0).Type;

            Assert.AreEqual("OverridenNamespace.Class1", typeReference.FullName, "ScriptNamespace overriden namespace not detected!");
        }