1
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
3
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4
// software and associated documentation files (the "Software"), to deal in the Software
5
// without restriction, including without limitation the rights to use, copy, modify, merge,
6
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7
// to whom the Software is furnished to do so, subject to the following conditions:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17
// DEALINGS IN THE SOFTWARE.
20
using NUnit.Framework;
22
namespace ICSharpCode.NRefactory.CSharp.Parser.Expression
25
public class LambdaExpressionTests
28
public void ImplicitlyTypedExpressionBody()
30
ParseUtilCSharp.AssertExpression(
32
new LambdaExpression {
33
Parameters = { new ParameterDeclaration { Name = "x" } },
34
Body = new BinaryOperatorExpression(new IdentifierExpression("x"), BinaryOperatorType.Add, new PrimitiveExpression(1))
39
public void ImplicitlyTypedExpressionBodyWithoutParenthesis()
41
ParseUtilCSharp.AssertExpression(
43
new LambdaExpression {
44
Parameters = { new ParameterDeclaration { Name = "x" } },
45
Body = new BinaryOperatorExpression(new IdentifierExpression("x"), BinaryOperatorType.Add, new PrimitiveExpression(1))
50
public void ImplicitlyTypedStatementBody()
52
ParseUtilCSharp.AssertExpression(
53
"(x) => { return x + 1; }",
54
new LambdaExpression {
55
Parameters = { new ParameterDeclaration { Name = "x" } },
56
Body = new BlockStatement {
58
Expression = new BinaryOperatorExpression(
59
new IdentifierExpression("x"), BinaryOperatorType.Add, new PrimitiveExpression(1))
64
public void ImplicitlyTypedStatementBodyWithoutParenthesis()
66
ParseUtilCSharp.AssertExpression(
67
"x => { return x + 1; }",
68
new LambdaExpression {
69
Parameters = { new ParameterDeclaration { Name = "x" } },
70
Body = new BlockStatement {
72
Expression = new BinaryOperatorExpression(
73
new IdentifierExpression("x"), BinaryOperatorType.Add, new PrimitiveExpression(1))
78
public void ExplicitlyTypedStatementBody()
80
ParseUtilCSharp.AssertExpression(
81
"(int x) => { return x + 1; }",
82
new LambdaExpression {
83
Parameters = { new ParameterDeclaration { Type = new PrimitiveType("int"), Name = "x" } },
84
Body = new BlockStatement {
86
Expression = new BinaryOperatorExpression(
87
new IdentifierExpression("x"), BinaryOperatorType.Add, new PrimitiveExpression(1))
92
public void ExplicitlyTypedWithRefParameter()
94
ParseUtilCSharp.AssertExpression(
95
"(ref int i) => i = 1",
96
new LambdaExpression {
98
new ParameterDeclaration {
99
ParameterModifier = ParameterModifier.Ref,
100
Type = new PrimitiveType("int"),
104
Body = new AssignmentExpression(new IdentifierExpression("i"), new PrimitiveExpression(1))
109
public void LambdaExpressionContainingConditionalExpression()
111
ParseUtilCSharp.AssertExpression(
112
"rr => rr != null ? rr.ResolvedType : null",
113
new LambdaExpression {
114
Parameters = { new ParameterDeclaration { Name = "rr" } },
115
Body = new ConditionalExpression {
116
Condition = new BinaryOperatorExpression(
117
new IdentifierExpression("rr"), BinaryOperatorType.InEquality, new NullReferenceExpression()),
118
TrueExpression = new IdentifierExpression("rr").Member("ResolvedType"),
119
FalseExpression = new NullReferenceExpression()
124
public void AsyncLambdaExpression()
126
ParseUtilCSharp.AssertExpression(
128
new LambdaExpression {
130
Parameters = { new ParameterDeclaration { Name = "x" } },
131
Body = new BinaryOperatorExpression(new IdentifierExpression("x"), BinaryOperatorType.Add, new PrimitiveExpression(1))
136
public void AsyncLambdaExpressionWithMultipleParameters()
138
ParseUtilCSharp.AssertExpression(
139
"async (x,y) => x + 1",
140
new LambdaExpression {
142
Parameters = { new ParameterDeclaration { Name = "x" }, new ParameterDeclaration { Name = "y" } },
143
Body = new BinaryOperatorExpression(new IdentifierExpression("x"), BinaryOperatorType.Add, new PrimitiveExpression(1))