public static ParseInformation CreateParseInfo(string code)
		{
			DefaultProjectContent projectContent = new DefaultProjectContent();
			PythonParser parser = new PythonParser();
			ICompilationUnit compilationUnit = parser.Parse(projectContent, @"C:\test.py", code);
			return new ParseInformation(compilationUnit);
		}
		public void SetUpFixture()
		{
			string python = "class Test:\r\n" +
							"\tdef foo(self):\r\n" +
							"\t\tpass";
			
			DefaultProjectContent projectContent = new DefaultProjectContent();
			PythonParser parser = new PythonParser();
			compilationUnit = parser.Parse(projectContent, @"C:\test.py", python);			
			if (compilationUnit.Classes.Count > 0) {
				c = compilationUnit.Classes[0];
				if (c.Methods.Count > 0) {
					method = c.Methods[0];
				}
				
				TextArea textArea = new TextArea();
				document = new TextDocument();
				textArea.Document = document;
				textArea.Document.Text = python;
				
				ParserFoldingStrategy foldingStrategy = new ParserFoldingStrategy(textArea);
				
				ParseInformation parseInfo = new ParseInformation(compilationUnit);
				foldingStrategy.UpdateFoldings(parseInfo);
				List<FoldingSection> folds = new List<FoldingSection>(foldingStrategy.FoldingManager.AllFoldings);
				
				if (folds.Count > 0) {
					classFold = folds[0];
				}
				if (folds.Count > 1) {
					methodFold = folds[1];
				}
			}
		}
		public void SetUpFixture()
		{
			resourceWriter = new MockResourceWriter();
			resourceService = new MockResourceService();
			resourceService.SetResourceWriter(resourceWriter);
			
			AvalonEdit.TextEditor textEditor = new AvalonEdit.TextEditor();
			document = textEditor.Document;
			textEditor.Text = GetTextEditorCode();
			
			PythonParser parser = new PythonParser();
			ICompilationUnit compilationUnit = parser.Parse(new DefaultProjectContent(), @"test.py", document.Text);

			using (DesignSurface designSurface = new DesignSurface(typeof(Form))) {
				IDesignerHost host = (IDesignerHost)designSurface.GetService(typeof(IDesignerHost));
				Form form = (Form)host.RootComponent;
				form.ClientSize = new Size(499, 309);

				PropertyDescriptorCollection descriptors = TypeDescriptor.GetProperties(form);
				PropertyDescriptor namePropertyDescriptor = descriptors.Find("Name", false);
				namePropertyDescriptor.SetValue(form, "MainForm");
				
				DesignerSerializationManager serializationManager = new DesignerSerializationManager(host);
				using (serializationManager.CreateSession()) {
					AvalonEditDocumentAdapter adapter = new AvalonEditDocumentAdapter(document, null);
					MockTextEditorOptions options = new MockTextEditorOptions();
					PythonDesignerGenerator generator = new PythonDesignerGenerator(options);
					generator.Merge(host, adapter, compilationUnit, serializationManager);
				}
			}
		}
		static Statement GetFirstStatement(string code)
		{
			PythonParser parser = new PythonParser();
			PythonAst ast = parser.CreateAst(@"snippet.py", new StringTextBuffer(code));
			SuiteStatement suiteStatement = (SuiteStatement)ast.Body;
			return suiteStatement.Statements[0];
		}
		ClassDefinition GetClassDefinition(string code)
		{
			PythonParser parser = new PythonParser();
			PythonAst ast = parser.CreateAst(@"test.py", new StringTextBuffer(code));
			SuiteStatement suite = ast.Body as SuiteStatement;
			return suite.Statements[0] as ClassDefinition;
		}
		public void SetUpFixture()
		{
			string python = "class";
			
			DefaultProjectContent projectContent = new DefaultProjectContent();
			PythonParser parser = new PythonParser();
			compilationUnit = parser.Parse(projectContent, @"C:\test.py", python);			
		}
Example #7
0
		public void SetLexerTags()
		{
			PythonParser parser = new PythonParser();
			string[] tags = new string[] {"Test"};
			parser.LexerTags = tags;
			
			Assert.AreEqual(tags, parser.LexerTags);
		}
		public void Init()
		{
			string python = "from math import *";
			
			DefaultProjectContent projectContent = new DefaultProjectContent();
			PythonParser parser = new PythonParser();
			compilationUnit = parser.Parse(projectContent, @"C:\test.py", python);
			import = compilationUnit.UsingScope.Usings[0] as PythonFromImport;
		}
		/// <summary>
		/// Resolves the type of the variable name specified.
		/// </summary>
		/// <param name="variableName">Name of the variable.</param>
		/// <param name="code">The python code containing the variable.</param>
		public string Resolve(string variableName, string code)
		{
			if (code != null) {
				PythonParser parser = new PythonParser();
				PythonAst ast = parser.CreateAst("resolver.py", code);
				return Resolve(variableName, ast);
			}
			return null;
		}
		public PythonResolverTestsHelper(string code)
		{
			ProjectContent = new ScriptingUtils.MockProjectContent();
			PythonParser parser = new PythonParser();
			string fileName = @"test.py";
			CompilationUnit = parser.Parse(ProjectContent, fileName, code) as DefaultCompilationUnit;
			
			ParseInfo = new ParseInformation(CompilationUnit);			
			Resolver = new PythonResolver();
		}
 /// <summary>
 /// Resolves the type of the variable name specified.
 /// </summary>
 /// <param name="variableName">Name of the variable.</param>
 /// <param name="code">The python code containing the variable.</param>
 public string Resolve(string variableName, string code)
 {
     if (code != null)
     {
         PythonParser parser = new PythonParser();
         PythonAst    ast    = parser.CreateAst("resolver.py", code);
         return(Resolve(variableName, ast));
     }
     return(null);
 }
		public void SetUpFixture()
		{
			string python = "class Test:\r\n" +
							"\tpass";
			
			DefaultProjectContent projectContent = new DefaultProjectContent();
			PythonParser parser = new PythonParser();
			compilationUnit = parser.Parse(projectContent, @"C:\Projects\Test\test.py", python);
			if (compilationUnit.Classes.Count > 0) {
				c = compilationUnit.Classes[0];
			}
		}
		/// <summary>
		/// Creates a control either a UserControl or Form from the python code.
		/// </summary>
		public IComponent CreateComponent(string pythonCode)
		{			
			PythonParser parser = new PythonParser();
			PythonAst ast = parser.CreateAst(@"Control.py", new StringTextBuffer(pythonCode));
			ast.Walk(this);
			
			// Did we find the InitializeComponent method?
			if (!FoundInitializeComponentMethod) {
				throw new PythonComponentWalkerException("Unable to find InitializeComponents method.");
			}
			return component;
		}
		public void SetUpFixture()
		{
			string python = "import System\r\n" +
							"\r\n" +
							"class Class1:\r\n" +
							"	def __init__(self):\r\n" +
							"		Console.\r\n" +
							"		pass\r\n";

			DefaultProjectContent projectContent = new DefaultProjectContent();
			PythonParser parser = new PythonParser();
			compilationUnit = parser.Parse(projectContent, @"C:\test.py", python);			
		}
		public void SetUpFixture()
		{
			PythonParser parser = new PythonParser();
			MockProjectContent mockProjectContent = new MockProjectContent();
			ICompilationUnit compilationUnit = parser.Parse(mockProjectContent, @"C:\Projects\Test\MainForm.py", GetPythonCode());

			parseInfo = new ParseInformation(compilationUnit);
			
			if (compilationUnit.Classes.Count > 0) {
				mainFormClass = compilationUnit.Classes[0];
				initializeComponentsMethod = FormsDesignerSecondaryDisplayBinding.GetInitializeComponents(mainFormClass);
			}
		}
Example #16
0
		public void InitBase()
		{
			projectContent = new MockProjectContent();
			PythonParser parser = new PythonParser();
			string fileName = @"C:\projects\test\test.py";
			compilationUnit = parser.Parse(projectContent, fileName, GetPythonScript());
			parseInfo = new ParseInformation(compilationUnit);
			
			resolver = new PythonResolver();
			
			expressionResult = GetExpressionResult();
			resolveResult = resolver.Resolve(expressionResult, parseInfo, GetPythonScript());
		}
        /// <summary>
        /// Creates a control either a UserControl or Form from the python code.
        /// </summary>
        public IComponent CreateComponent(string pythonCode)
        {
            PythonParser parser = new PythonParser();
            PythonAst    ast    = parser.CreateAst(@"Control.py", new StringTextBuffer(pythonCode));

            ast.Walk(this);

            // Did we find the InitializeComponent method?
            if (component == null)
            {
                throw new PythonComponentWalkerException("Unable to find InitializeComponents method.");
            }
            return(component);
        }
		public void InitializeComponentsUsedInsteadOfInitializeComponent()
		{
			PythonParser parser = new PythonParser();
			MockProjectContent mockProjectContent = new MockProjectContent();
			string code = GetFormCode().Replace("InitializeComponent", "InitializeComponents");
			ICompilationUnit compilationUnit = parser.Parse(mockProjectContent, @"C:\Projects\Test\MainForm.py", code);
			ParseInformation parseInfo = new ParseInformation(compilationUnit);
			IMethod expectedMethod = GetInitializeComponentMethod(compilationUnit);

			IMethod method = PythonDesignerGenerator.GetInitializeComponents(parseInfo);

			Assert.IsNotNull(method);
			Assert.AreSame(expectedMethod, method);
		}
		public void SetUpFixture()
		{
			textEditorOptions = new MockTextEditorOptions();
			generator = new DerivedPythonDesignerGenerator(textEditorOptions);
			mockViewContent = new MockTextEditorViewContent();
			viewContent = new DerivedFormDesignerViewContent(mockViewContent, new MockOpenedFile(fileName));
			generator.Attach(viewContent);
			viewContent.DesignerCodeFileContent = GetTextEditorCode();
			
			PythonParser parser = new PythonParser();
			ICompilationUnit parserCompilationUnit = parser.Parse(new DefaultProjectContent(), fileName, GetTextEditorCode());
			ParseInformation parseInfo = new ParseInformation(parserCompilationUnit);
			generator.ParseInfoToReturnFromParseFileMethod = parseInfo;
			
			AfterSetUpFixture();
		}
		public void SetUpFixture()
		{			
			PythonParser parser = new PythonParser();
			MockProjectContent mockProjectContent = new MockProjectContent();
			ICompilationUnit compilationUnit = parser.Parse(mockProjectContent, @"C:\Projects\Test\MainForm.py", GetFormCode());
	
			// Create parse info to return from ParseFile method.
			parseInfo = new ParseInformation(compilationUnit);
			
			// Get the InitializeComponent method from the
			// compilation unit.
			expectedInitializeComponentMethod = GetInitializeComponentMethod(compilationUnit);
			
			// Find the InitializeComponent method using the designer generator.			
			initializeComponentMethod = PythonDesignerGenerator.GetInitializeComponents(parseInfo);
		}
		public void SetUpFixture()
		{
			string python = "class Test(Base):\r\n" +
							"\tdef foo(self, sender, e):\r\n" +
							"\t\tpass";
			
			DefaultProjectContent projectContent = new DefaultProjectContent();
			PythonParser parser = new PythonParser();
			ICompilationUnit compilationUnit = parser.Parse(projectContent, @"C:\test.py", python);			
			if (compilationUnit.Classes.Count > 0) {
				IClass c = compilationUnit.Classes[0];
				method = c.Methods[0];
				if (method.Parameters.Count > 1) {
					senderParameter = method.Parameters[0];
					eventArgsParameter = method.Parameters[1];
				}
			}
		}
		public void SetUpFixture()
		{
			AvalonEdit.TextEditor textEditor = new AvalonEdit.TextEditor();
			document = textEditor.Document;
			textEditor.Text = GetTextEditorCode();
			
			PythonParser parser = new PythonParser();
			MockProjectContent projectContent = new MockProjectContent();
			MockProject project = new MockProject();
			project.RootNamespace = "RootNamespace";
			projectContent.Project = project;
			ICompilationUnit compilationUnit = parser.Parse(projectContent, @"test.py", document.Text);

			using (DesignSurface designSurface = new DesignSurface(typeof(Form))) {
				IDesignerHost host = (IDesignerHost)designSurface.GetService(typeof(IDesignerHost));
				IEventBindingService eventBindingService = new MockEventBindingService(host);
				Form form = (Form)host.RootComponent;
				form.ClientSize = new Size(200, 300);

				PropertyDescriptorCollection descriptors = TypeDescriptor.GetProperties(form);
				PropertyDescriptor namePropertyDescriptor = descriptors.Find("Name", false);
				namePropertyDescriptor.SetValue(form, "MainForm");
			
				// Add picture box
				PictureBox pictureBox = (PictureBox)host.CreateComponent(typeof(PictureBox), "pictureBox1");
				pictureBox.Location = new Point(0, 0);
				pictureBox.Image = new Bitmap(10, 10);
				pictureBox.Size = new Size(100, 120);
				pictureBox.TabIndex = 0;
				form.Controls.Add(pictureBox);
			
				MockTextEditorOptions options = new MockTextEditorOptions();
				options.ConvertTabsToSpaces = true;
				options.IndentationSize = 4;
				
				DesignerSerializationManager serializationManager = new DesignerSerializationManager(host);
				using (serializationManager.CreateSession()) {
					AvalonEditDocumentAdapter adapter = new AvalonEditDocumentAdapter(document, null);
					PythonDesignerGenerator generator = new PythonDesignerGenerator(options);
					generator.Merge(host, adapter, compilationUnit, serializationManager);
				}
			}
		}
		public void SetUpFixture()
		{
			resolver = new PythonResolver();
			mockProjectContent = new MockProjectContent();
			mockProjectContent.AddExistingNamespaceContents("System", new List<ICompletionEntry>());
			
			string python =
				"import System\r\n" +
				"class Test:\r\n" +
				"    def __init__(self):\r\n" +
				"        System.\r\n";
			
			PythonParser parser = new PythonParser();
			string fileName = @"C:\Projects\Test\test.py";
			DefaultCompilationUnit cu = parser.Parse(mockProjectContent, fileName, python) as DefaultCompilationUnit;
			ParseInformation parseInfo = new ParseInformation(cu);
			
			ExpressionResult expressionResult = new ExpressionResult("System", new DomRegion(4, 2), null, null);
			resolveResult = resolver.Resolve(expressionResult, parseInfo, python) as NamespaceResolveResult;
		}
		public void SetUpFixture()
		{
			string python =
				"class MyClass:\r\n" +
				"    def firstMethod(self):\r\n" +
				"        class NestedClass:\r\n" +
				"            def firstNestedClassMethod(self):\r\n" +
				"                pass\r\n" +
				"\r\n" +
				"    def secondMethod(self):\r\n" +
				"        pass\r\n" +
				"\r\n";
			
			DefaultProjectContent projectContent = new DefaultProjectContent();
			PythonParser parser = new PythonParser();
			compilationUnit = parser.Parse(projectContent, @"C:\test.py", python);
			if (compilationUnit.Classes.Count > 0) {
				c = compilationUnit.Classes[0];
			}
		}
		public void SetUpFixture()
		{
			string python = 
				"import unittest\r\n" +
				"\r\n" +
				"class simpleTest(unittest.TestCase):\r\n" +
				"    def testSuccess(self):\r\n" +
				"        assert True\r\n" +
				"\r\n" +
				"    def testFailure(self):\r\n" +
				"        assert False\r\n" +
				"\r\n";
			
			DefaultProjectContent projectContent = new DefaultProjectContent();
			PythonParser parser = new PythonParser();
			compilationUnit = parser.Parse(projectContent, @"C:\test.py", python);
			if (compilationUnit.Classes.Count > 0) {
				c = compilationUnit.Classes[0];
			}
		}
		public void SetUpFixture()
		{
			string python = 
				"import unittest\r\n" +
				"\r\n" +
				"class BaseTest(unittest.TestCase):\r\n" +
				"    def testSuccess(self):\r\n" +
				"        assert True\r\n" +
				"\r\n" +
				"class DerivedTest(BaseTest):\r\n" +
				"    pass\r\n" +
				"\r\n";
			
			projectContent = new DefaultProjectContent();
			PythonParser parser = new PythonParser();
			string fileName = @"C:\test.py";
			compilationUnit = parser.Parse(projectContent, fileName, python);
			projectContent.UpdateCompilationUnit(null, compilationUnit, fileName);
			if (compilationUnit.Classes.Count > 1) {
				c = compilationUnit.Classes[1];
			}
		}
		/// <summary>
		/// Checks the file's extension represents a python file.
		/// </summary>
		static bool IsPythonFile(string fileName)
		{
			PythonParser parser = new PythonParser();
			return parser.CanParse(fileName);
		}
Example #28
0
		public void SetUpFixture()
		{
			PythonMSBuildEngineHelper.InitMSBuildEngine();
			parser = new PythonParser();
		}
		public void ParseShouldNotThrowInvalidCastException()
		{
			PythonParser parser = new PythonParser();
			ICompilationUnit unit = parser.Parse(new DefaultProjectContent(), @"d:\projects\test\test.py", code);
			Assert.AreEqual(1, unit.Classes.Count);
		}
		public void CreateAstShouldNotThrowInvalidCastException()
		{
			PythonParser parser = new PythonParser();
			PythonAst ast = parser.CreateAst(@"d:\projects\test\test.py", new StringTextBuffer(code));
		}
Example #31
0
        /// <summary>
        /// Checks the file's extension represents a python file.
        /// </summary>
        static bool IsPythonFile(string fileName)
        {
            PythonParser parser = new PythonParser();

            return(parser.CanParse(fileName));
        }