Exemplo n.º 1
0
        public void GetOrCreate_ValidActivatorAfterInitialization_Success()
        {
            using (CompositionHost host = CompositionHost.CreateCompositionHost(new ExportDescriptorProvider[0]))
            {
                Assert.True(host.TryGetExport(new CompositionContract(typeof(CompositionContext)), out object export));
                LifetimeContext context = Assert.IsType<LifetimeContext>(export);

                CompositionOperation operation = null;
                var value = new object();
                object GetOrCreateActivate(LifetimeContext getOrCreateContext, CompositionOperation getOrCreateOperator)
                {
                    Assert.Same(context, getOrCreateContext);
                    Assert.Same(operation, getOrCreateOperator);

                    return value;
                }
                object Activator(LifetimeContext activatorContext, CompositionOperation activatorOperation)
                {
                    operation = activatorOperation;
                    Assert.Same(value, context.GetOrCreate(1, operation, GetOrCreateActivate));
                    return "Hi";
                }

                Assert.Equal("Hi", CompositionOperation.Run(context, Activator));
                Assert.Same(value, context.GetOrCreate(1, operation, GetOrCreateActivate));
            }
        }
Exemplo n.º 2
0
        public void GetOrCreate_NullOperation_ThrowsNullReferenceException()
        {
            using (CompositionHost host = CompositionHost.CreateCompositionHost(new ExportDescriptorProvider[0]))
            {
                Assert.True(host.TryGetExport(new CompositionContract(typeof(CompositionContext)), out object export));
                LifetimeContext context = Assert.IsType<LifetimeContext>(export);

                Assert.Throws<NullReferenceException>(() => context.GetOrCreate(1, null, Activator));
            }
        }