public void ThrowsForInvalidOperation()
        {
            var codeGeneratorStub             = new Mock <IReflectionEmitCodeGenerator>();
            var emittableOperandProviderStub  = new Mock <IEmittableOperandProvider>();
            var memberEmitterStub             = new Mock <IMemberEmitter>();
            var initializationBuilderStub     = new Mock <IInitializationBuilder>();
            var proxySerializationEnablerStub = new Mock <IProxySerializationEnabler>();

            _typeBuilderMock.Setup(stub => stub.RegisterWith(emittableOperandProviderStub.Object, _mutableType));
            _typeBuilderMock.Setup(stub => stub.SetParent(_mutableType.BaseType));
            _typeBuilderMock.Setup(stub => stub.CreateType()).Returns((Type)null);
            codeGeneratorStub
            .Setup(stub => stub.DefineType(It.IsAny <string>(), It.IsAny <TypeAttributes>(), It.IsAny <IEmittableOperandProvider>()))
            .Returns(_typeBuilderMock.Object);
            codeGeneratorStub.SetupGet(stub => stub.DebugInfoGenerator).Returns(_debugInfoGeneratorMock.Object);

            var generator = new MutableTypeCodeGenerator(
                _mutableType,
                _nestedTypeCodeGeneratorFactoryMock.Object,
                codeGeneratorStub.Object,
                emittableOperandProviderStub.Object,
                memberEmitterStub.Object,
                initializationBuilderStub.Object,
                proxySerializationEnablerStub.Object);

            CheckThrowsForInvalidOperation(generator.DefineTypeFacets);
            CheckThrowsForInvalidOperation(() => generator.CreateNestedTypeGenerators().ForceEnumeration());
            CheckThrowsForInvalidOperation(() => generator.CreateType());
            Assert.That(() => generator.DeclareType(), Throws.Nothing);

            CheckThrowsForInvalidOperation(generator.DeclareType);
            CheckThrowsForInvalidOperation(generator.DefineTypeFacets);
            CheckThrowsForInvalidOperation(() => generator.CreateType());
            Assert.That(() => generator.CreateNestedTypeGenerators().ForceEnumeration(), Throws.Nothing);

            CheckThrowsForInvalidOperation(generator.DeclareType);
            CheckThrowsForInvalidOperation(() => generator.CreateNestedTypeGenerators().ForceEnumeration());
            CheckThrowsForInvalidOperation(() => generator.CreateType());
            Assert.That(() => generator.DefineTypeFacets(), Throws.Nothing);

            CheckThrowsForInvalidOperation(generator.DeclareType);
            CheckThrowsForInvalidOperation(() => generator.CreateNestedTypeGenerators().ForceEnumeration());
            CheckThrowsForInvalidOperation(generator.DefineTypeFacets);
            Assert.That(() => generator.CreateType(), Throws.Nothing);
        }
Example #2
0
        public void ThrowsForInvalidOperation()
        {
            var codeGeneratorStub             = MockRepository.GenerateStub <IReflectionEmitCodeGenerator>();
            var emittableOperandProviderStub  = MockRepository.GenerateStub <IEmittableOperandProvider>();
            var memberEmitterStub             = MockRepository.GenerateStub <IMemberEmitter>();
            var initializationBuilderStub     = MockRepository.GenerateStub <IInitializationBuilder>();
            var proxySerializationEnablerStub = MockRepository.GenerateStub <IProxySerializationEnabler>();

            codeGeneratorStub.Stub(stub => stub.DefineType(null, 0, null)).IgnoreArguments().Return(_typeBuilderMock);
            codeGeneratorStub.Stub(stub => stub.DebugInfoGenerator).Return(_debugInfoGeneratorMock);

            var generator = new MutableTypeCodeGenerator(
                _mutableType,
                _nestedTypeCodeGeneratorFactoryMock,
                codeGeneratorStub,
                emittableOperandProviderStub,
                memberEmitterStub,
                initializationBuilderStub,
                proxySerializationEnablerStub);

            CheckThrowsForInvalidOperation(generator.DefineTypeFacets);
            CheckThrowsForInvalidOperation(() => generator.CreateNestedTypeGenerators().ForceEnumeration());
            CheckThrowsForInvalidOperation(() => generator.CreateType());
            Assert.That(() => generator.DeclareType(), Throws.Nothing);

            CheckThrowsForInvalidOperation(generator.DeclareType);
            CheckThrowsForInvalidOperation(generator.DefineTypeFacets);
            CheckThrowsForInvalidOperation(() => generator.CreateType());
            Assert.That(() => generator.CreateNestedTypeGenerators().ForceEnumeration(), Throws.Nothing);

            CheckThrowsForInvalidOperation(generator.DeclareType);
            CheckThrowsForInvalidOperation(() => generator.CreateNestedTypeGenerators().ForceEnumeration());
            CheckThrowsForInvalidOperation(() => generator.CreateType());
            Assert.That(() => generator.DefineTypeFacets(), Throws.Nothing);

            CheckThrowsForInvalidOperation(generator.DeclareType);
            CheckThrowsForInvalidOperation(() => generator.CreateNestedTypeGenerators().ForceEnumeration());
            CheckThrowsForInvalidOperation(generator.DefineTypeFacets);
            Assert.That(() => generator.CreateType(), Throws.Nothing);
        }
        public void CreateType()
        {
            var context   = PopulateContext(_generator, 3);
            var wasCalled = false;

            context.PostDeclarationsActionManager.AddAction(() => wasCalled = true);
            var fakeType = ReflectionObjectMother.GetSomeType();

            _typeBuilderMock.Setup(mock => mock.CreateType()).Returns(fakeType).Verifiable();

            var result = _generator.CreateType();

            _typeBuilderMock.Verify();
            Assert.That(wasCalled, Is.True);
            Assert.That(result, Is.SameAs(fakeType));
        }