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.
22
using NUnit.Framework;
23
using ICSharpCode.NRefactory.PatternMatching;
25
namespace ICSharpCode.NRefactory.CSharp.Parser.TypeMembers
28
public class PropertyDeclarationTests
31
public void SimpleGetSetPropertyDeclarationTest()
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);
40
public void GetSetPropertyDeclarationWithAccessorModifiers()
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);
49
public void SimpleGetPropertyDeclarationTest()
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);
58
public void SimpleSetPropertyDeclarationTest()
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);
67
public void PropertyRegionTest()
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");
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);
86
public void PropertyImplementingInterfaceTest()
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);
93
Assert.AreEqual("MyInterface", ((SimpleType)pd.PrivateImplementationType).Identifier);
97
public void PropertyImplementingGenericInterfaceTest()
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);
104
Assert.IsTrue(new SimpleType { Identifier = "MyInterface", TypeArguments = { new PrimitiveType("string") } }.IsMatch(pd.PrivateImplementationType));