public void DeclaringTuples_WithMistmatchedTuplePropertyNames_IssuesWarning()
 {
     CompilerAssert.StatementsFailCompilation(
         @"
         (string DirectoryName, string FileName, string Extension) pathData =
             (AltDirectoryName1: @""\\test\unc\path\to"", FileName: ""something"", Extension: "".ext"");
         pathData = (AltDirectoryName2: @""\\test\unc\path\to"", 
             FileName: ""something"", Extension:"".ext"");
         pathData.GetType();
         ",
         @"Warning CS8123: The tuple element name 'AltDirectoryName1' is ignored because a different name is specified by the target type '(string DirectoryName, string FileName, string Extension)'.",
         "Warning CS8123: The tuple element name 'AltDirectoryName2' is ignored because a different name is specified by the target type '(string DirectoryName, string FileName, string Extension)'."
         );
     CompilerAssert.StatementsFailCompilation(
         @"
         (string, string, string) pathData =
             (DirectoryName: @""\\test\unc\path\to"",
             FileName: ""something"",
             Extension: "".ext"");
         pathData.GetType();
         ",
         "Warning CS8123: The tuple element name 'DirectoryName' is ignored because a different name is specified by the target type '(string, string, string)'.",
         "Warning CS8123: The tuple element name 'Extension' is ignored because a different name is specified by the target type '(string, string, string)'.",
         "Warning CS8123: The tuple element name 'FileName' is ignored because a different name is specified by the target type '(string, string, string)'.");
 }
 public void Deconstruct_AssignmentToValueTypeIsNotSupported()
 {
     string[] expectedErrors =
     {
         "Error CS0029: Cannot implicitly convert type 'CSharp7.PathInfo' to '(string DirectoryName, string FileName, string Extension)'"
     };
     CompilerAssert.StatementsFailCompilation(
         @"
             PathInfo pathInfo = new PathInfo(
                     @""\\test\unc\path\to\something.ext"");
             // E.g. 4: Deconstructing declaration and assignment to a ValueTuple
             (string DirectoryName, string FileName, string Extension) path = pathInfo;
         ",
         expectedErrors);
 }
Esempio n. 3
0
 public void ExpressionBodiedMemberTests()
 {
     // IMPORTANT: This should work but the compiler doesn't appear to have implemented it yet.
     CompilerAssert.CodeFailCompilation(
         @"
         class TemporaryFile
         {
             public TemporaryFile(string fileName) =>
                 File = new FileInfo(fileName??throw new ArgumentNullException());
             ~TemporaryFile() => Dispose();
             FileInfo _File; 
             public FileInfo File 
             { 
                 get => _File; 
                 set => _File = value;
             }
             void Dispose() => File?.Delete();
         }
         ",
         ExpressionBodiedMemberErrors
         );
 }
        public void Deconstruct_GivenSingleOutOParameter_FailsCompilation()
        {
            string[] expectedErrors =
            {
                "Error CS1002: ; expected",
                "Error CS1002: ; expected",
                "Error CS1513: } expected",
                "Error CS1525: Invalid expression term '='",
                "Error CS0119: 'FileInfo' is a type, which is not valid in the given context",
                "Error CS0119: 'FileInfo' is a type, which is not valid in the given context",
                "Error CS0103: The name 'path' does not exist in the current context",
                "Error CS1026: ) expected"
            };


            CompilerAssert.StatementsFailCompilation(
                @"
                        PathInfo pathInfo = new PathInfo(
                            @""\\test\unc\path\to\something.ext"");
                        (FileInfo path) = pathInfo; 
                ",
                expectedErrors);
        }
Esempio n. 5
0
        public void ByRefCompileErrors()
        {
            void AssertStatementsFailCompilation(string body, params string[] expectedText)
            {
                string MethodScaffolding = $@"#pragma warning disable CS0168 // Disable variable declared but never used.
                {body}
                #pragma warning restore CS0168";

                CompilerAssert.StatementsFailCompilation(MethodScaffolding, expectedText);
            }

            AssertStatementsFailCompilation("ref int number;",
                                            "Error CS8174: A declaration of a by-reference variable must have an initializer");
            AssertStatementsFailCompilation("ref int number = ref 42;",
                                            "Error CS1510: A ref or out value must be an assignable variable");
            AssertStatementsFailCompilation("ref int number = null;",
                                            "Error CS1510: A ref or out value must be an assignable variable",
                                            "Error CS8172: Cannot initialize a by-reference variable with a value");
            AssertStatementsFailCompilation("ref string number = \"Inigo Montoya\";",
                                            "Error CS1510: A ref or out value must be an assignable variable",
                                            "Error CS8172: Cannot initialize a by-reference variable with a value");
            AssertStatementsFailCompilation(
                @"Guid guid = Guid.NewGuid();
                 ref object obj = ref guid;",
                "Error CS8173: The expression must be of type 'object' because it is being assigned by reference");
            AssertStatementsFailCompilation(
                @"Guid guid = Guid.NewGuid();
                 ref object obj = ref guid;",
                "Error CS8173: The expression must be of type 'object' because it is being assigned by reference");
            AssertStatementsFailCompilation(
                @"IEnumerable<ref int> numbers;",
                "Error CS1073: Unexpected token 'ref'");
            //AssertStatementsFailCompilation(
            //    @"ref int[] numbers; ",
            //    "Error CS1073: Unexpected token 'ref'");
        }