How to use ReturnStatement class of Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters.SimpleAST package

Best JustMockLite code snippet using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters.SimpleAST.ReturnStatement

Run JustMockLite automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

MinimialisticMethodGenerator.cs

Source: MinimialisticMethodGenerator.cs Github

copy
1// Copyright 2004-2011 Castle Project - http://www.castleproject.org/
2// 
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6// 
7//   http://www.apache.org/licenses/LICENSE-2.0
8// 
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15namespace Telerik.JustMock.Core.Castle.DynamicProxy.Contributors
16{
17	using System.Reflection;
18
19	using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;
20	using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters;
21	using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters.SimpleAST;
22
23	internal class MinimialisticMethodGenerator : MethodGenerator
24	{
25		public MinimialisticMethodGenerator(MetaMethod method, OverrideMethodDelegate overrideMethod)
26			: base(method, overrideMethod)
27		{
28		}
29
30		protected override MethodEmitter BuildProxiedMethodBody(MethodEmitter emitter, ClassEmitter @class,
31		                                                        ProxyGenerationOptions options, INamingScope namingScope)
32		{
33			InitOutParameters(emitter, MethodToOverride.GetParameters());
34
35			if (emitter.ReturnType == typeof(void))
36			{
37				emitter.CodeBuilder.AddStatement(new ReturnStatement());
38			}
39			else
40			{
41				emitter.CodeBuilder.AddStatement(new ReturnStatement(new DefaultValueExpression(emitter.ReturnType)));
42			}
43
44			return emitter;
45		}
46
47		private void InitOutParameters(MethodEmitter emitter, ParameterInfo[] parameters)
48		{
49			for (var index = 0; index < parameters.Length; index++)
50			{
51				var parameter = parameters[index];
52				if (parameter.IsOut)
53				{
54					emitter.CodeBuilder.AddStatement(
55						new AssignArgumentStatement(new ArgumentReference(parameter.ParameterType, index + 1, parameter.Attributes),
56						                            new DefaultValueExpression(parameter.ParameterType)));
57				}
58			}
59		}
60	}
61}
Full Screen

OptionallyForwardingMethodGenerator.cs

Source: OptionallyForwardingMethodGenerator.cs Github

copy
1// Copyright 2004-2011 Castle Project - http://www.castleproject.org/
2// 
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6// 
7//   http://www.apache.org/licenses/LICENSE-2.0
8// 
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15namespace Telerik.JustMock.Core.Castle.DynamicProxy.Contributors
16{
17	using System;
18	using System.Reflection;
19
20	using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;
21	using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters;
22	using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters.SimpleAST;
23
24	internal class OptionallyForwardingMethodGenerator : MethodGenerator
25	{
26		// TODO: This class largely duplicates code from Forwarding and Minimalistic generators. Should be refactored to change that
27		private readonly GetTargetReferenceDelegate getTargetReference;
28
29		public OptionallyForwardingMethodGenerator(MetaMethod method, OverrideMethodDelegate overrideMethod,
30		                                           GetTargetReferenceDelegate getTargetReference)
31			: base(method, overrideMethod)
32		{
33			this.getTargetReference = getTargetReference;
34		}
35
36		protected override MethodEmitter BuildProxiedMethodBody(MethodEmitter emitter, ClassEmitter @class,
37		                                                        ProxyGenerationOptions options, INamingScope namingScope)
38		{
39			var targetReference = getTargetReference(@class, MethodToOverride);
40
41			emitter.CodeBuilder.AddStatement(
42				new ExpressionStatement(
43					new IfNullExpression(targetReference, IfNull(emitter.ReturnType), IfNotNull(targetReference))));
44			return emitter;
45		}
46
47		private Expression IfNotNull(Reference targetReference)
48		{
49			var expression = new MultiStatementExpression();
50			var arguments = ArgumentsUtil.ConvertToArgumentReferenceExpression(MethodToOverride.GetParameters());
51
52			expression.AddStatement(new ReturnStatement(
53			                        	new MethodInvocationExpression(
54			                        		targetReference,
55			                        		MethodToOverride,
56			                        		arguments) { VirtualCall = true }));
57			return expression;
58		}
59
60		private Expression IfNull(Type returnType)
61		{
62			var expression = new MultiStatementExpression();
63			InitOutParameters(expression, MethodToOverride.GetParameters());
64
65			if (returnType == typeof(void))
66			{
67				expression.AddStatement(new ReturnStatement());
68			}
69			else
70			{
71				expression.AddStatement(new ReturnStatement(new DefaultValueExpression(returnType)));
72			}
73			return expression;
74		}
75
76		private void InitOutParameters(MultiStatementExpression expression, ParameterInfo[] parameters)
77		{
78			for (var index = 0; index < parameters.Length; index++)
79			{
80				var parameter = parameters[index];
81				if (parameter.IsOut)
82				{
83					expression.AddStatement(
84						new AssignArgumentStatement(new ArgumentReference(parameter.ParameterType, index + 1, parameter.Attributes),
85						                            new DefaultValueExpression(parameter.ParameterType)));
86				}
87			}
88		}
89	}
90}
Full Screen

ForwardingMethodGenerator.cs

Source: ForwardingMethodGenerator.cs Github

copy
1// Copyright 2004-2011 Castle Project - http://www.castleproject.org/
2// 
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6// 
7//   http://www.apache.org/licenses/LICENSE-2.0
8// 
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15namespace Telerik.JustMock.Core.Castle.DynamicProxy.Contributors
16{
17	using Telerik.JustMock.Core.Castle.DynamicProxy.Generators;
18	using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters;
19	using Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters.SimpleAST;
20
21	internal class ForwardingMethodGenerator : MethodGenerator
22	{
23		private readonly GetTargetReferenceDelegate getTargetReference;
24
25		public ForwardingMethodGenerator(MetaMethod method, OverrideMethodDelegate overrideMethod,
26		                                 GetTargetReferenceDelegate getTargetReference)
27			: base(method, overrideMethod)
28		{
29			this.getTargetReference = getTargetReference;
30		}
31
32		protected override MethodEmitter BuildProxiedMethodBody(MethodEmitter emitter, ClassEmitter @class,
33		                                                        ProxyGenerationOptions options, INamingScope namingScope)
34		{
35			var targetReference = getTargetReference(@class, MethodToOverride);
36			var arguments = ArgumentsUtil.ConvertToArgumentReferenceExpression(MethodToOverride.GetParameters());
37
38			emitter.CodeBuilder.AddStatement(new ReturnStatement(
39			                                 	new MethodInvocationExpression(
40			                                 		targetReference,
41			                                 		MethodToOverride,
42			                                 		arguments) { VirtualCall = true }));
43			return emitter;
44		}
45	}
46}
Full Screen

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.

Try LambdaTest

Most used methods in ReturnStatement

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)