Ejemplo n.º 1
0
        public void ImportsForUsingsConsumingExternAlias()
        {
            var source =
                @"
extern alias X;
using SXL = X::System.Xml.Linq;
using LO = X::System.Xml.Linq.LoadOptions;
using X::System.Xml;

class C
{
    int M()
    {
        X::System.Xml.Linq.LoadOptions.None.ToString();
        return 1;
    }
}
";
            var comp = CreateCompilation(
                source,
                new[] { SystemXmlLinqRef.WithAliases(ImmutableArray.Create("X")) }
                );

            comp.GetDiagnostics().Where(d => d.Severity > DiagnosticSeverity.Info).Verify();

            WithRuntimeInstance(
                comp,
                runtime =>
            {
                var importsList = GetImports(runtime, "C.M");

                var imports = importsList.Single();

                Assert.Equal(1, imports.ExternAliases.Length);

                var @using            = imports.Usings.Single();
                var importedNamespace = @using.NamespaceOrType;
                Assert.Equal(SymbolKind.Namespace, importedNamespace.Kind);
                Assert.Equal("System.Xml", importedNamespace.ToTestDisplayString());

                var usingAliases = imports.UsingAliases;
                Assert.Equal(2, usingAliases.Count);
                AssertEx.SetEqual(usingAliases.Keys, "SXL", "LO");

                var typeAlias = usingAliases["SXL"].Alias;
                Assert.Equal("SXL", typeAlias.Name);
                Assert.Equal("System.Xml.Linq", typeAlias.Target.ToTestDisplayString());

                var namespaceAlias = usingAliases["LO"].Alias;
                Assert.Equal("LO", namespaceAlias.Name);
                Assert.Equal(
                    "System.Xml.Linq.LoadOptions",
                    namespaceAlias.Target.ToTestDisplayString()
                    );
            }
                );
        }
Ejemplo n.º 2
0
        public void ImportsForUsingsConsumingExternAliasAndGlobal()
        {
            var source =
                @"
extern alias X;
using A = X::System.Xml.Linq;
using B = global::System.Xml.Linq;

class C
{
    int M()
    {
        A.LoadOptions.None.ToString();
        B.LoadOptions.None.ToString();
        return 1;
    }
}
";
            var comp = CreateCompilation(
                source,
                new[] { SystemXmlLinqRef.WithAliases(ImmutableArray.Create("global", "X")) }
                );

            comp.GetDiagnostics().Where(d => d.Severity > DiagnosticSeverity.Info).Verify();

            WithRuntimeInstance(
                comp,
                runtime =>
            {
                var importsList = GetImports(runtime, "C.M");

                var imports = importsList.Single();

                Assert.Equal(0, imports.Usings.Length);
                Assert.Equal(1, imports.ExternAliases.Length);

                var usingAliases = imports.UsingAliases;
                Assert.Equal(2, usingAliases.Count);
                AssertEx.SetEqual(usingAliases.Keys, "A", "B");

                var aliasA = usingAliases["A"].Alias;
                Assert.Equal("A", aliasA.Name);
                Assert.Equal("System.Xml.Linq", aliasA.Target.ToTestDisplayString());

                var aliasB = usingAliases["B"].Alias;
                Assert.Equal("B", aliasB.Name);
                Assert.Equal(aliasA.Target, aliasB.Target);
            }
                );
        }
Ejemplo n.º 3
0
        public void ImportsForExternAlias()
        {
            var source =
                @"
extern alias X;

class C
{
    int M()
    {
        X::System.Xml.Linq.LoadOptions.None.ToString();
        return 1;
    }
}
";
            var comp = CreateCompilation(
                source,
                new[] { SystemXmlLinqRef.WithAliases(ImmutableArray.Create("X")) }
                );

            comp.VerifyDiagnostics();

            WithRuntimeInstance(
                comp,
                runtime =>
            {
                var importsList = GetImports(runtime, "C.M");

                var imports = importsList.Single();

                Assert.Equal(0, imports.Usings.Length);
                Assert.Equal(0, imports.UsingAliases.Count);

                var externAliases = imports.ExternAliases;

                Assert.Equal(1, externAliases.Length);

                var aliasSymbol = externAliases.Single().Alias;
                Assert.Equal("X", aliasSymbol.Name);

                var targetSymbol = aliasSymbol.Target;
                Assert.Equal(SymbolKind.Namespace, targetSymbol.Kind);
                Assert.True(((NamespaceSymbol)targetSymbol).IsGlobalNamespace);
                Assert.Equal("System.Xml.Linq", targetSymbol.ContainingAssembly.Name);
            }
                );
        }
Ejemplo n.º 4
0
        public void ImportsForExternAlias()
        {
            var source = @"
extern alias X;

class C
{
    int M()
    {
        X::System.Xml.Linq.LoadOptions.None.ToString();
        return 1;
    }
}
";
            var comp   = CreateCompilationWithMscorlib(source, new[] { SystemXmlLinqRef.WithAliases(ImmutableArray.Create("X")) });

            comp.VerifyDiagnostics();

            var runtime     = CreateRuntimeInstance(comp, includeSymbols: true);
            var importsList = GetImports(runtime, "C.M", comp.SyntaxTrees.Single().GetRoot().DescendantNodes().OfType <Syntax.LiteralExpressionSyntax>().Single());

            var imports = importsList.Single();

            Assert.Equal(0, imports.Usings.Length);
            Assert.Equal(0, imports.UsingAliases.Count);

            var externAliases = imports.ExternAliases;

            Assert.Equal(1, externAliases.Length);

            var aliasSymbol = externAliases.Single().Alias;

            Assert.Equal("X", aliasSymbol.Name);

            var targetSymbol = aliasSymbol.Target;

            Assert.Equal(SymbolKind.Namespace, targetSymbol.Kind);
            Assert.True(((NamespaceSymbol)targetSymbol).IsGlobalNamespace);
            Assert.Equal("System.Xml.Linq", targetSymbol.ContainingAssembly.Name);

            var moduleInstance = runtime.Modules.Single(m => m.ModuleMetadata.Name.StartsWith("System.Xml.Linq", StringComparison.OrdinalIgnoreCase));

            AssertEx.SetEqual(moduleInstance.MetadataReference.Properties.Aliases, "X");
        }