9 changed files with 204 additions and 15 deletions
@ -0,0 +1,64 @@
@@ -0,0 +1,64 @@
|
||||
// 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 System.Collections.Generic; |
||||
using ICSharpCode.SharpDevelop.Dom; |
||||
using IronPython.Compiler.Ast; |
||||
|
||||
namespace ICSharpCode.PythonBinding |
||||
{ |
||||
public class PythonClassFields : PythonWalker |
||||
{ |
||||
FunctionDefinition functionDefinition; |
||||
IClass declaringType; |
||||
List<string> fieldNamesAdded; |
||||
|
||||
public PythonClassFields(FunctionDefinition functionDefinition) |
||||
{ |
||||
this.functionDefinition = functionDefinition; |
||||
} |
||||
|
||||
public void AddFields(IClass declaringType) |
||||
{ |
||||
this.declaringType = declaringType; |
||||
fieldNamesAdded = new List<string>(); |
||||
|
||||
functionDefinition.Body.Walk(this); |
||||
} |
||||
|
||||
public override bool Walk(AssignmentStatement node) |
||||
{ |
||||
string fieldName = GetFieldName(node); |
||||
AddFieldToDeclaringType(fieldName); |
||||
return false; |
||||
} |
||||
|
||||
string GetFieldName(AssignmentStatement node) |
||||
{ |
||||
string[] memberNames = PythonControlFieldExpression.GetMemberNames(node.Left[0] as MemberExpression); |
||||
return GetFieldName(memberNames); |
||||
} |
||||
|
||||
string GetFieldName(string[] memberNames) |
||||
{ |
||||
if (memberNames.Length > 1) { |
||||
if (PythonSelfResolver.IsSelfExpression(memberNames[0])) { |
||||
return memberNames[1]; |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
void AddFieldToDeclaringType(string fieldName) |
||||
{ |
||||
if (fieldName != null) { |
||||
if (!fieldNamesAdded.Contains(fieldName)) { |
||||
DefaultField field = new DefaultField(declaringType, fieldName); |
||||
declaringType.Fields.Add(field); |
||||
fieldNamesAdded.Add(fieldName); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,57 @@
@@ -0,0 +1,57 @@
|
||||
// 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.PythonBinding; |
||||
using ICSharpCode.SharpDevelop.Dom; |
||||
using NUnit.Framework; |
||||
using PythonBinding.Tests.Utils; |
||||
|
||||
namespace PythonBinding.Tests.Parsing |
||||
{ |
||||
[TestFixture] |
||||
public class PythonParserParseFieldTests |
||||
{ |
||||
IClass myClass; |
||||
|
||||
void ParseCode(string code) |
||||
{ |
||||
ParseInformation parseInfo = PythonParserHelper.CreateParseInfo(code); |
||||
myClass = parseInfo.CompilationUnit.Classes[0]; |
||||
} |
||||
|
||||
[Test] |
||||
public void Parse_ClassHasOneFieldCalledCount_ReturnsParseInfoWithClassWithFieldCalledCount() |
||||
{ |
||||
string code = |
||||
"class MyClass:\r\n" + |
||||
" def __init__(self):\r\n" + |
||||
" self._count = 0\r\n" + |
||||
"\r\n"; |
||||
|
||||
ParseCode(code); |
||||
IField field = myClass.Fields[0]; |
||||
string name = field.Name; |
||||
string expectedName = "_count"; |
||||
|
||||
Assert.AreEqual(expectedName, name); |
||||
} |
||||
|
||||
[Test] |
||||
public void Parse_ClassFieldInitialisedTwice_ReturnsParseInfoWithClassWithOnlyOneField() |
||||
{ |
||||
string code = |
||||
"class MyClass:\r\n" + |
||||
" def __init__(self):\r\n" + |
||||
" self._count = 0\r\n" + |
||||
" self._count = 3\r\n" + |
||||
"\r\n"; |
||||
|
||||
ParseCode(code); |
||||
int howManyFields = myClass.Fields.Count; |
||||
int expectedNumberOfFields = 1; |
||||
|
||||
Assert.AreEqual(expectedNumberOfFields, howManyFields); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue