Пример #1
0
        public async Task when_processing_document_then_generates_exports()
        {
            var workspace = MSBuildWorkspace.Create();
            var project   = workspace.CurrentSolution.AddProject("Clide", "Clide", "C#")
                            .WithCompilationOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
                            .AddMetadataReferences(Assembly.GetExecutingAssembly().GetReferencedAssemblies()
                                                   .Select(name => MetadataReference.CreateFromFile(Assembly.Load(name).ManifestModule.FullyQualifiedName)))
                            .AddMetadataReferences(Directory.EnumerateFiles(ModuleInitializer.BaseDirectory, "*.dll")
                                                   .Select(file => MetadataReference.CreateFromFile(Assembly.LoadFrom(file).ManifestModule.FullyQualifiedName)));

            Debug.WriteLine(typeof(CreationPolicy));

            project = project
                      .AddDocument("ComponentAttibute.cs", SourceText.From(
                                       File.ReadAllText(Path.Combine(ModuleInitializer.BaseDirectory, "Clide\\ComponentAttribute.cs"))))
                      .Project;

            var document = project.AddDocument("Producer", CSharpSyntaxTree.ParseText(@"
using System;
using System.Reactive.Disposables;
using System.ComponentModel.Composition;

namespace Clide 
{
	[Component(CreationPolicy.Shared)]
	public partial class Producer : IObservable<DerivedEvent>, IDisposable, Microsoft.VisualStudio.Shell.Interop.IVsNonSolutionProjectFactory
	{
		public IDisposable Subscribe(IObserver<DerivedEvent> observer)
		{
			observer.OnNext(new DerivedEvent());
			return Disposable.Empty;
		}

		public void Dispose() { }
	}

	public class BaseEvent { }
	public class DerivedEvent : BaseEvent { }
}
").GetRoot());

            // The project is immutable, need to get the one from the document.
            project = document.Project;

            var compilation = await project.GetCompilationAsync();

            // There should be no errors/warnings at all.
            if (compilation.GetDiagnostics().Length > 0)
            {
                Assert.True(false, string.Join(Environment.NewLine, compilation.GetDiagnostics().Select(d => d.ToString())));
            }

            var generator = new ExportsGenerator(compilation);
            var exports   = generator.GenerateExports(new[] { document }, new HashSet <string>(new[] { "Microsoft.VisualStudio" })).ToArray();

            Assert.Equal(1, exports.Length);

            compilation = await exports[0].Project.GetCompilationAsync();
            // There should be no errors/warnings at all.
            if (compilation.GetDiagnostics().Length > 0)
            {
                Assert.True(false, string.Join(Environment.NewLine, compilation.GetDiagnostics().Select(d => d.ToString())));
            }

            var text = await exports[0].GetTextAsync();

            output.WriteLine(text.ToString());
        }
Пример #2
0
		public async Task when_processing_document_then_generates_exports()
		{
			var workspace = MSBuildWorkspace.Create();
			var project = workspace.CurrentSolution.AddProject("Clide", "Clide", "C#")
				.WithCompilationOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
				.AddMetadataReferences(Assembly.GetExecutingAssembly().GetReferencedAssemblies()
					.Select(name => MetadataReference.CreateFromFile(Assembly.Load(name).ManifestModule.FullyQualifiedName)))
				.AddMetadataReferences(Directory.EnumerateFiles(ModuleInitializer.BaseDirectory, "*.dll")
					.Select(file => MetadataReference.CreateFromFile(Assembly.LoadFrom(file).ManifestModule.FullyQualifiedName)));

			Debug.WriteLine(typeof(CreationPolicy));

			project = project
				.AddDocument("ComponentAttibute.cs", SourceText.From(
					File.ReadAllText(Path.Combine(ModuleInitializer.BaseDirectory, "Clide\\ComponentAttribute.cs"))))
				.Project;

			var document = project.AddDocument("Producer", CSharpSyntaxTree.ParseText(@"
using System;
using System.Reactive.Disposables;
using System.ComponentModel.Composition;

namespace Clide 
{
	[Component(CreationPolicy.Shared)]
	public partial class Producer : IObservable<DerivedEvent>, IDisposable, Microsoft.VisualStudio.Shell.Interop.IVsNonSolutionProjectFactory
	{
		public IDisposable Subscribe(IObserver<DerivedEvent> observer)
		{
			observer.OnNext(new DerivedEvent());
			return Disposable.Empty;
		}

		public void Dispose() { }
	}

	public class BaseEvent { }
	public class DerivedEvent : BaseEvent { }
}
").GetRoot());

			// The project is immutable, need to get the one from the document.
			project = document.Project;

			var compilation = await project.GetCompilationAsync();
			// There should be no errors/warnings at all.
			if (compilation.GetDiagnostics().Length > 0)
			{
				Assert.True(false, string.Join(Environment.NewLine, compilation.GetDiagnostics().Select(d => d.ToString())));
			}

			var generator = new ExportsGenerator(compilation);
			var exports = generator.GenerateExports(new[] { document }, new HashSet<string>(new[] { "Microsoft.VisualStudio" })).ToArray();

			Assert.Equal(1, exports.Length);

			compilation = await exports[0].Project.GetCompilationAsync();
			// There should be no errors/warnings at all.
			if (compilation.GetDiagnostics().Length > 0)
			{
				Assert.True(false, string.Join(Environment.NewLine, compilation.GetDiagnostics().Select(d => d.ToString())));
			}

			var text = await exports[0].GetTextAsync();

			output.WriteLine(text.ToString());
		}