1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
|
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.IO;
using ICSharpCode.NRefactory.VB.Ast;
using ICSharpCode.NRefactory.VB.Parser;
using ICSharpCode.NRefactory.VB.PrettyPrinter;
using ICSharpCode.NRefactory.VB.Visitors;
using NUnit.Framework;
namespace ICSharpCode.NRefactory.VB.Tests.PrettyPrinter
{
[TestFixture]
public class VBNetOutputTest
{
void TestProgram(string program)
{
VBParser parser = ParserFactory.CreateParser(new StringReader(program));
parser.Parse();
Assert.AreEqual("", parser.Errors.ErrorOutput);
VBNetOutputVisitor outputVisitor = new VBNetOutputVisitor();
outputVisitor.Options.OutputByValModifier = true;
outputVisitor.VisitCompilationUnit(parser.CompilationUnit, null);
Assert.AreEqual("", outputVisitor.Errors.ErrorOutput);
Assert.AreEqual(StripWhitespace(program), StripWhitespace(outputVisitor.Text));
}
string StripWhitespace(string text)
{
text = text.Trim().Replace("\t", "").Replace("\r", "").Replace("\n", " ").Replace(" ", " ");
while (text.Contains(" ")) {
text = text.Replace(" ", " ");
}
return text;
}
void TestTypeMember(string program)
{
TestProgram("Class A\n" + program + "\nEnd Class");
}
void TestStatement(string statement)
{
TestTypeMember("Sub Method()\n" + statement + "\nEnd Sub");
}
void TestExpression(string expression)
{
VBParser parser = ParserFactory.CreateParser(new StringReader(expression));
Expression e = parser.ParseExpression();
Assert.AreEqual("", parser.Errors.ErrorOutput);
VBNetOutputVisitor outputVisitor = new VBNetOutputVisitor();
e.AcceptVisitor(outputVisitor, null);
Assert.AreEqual("", outputVisitor.Errors.ErrorOutput);
Assert.AreEqual(StripWhitespace(expression), StripWhitespace(outputVisitor.Text));
}
[Test]
public void Field()
{
TestTypeMember("Private a As Integer");
}
[Test]
public void Method()
{
TestTypeMember("Sub Method()\nEnd Sub");
}
[Test]
public void EnumWithBaseType()
{
TestProgram("Public Enum Foo As UShort\nEnd Enum");
}
[Test]
public void PartialModifier()
{
TestProgram("Public Partial Class Foo\nEnd Class");
}
[Test]
public void MustInheritClass()
{
TestProgram("Public MustInherit Class Foo\nEnd Class");
}
[Test]
public void GenericClassDefinition()
{
TestProgram("Public Class Foo(Of T As {IDisposable, ICloneable})\nEnd Class");
}
[Test]
public void GenericClassDefinitionWithBaseType()
{
TestProgram("Public Class Foo(Of T As IDisposable)\nInherits BaseType\nEnd Class");
}
[Test]
public void GenericMethodDefinition()
{
TestTypeMember("Public Sub Foo(Of T As {IDisposable, ICloneable})(ByVal arg As T)\nEnd Sub");
}
[Test]
public void ArrayRank()
{
TestStatement("Dim a As Object(,,)");
}
[Test]
public void ArrayInitialization()
{
TestStatement("Dim a As Object() = New Object(10) {}");
TestTypeMember("Private MultiDim As Integer(,) = {{1, 2}, {1, 3}}");
TestExpression("New Integer(, ) {{1, 1}, {1, 1}}");
TestTypeMember("Private _titles As String() = New String() {}");
}
[Test]
public void MethodCallWithOptionalArguments()
{
TestExpression("M(, )");
}
[Test]
public void IfStatement()
{
TestStatement("If a Then\n" +
"\tm1()\n" +
"ElseIf b Then\n" +
"\tm2()\n" +
"Else\n" +
"\tm3()\n" +
"End If");
}
[Test]
public void ForNextLoop()
{
TestStatement("For i = 0 To 10\n" +
"Next");
TestStatement("For i As Long = 10 To 0 Step -1\n" +
"Next");
}
[Test]
public void DoLoop()
{
TestStatement("Do\n" +
"Loop");
TestStatement("Do\n" +
"Loop While Not (i = 10)");
}
[Test]
public void SelectCase()
{
TestStatement(@"Select Case i
Case 0
Case 1 To 4
Case Else
End Select");
}
[Test]
public void UsingStatement()
{
TestStatement(@"Using nf As New Font(), nf2 As New List(Of Font)(), nf3 = Nothing
Bla(nf)
End Using");
}
[Test]
public void UntypedVariable()
{
TestStatement("Dim x = 0");
}
[Test]
public void UntypedField()
{
TestTypeMember("Dim x = 0");
}
[Test]
public void Assignment()
{
TestExpression("a = b");
}
[Test]
public void SpecialIdentifiers()
{
// Assembly, Ansi and Until are contextual keywords
// Custom is valid inside methods, but not valid for field names
TestExpression("Assembly = Ansi * [For] + Until - [Custom]");
}
[Test]
public void DictionaryAccess()
{
TestExpression("c!key");
}
[Test]
public void GenericMethodInvocation()
{
TestExpression("GenericMethod(Of T)(arg)");
}
[Test]
public void SpecialIdentifierName()
{
TestExpression("[Class]");
}
[Test]
public void GenericDelegate()
{
TestProgram("Public Delegate Function Predicate(Of T)(ByVal item As T) As String");
}
[Test]
public void Enum()
{
TestProgram("Enum MyTest\nRed\n Green\n Blue\nYellow\n End Enum");
}
[Test]
public void EnumWithInitializers()
{
TestProgram("Enum MyTest\nRed = 1\n Green = 2\n Blue = 4\n Yellow = 8\n End Enum");
}
[Test]
public void SyncLock()
{
TestStatement("SyncLock a\nWork()\nEnd SyncLock");
}
[Test]
public void Using()
{
TestStatement("Using a As New A()\na.Work()\nEnd Using");
}
[Test]
public void Cast()
{
TestExpression("CType(a, T)");
}
[Test]
public void DirectCast()
{
TestExpression("DirectCast(a, T)");
}
[Test]
public void TryCast()
{
TestExpression("TryCast(a, T)");
}
[Test]
public void PrimitiveCast()
{
TestExpression("CStr(a)");
}
[Test]
public void TypeOfIs()
{
TestExpression("TypeOf a Is String");
}
[Test]
public void PropertyWithAccessorAccessModifiers()
{
TestTypeMember("Public Property ExpectsValue() As Boolean\n" +
"\tPublic Get\n" +
"\tEnd Get\n" +
"\tProtected Set\n" +
"\tEnd Set\n" +
"End Property");
}
[Test]
public void AutoProperty()
{
TestTypeMember("Public Property Value()");
TestTypeMember("Public Property Value() As Integer");
TestTypeMember("Public Property Value() As Integer = 5");
TestTypeMember("Public Property Value() As New List()");
}
[Test]
public void AbstractProperty()
{
TestTypeMember("Public MustOverride Property ExpectsValue() As Boolean");
TestTypeMember("Public MustOverride ReadOnly Property ExpectsValue() As Boolean");
TestTypeMember("Public MustOverride WriteOnly Property ExpectsValue() As Boolean");
}
[Test]
public void AbstractMethod()
{
TestTypeMember("Public MustOverride Sub Run()");
TestTypeMember("Public MustOverride Function Run() As Boolean");
}
[Test]
public void InterfaceImplementingMethod()
{
TestTypeMember("Public Sub Run() Implements SomeInterface.Run\nEnd Sub");
TestTypeMember("Public Function Run() As Boolean Implements SomeInterface.Bla\nEnd Function");
}
[Test]
public void NamedAttributeArgument()
{
TestProgram("<Attribute(ArgName := \"value\")> _\n" +
"Class Test\n" +
"End Class");
}
[Test]
public void ReturnTypeAttribute()
{
TestTypeMember("Function A() As <Attribute> String\n" +
"End Function");
}
[Test]
public void AssemblyAttribute()
{
TestProgram("<Assembly: CLSCompliant>");
}
[Test]
public void ModuleAttribute()
{
TestProgram("<Module: SuppressMessageAttribute>");
}
[Test]
public void Interface()
{
TestProgram("Interface ITest\n" +
"Property GetterAndSetter() As Boolean\n" +
"ReadOnly Property GetterOnly() As Boolean\n" +
"WriteOnly Property SetterOnly() As Boolean\n" +
"Sub InterfaceMethod()\n" +
"Function InterfaceMethod2() As String\n" +
"End Interface");
}
[Test]
public void OnErrorStatement()
{
TestStatement("On Error Resume Next");
}
[Test]
public void OverloadedConversionOperators()
{
TestTypeMember("Public Shared Narrowing Operator CType(ByVal xmlNode As XmlNode) As TheBug\nEnd Operator");
TestTypeMember("Public Shared Widening Operator CType(ByVal bugNode As TheBug) As XmlNode\nEnd Operator");
}
[Test]
public void OverloadedTrueFalseOperators()
{
TestTypeMember("Public Shared Operator IsTrue(ByVal a As TheBug) As Boolean\nEnd Operator");
TestTypeMember("Public Shared Operator IsFalse(ByVal a As TheBug) As Boolean\nEnd Operator");
}
[Test]
public void OverloadedOperators()
{
TestTypeMember("Public Shared Operator +(ByVal bugNode As TheBug, ByVal bugNode2 As TheBug) As TheBug\nEnd Operator");
TestTypeMember("Public Shared Operator >>(ByVal bugNode As TheBug, ByVal b As Integer) As TheBug\nEnd Operator");
}
[Test]
public void AttributeOnParameter()
{
TestTypeMember("Sub Main(ByRef one As Integer, ByRef two As Integer, <Out> ByRef three As Integer)\nEnd Sub");
}
[Test]
public void FieldWithoutType()
{
TestTypeMember("Dim X");
}
[Test]
public void UsingStatementForExistingVariable()
{
TestStatement("Using obj\nEnd Using");
}
[Test]
public void ContinueFor()
{
TestStatement("Continue For");
}
[Test]
public void ForNextStatementWithFieldLoopVariable()
{
TestStatement("For Me.Field = 0 To 10\n" +
"Next Me.Field");
}
[Test]
public void WithStatement()
{
TestStatement("With Ejes\n" +
"\t.AddLine(New Point(Me.ClientSize.Width / 2, 0), (New Point(Me.ClientSize.Width / 2, Me.ClientSize.Height)))\n" +
"End With");
}
[Test]
public void NewConstraint()
{
TestProgram("Public Class Rational(Of T, O As {IRationalMath(Of T), New})\nEnd Class");
}
[Test]
public void StructConstraint()
{
TestProgram("Public Class Rational(Of T, O As {IRationalMath(Of T), Structure})\nEnd Class");
}
[Test]
public void ClassConstraint()
{
TestProgram("Public Class Rational(Of T, O As {IRationalMath(Of T), Class})\nEnd Class");
}
[Test]
public void Integer()
{
TestExpression("16");
}
[Test]
public void Double()
{
TestExpression("1.0");
}
[Test]
public void HexadecimalInteger()
{
TestExpression("&H10");
}
[Test]
public void HexadecimalMinusOne()
{
TestExpression("&Hffffffff");
}
[Test]
public void TypeCharacters()
{
TestExpression("347S");
TestExpression("347L");
TestExpression("347D");
TestExpression("347F");
TestExpression("347US");
TestExpression("347UI");
TestExpression("347UL");
TestExpression("\".\"C");
}
[Test]
public void AddressOf()
{
TestExpression("AddressOf Abc");
}
[Test]
public void ChainedConstructorCall()
{
TestExpression("MyBase.New()");
TestExpression("Me.New()");
TestExpression("MyClass.New()");
}
[Test]
public void NewMethodCall()
{
TestExpression("something.[New]()");
}
[Test]
public void ObjectInitializer()
{
TestExpression("New StringWriter() With { _\n" +
" .NewLine = Environment.NewLine, _\n" +
" .Encoding = Encoding.UTF8 _\n" +
"}");
}
[Test]
public void EventDefinition()
{
TestTypeMember("Public Event MyEvent(ByVal sender As Object)");
}
[Test]
public void Options()
{
TestProgram("Option Strict On\n" +
"Option Explicit On\n" +
"Option Infer On\n" +
"Option Compare Text");
}
[Test]
public void UntypedForeach()
{
TestStatement("For Each x In myGuidArray\nNext");
}
[Test]
public void MethodDefinitionWithOptionalParameter()
{
TestTypeMember("Sub M(Optional ByVal msg As String = Nothing, Optional ByRef output As String = Nothing)\nEnd Sub");
}
[Test]
public void Module()
{
TestProgram("Module Test\n" +
" Sub M()\n" +
" End Sub\n" +
"End Module");
}
[Test]
public void WithEvents()
{
TestTypeMember("Dim WithEvents a As Button");
}
[Test]
public void FriendWithEventsField()
{
TestTypeMember("Friend WithEvents Button1 As System.Windows.Forms.Button");
}
[Test]
public void SimpleFunctionLambda()
{
TestExpression("Function(x) x * x");
}
[Test]
public void SimpleFunctionLambdaWithType()
{
TestExpression("Function(x As Integer) x * x");
}
[Test]
public void SimpleSubLambdaWithType()
{
TestExpression("Sub(x As Integer) Console.WriteLine(x)");
}
[Test]
public void BlockSubLambdaWithType()
{
TestExpression("Sub(x As Integer)\n" +
" Console.WriteLine(x)\n" +
"End Sub");
}
[Test]
public void BlockFunctionLambdaWithType()
{
TestExpression("Function(x As Integer) As Integer\n" +
" If x < 2 Then\n" +
" Return x\n" +
" End If\n" +
" Return x * x\n" +
"End Function");
}
[Test]
public void XmlSimple()
{
TestExpression("<?xml?>\n" +
"<!-- test -->\n" +
"<Test>\n" +
" <A />\n" +
" <B test='a' <%= test %> />\n" +
"</Test>");
}
[Test]
public void XmlNested()
{
TestExpression(@"<menu>
<course name=""appetizer"">
<dish>Shrimp Cocktail</dish>
<dish>Escargot</dish>
</course>
<course name=""main"">
<dish>Filet Mignon</dish>
<dish>Garlic Potatoes</dish>
<dish>Broccoli</dish>
</course>
<course name=""dessert"">
<dish>Chocolate Cheesecake</dish>
</course>
</menu>");
}
[Test]
public void XmlDocument()
{
TestExpression(@"<?xml version=""1.0""?>
<menu>
<course name=""appetizer"">
<dish>Shrimp Cocktail</dish>
<dish>Escargot</dish>
</course>
</menu>");
}
[Test]
public void XmlNestedWithExpressions()
{
TestExpression(@"<?xml version=""1.0""?>
<menu>
<course name=""appetizer"">
<%= From m In menu _
Where m.Course = ""appetizer"" _
Select <dish><%= m.Food %></dish> %>
</course>
<course name=""main"">
<%= From m In menu _
Where m.Course = ""main"" _
Select <dish><%= m.Food %></dish> %>
</course>
<course name=""dessert"">
<%= From m In menu _
Where m.Course = ""dessert"" _
Select <dish><%= m.Food %></dish> %>
</course>
</menu>");
}
[Test]
public void XmlAccessExpressions()
{
TestExpression("xml.<menu>.<course>");
TestExpression("xml...<course>");
TestExpression("xml...<course>(2)");
TestExpression("item.@name");
}
}
}
|