public void NestedInterfaceInGenericClass()
        {
            // See SD2-1626
            DefaultProjectContent pc = new DefaultProjectContent();

            pc.ReferencedContents.Add(SharedProjectContentRegistryForTests.Instance.Mscorlib);

            DefaultCompilationUnit cu        = new DefaultCompilationUnit(pc);
            DefaultClass           container = new DefaultClass(cu, "TestClass");

            container.TypeParameters.Add(new DefaultTypeParameter(container, "T", 0));

            DefaultClass innerClass = new DefaultClass(cu, container);

            innerClass.FullyQualifiedName = "TestClass.INestedInterface";
            innerClass.ClassType          = ClassType.Interface;
            innerClass.TypeParameters.Add(new DefaultTypeParameter(innerClass, "T", 0));
            innerClass.Properties.Add(new DefaultProperty(innerClass, "P")
            {
                ReturnType = new GenericReturnType(innerClass.TypeParameters[0]),
                CanGet     = true
            });
            container.InnerClasses.Add(innerClass);
            pc.AddClassToNamespaceList(container);

            DefaultClass        targetClass = new DefaultClass(cu, "TargetClass");
            List <AbstractNode> nodes       = new List <AbstractNode>();

            IReturnType interf = new SearchClassReturnType(pc, targetClass, 0, 0, "TestClass.INestedInterface", 1);

            interf = new ConstructedReturnType(interf, new IReturnType[] { SharedProjectContentRegistryForTests.Instance.Mscorlib.GetClass("System.String", 0).DefaultReturnType });

            CSharpCodeGenerator codeGen = new CSharpCodeGenerator();

            codeGen.ImplementInterface(nodes, interf, true, targetClass);

            Assert.AreEqual(1, nodes.Count);
            CSharpOutputVisitor output = new CSharpOutputVisitor();

            output.Options.IndentationChar = ' ';
            output.Options.IndentSize      = 2;
            nodes[0].AcceptVisitor(output, null);
            Assert.AreEqual("string TestClass<string>.INestedInterface.P {\n  get {\n    throw new NotImplementedException();\n  }\n}", output.Text.Replace("\r", "").Trim());
        }
        public void Init()
        {
            DefaultProjectContent projectContent = new DefaultProjectContent();

            unit = new DefaultCompilationUnit(projectContent);
            DefaultClass c = new DefaultClass(unit, "NewNamespace.Foo");

            DefaultMethod buttonClickMethod = new DefaultMethod(c, "ButtonClick");

            AddSenderAndEventArgsParameters(buttonClickMethod);
            c.Methods.Add(buttonClickMethod);

            DefaultMethod onMouseDownMethod = new DefaultMethod(c, "OnMouseDown");

            AddSenderAndEventArgsParameters(onMouseDownMethod);
            c.Methods.Add(onMouseDownMethod);

            projectContent.AddClassToNamespaceList(c);

            parseInfo = new ParseInformation(unit);
        }
		public void NestedInterfaceInGenericClass()
		{
			// See SD2-1626
			DefaultProjectContent pc = new DefaultProjectContent();
			pc.ReferencedContents.Add(SharedProjectContentRegistryForTests.Instance.Mscorlib);
			
			DefaultCompilationUnit cu = new DefaultCompilationUnit(pc);
			DefaultClass container = new DefaultClass(cu, "TestClass");
			container.TypeParameters.Add(new DefaultTypeParameter(container, "T", 0));
			
			DefaultClass innerClass = new DefaultClass(cu, container);
			innerClass.FullyQualifiedName = "TestClass.INestedInterface";
			innerClass.ClassType = ClassType.Interface;
			innerClass.TypeParameters.Add(new DefaultTypeParameter(innerClass, "T", 0));
			innerClass.Properties.Add(new DefaultProperty(innerClass, "P") {
			                          	ReturnType = new GenericReturnType(innerClass.TypeParameters[0]),
			                          	CanGet = true
			                          });
			container.InnerClasses.Add(innerClass);
			pc.AddClassToNamespaceList(container);
			
			DefaultClass targetClass = new DefaultClass(cu, "TargetClass");
			List<AbstractNode> nodes = new List<AbstractNode>();
			
			IReturnType interf = new SearchClassReturnType(pc, targetClass, 0, 0, "TestClass.INestedInterface", 1);
			interf = new ConstructedReturnType(interf, new IReturnType[] { SharedProjectContentRegistryForTests.Instance.Mscorlib.GetClass("System.String", 0).DefaultReturnType });
			
			CSharpCodeGenerator codeGen = new CSharpCodeGenerator();
			codeGen.ImplementInterface(nodes, interf, true, targetClass);
			
			Assert.AreEqual(1, nodes.Count);
			CSharpOutputVisitor output = new CSharpOutputVisitor();
			output.Options.IndentationChar = ' ';
			output.Options.IndentSize = 2;
			nodes[0].AcceptVisitor(output, null);
			Assert.AreEqual("string TestClass<string>.INestedInterface.P {\n  get {\n    throw new NotImplementedException();\n  }\n}", output.Text.Replace("\r", "").Trim());
		}
        INode Parse(SupportedLanguage sourceLanguage, string sourceCode, out string error)
        {
            project = new DefaultProjectContent();
            project.ReferencedContents.AddRange(ReferencedContents);
            if (sourceLanguage == SupportedLanguage.VBNet)
            {
                project.Language       = LanguageProperties.VBNet;
                project.DefaultImports = new DefaultUsing(project);
                project.DefaultImports.Usings.AddRange(DefaultImportsToAdd);
            }
            else
            {
                project.Language = LanguageProperties.CSharp;
            }
            SnippetParser parser = new SnippetParser(sourceLanguage);
            INode         result = parser.Parse(sourceCode);

            error    = parser.Errors.ErrorOutput;
            specials = parser.Specials;
            if (parser.Errors.Count != 0)
            {
                return(null);
            }

            wasExpression = parser.SnippetType == SnippetType.Expression;
            if (wasExpression)
            {
                // Special case 'Expression': expressions may be replaced with other statements in the AST by the ConvertVisitor,
                // but we need to return a 'stable' node so that the correct transformed AST is returned.
                // Thus, we wrap any expressions into a statement block.
                result = MakeBlockFromExpression((Expression)result);
            }

            // now create a dummy compilation unit around the snippet result
            switch (parser.SnippetType)
            {
            case SnippetType.CompilationUnit:
                compilationUnit = (CompilationUnit)result;
                break;

            case SnippetType.Expression:
            case SnippetType.Statements:
                compilationUnit = MakeCompilationUnitFromTypeMembers(
                    MakeMethodFromBlock(
                        (BlockStatement)result
                        ));
                break;

            case SnippetType.TypeMembers:
                compilationUnit = MakeCompilationUnitFromTypeMembers(result.Children);
                break;

            default:
                throw new NotSupportedException("Unknown snippet type: " + parser.SnippetType);
            }

            // convert NRefactory CU in DOM CU
            NRefactoryASTConvertVisitor visitor = new NRefactoryASTConvertVisitor(project);

            visitor.VisitCompilationUnit(compilationUnit, null);
            visitor.Cu.FileName = sourceLanguage == SupportedLanguage.CSharp ? "a.cs" : "a.vb";

            // and register the compilation unit in the DOM
            foreach (IClass c in visitor.Cu.Classes)
            {
                project.AddClassToNamespaceList(c);
            }
            parseInfo = new ParseInformation();
            parseInfo.SetCompilationUnit(visitor.Cu);

            return(result);
        }
		INode Parse(SupportedLanguage sourceLanguage, string sourceCode, out string error)
		{
			project = new DefaultProjectContent();
			project.ReferencedContents.AddRange(ReferencedContents);
			if (sourceLanguage == SupportedLanguage.VBNet) {
				project.Language = LanguageProperties.VBNet;
				project.DefaultImports = new DefaultUsing(project);
				project.DefaultImports.Usings.AddRange(DefaultImportsToAdd);
			} else {
				project.Language = LanguageProperties.CSharp;
			}
			SnippetParser parser = new SnippetParser(sourceLanguage);
			INode result = parser.Parse(sourceCode);
			error = parser.Errors.ErrorOutput;
			specials = parser.Specials;
			if (parser.Errors.Count != 0)
				return null;
			
			wasExpression = parser.SnippetType == SnippetType.Expression;
			if (wasExpression) {
				// Special case 'Expression': expressions may be replaced with other statements in the AST by the ConvertVisitor,
				// but we need to return a 'stable' node so that the correct transformed AST is returned.
				// Thus, we wrap any expressions into a statement block.
				result = MakeBlockFromExpression((Expression)result);
			}
			
			// now create a dummy compilation unit around the snippet result
			switch (parser.SnippetType) {
				case SnippetType.CompilationUnit:
					compilationUnit = (CompilationUnit)result;
					break;
				case SnippetType.Expression:
				case SnippetType.Statements:
					compilationUnit = MakeCompilationUnitFromTypeMembers(
						MakeMethodFromBlock(
							(BlockStatement)result
						));
					break;
				case SnippetType.TypeMembers:
					compilationUnit = MakeCompilationUnitFromTypeMembers(result.Children);
					break;
				default:
					throw new NotSupportedException("Unknown snippet type: " + parser.SnippetType);
			}
			
			// convert NRefactory CU in DOM CU
			NRefactoryASTConvertVisitor visitor = new NRefactoryASTConvertVisitor(project, sourceLanguage);
			visitor.VisitCompilationUnit(compilationUnit, null);
			visitor.Cu.FileName = sourceLanguage == SupportedLanguage.CSharp ? "a.cs" : "a.vb";
			
			// and register the compilation unit in the DOM
			foreach (IClass c in visitor.Cu.Classes) {
				project.AddClassToNamespaceList(c);
			}
			parseInfo = new ParseInformation(visitor.Cu);
			
			return result;
		}
示例#6
0
        INode Parse(SupportedLanguage sourceLanguage, string sourceCode, out string error)
        {
            project = new DefaultProjectContent();
            project.ReferencedContents.AddRange(ReferencedContents);
            if (sourceLanguage == SupportedLanguage.VBNet)
            {
                project.DefaultImports = new DefaultUsing(project);
                project.DefaultImports.Usings.AddRange(DefaultImportsToAdd);
            }
            SnippetParser parser = new SnippetParser(sourceLanguage);
            INode         result = parser.Parse(sourceCode);

            error    = parser.Errors.ErrorOutput;
            specials = parser.Specials;
            if (parser.Errors.Count != 0)
            {
                return(null);
            }

            // now create a dummy compilation unit around the snippet result
            switch (parser.SnippetType)
            {
            case SnippetType.CompilationUnit:
                compilationUnit = (CompilationUnit)result;
                break;

            case SnippetType.Expression:
                compilationUnit = MakeCompilationUnitFromTypeMembers(
                    MakeMethodFromBlock(
                        MakeBlockFromExpression(
                            (Expression)result
                            )));
                break;

            case SnippetType.Statements:
                compilationUnit = MakeCompilationUnitFromTypeMembers(
                    MakeMethodFromBlock(
                        (BlockStatement)result
                        ));
                break;

            case SnippetType.TypeMembers:
                compilationUnit = MakeCompilationUnitFromTypeMembers(result.Children);
                break;

            default:
                throw new NotSupportedException("Unknown snippet type: " + parser.SnippetType);
            }

            // convert NRefactory CU in DOM CU
            NRefactoryASTConvertVisitor visitor = new NRefactoryASTConvertVisitor(project);

            visitor.VisitCompilationUnit(compilationUnit, null);
            visitor.Cu.FileName = sourceLanguage == SupportedLanguage.CSharp ? "a.cs" : "a.vb";

            // and register the compilation unit in the DOM
            foreach (IClass c in visitor.Cu.Classes)
            {
                project.AddClassToNamespaceList(c);
            }
            parseInfo = new ParseInformation();
            parseInfo.SetCompilationUnit(visitor.Cu);

            return(result);
        }
		INode Parse(SupportedLanguage sourceLanguage, string sourceCode, out string error)
		{
			project = new DefaultProjectContent();
			project.ReferencedContents.AddRange(ReferencedContents);
			if (sourceLanguage == SupportedLanguage.VBNet) {
				project.DefaultImports = new DefaultUsing(project);
				project.DefaultImports.Usings.AddRange(DefaultImportsToAdd);
			}
			SnippetParser parser = new SnippetParser(sourceLanguage);
			INode result = parser.Parse(sourceCode);
			error = parser.Errors.ErrorOutput;
			specials = parser.Specials;
			if (parser.Errors.Count != 0)
				return null;
			
			// now create a dummy compilation unit around the snippet result
			switch (parser.SnippetType) {
				case SnippetType.CompilationUnit:
					compilationUnit = (CompilationUnit)result;
					break;
				case SnippetType.Expression:
					compilationUnit = MakeCompilationUnitFromTypeMembers(
						MakeMethodFromBlock(
							MakeBlockFromExpression(
								(Expression)result
							)));
					break;
				case SnippetType.Statements:
					compilationUnit = MakeCompilationUnitFromTypeMembers(
						MakeMethodFromBlock(
							(BlockStatement)result
						));
					break;
				case SnippetType.TypeMembers:
					compilationUnit = MakeCompilationUnitFromTypeMembers(result.Children);
					break;
				default:
					throw new NotSupportedException("Unknown snippet type: " + parser.SnippetType);
			}
			
			// convert NRefactory CU in DOM CU
			NRefactoryASTConvertVisitor visitor = new NRefactoryASTConvertVisitor(project);
			visitor.VisitCompilationUnit(compilationUnit, null);
			visitor.Cu.FileName = sourceLanguage == SupportedLanguage.CSharp ? "a.cs" : "a.vb";
			
			// and register the compilation unit in the DOM
			foreach (IClass c in visitor.Cu.Classes) {
				project.AddClassToNamespaceList(c);
			}
			parseInfo = new ParseInformation();
			parseInfo.SetCompilationUnit(visitor.Cu);
			
			return result;
		}