~ubuntu-branches/ubuntu/edgy/monodevelop/edgy

« back to all changes in this revision

Viewing changes to Core/src/NRefactory/Test/Output/VBNet/CSharpToVBConverterTest.cs

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2006-09-15 02:15:25 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20060915021525-ngsnriip5e3uqi9d
Tags: 0.12-0ubuntu1
* New upstream release
* debian/patches/gtk-sharp-2.10.dpatch,
  debian/patches/versioncontrol_buildfix.dpatch:
  + Dropped, merged upstream
* debian/patches/use_nunit2.2.dpatch,
  debian/patches/firefox.dpatch:
  + Updated

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="Daniel Grunwald" email="daniel@danielgrunwald.de"/>
5
 
//     <version>$Revision: 1080 $</version>
6
 
// </file>
7
 
 
8
 
using System;
9
 
using System.Text;
10
 
using System.IO;
11
 
using NUnit.Framework;
12
 
using ICSharpCode.NRefactory.Parser;
13
 
using ICSharpCode.NRefactory.Parser.AST;
14
 
using ICSharpCode.NRefactory.PrettyPrinter;
15
 
 
16
 
namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
17
 
{
18
 
        [TestFixture]
19
 
        public class CSharpToVBConverterTest
20
 
        {
21
 
                public void TestProgram(string input, string expectedOutput)
22
 
                {
23
 
                        IParser parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader(input));
24
 
                        parser.Parse();
25
 
                        Assert.AreEqual("", parser.Errors.ErrorOutput);
26
 
                        parser.CompilationUnit.AcceptVisitor(new CSharpToVBNetConvertVisitor(), null);
27
 
                        VBNetOutputVisitor outputVisitor = new VBNetOutputVisitor();
28
 
                        outputVisitor.Visit(parser.CompilationUnit, null);
29
 
                        Assert.AreEqual("", outputVisitor.Errors.ErrorOutput);
30
 
                        Assert.AreEqual(expectedOutput, outputVisitor.Text);
31
 
                }
32
 
                
33
 
                public void TestMember(string input, string expectedOutput)
34
 
                {
35
 
                        StringBuilder b = new StringBuilder();
36
 
                        b.AppendLine("Class tmp1");
37
 
                        using (StringReader r = new StringReader(expectedOutput)) {
38
 
                                string line;
39
 
                                while ((line = r.ReadLine()) != null) {
40
 
                                        b.Append("\t");
41
 
                                        b.AppendLine(line);
42
 
                                }
43
 
                        }
44
 
                        b.AppendLine("End Class");
45
 
                        TestProgram("class tmp1 { \n" + input + "\n}", b.ToString());
46
 
                }
47
 
                
48
 
                public void TestStatement(string input, string expectedOutput)
49
 
                {
50
 
                        StringBuilder b = new StringBuilder();
51
 
                        b.AppendLine("Class tmp1");
52
 
                        b.AppendLine("\tPrivate Sub tmp2()");
53
 
                        using (StringReader r = new StringReader(expectedOutput)) {
54
 
                                string line;
55
 
                                while ((line = r.ReadLine()) != null) {
56
 
                                        b.Append("\t\t");
57
 
                                        b.AppendLine(line);
58
 
                                }
59
 
                        }
60
 
                        b.AppendLine("\tEnd Sub");
61
 
                        b.AppendLine("End Class");
62
 
                        TestProgram("class tmp1 { void tmp2() {\n" + input + "\n}}", b.ToString());
63
 
                }
64
 
                
65
 
                [Test]
66
 
                public void ForWithUnknownConditionAndSingleStatement()
67
 
                {
68
 
                        TestStatement("for (i = 0; unknownCondition; i++) b[i] = s[i];",
69
 
                                      "i = 0\n" +
70
 
                                      "While unknownCondition\n" +
71
 
                                      "\tb(i) = s(i)\n" +
72
 
                                      "\ti += 1\n" +
73
 
                                      "End While");
74
 
                }
75
 
                
76
 
                [Test]
77
 
                public void ForWithUnknownConditionAndBlock()
78
 
                {
79
 
                        TestStatement("for (i = 0; unknownCondition; i++) { b[i] = s[i]; }",
80
 
                                      "i = 0\n" +
81
 
                                      "While unknownCondition\n" +
82
 
                                      "\tb(i) = s(i)\n" +
83
 
                                      "\ti += 1\n" +
84
 
                                      "End While");
85
 
                }
86
 
                
87
 
                [Test]
88
 
                public void ForWithSingleStatement()
89
 
                {
90
 
                        TestStatement("for (i = 0; i < end; i++) b[i] = s[i];",
91
 
                                      "For i = 0 To [end] - 1\n" +
92
 
                                      "\tb(i) = s(i)\n" +
93
 
                                      "Next");
94
 
                }
95
 
                [Test]
96
 
                public void ForWithBlock()
97
 
                {
98
 
                        TestStatement("for (i = 0; i < end; i++) { b[i] = s[i]; }",
99
 
                                      "For i = 0 To [end] - 1\n" +
100
 
                                      "\tb(i) = s(i)\n" +
101
 
                                      "Next");
102
 
                }
103
 
                
104
 
                [Test]
105
 
                public void AddEventHandler()
106
 
                {
107
 
                        TestStatement("this.button1.Click += new System.EventHandler(this.OnButton1Click);",
108
 
                                      "AddHandler Me.button1.Click, AddressOf Me.OnButton1Click");
109
 
                }
110
 
                
111
 
                [Test]
112
 
                public void RemoveEventHandler()
113
 
                {
114
 
                        TestStatement("this.button1.Click -= new System.EventHandler(this.OnButton1Click);",
115
 
                                      "RemoveHandler Me.button1.Click, AddressOf Me.OnButton1Click");
116
 
                }
117
 
                
118
 
                [Test]
119
 
                public void RaiseEvent()
120
 
                {
121
 
                        TestStatement("if (MyEvent != null) MyEvent(this, EventArgs.Empty);",
122
 
                                      "RaiseEvent MyEvent(Me, EventArgs.Empty)");
123
 
                        TestStatement("if (null != MyEvent) { MyEvent(this, EventArgs.Empty); }",
124
 
                                      "RaiseEvent MyEvent(Me, EventArgs.Empty)");
125
 
                }
126
 
                
127
 
                [Test]
128
 
                public void IfStatementSimilarToRaiseEvent()
129
 
                {
130
 
                        TestStatement("if (FullImage != null) DrawImage();",
131
 
                                      "If FullImage IsNot Nothing Then\n" +
132
 
                                      "\tDrawImage()\n" +
133
 
                                      "End If");
134
 
                        // regression test:
135
 
                        TestStatement("if (FullImage != null) e.DrawImage();",
136
 
                                      "If FullImage IsNot Nothing Then\n" +
137
 
                                      "\te.DrawImage()\n" +
138
 
                                      "End If");
139
 
                        // with braces:
140
 
                        TestStatement("if (FullImage != null) { DrawImage(); }",
141
 
                                      "If FullImage IsNot Nothing Then\n" +
142
 
                                      "\tDrawImage()\n" +
143
 
                                      "End If");
144
 
                        TestStatement("if (FullImage != null) { e.DrawImage(); }",
145
 
                                      "If FullImage IsNot Nothing Then\n" +
146
 
                                      "\te.DrawImage()\n" +
147
 
                                      "End If");
148
 
                        // another bug related to the IfStatement code:
149
 
                        TestStatement("if (Tiles != null) foreach (Tile t in Tiles) this.TileTray.Controls.Remove(t);",
150
 
                                      "If Tiles IsNot Nothing Then\n" +
151
 
                                      "\tFor Each t As Tile In Tiles\n" +
152
 
                                      "\t\tMe.TileTray.Controls.Remove(t)\n" +
153
 
                                      "\tNext\n" +
154
 
                                      "End If");
155
 
                }
156
 
                
157
 
                [Test]
158
 
                public void AnonymousMethod()
159
 
                {
160
 
                        TestMember("void A() { someEvent += delegate(int argument) { return argument * 2; }; }",
161
 
                                   "Private Sub A()\n" +
162
 
                                   "\tAddHandler someEvent, AddressOf ConvertedAnonymousMethod1\n" +
163
 
                                   "End Sub\n" +
164
 
                                   "Private Sub ConvertedAnonymousMethod1(ByVal argument As Integer)\n" +
165
 
                                   "\tReturn argument * 2\n" +
166
 
                                   "End Sub");
167
 
                }
168
 
                
169
 
                [Test]
170
 
                public void AnonymousMethodInVarDeclaration()
171
 
                {
172
 
                        TestMember("void A() { SomeDelegate i = delegate(int argument) { return argument * 2; }; }",
173
 
                                   "Private Sub A()\n" +
174
 
                                   "\tDim i As SomeDelegate = AddressOf ConvertedAnonymousMethod1\n" +
175
 
                                   "End Sub\n" +
176
 
                                   "Private Sub ConvertedAnonymousMethod1(ByVal argument As Integer)\n" +
177
 
                                   "\tReturn argument * 2\n" +
178
 
                                   "End Sub");
179
 
                }
180
 
                
181
 
                [Test]
182
 
                public void RegisterEvent()
183
 
                {
184
 
                        TestStatement("someEvent += tmp2;",
185
 
                                      "AddHandler someEvent, AddressOf tmp2");
186
 
                        TestStatement("someEvent += this.tmp2;",
187
 
                                      "AddHandler someEvent, AddressOf tmp2");
188
 
                        TestStatement("someEvent += new SomeDelegate(tmp2);",
189
 
                                      "AddHandler someEvent, AddressOf tmp2");
190
 
                        TestStatement("someEvent += new SomeDelegate(this.tmp2);",
191
 
                                      "AddHandler someEvent, AddressOf tmp2");
192
 
                }
193
 
                
194
 
                [Test]
195
 
                public void StaticMethod()
196
 
                {
197
 
                        TestMember("static void A() {}",
198
 
                                   "Private Shared Sub A()\nEnd Sub");
199
 
                }
200
 
                
201
 
                [Test]
202
 
                public void PInvoke()
203
 
                {
204
 
                        TestMember("[DllImport(\"user32.dll\", CharSet = CharSet.Auto)]\n" +
205
 
                                   "public static extern int MessageBox(IntPtr hwnd, string t, string caption, UInt32 t2);",
206
 
                                   "<DllImport(\"user32.dll\", CharSet := CharSet.Auto)> _\n" +
207
 
                                   "Public Shared Function MessageBox(ByVal hwnd As IntPtr, ByVal t As String, ByVal caption As String, ByVal t2 As UInt32) As Integer\n" +
208
 
                                   "End Function");
209
 
                        
210
 
                        TestMember("[DllImport(\"user32.dll\", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]\n" +
211
 
                                   "public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, UIntPtr wParam, IntPtr lParam);",
212
 
                                   "Public Declare Ansi Function SendMessage Lib \"user32.dll\" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As UIntPtr, ByVal lParam As IntPtr) As IntPtr");
213
 
                        
214
 
                        TestMember("[DllImport(\"user32.dll\", SetLastError = true, ExactSpelling = true, EntryPoint = \"SendMessageW\")]\n" +
215
 
                                   "public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, UIntPtr wParam, IntPtr lParam);",
216
 
                                   "Public Declare Auto Function SendMessage Lib \"user32.dll\" Alias \"SendMessageW\" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As UIntPtr, ByVal lParam As IntPtr) As IntPtr");
217
 
                }
218
 
                
219
 
                [Test]
220
 
                public void Constructor()
221
 
                {
222
 
                        TestMember("public tmp1() : base(1) { }",
223
 
                                   "Public Sub New()\n\tMyBase.New(1)\nEnd Sub");
224
 
                        TestMember("public tmp1() : this(1) { }",
225
 
                                   "Public Sub New()\n\tMe.New(1)\nEnd Sub");
226
 
                }
227
 
                
228
 
                [Test]
229
 
                public void Destructor()
230
 
                {
231
 
                        TestMember("~tmp1() { Dead(); }",
232
 
                                   "Protected Overrides Sub Finalize()\n" +
233
 
                                   "\tTry\n" +
234
 
                                   "\t\tDead()\n" +
235
 
                                   "\tFinally\n" +
236
 
                                   "\t\tMyBase.Finalize()\n" +
237
 
                                   "\tEnd Try\n" +
238
 
                                   "End Sub");
239
 
                }
240
 
                
241
 
                [Test]
242
 
                public void RenameConflictingNames()
243
 
                {
244
 
                        TestMember("int count;" +
245
 
                                   "public int Count { get { return count; } }" +
246
 
                                   "void Test1(int count) { count = 3; }" +
247
 
                                   "void Test2() { int count; count = 3; }" +
248
 
                                   "void Test3() { foreach (int count in someList) { count = 3; } }",
249
 
                                   
250
 
                                   "Private m_count As Integer\n" +
251
 
                                   "Public ReadOnly Property Count() As Integer\n" +
252
 
                                   "\tGet\n" +
253
 
                                   "\t\tReturn m_count\n" +
254
 
                                   "\tEnd Get\n" +
255
 
                                   "End Property\n" +
256
 
                                   "Private Sub Test1(ByVal count As Integer)\n" +
257
 
                                   "\tcount = 3\n" +
258
 
                                   "End Sub\n" +
259
 
                                   "Private Sub Test2()\n" +
260
 
                                   "\tDim count As Integer\n" +
261
 
                                   "\tcount = 3\n" +
262
 
                                   "End Sub\n" +
263
 
                                   "Private Sub Test3()\n" +
264
 
                                   "\tFor Each count As Integer In someList\n" +
265
 
                                   "\t\tcount = 3\n" +
266
 
                                   "\tNext\n" +
267
 
                                   "End Sub");
268
 
                }
269
 
        }
270
 
}