~ubuntu-branches/ubuntu/feisty/monodevelop/feisty

« back to all changes in this revision

Viewing changes to Core/src/NRefactory/Test/Parser/ParseUtilCSharp.cs

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2006-08-18 00:51:23 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060818005123-5iit07y0j7wjg55f
Tags: 0.11+svn20060818-0ubuntu1
* New SVN snapshot
  + Works with Gtk# 2.9.0
* debian/control:
  + Updated Build-Depends
* debian/patches/use_nunit2.2.dpatch,
  debian/patches/use_real_libs.dpatch:
  + Updated
* debian/patches/versioncontrol_buildfix.dpatch:
  + Fix build failure in the version control addin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// <file>
 
2
//     <copyright see="prj:///doc/copyright.txt"/>
 
3
//     <license see="prj:///doc/license.txt"/>
 
4
//     <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
 
5
//     <version>$Revision: 915 $</version>
 
6
// </file>
 
7
 
 
8
using System;
 
9
using System.Drawing;
 
10
using System.IO;
 
11
 
 
12
using NUnit.Framework;
 
13
 
 
14
using ICSharpCode.NRefactory.Parser;
 
15
using ICSharpCode.NRefactory.Parser.AST;
 
16
 
 
17
namespace ICSharpCode.NRefactory.Tests.AST
 
18
{
 
19
        public class ParseUtilCSharp
 
20
        {
 
21
                public static T ParseGlobal<T>(string program) where T : INode
 
22
                {
 
23
                        return ParseGlobal<T>(program, false);
 
24
                }
 
25
                
 
26
                public static T ParseGlobal<T>(string program, bool expectError) where T : INode
 
27
                {
 
28
                        return ParseGlobal<T>(program, expectError, false);
 
29
                }
 
30
                
 
31
                public static T ParseGlobal<T>(string program, bool expectError, bool skipMethodBodies) where T : INode
 
32
                {
 
33
                        IParser parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader(program));
 
34
                        parser.ParseMethodBodies = !skipMethodBodies;
 
35
                        parser.Parse();
 
36
                        Assert.IsNotNull(parser.Errors);
 
37
                        if (expectError)
 
38
                                Assert.IsTrue(parser.Errors.ErrorOutput.Length > 0, "There were errors expected, but parser finished without errors.");
 
39
                        else
 
40
                                Assert.AreEqual("", parser.Errors.ErrorOutput);
 
41
                        Assert.IsNotNull(parser.CompilationUnit);
 
42
                        Assert.IsNotNull(parser.CompilationUnit.Children);
 
43
                        Assert.IsNotNull(parser.CompilationUnit.Children[0]);
 
44
                        Assert.IsTrue(parser.CompilationUnit.Children.Count > 0);
 
45
                        Type type = typeof(T);
 
46
                        Assert.IsTrue(type.IsAssignableFrom(parser.CompilationUnit.Children[0].GetType()), String.Format("Parsed expression was {0} instead of {1} ({2})", parser.CompilationUnit.Children[0].GetType(), type, parser.CompilationUnit.Children[0]));
 
47
                        return (T)parser.CompilationUnit.Children[0];
 
48
                }
 
49
                
 
50
                public static T ParseTypeMember<T>(string typeMember) where T : INode
 
51
                {
 
52
                        return ParseTypeMember<T>(typeMember, false);
 
53
                }
 
54
                
 
55
                public static T ParseTypeMember<T>(string typeMember, bool expectError) where T : INode
 
56
                {
 
57
                        TypeDeclaration td = ParseGlobal<TypeDeclaration>("class MyClass {" + typeMember + "}", expectError);
 
58
                        Assert.IsTrue(td.Children.Count > 0);
 
59
                        Type type = typeof(T);
 
60
                        Assert.IsTrue(type.IsAssignableFrom(td.Children[0].GetType()), String.Format("Parsed expression was {0} instead of {1} ({2})", td.GetType(), type, td));
 
61
                        return (T)td.Children[0];
 
62
                }
 
63
                
 
64
                public static T ParseStatement<T>(string statement) where T : INode
 
65
                {
 
66
                        MethodDeclaration md = ParseTypeMember<MethodDeclaration>("void A() { " + statement + " }");
 
67
                        Assert.IsTrue(md.Body.Children.Count > 0);
 
68
                        Type type = typeof(T);
 
69
                        Assert.IsTrue(type.IsAssignableFrom(md.Body.Children[0].GetType()), String.Format("Parsed expression was {0} instead of {1} ({2})", md.GetType(), type, md));
 
70
                        return (T)md.Body.Children[0];
 
71
                }
 
72
                
 
73
                public static T ParseExpression<T>(string expr) where T : INode
 
74
                {
 
75
                        return ParseExpression<T>(expr, false);
 
76
                }
 
77
                
 
78
                public static T ParseExpression<T>(string expr, bool expectErrors) where T : INode
 
79
                {
 
80
                        IParser parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader(expr + ";"));
 
81
                        object parsedExpression = parser.ParseExpression();
 
82
                        if (expectErrors)
 
83
                                Assert.IsTrue(parser.Errors.ErrorOutput.Length > 0, "There were errors expected, but parser finished without errors.");
 
84
                        else
 
85
                                Assert.AreEqual("", parser.Errors.ErrorOutput);
 
86
                        Type type = typeof(T);
 
87
                        Assert.IsTrue(type.IsAssignableFrom(parsedExpression.GetType()), String.Format("Parsed expression was {0} instead of {1} ({2})", parsedExpression.GetType(), type, parsedExpression));
 
88
                        return (T)parsedExpression;
 
89
                }
 
90
        }
 
91
}