5 changed files with 136 additions and 1 deletions
@ -0,0 +1,54 @@ |
|||||||
|
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||||
|
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
||||||
|
|
||||||
|
using System; |
||||||
|
using ICSharpCode.SharpDevelop.Dom; |
||||||
|
using IronPython.Compiler.Ast; |
||||||
|
|
||||||
|
namespace ICSharpCode.PythonBinding |
||||||
|
{ |
||||||
|
public class PythonPropertyAssignment |
||||||
|
{ |
||||||
|
AssignmentStatement assignmentStatement; |
||||||
|
|
||||||
|
public PythonPropertyAssignment(AssignmentStatement assignmentStatement) |
||||||
|
{ |
||||||
|
this.assignmentStatement = assignmentStatement; |
||||||
|
} |
||||||
|
|
||||||
|
public bool IsProperty() |
||||||
|
{ |
||||||
|
CallExpression rhs = assignmentStatement.Right as CallExpression; |
||||||
|
if (rhs != null) { |
||||||
|
NameExpression nameExpression = rhs.Target as NameExpression; |
||||||
|
if (nameExpression != null) { |
||||||
|
return nameExpression.Name == "property"; |
||||||
|
} |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
public void AddPropertyToClass(IClass c) |
||||||
|
{ |
||||||
|
NameExpression nameExpression = assignmentStatement.Left[0] as NameExpression; |
||||||
|
if (nameExpression != null) { |
||||||
|
string propertyName = nameExpression.Name; |
||||||
|
AddPropertyToClass(c, propertyName); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void AddPropertyToClass(IClass c, string propertyName) |
||||||
|
{ |
||||||
|
DefaultProperty property = new DefaultProperty(c, propertyName); |
||||||
|
property.Region = GetPropertyRegion(); |
||||||
|
c.Properties.Add(property); |
||||||
|
} |
||||||
|
|
||||||
|
DomRegion GetPropertyRegion() |
||||||
|
{ |
||||||
|
int line = assignmentStatement.Start.Line; |
||||||
|
int column = assignmentStatement.Start.Column; |
||||||
|
return new DomRegion(line, column, line, column); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,63 @@ |
|||||||
|
using System; |
||||||
|
using ICSharpCode.PythonBinding; |
||||||
|
using ICSharpCode.SharpDevelop.Dom; |
||||||
|
using NUnit.Framework; |
||||||
|
using PythonBinding.Tests.Utils; |
||||||
|
|
||||||
|
namespace PythonBinding.Tests.Parsing |
||||||
|
{ |
||||||
|
[TestFixture] |
||||||
|
public class PythonParserParsePropertyTests |
||||||
|
{ |
||||||
|
IProperty property; |
||||||
|
|
||||||
|
void ParseClassWithProperty() |
||||||
|
{ |
||||||
|
string code = |
||||||
|
"class MyClass:\r\n" + |
||||||
|
" def __init__(self):\r\n" + |
||||||
|
" self._count = 0\r\n" + |
||||||
|
"\r\n" + |
||||||
|
" def get_Count(self):\r\n" + |
||||||
|
" return self._count\r\n" + |
||||||
|
"\r\n" + |
||||||
|
" def _set_Count(self, value):\r\n" + |
||||||
|
" self._count = value\r\n" + |
||||||
|
"\r\n" + |
||||||
|
" Count = property(fget=get_Count, fset=set_Count)\r\n"; |
||||||
|
|
||||||
|
ParseInformation parseInfo = PythonParserHelper.CreateParseInfo(code); |
||||||
|
property = parseInfo.CompilationUnit.Classes[0].Properties[0]; |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void Parse_ClassHasPropertyCalledCount_ReturnParseInfoWithClassWithPropertyCalledCount() |
||||||
|
{ |
||||||
|
ParseClassWithProperty(); |
||||||
|
string name = property.Name; |
||||||
|
|
||||||
|
string expectedName = "Count"; |
||||||
|
|
||||||
|
Assert.AreEqual(expectedName, name); |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Dom regions are one based.
|
||||||
|
/// </summary>
|
||||||
|
[Test] |
||||||
|
public void Parse_ClassHasPropertyCalledCount_PropertyRegion() |
||||||
|
{ |
||||||
|
ParseClassWithProperty(); |
||||||
|
DomRegion region = property.Region; |
||||||
|
|
||||||
|
DomRegion expectedRegion = new DomRegion( |
||||||
|
beginLine: 11, |
||||||
|
beginColumn: 5, |
||||||
|
endLine: 11, |
||||||
|
endColumn: 5 |
||||||
|
); |
||||||
|
|
||||||
|
Assert.AreEqual(expectedRegion, region); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue