5
// Mike Krüger <mkrueger@novell.com>
7
// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
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:
17
// The above copyright notice and this permission notice shall be
18
// included in all copies or substantial portions of the Software.
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.
31
using System.Collections.Generic;
32
using NUnit.Framework;
33
using MonoDevelop.Projects.Dom;
34
using MonoDevelop.Projects.Dom.Parser;
35
using MonoDevelop.CSharpBinding;
37
namespace MonoDevelop.CSharpBinding.Tests
40
public class TopLevelTests : UnitTests.TestBase
42
void DoTestUsings (IParser parser)
44
ICompilationUnit unit = parser.Parse ("a.cs",
46
using NUnit.Framework;").CompilationUnit;
47
foreach (IUsing u in unit.Usings) {
48
foreach (string ns in u.Namespaces) {
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);
56
Assert.Fail ("Unknown using: " + ns);
63
public void TestUsings ()
65
DoTestUsings (new NRefactoryParser ());
66
//DoTestUsings (new DomParser ());
69
void DoTestEnums (IParser parser)
71
ICompilationUnit unit = parser.Parse ("a.cs",
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);
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);
93
Assert.Fail ("Unknown field: " + f.Name);
99
public void TestEnums ()
101
DoTestEnums (new NRefactoryParser ());
102
// DoTestEnums (new DomParser ());
105
void DoTestStruct (IParser parser)
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);
115
public void TestStruct ()
117
DoTestStruct (new NRefactoryParser ());
118
// DoTestStruct (new DomParser ());
121
void DoTestInterface (IParser parser)
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);
131
public void TestInterface ()
133
DoTestInterface (new NRefactoryParser ());
134
// DoTestInterface (new DomParser ());
137
void DoTestDelegate (IParser parser)
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);
152
Assert.Fail ("Unknown parameter: " + parameter.Name);
159
public void TestDelegate ()
161
DoTestDelegate (new NRefactoryParser ());
162
// DoTestDelegate (new DomParser ());
165
void DoTestClass (IParser parser)
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);
181
public void TestClass ()
183
DoTestClass (new NRefactoryParser ());
184
// DoTestClass (new DomParser ());
187
void DoTestNamespace (IParser parser)
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);
200
public void TestNamespace ()
202
DoTestNamespace (new NRefactoryParser ());
203
// DoTestNamespace (new DomParser ());
206
void DoTestAttributes (IParser parser)
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 ();
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);
221
Assert.IsTrue (e.MoveNext ());
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);
232
Assert.IsTrue (e.MoveNext ());
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);
245
Assert.IsFalse (e.MoveNext ());
249
public void TestAttributes ()
251
DoTestAttributes (new NRefactoryParser ());
252
// DoTestAttributes (new DomParser ());