public void DefineTypeFacet_NoParent_NoNestedTypes_NoTypeInitializer_NoInitializations() { var mutableType = MutableTypeObjectMother.CreateInterface(); Assert.That(mutableType.BaseType, Is.Null); Assert.That(mutableType.MutableTypeInitializer, Is.Null); Assert.That(mutableType.Initialization.Expressions, Is.Empty); var generator = new MutableTypeCodeGenerator( mutableType, _nestedTypeCodeGeneratorFactoryMock, _codeGeneratorMock, _emittableOperandProviderMock, _memberEmitterMock, _initializationBuilderMock, _proxySerializationEnablerMock); PopulateContext(generator, 2); // No call to SetParent because of null BaseType. // No call to AddConstructor because of null TypeInitializer. _initializationBuilderMock.Expect(mock => mock.CreateInitializationMembers(mutableType)).Return(null); _proxySerializationEnablerMock.Expect(mock => mock.MakeSerializable(mutableType, null)); _mockRepository.ReplayAll(); generator.DefineTypeFacets(); _mockRepository.VerifyAll(); }
public void Initialization_NullNamespace() { var mutableType = MutableTypeObjectMother.Create(name: "MyType", @namespace: null); Assert.That(mutableType.Namespace, Is.Null); Assert.That(mutableType.FullName, Is.EqualTo("MyType")); }
public void SetUp() { _builder = new InitializationBuilder(); _proxySerializationEnablerMock = new Mock <IProxySerializationEnabler> (MockBehavior.Strict); _mutableType = MutableTypeObjectMother.Create(); }
public void SetUp() { _mutableType = MutableTypeObjectMother.Create(baseType: typeof(DomainType)); _context = new TestableBodyContextBase(_mutableType, isStatic: false); _staticContext = new TestableBodyContextBase(_mutableType, isStatic: true); }
public void SetUp() { _proxy = MutableTypeObjectMother.Create(); _addedCtor = _proxy.AddConstructor(); _tracker = new ProxyTypeModificationTracker(_proxy, new[] { _addedCtor.Body }); }
public virtual void SetUp() { _mockRepository = new MockRepository(); _mutableType = MutableTypeObjectMother.Create(); _nestedTypeCodeGeneratorFactoryMock = _mockRepository.StrictMock <IMutableNestedTypeCodeGeneratorFactory>(); _codeGeneratorMock = _mockRepository.StrictMock <IReflectionEmitCodeGenerator>(); _emittableOperandProviderMock = _mockRepository.StrictMock <IEmittableOperandProvider> (); _memberEmitterMock = _mockRepository.StrictMock <IMemberEmitter>(); _initializationBuilderMock = _mockRepository.StrictMock <IInitializationBuilder>(); _proxySerializationEnablerMock = _mockRepository.StrictMock <IProxySerializationEnabler>(); _generator = new MutableTypeCodeGenerator( _mutableType, _nestedTypeCodeGeneratorFactoryMock, _codeGeneratorMock, _emittableOperandProviderMock, _memberEmitterMock, _initializationBuilderMock, _proxySerializationEnablerMock); _typeBuilderMock = _mockRepository.StrictMock <ITypeBuilder>(); _debugInfoGeneratorMock = _mockRepository.StrictMock <DebugInfoGenerator>(); _fakeInitializationField = ReflectionObjectMother.GetSomeField(); _fakeInitializationMethod = ReflectionObjectMother.GetSomeMethod(); _fakeInitializationMembers = Tuple.Create(_fakeInitializationField, _fakeInitializationMethod); }
public void Create() { var fakeEmittableOperandProvider = MockRepository.GenerateStrictMock <IEmittableOperandProvider>(); var fakeMemberEmitter = MockRepository.GenerateStrictMock <IMemberEmitter>(); _codeGeneratorMock.Expect(mock => mock.CreateEmittableOperandProvider()).Return(fakeEmittableOperandProvider); _memberEmitterFactoryMock.Expect(mock => mock.CreateMemberEmitter(fakeEmittableOperandProvider)).Return(fakeMemberEmitter); var mutableType1 = MutableTypeObjectMother.Create(); var mutableType2 = MutableTypeObjectMother.Create(); var result = _factory.CreateGenerators(new[] { mutableType1, mutableType2 }.AsOneTime()).ToList(); Assert.That(result, Has.Count.EqualTo(2)); Assert.That(result[0], Is.TypeOf <MutableTypeCodeGenerator>()); Assert.That(PrivateInvoke.GetNonPublicField(result[0], "_mutableType"), Is.SameAs(mutableType1)); Assert.That(PrivateInvoke.GetNonPublicField(result[0], "_codeGenerator"), Is.SameAs(_codeGeneratorMock)); Assert.That(PrivateInvoke.GetNonPublicField(result[0], "_memberEmitter"), Is.SameAs(fakeMemberEmitter)); Assert.That(PrivateInvoke.GetNonPublicField(result[0], "_initializationBuilder"), Is.SameAs(_initializationBuilderMock)); Assert.That(PrivateInvoke.GetNonPublicField(result[0], "_proxySerializationEnabler"), Is.SameAs(_proxySerializationEnablerMock)); Assert.That(PrivateInvoke.GetNonPublicField(result[1], "_mutableType"), Is.SameAs(mutableType2)); Assert.That( PrivateInvoke.GetNonPublicField(result[1], "_memberEmitter"), Is.SameAs(fakeMemberEmitter), "Generators share the MemberEmitter (and therefore also the EmittableOperandProvider)."); }
public void SetUp() { _declaringType = MutableTypeObjectMother.Create(); _isStatic = BooleanObjectMother.GetRandomBoolean(); _context = new TestableMethodBaseBodyContextBase(_declaringType, new ParameterExpression[0], _isStatic); }
public void MakeSerializable_SerializableInterfaceType() { var proxyType = MutableTypeObjectMother.Create(typeof(SerializableInterfaceType), attributes: TypeAttributes.Serializable); _enabler.MakeSerializable(proxyType, _participantConfigurationID, _assembledTypeIdentifierProviderStub.Object, _typeID); Assert.That(proxyType.AddedInterfaces, Is.Empty); Assert.That(proxyType.AddedMethods, Has.Count.EqualTo(1)); var baseMethod = NormalizingMemberInfoFromExpressionUtility.GetMethod((SerializableInterfaceType o) => o.GetObjectData(null, new StreamingContext())); var method = proxyType.AddedMethods.Single(); var serializationInfo = method.ParameterExpressions[0]; var expectedBody = Expression.Block( Expression.Call(new ThisExpression(proxyType), new NonVirtualCallMethodInfoAdapter(baseMethod), method.ParameterExpressions.Cast <Expression>()), Expression.Block( Expression.Call(serializationInfo, "SetType", Type.EmptyTypes, Expression.Constant(typeof(ObjectWithDeserializationConstructorProxy))), Expression.Call( serializationInfo, "AddValue", Type.EmptyTypes, Expression.Constant("<tp>participantConfigurationID"), Expression.Constant(_participantConfigurationID)), Expression.Call(serializationInfo, "AddValue", Type.EmptyTypes, Expression.Constant("<tp>assembledTypeIDData"), _assembledTypeIDData))); ExpressionTreeComparer.CheckAreEqualTrees(expectedBody, method.Body); }
public void GetAttributeFlagsImpl_Abstract() { var proxyType = MutableTypeObjectMother.Create(baseType: typeof(AbstractType)); Assert.That(proxyType.IsAbstract, Is.True); Assert.That(proxyType.Attributes, Is.EqualTo(TypeAttributes.Public | TypeAttributes.BeforeFieldInit | TypeAttributes.Abstract)); }
public void Initialization() { var declaringType = MutableTypeObjectMother.Create(); var attributes = (MethodAttributes)7 | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName; var parameters = ParameterDeclarationObjectMother.CreateMultiple(2); var body = ExpressionTreeObjectMother.GetSomeExpression(typeof(void)); var ctor = new MutableConstructorInfo(declaringType, attributes, parameters.AsOneTime(), body); Assert.That(ctor.DeclaringType, Is.SameAs(declaringType)); Assert.That(ctor.MutableDeclaringType, Is.SameAs(declaringType)); Assert.That(ctor.Name, Is.EqualTo(".ctor")); var actualParameters = ctor.GetParameters(); Assert.That(actualParameters, Has.Length.EqualTo(2)); CustomParameterInfoTest.CheckParameter(actualParameters[0], ctor, 0, parameters[0].Name, parameters[0].Type, parameters[0].Attributes); CustomParameterInfoTest.CheckParameter(actualParameters[1], ctor, 1, parameters[1].Name, parameters[1].Type, parameters[1].Attributes); Assert.That(ctor.MutableParameters, Is.EqualTo(actualParameters)); var paramExpressions = ctor.ParameterExpressions; Assert.That(paramExpressions, Has.Count.EqualTo(2)); Assert.That(paramExpressions[0], Has.Property("Name").EqualTo(parameters[0].Name).And.Property("Type").SameAs(parameters[0].Type)); Assert.That(paramExpressions[1], Has.Property("Name").EqualTo(parameters[1].Name).And.Property("Type").SameAs(parameters[1].Type)); Assert.That(ctor.Body, Is.SameAs(body)); }
public void AssembleType_ExceptionInCodeGeneraton() { var typeModificationContextStub = new Mock <ITypeModificationTracker>(); typeModificationContextStub.Setup(_ => _.Type).Returns(() => MutableTypeObjectMother.Create()); typeModificationContextStub.Setup(_ => _.IsModified()).Returns(true); _mutableTypeFactoryMock.Setup(_ => _.CreateProxy(_requestedType, ProxyKind.AssembledType)).Returns(typeModificationContextStub.Object); _complexSerializationEnablerMock.Setup(_ => _.MakeSerializable(It.IsAny <MutableType>(), It.IsAny <string>(), It.IsAny <IAssembledTypeIdentifierProvider>(), It.IsAny <AssembledTypeID>())); var typeAssemblyContextCodeGeneratorMock = new Mock <IMutableTypeBatchCodeGenerator> (MockBehavior.Strict); var exception1 = new InvalidOperationException("blub"); var exception2 = new NotSupportedException("blub"); var exception3 = new Exception(); var sequence = new MockSequence(); typeAssemblyContextCodeGeneratorMock.InSequence(sequence).Setup(mock => mock.GenerateTypes(It.IsAny <IEnumerable <MutableType> >())).Throws(exception1); typeAssemblyContextCodeGeneratorMock.InSequence(sequence).Setup(mock => mock.GenerateTypes(It.IsAny <IEnumerable <MutableType> >())).Throws(exception2); typeAssemblyContextCodeGeneratorMock.InSequence(sequence).Setup(mock => mock.GenerateTypes(It.IsAny <IEnumerable <MutableType> >())).Throws(exception3); var typeAssembler = CreateTypeAssembler(participants: new Mock <IParticipant>().Object); var expectedMessageRegex = "An error occurred during code generation for '" + _requestedType.Name + "':\r\nblub\r\n" + @"The following participants are currently configured and may have caused the error: 'IParticipantProxy.*'\."; Assert.That( () => typeAssembler.AssembleType(_typeID, _participantStateMock.Object, typeAssemblyContextCodeGeneratorMock.Object), Throws.InvalidOperationException.With.InnerException.SameAs(exception1).And.With.Message.Matches(expectedMessageRegex)); Assert.That( () => typeAssembler.AssembleType(_typeID, _participantStateMock.Object, typeAssemblyContextCodeGeneratorMock.Object), Throws.TypeOf <NotSupportedException>().With.InnerException.SameAs(exception2).And.With.Message.Matches(expectedMessageRegex)); Assert.That( () => typeAssembler.AssembleType(_typeID, _participantStateMock.Object, typeAssemblyContextCodeGeneratorMock.Object), Throws.Exception.SameAs(exception3)); }
public void SetUp() { _builder = new InitializationBuilder(); _proxySerializationEnablerMock = MockRepository.GenerateStrictMock <IProxySerializationEnabler>(); _mutableType = MutableTypeObjectMother.Create(); }
public void Initialization() { var declaringType = MutableTypeObjectMother.Create(name: "DeclaringType"); var baseType = ReflectionObjectMother.GetSomeSubclassableType(); var name = "MyType"; var @namespace = "MyNs"; var attributes = (TypeAttributes)7; var mutableType = new MutableType( declaringType, baseType, name, @namespace, attributes, _interfaceMappingComputerMock, _mutableMemberFactoryMock); Assert.That(mutableType.DeclaringType, Is.SameAs(declaringType)); Assert.That(mutableType.MutableDeclaringType, Is.SameAs(declaringType)); Assert.That(mutableType.BaseType, Is.SameAs(baseType)); Assert.That(mutableType.Name, Is.EqualTo(name)); Assert.That(mutableType.Namespace, Is.EqualTo(@namespace)); Assert.That(mutableType.FullName, Is.EqualTo("MyNs.DeclaringType+MyType")); Assert.That(mutableType.Attributes, Is.EqualTo(attributes)); Assert.That(mutableType.IsGenericType, Is.False); Assert.That(mutableType.IsGenericTypeDefinition, Is.False); Assert.That(mutableType.GetGenericArguments(), Is.Empty); Assert.That(mutableType.AddedNestedTypes, Is.Empty); Assert.That(mutableType.AddedCustomAttributes, Is.Empty); Assert.That(mutableType.Initialization, Is.Not.Null); Assert.That(mutableType.AddedInterfaces, Is.Empty); Assert.That(mutableType.AddedFields, Is.Empty); Assert.That(mutableType.AddedConstructors, Is.Empty); Assert.That(mutableType.AddedMethods, Is.Empty); Assert.That(mutableType.AddedProperties, Is.Empty); Assert.That(mutableType.AddedEvents, Is.Empty); }
public void name() { var type = MutableTypeObjectMother.CreateForExisting(typeof(MyClass)); var method = type.AllMutableMethods.Single(); method.SetBody(ctx => ctx.GetBaseCall(method, ctx.Parameters.Cast <Expression>())); }
public static Type GetSomeTypeBuilderInstantiation() { var type = typeof(UnspecifiedType <>).MakeGenericType(MutableTypeObjectMother.Create(baseType: null, memberSelector: null)); Assertion.IsTrue(type.GetType().FullName == "System.Reflection.Emit.TypeBuilderInstantiation"); return(type); }
public void SetUp() { _declaringType = MutableTypeObjectMother.Create(baseType: typeof(DomainType)); _method = MutableMethodInfoObjectMother.Create(_declaringType, "NonVirtualMethod"); _virtualMethod = MutableMethodInfoObjectMother.Create(_declaringType, attributes: MethodAttributes.Virtual); }
public void SetBody() { var declaringType = MutableTypeObjectMother.Create(); var attribtes = MethodAttributes.Virtual; // Methods which have a base method must be virtual. var returnType = typeof(object); var parameters = ParameterDeclarationObjectMother.CreateMultiple(2); var baseMethod = ReflectionObjectMother.GetSomeVirtualMethod(); // Base method must be virtual. var genericParameters = new[] { MutableGenericParameterObjectMother.Create() }; var method = MutableMethodInfoObjectMother.Create( declaringType, "Method", attribtes, returnType, parameters, baseMethod, genericParameters: genericParameters); var fakeBody = ExpressionTreeObjectMother.GetSomeExpression(typeof(int)); Func <MethodBodyModificationContext, Expression> bodyProvider = ctx => { Assert.That(ctx.DeclaringType, Is.SameAs(declaringType)); Assert.That(ctx.IsStatic, Is.False); Assert.That(ctx.Parameters, Is.EqualTo(method.ParameterExpressions).And.Not.Empty); Assert.That(ctx.GenericParameters, Is.EqualTo(genericParameters)); Assert.That(ctx.ReturnType, Is.SameAs(returnType)); Assert.That(ctx.BaseMethod, Is.SameAs(baseMethod)); Assert.That(ctx.PreviousBody, Is.SameAs(method.Body)); return(fakeBody); }; method.SetBody(bodyProvider); var expectedBody = Expression.Convert(fakeBody, returnType); ExpressionTreeComparer.CheckAreEqualTrees(expectedBody, method.Body); }
public void MakeSerializable_SerializableType() { var proxyType = MutableTypeObjectMother.Create(typeof(SomeType), attributes: TypeAttributes.Serializable); _enabler.MakeSerializable(proxyType, _participantConfigurationID, _assembledTypeIdentifierProviderStub.Object, _typeID); Assert.That(proxyType.AddedInterfaces, Is.EqualTo(new[] { typeof(ISerializable) })); Assert.That(proxyType.AddedConstructors, Is.Empty); Assert.That(proxyType.AddedMethods, Has.Count.EqualTo(1)); var method = proxyType.AddedMethods.Single(); var serializationInfo = method.ParameterExpressions[0]; var expectedMethodBody = Expression.Block( Expression.Block( Expression.Call( serializationInfo, "SetType", Type.EmptyTypes, Expression.Constant(typeof(ObjectWithoutDeserializationConstructorProxy))), Expression.Call( serializationInfo, "AddValue", Type.EmptyTypes, Expression.Constant("<tp>participantConfigurationID"), Expression.Constant(_participantConfigurationID)), Expression.Call(serializationInfo, "AddValue", Type.EmptyTypes, Expression.Constant("<tp>assembledTypeIDData"), _assembledTypeIDData)), Expression.Call( typeof(ReflectionSerializationHelper), "AddFieldValues", Type.EmptyTypes, serializationInfo, new ThisExpression(proxyType))); ExpressionTreeComparer.CheckAreEqualTrees(expectedMethodBody, method.Body); }
public void SetUp() { _mutableType = MutableTypeObjectMother.Create(); _generatedType = typeof(GeneratedType); _context = new GeneratedTypesContext(new[] { new KeyValuePair <MutableType, Type> (_mutableType, _generatedType) }); }
public void MakeSerializable_CannotOverrideOrReImplementGetObjectData() { var proxyType = MutableTypeObjectMother.Create(typeof(ExplicitSerializableInterfaceType), attributes: TypeAttributes.Serializable); _enabler.MakeSerializable(proxyType, _participantConfigurationID, _assembledTypeIdentifierProviderStub.Object, _typeID); Assert.That(proxyType.AddedMethods, Is.Empty); }
public void SetUp() { _methodFactoryMock = new Mock <IMethodFactory> (MockBehavior.Strict); _factory = new PropertyFactory(_methodFactoryMock.Object); _mutableType = MutableTypeObjectMother.Create(); }
public void SetUp() { _computer = new InterfaceMappingComputer(); _mutableType = MutableTypeObjectMother.Create(baseType: typeof(DomainType)); _interfaceMapProviderMock = new Mock <IInterfaceMappingProvider> (MockBehavior.Strict); }
public void SetUp() { _relatedMethodFinderMock = MockRepository.GenerateStrictMock <IRelatedMethodFinder>(); _factory = new MethodFactory(_relatedMethodFinderMock); _mutableType = MutableTypeObjectMother.Create(baseType: typeof(DomainType)); }
public void SetUp() { _declaringType = MutableTypeObjectMother.Create(baseType: typeof(DomainType), name: "Domain_Proxy", copyCtorsFromBase: true); _parameters = new[] { Expression.Parameter(typeof(string)) }; _context = new TestableConstructorBodyContextBase(_declaringType, false, _parameters.AsOneTime()); _staticContext = new TestableConstructorBodyContextBase(_declaringType, true, _parameters.AsOneTime()); }
public void SetUp() { _methodFactoryMock = MockRepository.GenerateStrictMock <IMethodFactory>(); _factory = new EventFactory(_methodFactoryMock); _mutableType = MutableTypeObjectMother.Create(); }
public void SetUp() { _computer = new InterfaceMappingComputer(); _mutableType = MutableTypeObjectMother.Create(baseType: typeof(DomainType)); _interfaceMapProviderMock = MockRepository.GenerateStrictMock <IInterfaceMappingProvider>(); }
public void SetUp() { _relatedMethodFinderMock = new Mock <IRelatedMethodFinder> (MockBehavior.Strict); _factory = new MethodFactory(_relatedMethodFinderMock.Object); _mutableType = MutableTypeObjectMother.Create(baseType: typeof(DomainType)); }
public void MakeSerializable_SomeType() { var proxyType = MutableTypeObjectMother.Create(typeof(SomeType)); _enabler.MakeSerializable(proxyType, _participantConfigurationID, _assembledTypeIdentifierProviderStub.Object, _typeID); Assert.That(proxyType.AddedInterfaces, Is.Empty); Assert.That(proxyType.AddedMethods, Is.Empty); }
public void Sort_BaseType() { var baseType = MutableTypeObjectMother.Create(); var derivedType = MutableTypeObjectMother.Create(baseType: baseType); var result = _sorter.Sort(new[] { derivedType, baseType }.AsOneTime()); Assert.That(result, Is.EqualTo(new[] { baseType, derivedType })); }