~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric-updates

« back to all changes in this revision

Viewing changes to tests/UnitTests/MonoDevelop.CSharpBinding/TopLevelTests.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-18 08:40:51 UTC
  • mfrom: (1.2.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090218084051-gh8m6ukvokbwj7cf
Tags: 1.9.2+dfsg-1ubuntu1
* Merge from Debian Experimental (LP: #330519), remaining Ubuntu changes:
  + debian/control:
    - Update for Gnome# 2.24
    - Add libmono-cairo1.0-cil to build-deps to fool pkg-config check

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// ParserTest.cs
 
3
//
 
4
// Author:
 
5
//   Mike Krüger <mkrueger@novell.com>
 
6
//
 
7
// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining
 
10
// a copy of this software and associated documentation files (the
 
11
// "Software"), to deal in the Software without restriction, including
 
12
// without limitation the rights to use, copy, modify, merge, publish,
 
13
// distribute, sublicense, and/or sell copies of the Software, and to
 
14
// permit persons to whom the Software is furnished to do so, subject to
 
15
// the following conditions:
 
16
// 
 
17
// The above copyright notice and this permission notice shall be
 
18
// included in all copies or substantial portions of the Software.
 
19
// 
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
27
//
 
28
 
 
29
using System;
 
30
using System.CodeDom;
 
31
using System.Collections.Generic;
 
32
using NUnit.Framework;
 
33
using MonoDevelop.Projects.Dom;
 
34
using MonoDevelop.Projects.Dom.Parser;
 
35
using MonoDevelop.CSharpBinding;
 
36
 
 
37
namespace MonoDevelop.CSharpBinding.Tests
 
38
{
 
39
        [TestFixture]
 
40
        public class TopLevelTests : UnitTests.TestBase
 
41
        {
 
42
                void DoTestUsings (IParser parser)
 
43
                {
 
44
                        ICompilationUnit unit = parser.Parse ("a.cs", 
 
45
@"using System;
 
46
using NUnit.Framework;").CompilationUnit;
 
47
                        foreach (IUsing u in unit.Usings) {
 
48
                                foreach (string ns in u.Namespaces) {
 
49
                                        if (ns == "System") {
 
50
                                                Assert.AreEqual (1, u.Region.End.Line);
 
51
                                                Assert.AreEqual (1, u.Region.Start.Line);
 
52
                                        } else if (ns == "NUnit.Framework") {
 
53
                                                Assert.AreEqual (2, u.Region.End.Line);
 
54
                                                Assert.AreEqual (2, u.Region.Start.Line);
 
55
                                        } else {
 
56
                                                Assert.Fail ("Unknown using: " + ns);
 
57
                                        }
 
58
                                }
 
59
                        }
 
60
                }
 
61
                
 
62
                [Test]
 
63
                public void TestUsings ()
 
64
                {
 
65
                        DoTestUsings (new NRefactoryParser ());
 
66
                        //DoTestUsings (new DomParser ());
 
67
                }
 
68
                
 
69
                void DoTestEnums (IParser parser)
 
70
                {
 
71
                        ICompilationUnit unit = parser.Parse ("a.cs", 
 
72
@"enum TestEnum {
 
73
        A,
 
74
        B,
 
75
        C
 
76
}").CompilationUnit;
 
77
                        Assert.AreEqual (1, unit.Types.Count);
 
78
                        IType type = unit.Types[0];
 
79
                        Assert.AreEqual (ClassType.Enum, type.ClassType);
 
80
                        Assert.AreEqual ("TestEnum", type.Name);
 
81
                        Assert.AreEqual (3, type.FieldCount);
 
82
                        foreach (IField f in type.Fields) {
 
83
                                Assert.IsTrue (f.IsConst);
 
84
                                Assert.IsTrue (f.IsSpecialName);
 
85
                                Assert.IsTrue (f.IsPublic);
 
86
                                if (f.Name == "A") {
 
87
                                        Assert.AreEqual (2, f.Location.Line);
 
88
                                } else if (f.Name == "B") {
 
89
                                        Assert.AreEqual (3, f.Location.Line);
 
90
                                } else if (f.Name == "C") {
 
91
                                        Assert.AreEqual (4, f.Location.Line);
 
92
                                } else {
 
93
                                        Assert.Fail ("Unknown field: " + f.Name);
 
94
                                }
 
95
                        }
 
96
                }
 
97
                
 
98
                [Test]
 
99
                public void TestEnums ()
 
100
                {
 
101
                        DoTestEnums (new NRefactoryParser ());
 
102
//                      DoTestEnums (new DomParser ());
 
103
                }
 
104
                
 
105
                void DoTestStruct (IParser parser)
 
106
                {
 
107
                        ICompilationUnit unit = parser.Parse ("a.cs", @"struct TestStruct { }").CompilationUnit;
 
108
                        Assert.AreEqual (1, unit.Types.Count);
 
109
                        IType type = unit.Types[0];
 
110
                        Assert.AreEqual (ClassType.Struct, type.ClassType);
 
111
                        Assert.AreEqual ("TestStruct", type.Name);
 
112
                }
 
113
                
 
114
                [Test]
 
115
                public void TestStruct ()
 
116
                {
 
117
                        DoTestStruct (new NRefactoryParser ());
 
118
//                      DoTestStruct (new DomParser ());
 
119
                }
 
120
                
 
121
                void DoTestInterface (IParser parser)
 
122
                {
 
123
                        ICompilationUnit unit = parser.Parse ("a.cs", @"interface TestInterface { }").CompilationUnit;
 
124
                        Assert.AreEqual (1, unit.Types.Count);
 
125
                        IType type = unit.Types[0];
 
126
                        Assert.AreEqual (ClassType.Interface, type.ClassType);
 
127
                        Assert.AreEqual ("TestInterface", type.Name);
 
128
                }
 
129
                
 
130
                [Test]
 
131
                public void TestInterface ()
 
132
                {
 
133
                        DoTestInterface (new NRefactoryParser ());
 
134
//                      DoTestInterface (new DomParser ());
 
135
                }
 
136
                
 
137
                void DoTestDelegate (IParser parser)
 
138
                {
 
139
                        ICompilationUnit unit = parser.Parse ("a.cs", @"delegate void TestDelegate (int a, string b);").CompilationUnit;
 
140
                        Assert.AreEqual (1, unit.Types.Count);
 
141
                        IType type = unit.Types[0];
 
142
                        Assert.AreEqual (ClassType.Delegate, type.ClassType);
 
143
                        Assert.AreEqual ("TestDelegate", type.Name);
 
144
                        foreach (IMethod method in type.Methods) {
 
145
                                Assert.AreEqual ("Void", method.ReturnType.Name);
 
146
                                foreach (IParameter parameter in method.Parameters) {
 
147
                                        if (parameter.Name == "a") {
 
148
                                                Assert.AreEqual ("System.Int32", parameter.ReturnType.FullName);
 
149
                                        } else if (parameter.Name == "b") {
 
150
                                                Assert.AreEqual ("System.String", parameter.ReturnType.FullName);
 
151
                                        } else {
 
152
                                                Assert.Fail ("Unknown parameter: " + parameter.Name);
 
153
                                        }
 
154
                                }
 
155
                        }
 
156
                }
 
157
                
 
158
                [Test]
 
159
                public void TestDelegate ()
 
160
                {
 
161
                        DoTestDelegate (new NRefactoryParser ());
 
162
//                      DoTestDelegate (new DomParser ());
 
163
                }
 
164
                
 
165
                void DoTestClass (IParser parser)
 
166
                {
 
167
                        ICompilationUnit unit = parser.Parse ("a.cs", @"public partial class TestClass<T, S> : MyBaseClass where T : Constraint { }").CompilationUnit;
 
168
                        Assert.AreEqual (1, unit.Types.Count);
 
169
                        IType type = unit.Types[0];
 
170
                        Assert.AreEqual (ClassType.Class, type.ClassType);
 
171
                        Assert.AreEqual ("TestClass", type.Name);
 
172
                        Assert.AreEqual ("MyBaseClass", type.BaseType.Name);
 
173
                        Assert.AreEqual (Modifiers.Partial | Modifiers.Public, type.Modifiers);
 
174
                        Assert.AreEqual (2, type.TypeParameters.Count);
 
175
                        Assert.AreEqual ("T", type.TypeParameters[0].Name);
 
176
                        Assert.AreEqual ("Constraint", type.TypeParameters[0].Constraints[0].Name);
 
177
                        Assert.AreEqual ("S", type.TypeParameters[1].Name);
 
178
                }
 
179
                
 
180
                [Test]
 
181
                public void TestClass ()
 
182
                {
 
183
                        DoTestClass (new NRefactoryParser ());
 
184
//                      DoTestClass (new DomParser ());
 
185
                }
 
186
                
 
187
                void DoTestNamespace (IParser parser)
 
188
                {
 
189
                        ICompilationUnit unit = parser.Parse ("a.cs", @"namespace Test1.Test2.Test3 { class A { } }").CompilationUnit;
 
190
                        Assert.AreEqual (3, unit.Usings.Count);
 
191
                        Assert.AreEqual ("Test1.Test2.Test3", unit.Usings[0].Namespaces[0]);
 
192
                        Assert.AreEqual ("Test1.Test2", unit.Usings[1].Namespaces[0]);
 
193
                        Assert.AreEqual ("Test1", unit.Usings[2].Namespaces[0]);
 
194
                        Assert.AreEqual (1, unit.Types.Count);
 
195
                        IType type = unit.Types[0];
 
196
                        Assert.AreEqual ("Test1.Test2.Test3", type.Namespace);
 
197
                }
 
198
                
 
199
                [Test]
 
200
                public void TestNamespace ()
 
201
                {
 
202
                        DoTestNamespace (new NRefactoryParser ());
 
203
//                      DoTestNamespace (new DomParser ());
 
204
                }
 
205
                
 
206
                void DoTestAttributes (IParser parser)
 
207
                {
 
208
                        ICompilationUnit unit = parser.Parse ("a.cs", @"[Attr1][Attr2(1,true)][Attr3('c',a=1,b=""hi"")] public class TestClass { }").CompilationUnit;
 
209
                        Assert.AreEqual (1, unit.Types.Count);
 
210
                        IType type = unit.Types[0];
 
211
                        Assert.AreEqual (ClassType.Class, type.ClassType);
 
212
                        Assert.AreEqual ("TestClass", type.Name);
 
213
                        IEnumerator<IAttribute> e = type.Attributes.GetEnumerator ();
 
214
                        
 
215
                        Assert.IsTrue (e.MoveNext ());
 
216
                        IAttribute att = e.Current;
 
217
                        Assert.AreEqual ("Attr1", att.Name);
 
218
                        Assert.AreEqual (0, att.PositionalArguments.Count);
 
219
                        Assert.AreEqual (0, att.NamedArguments.Count);
 
220
                        
 
221
                        Assert.IsTrue (e.MoveNext ());
 
222
                        att = e.Current;
 
223
                        Assert.AreEqual ("Attr2", att.Name);
 
224
                        Assert.AreEqual (2, att.PositionalArguments.Count);
 
225
                        Assert.AreEqual (0, att.NamedArguments.Count);
 
226
                        Assert.IsTrue (att.PositionalArguments [0] is CodePrimitiveExpression);
 
227
                        CodePrimitiveExpression exp = (CodePrimitiveExpression) att.PositionalArguments [0];
 
228
                        Assert.AreEqual (1, exp.Value);
 
229
                        exp = (CodePrimitiveExpression) att.PositionalArguments [1];
 
230
                        Assert.AreEqual (true, exp.Value);
 
231
                        
 
232
                        Assert.IsTrue (e.MoveNext ());
 
233
                        att = e.Current;
 
234
                        Assert.AreEqual ("Attr3", att.Name);
 
235
                        Assert.AreEqual (1, att.PositionalArguments.Count);
 
236
                        Assert.AreEqual (2, att.NamedArguments.Count);
 
237
                        Assert.IsTrue (att.PositionalArguments [0] is CodePrimitiveExpression);
 
238
                        exp = (CodePrimitiveExpression) att.PositionalArguments [0];
 
239
                        Assert.AreEqual ('c', exp.Value);
 
240
                        exp = (CodePrimitiveExpression) att.NamedArguments ["a"];
 
241
                        Assert.AreEqual (1, exp.Value);
 
242
                        exp = (CodePrimitiveExpression) att.NamedArguments ["b"];
 
243
                        Assert.AreEqual ("hi", exp.Value);
 
244
                        
 
245
                        Assert.IsFalse (e.MoveNext ());
 
246
                }
 
247
                
 
248
                [Test()]
 
249
                public void TestAttributes ()
 
250
                {
 
251
                        DoTestAttributes (new NRefactoryParser ());
 
252
//                      DoTestAttributes (new DomParser ());
 
253
                }
 
254
        }
 
255
}