Run JustMockLite automation tests on LambdaTest cloud grid
Perform automation testing on 3000+ real desktop and mobile devices online.
// Copyright 2004-2011 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
namespace Telerik.JustMock.Core.Castle.DynamicProxy.Contributors
{
using System.Reflection;
using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;
using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters;
using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters.SimpleAST;
internal class MinimialisticMethodGenerator : MethodGenerator
{
public MinimialisticMethodGenerator(MetaMethod method, OverrideMethodDelegate overrideMethod)
: base(method, overrideMethod)
{
}
protected override MethodEmitter BuildProxiedMethodBody(MethodEmitter emitter, ClassEmitter @class,
ProxyGenerationOptions options, INamingScope namingScope)
{
InitOutParameters(emitter, MethodToOverride.GetParameters());
if (emitter.ReturnType == typeof(void))
{
emitter.CodeBuilder.AddStatement(new ReturnStatement());
}
else
{
emitter.CodeBuilder.AddStatement(new ReturnStatement(new DefaultValueExpression(emitter.ReturnType)));
}
return emitter;
}
private void InitOutParameters(MethodEmitter emitter, ParameterInfo[] parameters)
{
for (var index = 0; index < parameters.Length; index++)
{
var parameter = parameters[index];
if (parameter.IsOut)
{
emitter.CodeBuilder.AddStatement(
new AssignArgumentStatement(new ArgumentReference(parameter.ParameterType, index + 1, parameter.Attributes),
new DefaultValueExpression(parameter.ParameterType)));
}
}
}
}
}
// Copyright 2004-2011 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
namespace Telerik.JustMock.Core.Castle.DynamicProxy.Contributors
{
using System;
using System.Reflection;
using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;
using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters;
using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters.SimpleAST;
internal class OptionallyForwardingMethodGenerator : MethodGenerator
{
// TODO: This class largely duplicates code from Forwarding and Minimalistic generators. Should be refactored to change that
private readonly GetTargetReferenceDelegate getTargetReference;
public OptionallyForwardingMethodGenerator(MetaMethod method, OverrideMethodDelegate overrideMethod,
GetTargetReferenceDelegate getTargetReference)
: base(method, overrideMethod)
{
this.getTargetReference = getTargetReference;
}
protected override MethodEmitter BuildProxiedMethodBody(MethodEmitter emitter, ClassEmitter @class,
ProxyGenerationOptions options, INamingScope namingScope)
{
var targetReference = getTargetReference(@class, MethodToOverride);
emitter.CodeBuilder.AddStatement(
new ExpressionStatement(
new IfNullExpression(targetReference, IfNull(emitter.ReturnType), IfNotNull(targetReference))));
return emitter;
}
private Expression IfNotNull(Reference targetReference)
{
var expression = new MultiStatementExpression();
var arguments = ArgumentsUtil.ConvertToArgumentReferenceExpression(MethodToOverride.GetParameters());
expression.AddStatement(new ReturnStatement(
new MethodInvocationExpression(
targetReference,
MethodToOverride,
arguments) { VirtualCall = true }));
return expression;
}
private Expression IfNull(Type returnType)
{
var expression = new MultiStatementExpression();
InitOutParameters(expression, MethodToOverride.GetParameters());
if (returnType == typeof(void))
{
expression.AddStatement(new ReturnStatement());
}
else
{
expression.AddStatement(new ReturnStatement(new DefaultValueExpression(returnType)));
}
return expression;
}
private void InitOutParameters(MultiStatementExpression expression, ParameterInfo[] parameters)
{
for (var index = 0; index < parameters.Length; index++)
{
var parameter = parameters[index];
if (parameter.IsOut)
{
expression.AddStatement(
new AssignArgumentStatement(new ArgumentReference(parameter.ParameterType, index + 1, parameter.Attributes),
new DefaultValueExpression(parameter.ParameterType)));
}
}
}
}
}
// Copyright 2004-2011 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
namespace Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters.SimpleAST
{
using System;
using System.Reflection;
using System.Reflection.Emit;
internal class DefaultValueExpression : Expression
{
private readonly Type type;
public DefaultValueExpression(Type type)
{
this.type = type;
}
public override void Emit(IMemberEmitter member, ILGenerator gen)
{
// TODO: check if this can be simplified by using more of OpCodeUtil and other existing types
if (IsPrimitiveOrClass(type))
{
OpCodeUtil.EmitLoadOpCodeForDefaultValueOfType(gen, type);
}
else if (type.GetTypeInfo().IsValueType || type.GetTypeInfo().IsGenericParameter)
{
// TODO: handle decimal explicitly
var local = gen.DeclareLocal(type);
gen.Emit(OpCodes.Ldloca_S, local);
gen.Emit(OpCodes.Initobj, type);
gen.Emit(OpCodes.Ldloc, local);
}
else if (type.GetTypeInfo().IsByRef)
{
EmitByRef(gen);
}
else
{
throw new ProxyGenerationException("Can't emit default value for type " + type);
}
}
private void EmitByRef(ILGenerator gen)
{
var elementType = type.GetElementType();
if (IsPrimitiveOrClass(elementType))
{
OpCodeUtil.EmitLoadOpCodeForDefaultValueOfType(gen, elementType);
OpCodeUtil.EmitStoreIndirectOpCodeForType(gen, elementType);
}
else if (elementType.GetTypeInfo().IsGenericParameter || elementType.GetTypeInfo().IsValueType)
{
gen.Emit(OpCodes.Initobj, elementType);
}
else
{
throw new ProxyGenerationException("Can't emit default value for reference of type " + elementType);
}
}
private bool IsPrimitiveOrClass(Type type)
{
if ((type.GetTypeInfo().IsPrimitive && type != typeof(IntPtr)))
{
return true;
}
return ((type.GetTypeInfo().IsClass || type.GetTypeInfo().IsInterface) &&
type.GetTypeInfo().IsGenericParameter == false &&
type.GetTypeInfo().IsByRef == false);
}
}
}
Accelerate Your Automation Test Cycles With LambdaTest
Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.