~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/CSharp/Parser/Expression/LambdaExpressionTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
 
2
// 
 
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:
 
8
// 
 
9
// The above copyright notice and this permission notice shall be included in all copies or
 
10
// substantial portions of the Software.
 
11
// 
 
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.
 
18
 
 
19
using System;
 
20
using NUnit.Framework;
 
21
 
 
22
namespace ICSharpCode.NRefactory.CSharp.Parser.Expression
 
23
{
 
24
        [TestFixture]
 
25
        public class LambdaExpressionTests
 
26
        {
 
27
                [Test]
 
28
                public void ImplicitlyTypedExpressionBody()
 
29
                {
 
30
                        ParseUtilCSharp.AssertExpression(
 
31
                                "(x) => x + 1",
 
32
                                new LambdaExpression {
 
33
                                        Parameters = { new ParameterDeclaration { Name = "x" } },
 
34
                                        Body = new BinaryOperatorExpression(new IdentifierExpression("x"), BinaryOperatorType.Add, new PrimitiveExpression(1))
 
35
                                });
 
36
                }
 
37
                
 
38
                [Test]
 
39
                public void ImplicitlyTypedExpressionBodyWithoutParenthesis()
 
40
                {
 
41
                        ParseUtilCSharp.AssertExpression(
 
42
                                "x => x + 1",
 
43
                                new LambdaExpression {
 
44
                                        Parameters = { new ParameterDeclaration { Name = "x" } },
 
45
                                        Body = new BinaryOperatorExpression(new IdentifierExpression("x"), BinaryOperatorType.Add, new PrimitiveExpression(1))
 
46
                                });
 
47
                }
 
48
                
 
49
                [Test]
 
50
                public void ImplicitlyTypedStatementBody()
 
51
                {
 
52
                        ParseUtilCSharp.AssertExpression(
 
53
                                "(x) => { return x + 1; }",
 
54
                                new LambdaExpression {
 
55
                                        Parameters = { new ParameterDeclaration { Name = "x" } },
 
56
                                        Body = new BlockStatement {
 
57
                                                new ReturnStatement {
 
58
                                                        Expression = new BinaryOperatorExpression(
 
59
                                                                new IdentifierExpression("x"), BinaryOperatorType.Add, new PrimitiveExpression(1))
 
60
                                                }}});
 
61
                }
 
62
                
 
63
                [Test]
 
64
                public void ImplicitlyTypedStatementBodyWithoutParenthesis()
 
65
                {
 
66
                        ParseUtilCSharp.AssertExpression(
 
67
                                "x => { return x + 1; }",
 
68
                                new LambdaExpression {
 
69
                                        Parameters = { new ParameterDeclaration { Name = "x" } },
 
70
                                        Body = new BlockStatement {
 
71
                                                new ReturnStatement {
 
72
                                                        Expression = new BinaryOperatorExpression(
 
73
                                                                new IdentifierExpression("x"), BinaryOperatorType.Add, new PrimitiveExpression(1))
 
74
                                                }}});
 
75
                }
 
76
                
 
77
                [Test]
 
78
                public void ExplicitlyTypedStatementBody()
 
79
                {
 
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 {
 
85
                                                new ReturnStatement {
 
86
                                                        Expression = new BinaryOperatorExpression(
 
87
                                                                new IdentifierExpression("x"), BinaryOperatorType.Add, new PrimitiveExpression(1))
 
88
                                                }}});
 
89
                }
 
90
                
 
91
                [Test]
 
92
                public void ExplicitlyTypedWithRefParameter()
 
93
                {
 
94
                        ParseUtilCSharp.AssertExpression(
 
95
                                "(ref int i) => i = 1",
 
96
                                new LambdaExpression {
 
97
                                        Parameters = {
 
98
                                                new ParameterDeclaration {
 
99
                                                        ParameterModifier = ParameterModifier.Ref,
 
100
                                                        Type = new PrimitiveType("int"),
 
101
                                                        Name = "i"
 
102
                                                }
 
103
                                        },
 
104
                                        Body = new AssignmentExpression(new IdentifierExpression("i"), new PrimitiveExpression(1))
 
105
                                });
 
106
                }
 
107
                
 
108
                [Test]
 
109
                public void LambdaExpressionContainingConditionalExpression()
 
110
                {
 
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()
 
120
                                        }});
 
121
                }
 
122
                
 
123
                [Test]
 
124
                public void AsyncLambdaExpression()
 
125
                {
 
126
                        ParseUtilCSharp.AssertExpression(
 
127
                                "async x => x + 1",
 
128
                                new LambdaExpression {
 
129
                                        IsAsync = true,
 
130
                                        Parameters = { new ParameterDeclaration { Name = "x" } },
 
131
                                        Body = new BinaryOperatorExpression(new IdentifierExpression("x"), BinaryOperatorType.Add, new PrimitiveExpression(1))
 
132
                                });
 
133
                }
 
134
                
 
135
                [Test]
 
136
                public void AsyncLambdaExpressionWithMultipleParameters()
 
137
                {
 
138
                        ParseUtilCSharp.AssertExpression(
 
139
                                "async (x,y) => x + 1",
 
140
                                new LambdaExpression {
 
141
                                        IsAsync = true,
 
142
                                        Parameters = { new ParameterDeclaration { Name = "x" }, new ParameterDeclaration { Name = "y" } },
 
143
                                        Body = new BinaryOperatorExpression(new IdentifierExpression("x"), BinaryOperatorType.Add, new PrimitiveExpression(1))
 
144
                                });
 
145
                }
 
146
        }
 
147
}