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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/CSharp/Parser/TypeMembers/PropertyDeclarationTests.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 System.IO;
 
21
using System.Linq;
 
22
using NUnit.Framework;
 
23
using ICSharpCode.NRefactory.PatternMatching;
 
24
 
 
25
namespace ICSharpCode.NRefactory.CSharp.Parser.TypeMembers
 
26
{
 
27
        [TestFixture]
 
28
        public class PropertyDeclarationTests
 
29
        {
 
30
                [Test]
 
31
                public void SimpleGetSetPropertyDeclarationTest()
 
32
                {
 
33
                        PropertyDeclaration pd = ParseUtilCSharp.ParseTypeMember<PropertyDeclaration>("int MyProperty { get {} set {} } ");
 
34
                        Assert.AreEqual("MyProperty", pd.Name);
 
35
                        Assert.IsFalse(pd.Getter.IsNull);
 
36
                        Assert.IsFalse(pd.Setter.IsNull);
 
37
                }
 
38
                
 
39
                [Test]
 
40
                public void GetSetPropertyDeclarationWithAccessorModifiers()
 
41
                {
 
42
                        PropertyDeclaration pd = ParseUtilCSharp.ParseTypeMember<PropertyDeclaration>("int MyProperty { private get {} protected internal set {} } ");
 
43
                        Assert.AreEqual("MyProperty", pd.Name);
 
44
                        Assert.IsFalse(pd.Getter.IsNull);
 
45
                        Assert.IsFalse(pd.Setter.IsNull);
 
46
                }
 
47
                
 
48
                [Test]
 
49
                public void SimpleGetPropertyDeclarationTest()
 
50
                {
 
51
                        PropertyDeclaration pd = ParseUtilCSharp.ParseTypeMember<PropertyDeclaration>("int MyProperty { get {} } ");
 
52
                        Assert.AreEqual("MyProperty", pd.Name);
 
53
                        Assert.IsFalse(pd.Getter.IsNull);
 
54
                        Assert.IsTrue(pd.Setter.IsNull);
 
55
                }
 
56
                
 
57
                [Test]
 
58
                public void SimpleSetPropertyDeclarationTest()
 
59
                {
 
60
                        PropertyDeclaration pd = ParseUtilCSharp.ParseTypeMember<PropertyDeclaration>("int MyProperty { set {} } ");
 
61
                        Assert.AreEqual("MyProperty", pd.Name);
 
62
                        Assert.IsTrue(pd.Getter.IsNull);
 
63
                        Assert.IsFalse(pd.Setter.IsNull);
 
64
                }
 
65
                
 
66
                [Test]
 
67
                public void PropertyRegionTest()
 
68
                {
 
69
                        const string code = "class T {\n\tint Prop {\n\t\tget { return f; }\n\t\tset { f = value; }\n\t}\n}\n";
 
70
                        int line2Pos = code.IndexOf("\tint Prop");
 
71
                        int line3Pos = code.IndexOf("\t\tget");
 
72
                        int line4Pos = code.IndexOf("\t\tset");
 
73
                        
 
74
                        CSharpParser parser = new CSharpParser();
 
75
                        SyntaxTree syntaxTree = parser.Parse(code);
 
76
                        PropertyDeclaration pd = (PropertyDeclaration)syntaxTree.Children.Where (c => c.Role != Roles.NewLine).Single().GetChildByRole(Roles.TypeMemberRole);
 
77
                        Assert.AreEqual(new TextLocation(2, code.IndexOf("{\n\t\tget") - line2Pos + 1), pd.GetChildByRole(Roles.LBrace).StartLocation);
 
78
                        Assert.AreEqual(new TextLocation(5, 3), pd.EndLocation);
 
79
                        Assert.AreEqual(new TextLocation(3, code.IndexOf("{ return") - line3Pos + 1), pd.Getter.Body.StartLocation);
 
80
                        Assert.AreEqual(new TextLocation(3, code.IndexOf("}\n\t\tset") + 1 - line3Pos + 1), pd.Getter.Body.EndLocation);
 
81
                        Assert.AreEqual(new TextLocation(4, code.IndexOf("{ f =") - line4Pos + 1), pd.Setter.Body.StartLocation);
 
82
                        Assert.AreEqual(new TextLocation(4, code.IndexOf("}\n\t}") + 1 - line4Pos + 1), pd.Setter.Body.EndLocation);
 
83
                }
 
84
                
 
85
                [Test]
 
86
                public void PropertyImplementingInterfaceTest()
 
87
                {
 
88
                        PropertyDeclaration pd = ParseUtilCSharp.ParseTypeMember<PropertyDeclaration>("int MyInterface.MyProperty { get {} } ");
 
89
                        Assert.AreEqual("MyProperty", pd.Name);
 
90
                        Assert.IsFalse(pd.Getter.IsNull);
 
91
                        Assert.IsTrue(pd.Setter.IsNull);
 
92
                        
 
93
                        Assert.AreEqual("MyInterface", ((SimpleType)pd.PrivateImplementationType).Identifier);
 
94
                }
 
95
                
 
96
                [Test]
 
97
                public void PropertyImplementingGenericInterfaceTest()
 
98
                {
 
99
                        PropertyDeclaration pd = ParseUtilCSharp.ParseTypeMember<PropertyDeclaration>("int MyInterface<string>.MyProperty { get {} } ");
 
100
                        Assert.AreEqual("MyProperty", pd.Name);
 
101
                        Assert.IsFalse(pd.Getter.IsNull);
 
102
                        Assert.IsTrue(pd.Setter.IsNull);
 
103
                        
 
104
                        Assert.IsTrue(new SimpleType { Identifier = "MyInterface", TypeArguments = { new PrimitiveType("string") } }.IsMatch(pd.PrivateImplementationType));
 
105
                }
 
106
        }
 
107
}