PageRenderTime 76ms CodeModel.GetById 27ms app.highlight 39ms RepoModel.GetById 2ms app.codeStats 0ms

/Src/Compilers/CSharp/Test/Syntax/Parsing/RoundTrippingTests.cs

https://github.com/EkardNT/Roslyn
C# | 1685 lines | 1549 code | 126 blank | 10 comment | 5 complexity | fc97f378ef19479f3acf59883f6d4524 MD5 | raw file
   1// Copyright (c) Microsoft Open Technologies, Inc.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
   2
   3using System.Linq;
   4using Microsoft.CodeAnalysis.CSharp.Symbols;
   5using Microsoft.CodeAnalysis.CSharp.Syntax;
   6using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
   7using Microsoft.CodeAnalysis.Text;
   8using Roslyn.Test.Utilities;
   9using Xunit;
  10
  11namespace Microsoft.CodeAnalysis.CSharp.UnitTests
  12{
  13    public class RoundTrippingTests
  14    {
  15        #region Helper
  16
  17        internal static void ParseAndRoundTripping(string text, int errorCount = 0, int memberCount = 0)
  18        {
  19            ParseAndRoundTripping(text, TestOptions.RegularWithDocumentationComments, errorCount, memberCount);
  20        }
  21
  22        internal static void ParseAndRoundTripping(string text, CSharpParseOptions options, int errorCount = 0, int memberCount = 0)
  23        {
  24            var tree = SyntaxFactory.ParseSyntaxTree(SourceText.From(text), options);
  25            var toText = tree.GetCompilationUnitRoot().ToFullString();
  26
  27            Assert.Equal(text, toText);
  28
  29            // -1 mean there are errors but actual number of errors is not important.
  30            // it makes the test more robust in case error count changes
  31            if (errorCount == -1)
  32            {
  33                Assert.NotEqual(0, tree.GetCompilationUnitRoot().ErrorsAndWarnings().Length);
  34            }
  35            else
  36            {
  37                Assert.Equal(errorCount, tree.GetCompilationUnitRoot().ErrorsAndWarnings().Length);
  38            }
  39
  40            // check member count only if > 0
  41            if (memberCount > 0)
  42            {
  43                Assert.Equal(memberCount, tree.GetCompilationUnitRoot().Members.Count);
  44            }
  45
  46            ParentChecker.CheckParents(tree.GetCompilationUnitRoot(), tree);
  47        }
  48
  49        public static void ParseAndCheckTerminalSpans(string text)
  50        {
  51            var tree = SyntaxFactory.ParseSyntaxTree(text);
  52            var toText = tree.GetCompilationUnitRoot().ToFullString();
  53            Assert.Equal(text, toText);
  54
  55            var nodes = tree.GetCompilationUnitRoot().DescendantTokens(tk => tk.FullWidth > 0).ToList();
  56            if (nodes.Count > 0)
  57            {
  58                var prevSpan = nodes[0].FullSpan;
  59                for (int i = 1; i < nodes.Count; i++)
  60                {
  61                    var span = nodes[i].FullSpan;
  62                    Assert.Equal(prevSpan.End, span.Start);
  63                    prevSpan = span;
  64                }
  65            }
  66        }
  67
  68        #endregion
  69
  70        [Fact]
  71        public void AutoPropInitializers()
  72        {
  73            var experimental = TestOptions.ExperimentalParseOptions;
  74            ParseAndRoundTripping("class C { int GetInt { get; } = 0; }", experimental, memberCount: 1);
  75            ParseAndRoundTripping("class C { int GetInt { get; } = 0 }", experimental, 1, 1);
  76            ParseAndRoundTripping("class C { public int GetInt { get; } = 0; }", experimental, memberCount: 1);
  77            ParseAndRoundTripping("class C { int GetInt { get; } = 0;; }", experimental, 1, 1);
  78            ParseAndRoundTripping("class C { int GetInt { get;; } = 0;; }", experimental, 2, 1);
  79            ParseAndRoundTripping("interface I { int GetInt { get; } = 0; }", experimental, memberCount: 1);
  80            ParseAndRoundTripping("interface I { int GetInt { get; } = 0 }", experimental, 1, 1);
  81            ParseAndRoundTripping("interface I { public int GetInt { get; } = 0; }", experimental, memberCount: 1);
  82        }
  83
  84        [Fact()]
  85        [WorkItem(530410, "DevDiv")]
  86        public void NullChar()
  87        {
  88            ParseAndRoundTripping("\0", 1);
  89            ParseAndRoundTripping("abc\0def", 3);
  90            ParseAndRoundTripping("\0abc", 2);
  91            ParseAndRoundTripping("class C { string s = \"\0\"; }", 0);
  92        }
  93
  94        [Fact()]
  95        [WorkItem(530410, "DevDiv")]
  96        public void CharMaxValue()
  97        {
  98            string text = "abc" + char.MaxValue + "def";
  99            var tree = SyntaxFactory.ParseSyntaxTree(SourceText.From(text), path: "");
 100            var toText = tree.GetCompilationUnitRoot().ToFullString();
 101            Assert.Equal(text, toText);
 102        }
 103
 104        [Fact]
 105        public void TestOptionalFloat()
 106        {
 107            ParseAndRoundTripping(Resources.OptionalFloat);
 108        }
 109
 110        [Fact]
 111        public void TestOptionalParamsArray()
 112        {
 113            ParseAndRoundTripping(Resources.OptionalParamsArray);
 114        }
 115
 116        [WorkItem(862632, "DevDiv/Personal")]
 117        [Fact]
 118        public void TestNegInvalidExternAlias01()
 119        {
 120            ParseAndRoundTripping(Resources.InvalidExternAlias01, -1);
 121        }
 122
 123        [WorkItem(901348, "DevDiv/Personal")]
 124        [Fact]
 125        public void TestNegPartialAliasedName()
 126        {
 127            ParseAndRoundTripping(Resources.PartialAliasedName, -1);
 128        }
 129
 130        [WorkItem(894884, "DevDiv/Personal")]
 131        [Fact]
 132        public void TestNegPartialInKeyword()
 133        {
 134            ParseAndRoundTripping(Resources.PartialInKeyword, -1);
 135        }
 136
 137        [WorkItem(901493, "DevDiv/Personal")]
 138        [Fact]
 139        public void TestNegPartialAttribute()
 140        {
 141            // although this code snippet has multiple statements on top level we report that as semantic errors, not parse errors:
 142            ParseAndRoundTripping(Resources.PartialNewAttributeArray, 0);
 143        }
 144
 145        [WorkItem(901498, "DevDiv/Personal")]
 146        [Fact]
 147        public void TestNegPartialPreProcessorExpression()
 148        {
 149            ParseAndRoundTripping(Resources.PartialPreprocessorExpression, -1);
 150        }
 151
 152        [WorkItem(901508, "DevDiv/Personal")]
 153        [Fact]
 154        public void TestNegPartialUnicodeIdentifier()
 155        {
 156            ParseAndRoundTripping(Resources.PartialUnicodeIdentifier, -1);
 157        }
 158
 159        [WorkItem(901516, "DevDiv/Personal")]
 160        [Fact]
 161        public void TestNegIncompleteSwitchBlock()
 162        {
 163            ParseAndRoundTripping(Resources.PartialSwitchBlock, -1);
 164        }
 165
 166        [Fact]
 167        public void TestNegBug862116()
 168        {
 169            var text = @"
 170namespace x
 171{
 172    public class a
 173    {
 174        public int hiddenMember2;
 175    }
 176    public class b : a
 177    {
 178        public override int hiddenMember2
 179        {
 180        public static void Main()
 181        {
 182        }
 183    }
 184}
 185";
 186            ParseAndRoundTripping(text, -1);
 187        }
 188
 189        [Fact]
 190        public void TestNegBug862635()
 191        {
 192            var text = @"
 193class Test
 194{
 195    static void Main() 
 196    {
 197        ::Console.WriteLine(""Missing identifier before :: "");
 198    }
 199}
 200";
 201            ParseAndRoundTripping(text, -1);
 202        }
 203
 204        [Fact]
 205        public void Bug862637()
 206        {
 207            var text = @"
 208using System;
 209public class Test
 210{
 211    static void Main()
 212    {
 213         Console.WriteLine(dDep.GetType().Assembly.FullName);
 214    }
 215}
 216";
 217            ParseAndRoundTripping(text);
 218        }
 219
 220        [Fact]
 221        public void Bug862640()
 222        {
 223            var text = @"
 224public class Production
 225{
 226    public Production()
 227    {
 228        ((VoidDelegate)delegate
 229        {
 230            this.someType.Iterate(delegate(object o)
 231            {
 232                System.Console.WriteLine(((BoolDelegate)delegate { return object.Equals(o, this.epsilon); })());
 233            });
 234        })();
 235    }
 236}
 237";
 238            ParseAndRoundTripping(text);
 239        }
 240
 241        [Fact]
 242        public void TestNegBug862642()
 243        {
 244            var text = @"
 245alias myAlias;
 246class myClass
 247{
 248}
 249";
 250            // top-level field declaration is a semantic error
 251            ParseAndRoundTripping(text);
 252        }
 253
 254        [Fact]
 255        public void TestNegBug862643()
 256        {
 257            var text = @"
 258using System;
 259[AttributeUsage(AttributeTargets.All)]
 260public class Foo : Attribute
 261    {
 262    public int Name;
 263    public Foo (int sName) {Name = sName;}
 264    }
 265public class Class1 {
 266 int Meth2 ([event:Foo(5)]int parm) {return 0;}
 267 public int IP { get {return 0;} set {}}
 268}
 269";
 270            ParseAndRoundTripping(text);
 271        }
 272
 273        [Fact]
 274        public void Bug862644()
 275        {
 276            var text = @"
 277using System;
 278public class Test
 279{
 280 [method:MyAttribute(TypeObject = new int[1].GetType())]
 281 public void foo()
 282 {
 283 }
 284}
 285";
 286            ParseAndRoundTripping(text);
 287        }
 288
 289        [Fact]
 290        public void Bug862646()
 291        {
 292            var text = @"
 293// C# compiler emits Void& type
 294";
 295            ParseAndRoundTripping(text);
 296        }
 297
 298        [Fact]
 299        public void Bug862648()
 300        {
 301            var text = @"
 302class TestClass
 303{
 304    static void Main()
 305    {
 306       int partial;
 307    }
 308}
 309";
 310            ParseAndRoundTripping(text);
 311        }
 312
 313        [Fact]
 314        public void Bug870754()
 315        {
 316            var text = @"class C{
 317C(){
 318int y = 3;
 319(y).ToString();
 320}
 321}
 322";
 323            ParseAndRoundTripping(text);
 324        }
 325
 326        [Fact]
 327        public void TestNegBug875711()
 328        {
 329            var text = @"
 330using System;
 331public class A
 332{
 333 public static operator ++ A(int i)
 334 {
 335 }
 336}
 337";
 338            ParseAndRoundTripping(text, -1);
 339        }
 340
 341        [Fact]
 342        public void TestNegBug876359()
 343        {
 344            var text = @"
 345class C {
 346  fixed x;
 347}
 348";
 349            ParseAndRoundTripping(text, -1);
 350        }
 351
 352        [Fact]
 353        public void TestNegBug876363()
 354        {
 355            var text = @"
 356class X { void f() {
 357int a = 1; \u000a int b = (int)2.3;
 358}
 359}
 360";
 361            ParseAndRoundTripping(text, -1);
 362        }
 363
 364        [Fact]
 365        public void Bug876565()
 366        {
 367            var text = @"
 368public class C
 369{
 370    public static int Main()
 371    {
 372        int result = 0;
 373        Func<int?, IStr0?> f1 = (int? x) => x;
 374    }
 375}
 376";
 377            ParseAndRoundTripping(text);
 378        }
 379
 380        [Fact]
 381        public void Bug876573()
 382        {
 383            var text = @"
 384[partial]
 385partial class partial { }
 386partial class partial
 387{
 388    public partial()
 389    {
 390        fld1 = fld2 = fld3 = fld4 = -1;
 391    }
 392}
 393";
 394            ParseAndRoundTripping(text);
 395        }
 396
 397        [Fact]
 398        public void TestNegBug876575()
 399        {
 400            var text = @"partial enum E{}";
 401            ParseAndRoundTripping(text, 1);
 402        }
 403
 404        [Fact]
 405        public void Bug877232()
 406        {
 407            var text = @"
 408class MyClass : MyBase {
 409 MyClass() : base() {
 410 }
 411}
 412";
 413            ParseAndRoundTripping(text);
 414        }
 415
 416        [Fact]
 417        public void TestNegBug877242()
 418        {
 419            var text = @"
 420private namespace test
 421{
 422}
 423";
 424            ParseAndRoundTripping(text, -1);
 425        }
 426
 427        [Fact]
 428        public void Bug877246()
 429        {
 430            var text = @"
 431public struct Test
 432{
 433    static int Main()
 434    {
 435        test.emptyStructGenNullableArr = new EmptyStructGen<string, EmptyClass>?[++i];
 436    }
 437}
 438";
 439            ParseAndRoundTripping(text);
 440        }
 441
 442        [Fact]
 443        public void Bug877251()
 444        {
 445            var text = @"
 446static class Test
 447{
 448    static void Main()
 449    {
 450        A a = new A { 5, { 1, 2, {1, 2} }, 3 };
 451    }
 452}
 453";
 454            ParseAndRoundTripping(text, -1);
 455        }
 456
 457        [Fact]
 458        public void TestNegBug877256()
 459        {
 460            var text = @"
 461using System;
 462public static class Extensions
 463{
 464 static ~Extensions(this Struct s) {}
 465 static ~Extensions(this Struct? s) {}
 466}
 467";
 468            ParseAndRoundTripping(text, -1);
 469        }
 470
 471        [Fact]
 472        public void Bug877313()
 473        {
 474            var text = @"
 475partial class ConstaintsDef
 476{
 477    partial void PM<T1, T2>(T1 v1, ref T2 v2, params T2[] v3)
 478        where T2 : new()
 479        where T1 : System.IComparable<T1>, System.Collections.IEnumerable;
 480    partial void PM<T1, T2>(T1 v1, ref T2 v2, params T2[] v3)
 481    {
 482        v2 = v3[0];
 483    }
 484}
 485";
 486            ParseAndRoundTripping(text);
 487        }
 488
 489        [Fact]
 490        public void TestNegBug877318()
 491        {
 492            var text = @"
 493partial class PartialPartial
 494{
 495    int i = 1;
 496    partial partial void PM();
 497    partial partial void PM()
 498    {
 499        i = 0;
 500    }
 501    static int Main()
 502    {
 503        PartialPartial t = new PartialPartial();
 504        t.PM();
 505        return t.i;
 506    }
 507}
 508";
 509            ParseAndRoundTripping(text, -1);
 510        }
 511
 512        [Fact]
 513        public void TestNegBug879395()
 514        {
 515            var text = @"
 516module m1
 517end module
 518";
 519            ParseAndRoundTripping(text, 2);
 520        }
 521
 522        [Fact]
 523        public void Bug880479()
 524        {
 525            var text = @"
 526class c1
 527{
 528void foo(int a, int b, int c)
 529{
 530}
 531}
 532";
 533            ParseAndCheckTerminalSpans(text);
 534        }
 535
 536        [Fact]
 537        public void TestNegBug881436()
 538        {
 539            var text = @"
 540public class Test
 541{
 542    public static void Main()
 543    {
 544        var v1 = new { X = x; Y = y, Z = z };
 545    }
 546}
 547";
 548            ParseAndRoundTripping(text, -1);
 549        }
 550
 551        [Fact]
 552        public void Bug881480()
 553        {
 554            var text = @"
 555// <Code> 
 556 
 557public static class SubGenericClass<T> : GenericClass<T>
 558{
 559    public SubGenericClass() : base() { }
 560}
 561";
 562            ParseAndRoundTripping(text);
 563        }
 564
 565        [Fact]
 566        public void Bug881485()
 567        {
 568            var text = @"
 569class Test
 570{
 571    public static int Test2()
 572    {
 573        var testResult = testFunc((long?)-1);
 574   }
 575}
 576";
 577            ParseAndRoundTripping(text);
 578        }
 579
 580        [Fact]
 581        public void TestNegBug881488()
 582        {
 583            var text = @"partial class A
 584{
 585       partial void C<T>(T )=>{ t) { }
 586}
 587";
 588            ParseAndRoundTripping(text, -1);
 589        }
 590
 591        [Fact]
 592        public void TestNegBug882388()
 593        {
 594            var text = @"
 595public class Class1
 596{
 597    public int Meth2(int i) {
 598        [return:Foo(5)]
 599        return 0;
 600    }
 601}
 602";
 603            ParseAndRoundTripping(text, -1);
 604        }
 605
 606        [Fact]
 607        public void Bug882417()
 608        {
 609            var text = @"
 610[AttributeUsage(AttributeTargets.Class)]
 611public class HelpAttribute : Attribute
 612{
 613 public HelpAttribute(byte b1) {
 614     b = b1;
 615 }
 616 byte b = 0;
 617 public byte Verify {get {return b;} }
 618}
 619";
 620            ParseAndRoundTripping(text);
 621        }
 622
 623        [Fact]
 624        public void TestNegBug882424()
 625        {
 626            var text = @"
 627public class MyClass {
 628 //invalid simple name
 629 int -foo(){
 630  return 1;
 631 }
 632}
 633";
 634            ParseAndRoundTripping(text, -1);
 635        }
 636
 637        [Fact]
 638        public void TestNegBug882432()
 639        {
 640            var text = @"
 641public class Base1 {
 642    public static E1 {a, b, c, };
 643}
 644";
 645            ParseAndRoundTripping(text, -1);
 646        }
 647
 648        [Fact]
 649        public void TestNegBug882444()
 650        {
 651            var text = @"
 652namespace nms {
 653public class MyException : ApplicationException {
 654    public MyException(String str) : base ApplicationException (str)
 655    {}
 656};
 657";
 658            ParseAndRoundTripping(text, -1, 1);
 659        }
 660
 661        [Fact]
 662        public void Bug882459()
 663        {
 664            var text = @"
 665public partial class Base
 666{
 667    ViolateClassConstraint Fld01 = new Base().Meth<ViolateClassConstraint>(new ViolateClassConstraint()); //E:CS0315
 668}
 669";
 670            ParseAndRoundTripping(text);
 671        }
 672
 673        [Fact]
 674        public void TestNegBug882463()
 675        {
 676            var text = @"
 677public class Test
 678{
 679 yield break;
 680 static int Main()
 681 { 
 682  return 1;
 683 }
 684}
 685";
 686            ParseAndRoundTripping(text, -1);
 687        }
 688
 689        [Fact]
 690        public void TestNegBug882465()
 691        {
 692            var text = @"
 693public class Comments
 694{
 695 // /* This is a comment 
 696    This is a comment 
 697 */
 698}
 699";
 700            ParseAndRoundTripping(text, -1);
 701        }
 702
 703        [Fact]
 704        public void Bug882471()
 705        {
 706            var text = @"
 707class Test
 708{
 709    static int Main()
 710    {
 711        string \u0069f;
 712    }
 713}
 714";
 715            ParseAndRoundTripping(text, 0, 1);
 716        }
 717
 718        [Fact]
 719        public void Bug882481()
 720        {
 721            var text = @"
 722#define \u0066oxbar
 723#if foxbar
 724class Foo { }
 725#endif
 726";
 727            ParseAndRoundTripping(text, 0, 1);
 728        }
 729
 730        [Fact]
 731        public void TestNegBug882482()
 732        {
 733            var text = @"
 734using System;
 735class main
 736{
 737 public static void Main()
 738 {
 739  i\u0066 (true)
 740   Console.WriteLine(""This should not have worked!"");
 741 }
 742}
 743";
 744            ParseAndRoundTripping(text, -1);
 745        }
 746
 747        [Fact]
 748        public void Bug882957()
 749        {
 750            var text = @"
 751partial class CNExp
 752{
 753    public static long? operator &(CNExp v1, long? v2)
 754    {
 755        return null;
 756    }
 757}
 758";
 759            ParseAndRoundTripping(text);
 760        }
 761
 762        [Fact]
 763        public void Bug882984()
 764        {
 765            var text = @"
 766unsafe partial class C 
 767{
 768    byte* buf;
 769    public byte* ubuf
 770    {
 771        set { buf = value;}
 772    }
 773}
 774";
 775            ParseAndRoundTripping(text);
 776        }
 777
 778        [Fact]
 779        public void Bug883177()
 780        {
 781            var text = @"
 782unsafe
 783struct Test
 784{
 785    public fixed int i[1];
 786    byte* myIntBuf;
 787    static int Main()
 788    {
 789        int retval = 0;
 790        Test t = new Test();
 791        t.i[0] = 0;
 792        t.myIntBuf = (byte*) t.i;
 793        if (*t.myIntBuf !=0)
 794            retval = 1;
 795        if (retval != 0)
 796            System.Console.WriteLine(""Failed."");
 797        return retval;
 798    }
 799}
 800";
 801            ParseAndRoundTripping(text);
 802        }
 803
 804        [Fact]
 805        [Trait("Regression", "Spans")]
 806        public void Bug884246()
 807        {
 808            var text = @"
 809using System.Reflection;
 810#if VER1
 811[assembly:AssemblyVersionAttribute(""1.0.0.0"")]
 812#elif VER2
 813[assembly:AssemblyVersionAttribute(""2.0.0.0"")]
 814#endif
 815public class otherClass{}";
 816
 817            ParseAndCheckTerminalSpans(text);
 818        }
 819
 820        [Fact]
 821        public void Bug890389()
 822        {
 823            var text = @"using System;
 824class C
 825{
 826void Foo()
 827{
 828Func<string> i = 3.ToString;
 829}
 830}
 831";
 832            ParseAndRoundTripping(text);
 833        }
 834
 835        [Fact]
 836        public void TestNegBug892249()
 837        {
 838            var text = @"using System;
 839class AAttribute : Attribute {
 840  public AAttribute(object o) { }
 841}
 842[A(new string[] is { ""hello"" })]
 843class C {
 844}
 845";
 846            ParseAndRoundTripping(text, -1);
 847        }
 848
 849        [Fact]
 850        public void TestNegBug892255()
 851        {
 852            var text = @"using System;
 853public class Test
 854{
 855    public static int Main(string [] args)
 856    {
 857  int ret = 1;
 858  switch (false) {
 859  case true:
 860   ret = 1;
 861   break;
 862  default false:
 863   ret = 1;
 864  }
 865        return(ret);
 866    }
 867}
 868";
 869            ParseAndRoundTripping(text, -1);
 870        }
 871
 872        [WorkItem(894494, "DevDiv/Personal")]
 873        [Fact]
 874        public void TestRegressNegExtraPublicKeyword()
 875        {
 876            var text = @"
 877using System;
 878using System.Collections.Generic;
 879using System.Linq;
 880using System.Text;
 881namespace ConsoleApplication1
 882{
 883 class Program
 884 {
 885  static void Main(string[] args)
 886  {
 887  }
 888 }
 889}
 890 
 891public class Class_1_L0
 892{
 893/";
 894            ParseAndRoundTripping(text, -1);
 895        }
 896
 897        [Fact]
 898        public void TestNegBug894884()
 899        {
 900            var text = @"
 901class C
 902{
 903public static int Main()
 904{
 905switch(str)
 906{ 
 907default:
 908List<in
 909";
 910            ParseAndRoundTripping(text, -1);
 911        }
 912
 913        [WorkItem(895762, "DevDiv/Personal")]
 914        [Fact]
 915        public void TestRegressInfiniteLoopXmlDoc()
 916        {
 917            var text = @"
 918public struct MemberClass<T>
 919{
 920/// <summary>
 921/// We use this to get the values we cannot get directly
 922/// </summary>
 923/// <param n";
 924
 925            ParseAndRoundTripping(text, -1);
 926        }
 927
 928        [Fact]
 929        public void Bug909041()
 930        {
 931            var text = @"
 932interface A
 933{
 934void M<T>(T t) where T : class;
 935}
 936";
 937            ParseAndRoundTripping(text);
 938        }
 939
 940        [Fact]
 941        public void Bug909041b()
 942        {
 943            var text = @"
 944public delegate void Del<T>(T t) where T : IEnumerable;
 945public class A {}
 946";
 947            ParseAndRoundTripping(text);
 948        }
 949
 950        [Fact]
 951        public void Bug909041c()
 952        {
 953            var text = @"
 954class A
 955{
 956static extern bool Bar<U>() where U : class;
 957}
 958";
 959            ParseAndRoundTripping(text);
 960        }
 961
 962        [Fact]
 963        public void Bug909045()
 964        {
 965            var text = @"
 966public class A
 967{
 968public void M<T, V>(T t, V v)
 969where T : new()
 970where V : class { }
 971}
 972";
 973            ParseAndRoundTripping(text);
 974        }
 975
 976        [WorkItem(909049, "DevDiv/Personal")]
 977        [WorkItem(911392, "DevDiv/Personal")]
 978        [Fact]
 979        public void RegressError4AttributeWithTarget()
 980        {
 981            var text = @"using System;
 982using System.Runtime.InteropServices;
 983
 984interface IFoo
 985{
 986    [return: MarshalAs(UnmanagedType.I2)]
 987    short M();
 988
 989    int Prop
 990    {
 991        [return: MarshalAs(UnmanagedType.I4)]
 992        get;
 993        [param: MarshalAs(UnmanagedType.I4)]
 994        set;
 995    }
 996
 997    long this[[MarshalAs(UnmanagedType.BStr)] string s]
 998    {
 999        [return: MarshalAs(UnmanagedType.I8)]
1000        get;
1001        set;
1002    }
1003}
1004
1005public class Foo
1006{
1007    public delegate void MyDelegate();
1008    public event MyDelegate eventMethod
1009    {
1010        [method: ComVisible(true)]
1011        add { }
1012        remove { }
1013    }
1014}";
1015            ParseAndRoundTripping(text);
1016        }
1017
1018        [Fact]
1019        public void Bug909063()
1020        {
1021            var text = @"
1022class A
1023{
1024public int i, j;
1025public static void Main()
1026{
1027var v = new A() { i=0, j=1, };
1028var vv = new[] { 1, 2, };
1029}
1030}
1031";
1032            ParseAndRoundTripping(text);
1033        }
1034
1035        [Fact]
1036        public void Bug909333()
1037        {
1038            var text = @"
1039extern alias libAlias;
1040class myClass
1041{
1042static int Main()
1043{
1044libAlias::otherClass oc = new libAlias.otherClass(); // '::' and '.'
1045return 0;
1046}
1047}
1048";
1049            ParseAndRoundTripping(text);
1050        }
1051
1052        [Fact]
1053        public void Bug909334()
1054        {
1055            var text = @"
1056class Test
1057{
1058unsafe static int Main()
1059{
1060global::System.Int32* p = stackalloc global::System.Int32[5];
1061return 0;
1062}
1063}
1064";
1065            ParseAndRoundTripping(text);
1066        }
1067
1068        [Fact]
1069        public void Bug909337()
1070        {
1071            var text = @"
1072using System.Collections;
1073public class Test<I> 
1074{
1075//ctor
1076public Test(I i)
1077{ 
1078}
1079// dtor
1080~Test() {}
1081}
1082";
1083            ParseAndRoundTripping(text);
1084        }
1085
1086        [Fact]
1087        public void Bug909338()
1088        {
1089            var text = @"
1090class Test
1091{
1092static void Main()
1093{
1094var v = typeof(void);
1095}
1096}
1097";
1098            ParseAndRoundTripping(text);
1099        }
1100
1101        [Fact]
1102        public void Bug909350()
1103        {
1104            var text = @"
1105[Author(""Brian Kernighan""), Author(""Dennis Ritchie""),] 
1106class Class1
1107{
1108}
1109enum E
1110{
1111    One, Two,
1112}
1113";
1114            ParseAndRoundTripping(text);
1115        }
1116
1117        [Fact]
1118        public void Bug909371()
1119        {
1120            var text = @"
1121class A
1122{
1123byte M(byte b) 
1124{ 
1125return b; 
1126}
1127}
1128";
1129            ParseAndRoundTripping(text);
1130        }
1131
1132        [Fact]
1133        public void Bug909371b()
1134        {
1135            var text = @"
1136[AttributeUsage(AttributeTargets.Class)]
1137public class HelpAttribute : Attribute
1138{
1139byte b = 0;
1140public HelpAttribute(byte b1) {
1141b = b1;
1142}
1143}
1144";
1145            ParseAndRoundTripping(text);
1146        }
1147
1148        [WorkItem(909372, "DevDiv/Personal")]
1149        [Fact]
1150        public void RegressError4ValidOperatorOverloading()
1151        {
1152            var text = @"using System;
1153
1154public class A
1155{
1156    // unary
1157    public static bool operator true(A a)
1158    {
1159        return false;
1160    }
1161
1162    public static bool operator false(A a)
1163    {
1164        return true;
1165    }
1166
1167    public static A operator ++(A a)
1168    {
1169        return a;
1170    }
1171
1172    public static A operator --(A a)
1173    {
1174        return a;
1175    }
1176
1177    // binary
1178    public static bool operator <(A a, A b)
1179    {
1180        return true;
1181    }
1182    public static bool operator >(A a, A b)
1183    {
1184        return false;
1185    }
1186    public static bool operator <=(A a, A b)
1187    {
1188        return true;
1189    }
1190    public static bool operator >=(A a, A b)
1191    {
1192        return false;
1193    }
1194}";
1195            ParseAndRoundTripping(text);
1196        }
1197
1198        [Fact]
1199        public void Bug909419()
1200        {
1201            var text = @"
1202class Test
1203{
1204static int Main()
1205{
1206  int n1 = test is Test ? 0 : 1;
1207  int n2 = null == test as Test ? 0 : 1;
1208  return n1 + n2;
1209}
1210}
1211";
1212            ParseAndRoundTripping(text);
1213        }
1214
1215        [Fact]
1216        public void Bug909425()
1217        {
1218            var text = @"
1219public class MyClass
1220{
1221public static int Main() 
1222{
1223  float f1 = 0.7e-44f;
1224  double d1 = 5.0e-324;
1225  return M(0.0e+999);
1226}
1227static int M(double d) { return 0; }
1228}
1229";
1230            ParseAndRoundTripping(text);
1231        }
1232
1233        [WorkItem(909449, "DevDiv/Personal")]
1234        [Fact]
1235        public void RegressOverAggressiveWarningForUlongSuffix()
1236        {
1237            var text = @"class Program
1238{
1239static void Main()
1240{
1241ulong x1 = 7L;
1242// ulong x2 = 7l; // should Warn
1243ulong x4 = 7Ul; // should NOT warn
1244ulong x6 = 7ul; // should NOT warn
1245}
1246}";
1247            ParseAndRoundTripping(text);
1248        }
1249
1250        [Fact]
1251        public void Bug909451()
1252        {
1253            var text = @"
1254public class AnonymousTypeTest : ParentClass
1255{
1256public void Run()
1257{
1258var p1 = new { base.Number };
1259}
1260}
1261";
1262            ParseAndRoundTripping(text);
1263        }
1264
1265        [WorkItem(909828, "DevDiv/Personal")]
1266        [Fact]
1267        public void RegressError4ValidUlongLiteral()
1268        {
1269            var text = @"public class Test
1270{
1271public static int Main()
1272{
1273ulong n = 9223372036854775808U; //this should fit ulong
1274ulong n1 = 9223372036854775808Ul;
1275ulong n2 = 9223372036854775808uL;
1276ulong n3 = 9223372036854775808u;
1277return 0;
1278}
1279}
1280";
1281            ParseAndRoundTripping(text);
1282        }
1283
1284        [WorkItem(922886, "DevDiv/Personal")]
1285        [Fact]
1286        public void RegressError4ValidNumericLiteral()
1287        {
1288            var text = @"public class Test
1289{
1290public static void Main()
1291{
1292sbyte min1 = -128, max1 = 127;
1293short min2 = -32768, max2 = 32767;
1294int   min3 = -2147483648, max3 = 2147483647;
1295long  min4 = -9223372036854775808L, max4 = 9223372036854775807L;
1296byte max5 = 255;
1297ushort max6 = 65535;
1298uint max7 = 4294967295;
1299ulong max8 = 18446744073709551615;
1300}
1301}
1302";
1303            ParseAndRoundTripping(text);
1304        }
1305
1306        [WorkItem(911058, "DevDiv/Personal")]
1307        [Fact]
1308        public void RegressError4IdentifierStartWithAt1()
1309        {
1310            var text = @"using System;
1311
1312[AttributeUsage(AttributeTargets.All)]
1313public class X : Attribute { }
1314[@X]
1315class A { }
1316
1317namespace @namespace
1318{
1319    class C1 { }
1320    class @class : C1 { }
1321}
1322
1323namespace N2
1324{
1325    class Test
1326    {
1327        static int Main()
1328        {
1329            global::@namespace.@class c1 = new global::@namespace.@class();
1330            global::@namespace.C1 c2 = new global::@namespace.@C1();
1331
1332            return 0;
1333        }
1334    }
1335}";
1336            ParseAndRoundTripping(text);
1337        }
1338
1339        [WorkItem(911059, "DevDiv/Personal")]
1340        [Fact]
1341        public void RegressError4IdentifierStartWithAt2()
1342        {
1343            var text = @"public class A
1344        {
1345            public int @__namespace = 0;
1346
1347            class @public // ok
1348            {
1349                private void M(int @int) { } // error
1350            }
1351            internal class @private // error
1352            {
1353            }
1354        }";
1355
1356            ParseAndRoundTripping(text);
1357        }
1358
1359        [WorkItem(911418, "DevDiv/Personal")]
1360        [Fact]
1361        public void RegressNegNoError4InvalidAttributeTarget()
1362        {
1363            var text = @"using System;
1364using System.Reflection;
1365
1366public class foo 
1367{
1368    [method: method:A]
1369    public static void Main() 
1370    {
1371    }
1372}
1373[AttributeUsage(AttributeTargets.All,AllowMultiple=true)]
1374public class A : Attribute
1375{
1376}";
1377            ParseAndRoundTripping(text, -1);
1378
1379            // Assert.Equal((int)ErrorCode.ERR_SyntaxError, tree.Errors()[0].Code); // CS1003
1380            // Assert.Equal((int)ErrorCode.ERR_InvalidMemberDecl, tree.Errors()[1].Code); // CS1519
1381        }
1382
1383        [WorkItem(911477, "DevDiv/Personal")]
1384        [Fact]
1385        public void RegressWrongError4WarningExternOnCtor()
1386        {
1387            var text = @"using System;
1388public class C
1389{
1390    extern C();
1391    public static int Main()
1392    {
1393        return 1;
1394    }
1395}";
1396
1397            // The warning WRN_ExternCtorNoImplementation is given in semantic analysis.
1398            ParseAndRoundTripping(text); // , 1);
1399
1400            // Assert.Equal((int)ErrorCode.WRN_ExternCtorNoImplementation, tree.Errors()[0].Code); // W CS0824
1401        }
1402
1403        [WorkItem(911488, "DevDiv/Personal")]
1404        [Fact]
1405        public void RegressError4MemberOnSimpleTypeAsKeyword()
1406        {
1407            var text = @"using System;
1408public class Test
1409{
1410    public void M()
1411    {
1412               var v = int.MaxValue; // error
1413        bool b = false;
1414        string s = ""true"";
1415        Boolean.TryParse(s, out b); // ok
1416        bool.TryParse(s, out b);  // error
1417    }
1418}";
1419            ParseAndRoundTripping(text);
1420        }
1421
1422        [WorkItem(911505, "DevDiv/Personal")]
1423        [Fact]
1424        public void RegressWarning4EscapeCharInXmlDocAsText()
1425        {
1426            var text = @"using System;
1427/// <summary>
1428/// << A '&' B >>
1429/// </summary>
1430public class Test
1431{
1432    bool Find()
1433    {
1434        return false;
1435    }
1436}";
1437            ParseAndRoundTripping(text, -1);
1438        }
1439
1440        [WorkItem(911518, "DevDiv/Personal")]
1441        [Fact]
1442        public void RegressError4AnonmousTypeWithTailingComma()
1443        {
1444            var text = @"using System;
1445public class Test
1446{
1447    public static void Main()
1448    {
1449        int x = 0;
1450        var v1 = new { X = x, };
1451    }
1452}";
1453            ParseAndRoundTripping(text);
1454        }
1455
1456        [WorkItem(911521, "DevDiv/Personal")]
1457        [Fact]
1458        public void RegressError4QueryWithVarInLet()
1459        {
1460            var text = @"class Q
1461{
1462    static void Main()
1463    {
1464        var expr1 = new[] { 1, 2, 3, };
1465        var expr2 = new[] { 3, 4, 5, };
1466        var q = from i in (expr1) let j = expr2 select i;
1467
1468        var qq = from x1 in new[] { 3, 9, 5, 5, 0, 8, 6, 8, 9, }
1469                  orderby x1 descending
1470                  let x47 = x1
1471                  select (x47) - (x1);
1472
1473    }
1474}";
1475            ParseAndRoundTripping(text);
1476        }
1477
1478        [WorkItem(911525, "DevDiv/Personal")]
1479        [Fact]
1480        public void RegressError4AttributeWithNamedParam()
1481        {
1482            var text = @"using System;
1483public class TestAttribute : Attribute
1484{
1485  public TestAttribute(int i = 0, int j = 1) { }
1486  public int i { get; set; }
1487}
1488[Test(123, j:-1)]
1489public class A 
1490{
1491  public static int Main()
1492  {
1493    return 0;
1494  }
1495}
1496";
1497            ParseAndRoundTripping(text);
1498        }
1499
1500        [WorkItem(917285, "DevDiv/Personal")]
1501        [Fact]
1502        public void RegressAssertForLFCRSequence()
1503        {
1504            var text = "class Test\r\n{\n\r}\r\n";
1505
1506            ParseAndRoundTripping(text);
1507        }
1508
1509        [WorkItem(917771, "DevDiv/Personal")]
1510        [WorkItem(918947, "DevDiv/Personal")]
1511        [Fact]
1512        public void RegressNotCheckNullRef()
1513        {
1514            var text = @"public struct MyStruct
1515{
1516public delegate void TypeName<T>(ref T t, dynamic d);
1517public delegate Y @dynamic<X, Y>(X u, params dynamic[] ary);
1518public enum EM { };
1519}
1520";
1521            ParseAndRoundTripping(text);
1522        }
1523
1524        [WorkItem(917771, "DevDiv/Personal")]
1525        [Fact]
1526        public void RegressNegNotCheckNullRef()
1527        {
1528            var text = @"class A
1529{
1530A a { 0, 1 };
1531}
1532";
1533            ParseAndRoundTripping(text, 2);
1534        }
1535
1536        [WorkItem(922887, "DevDiv/Personal")]
1537        [Fact]
1538        public void RegressError4ExternOperator()
1539        {
1540            var text = @"public class A
1541{
1542    public static extern int operator !(A a);
1543    public static extern int operator +(A a, int n);
1544}
1545";
1546            ParseAndRoundTripping(text);
1547        }
1548
1549        [Fact, WorkItem(536922, "DevDiv")]
1550        public void RegressError4QueryWithNullable()
1551        {
1552            var text = @"using System.Linq;
1553class A
1554{
1555    static void Main()
1556    {
1557        object[] p = { 1, 2, 3 };
1558        var q = from x in p
1559                where x is int?
1560                select x;
1561    }
1562}";
1563            ParseAndRoundTripping(text);
1564        }
1565
1566        [Fact, WorkItem(537265, "DevDiv")]
1567        public void PartialMethodWithLanguageVersion2()
1568        {
1569            var text = @"partial class P
1570{
1571    partial void M();
1572}
1573";
1574            CSharpParseOptions options = new CSharpParseOptions(languageVersion: LanguageVersion.CSharp2);
1575
1576            var itext = SourceText.From(text);
1577            var tree = SyntaxFactory.ParseSyntaxTree(itext, options, "");
1578            var newTest = tree.GetCompilationUnitRoot().ToFullString();
1579            Assert.Equal(text, newTest);
1580        }
1581
1582        [WorkItem(527490, "DevDiv")]
1583        [Fact]
1584        public void VariableDeclarationAsTypeOfArgument()
1585        {
1586            string text = "typeof(System.String value)";
1587            var typeOfExpression = SyntaxFactory.ParseExpression(text, consumeFullText: true);
1588            Assert.Equal(text, typeOfExpression.ToFullString());
1589            Assert.NotEmpty(typeOfExpression.GetDiagnostics());
1590
1591            typeOfExpression = SyntaxFactory.ParseExpression(text, consumeFullText: false);
1592            Assert.Equal("typeof(System.String ", typeOfExpression.ToFullString());
1593            Assert.NotEmpty(typeOfExpression.GetDiagnostics());
1594        }
1595
1596        [WorkItem(540809, "DevDiv")]
1597        [Fact]
1598        public void IncompleteGlobalAlias()
1599        {
1600            var text = @"namespace N2
1601{
1602    [global:";
1603
1604            ParseAndRoundTripping(text, errorCount: 3);
1605        }
1606
1607        [WorkItem(542229, "DevDiv")]
1608        [Fact]
1609        public void MethodCallWithQueryArgInsideQueryExpr()
1610        {
1611            var text = @"
1612using System;
1613using System.Collections.Generic;
1614using System.Linq;
1615
1616class Program
1617{
1618    public static bool Method1(IEnumerable<int> f1)
1619    {
1620        return true;
1621    }
1622
1623    static void Main(string[] args)
1624    {
1625        var numbers = new int[] { 4, 5 };
1626        var f1 = from num1 in numbers where Method1(from num2 in numbers select num2) select num1;
1627    }
1628}";
1629
1630            ParseAndRoundTripping(text, 0);
1631        }
1632
1633        [WorkItem(542229, "DevDiv")]
1634        [Fact]
1635        public void MethodCallWithFromArgInsideQueryExpr()
1636        {
1637            var text = @"
1638using System;
1639using System.Collections.Generic;
1640using System.Linq;
1641
1642class Program
1643{
1644    public static bool Method1(IEnumerable<int> f1)
1645    {
1646        return true;
1647    }
1648
1649    static void Main(string[] args)
1650    {
1651        var numbers = new int[] { 4, 5 };
1652        var f1 = from num1 in numbers where Method1(from) select num1;
1653    }
1654}";
1655
1656            ParseAndRoundTripping(text, -1);
1657        }
1658
1659        [WorkItem(542229, "DevDiv")]
1660        [Fact]
1661        public void ArrayCreationWithQueryArgInsideQueryExpr()
1662        {
1663            var text = @"
1664using System;
1665using System.Collections.Generic;
1666using System.Linq;
1667
1668class Program
1669{
1670    public static bool Method1(IEnumerable<int> f1)
1671    {
1672        return true;
1673    }
1674
1675    static void Main(string[] args)
1676    {
1677        var numbers = new int[] { 4, 5 };
1678        var f1 = from num1 in new int[from num2 in numbers select num2] select num1; //not valid but this is only a parser test
1679    }
1680}";
1681
1682            ParseAndRoundTripping(text, 0);
1683        }
1684    }
1685}