Exemple #1
0
        public async Task LoadedFileWithReturnAndGoto()
        {
            var resolver = TestSourceReferenceResolver.Create(
                KeyValuePair.Create("a.csx", @"
goto EOF;
NEXT:
return 1;
EOF:;
2"));
            var options = ScriptOptions.Default.WithSourceResolver(resolver);

            var script = CSharpScript.Create(@"
#load ""a.csx""
goto NEXT;
return 3;
NEXT:;", options);
            var result = await script.EvaluateAsync();

            Assert.Null(result);

            script = CSharpScript.Create(@"
#load ""a.csx""
L1: goto EOF;
L2: return 3;
EOF:
EOF2: ;
4", options);
            result = await script.EvaluateAsync();

            Assert.Equal(4, result);
        }
Exemple #2
0
        public void LoadedFile()
        {
            var sourceA =
                @"goto A;
A: goto B;";
            var sourceB =
                @"#load ""a.csx""
goto B;
B: goto A;";
            var resolver = TestSourceReferenceResolver.Create(
                KeyValuePairUtil.Create("a.csx", sourceA)
                );
            var options     = TestOptions.DebugDll.WithSourceReferenceResolver(resolver);
            var compilation = CreateCompilationWithMscorlib45(
                sourceB,
                options: options,
                parseOptions: TestOptions.Script
                );

            compilation
            .GetDiagnostics()
            .Verify(
                // a.csx(2,9): error CS0159: No such label 'B' within the scope of the goto statement
                // A: goto B;
                Diagnostic(ErrorCode.ERR_LabelNotFound, "B")
                .WithArguments("B")
                .WithLocation(2, 9),
                // (3,9): error CS0159: No such label 'A' within the scope of the goto statement
                // B: goto A;
                Diagnostic(ErrorCode.ERR_LabelNotFound, "A")
                .WithArguments("A")
                .WithLocation(3, 9)
                );
        }
Exemple #3
0
        public void FileWithErrors()
        {
            using (new EnsureEnglishUICulture())
            {
                var code     = "#load \"a.csx\"";
                var resolver = TestSourceReferenceResolver.Create(
                    KeyValuePair.Create("a.csx", @"
                    #load ""b.csx""
                    asdf();"));
                var options     = TestOptions.DebugDll.WithSourceReferenceResolver(resolver);
                var compilation = CreateCompilationWithMscorlib45(code, options: options, parseOptions: TestOptions.Script);

                Assert.Equal(2, compilation.SyntaxTrees.Length);
                compilation.GetParseDiagnostics().Verify(
                    // a.csx(2,27): error CS1504: Source file 'b.csx' could not be opened -- Could not find file.
                    //                     #load "b.csx";
                    Diagnostic(ErrorCode.ERR_NoSourceFile, @"""b.csx""").WithArguments("b.csx", "Could not find file.").WithLocation(2, 27));
                compilation.GetDiagnostics().Verify(
                    // a.csx(2,27): error CS1504: Source file 'b.csx' could not be opened -- Could not find file.
                    //                     #load "b.csx";
                    Diagnostic(ErrorCode.ERR_NoSourceFile, @"""b.csx""").WithArguments("b.csx", "Could not find file.").WithLocation(2, 27),
                    // a.csx(3,21): error CS0103: The name 'asdf' does not exist in the current context
                    //                     asdf();
                    Diagnostic(ErrorCode.ERR_NameNotInContext, "asdf").WithArguments("asdf").WithLocation(3, 21));
            }
        }
Exemple #4
0
        private void GlobalUsingsToLoadedScript()
        {
            const string scriptSource =
                @"
System.Type t;

GetTempPath(); // global using static exposed
t = typeof(File); // global using exposed
";

            const string submissionSource =
                @"
#load ""a.csx""
";

            var resolver = TestSourceReferenceResolver.Create(
                new Dictionary <string, string> {
                { "a.csx", scriptSource }
            }
                );

            var compilation = CreateSubmission(
                submissionSource,
                options: TestOptions.DebugDll
                .WithSourceReferenceResolver(resolver)
                .WithUsings("System.IO", "System.IO.Path")
                );

            compilation.VerifyDiagnostics();
        }
Exemple #5
0
        public async Task ReturnInLoadedFileTrailingVoidExpression()
        {
            using (var redirect = new OutputRedirect(CultureInfo.InvariantCulture))
            {
                var resolver = TestSourceReferenceResolver.Create(
                    KeyValuePair.Create("a.csx", @"
if (false)
{
    return 1;
}
System.Console.WriteLine(42)"));
                var options = ScriptOptions.Default.WithSourceResolver(resolver);

                var script = CSharpScript.Create("#load \"a.csx\"", options);
                var result = await script.EvaluateAsync();

                Assert.Null(result);

                script = CSharpScript.Create(@"
#load ""a.csx""
2", options);
                result = await script.EvaluateAsync();

                Assert.Equal(2, result);
            }
        }
        private static SourceReferenceResolver CreateResolver(params KeyValuePair <string, string>[] scripts)
        {
            var sources = new Dictionary <string, string>();

            foreach (var script in scripts)
            {
                sources.Add(script.Key, script.Value);
            }
            return(TestSourceReferenceResolver.Create(sources));
        }
Exemple #7
0
        public void FileThatCannotBeDecoded()
        {
            var code     = "#load \"b.csx\"";
            var resolver = TestSourceReferenceResolver.Create(
                KeyValuePair.Create <string, object>("a.csx", new byte[] { 0xd8, 0x00, 0x00, 0x00 }),
                KeyValuePair.Create <string, object>("b.csx", "#load \"a.csx\""));
            var options     = TestOptions.DebugDll.WithSourceReferenceResolver(resolver);
            var compilation = CreateCompilationWithMscorlib45(code, sourceFileName: "external1.csx", options: options, parseOptions: TestOptions.Script);
            var external1   = compilation.SyntaxTrees.Last();
            var external2   = Parse(code, "external2.csx", TestOptions.Script);

            compilation = compilation.AddSyntaxTrees(external2);

            Assert.Equal(3, compilation.SyntaxTrees.Length);
            compilation.GetParseDiagnostics().Verify(
                // (1,7): error CS2015: 'a.csx' is a binary file instead of a text file
                // #load "a.csx"
                Diagnostic(ErrorCode.ERR_BinaryFile, @"""a.csx""").WithArguments("a.csx").WithLocation(1, 7));

            var external3 = Parse(@"
                #load ""b.csx""
                #load ""a.csx""", filename: "external3.csx", options: TestOptions.Script);

            compilation = compilation.ReplaceSyntaxTree(external1, external3);

            Assert.Equal(3, compilation.SyntaxTrees.Length);
            compilation.GetParseDiagnostics().Verify(
                // external3.csx(3,23): error CS2015: 'a.csx' is a binary file instead of a text file
                //                 #load "a.csx"
                Diagnostic(ErrorCode.ERR_BinaryFile, @"""a.csx""").WithArguments("a.csx").WithLocation(3, 23),
                // b.csx(1,7): error CS2015: 'a.csx' is a binary file instead of a text file
                // #load "a.csx"
                Diagnostic(ErrorCode.ERR_BinaryFile, @"""a.csx""").WithArguments("a.csx").WithLocation(1, 7));

            var external4 = Parse("#load \"a.csx\"", "external4.csx", TestOptions.Script);

            compilation = compilation.ReplaceSyntaxTree(external3, external4);

            Assert.Equal(3, compilation.SyntaxTrees.Length);
            compilation.GetParseDiagnostics().Verify(
                // external4.csx(1,7): error CS2015: 'a.csx' is a binary file instead of a text file
                // #load "a.csx"
                Diagnostic(ErrorCode.ERR_BinaryFile, @"""a.csx""").WithArguments("a.csx").WithLocation(1, 7),
                // b.csx(1,7): error CS2015: 'a.csx' is a binary file instead of a text file
                // #load "a.csx"
                Diagnostic(ErrorCode.ERR_BinaryFile, @"""a.csx""").WithArguments("a.csx").WithLocation(1, 7));

            compilation = compilation.RemoveSyntaxTrees(external2);

            Assert.Equal(external4, compilation.SyntaxTrees.Single());
            compilation.GetParseDiagnostics().Verify(
                // external4.csx(1,7): error CS2015: 'a.csx' is a binary file instead of a text file
                // #load "a.csx"
                Diagnostic(ErrorCode.ERR_BinaryFile, @"""a.csx""").WithArguments("a.csx").WithLocation(1, 7));
        }
Exemple #8
0
        private void UsingsFromLoadedScript()
        {
            const string scriptSource =
                @"
using static System.IO.Path;
using System.IO;
using F = System.IO.File;

class C { }
";

            const string submissionSource =
                @"
#load ""a.csx""

System.Type t;

GetTempPath(); // using static not exposed
t = typeof(File); // using not exposed
t = typeof(F); // using alias not exposed

t = typeof(C); // declaration exposed
";

            var resolver = TestSourceReferenceResolver.Create(
                new Dictionary <string, string> {
                { "a.csx", scriptSource }
            }
                );

            var compilation = CreateSubmission(
                submissionSource,
                options: TestOptions.DebugDll.WithSourceReferenceResolver(resolver)
                );

            compilation.VerifyDiagnostics(
                // (6,1): error CS0103: The name 'GetTempPath' does not exist in the current context
                // GetTempPath(); // using static not exposed
                Diagnostic(ErrorCode.ERR_NameNotInContext, "GetTempPath")
                .WithArguments("GetTempPath")
                .WithLocation(6, 1),
                // (7,12): error CS0246: The type or namespace name 'File' could not be found (are you missing a using directive or an assembly reference?)
                // t = typeof(File); // using not exposed
                Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "File")
                .WithArguments("File")
                .WithLocation(7, 12),
                // (8,12): error CS0246: The type or namespace name 'F' could not be found (are you missing a using directive or an assembly reference?)
                // t = typeof(F); // using alias not exposed
                Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "F")
                .WithArguments("F")
                .WithLocation(8, 12)
                );
        }
Exemple #9
0
        public void Cycles()
        {
            var code     = "#load \"a.csx\"";
            var resolver = TestSourceReferenceResolver.Create(
                KeyValuePairUtil.Create("a.csx", code)
                );
            var options     = TestOptions.DebugDll.WithSourceReferenceResolver(resolver);
            var compilation = CreateCompilationWithMscorlib45(
                code,
                options: options,
                parseOptions: TestOptions.Script
                );

            Assert.Equal(2, compilation.SyntaxTrees.Length);
            compilation.VerifyDiagnostics();

            var newTree = Parse(code, "a.csx", TestOptions.Script);

            compilation = compilation.ReplaceSyntaxTree(compilation.SyntaxTrees.Last(), newTree);

            Assert.Equal(2, compilation.SyntaxTrees.Length);
            compilation.VerifyDiagnostics();

            compilation = compilation.RemoveSyntaxTrees(newTree);

            Assert.Empty(compilation.SyntaxTrees);
            compilation.VerifyDiagnostics();

            resolver = TestSourceReferenceResolver.Create(
                KeyValuePairUtil.Create("a.csx", "#load \"b.csx\""),
                KeyValuePairUtil.Create("b.csx", code)
                );
            options     = TestOptions.DebugDll.WithSourceReferenceResolver(resolver);
            compilation = CreateCompilationWithMscorlib45(
                code,
                options: options,
                parseOptions: TestOptions.Script
                );

            Assert.Equal(3, compilation.SyntaxTrees.Length);
            compilation.VerifyDiagnostics();

            newTree     = Parse(code, "a.csx", TestOptions.Script);
            compilation = compilation.ReplaceSyntaxTree(compilation.SyntaxTrees.Last(), newTree);

            Assert.Equal(3, compilation.SyntaxTrees.Length);
            compilation.VerifyDiagnostics();

            compilation = compilation.RemoveSyntaxTrees(newTree);

            Assert.Empty(compilation.SyntaxTrees);
            compilation.VerifyDiagnostics();
        }
Exemple #10
0
        public async Task LoadedFileWithVoidReturn()
        {
            var resolver = TestSourceReferenceResolver.Create(
                KeyValuePair.Create("a.csx", @"
var i = 42;
return;
i = -1;"));
            var options = ScriptOptions.Default.WithSourceResolver(resolver);
            var script  = CSharpScript.Create <int>(@"
#load ""a.csx""
i", options);
            var result  = await script.EvaluateAsync();

            Assert.Equal(0, result);
        }
Exemple #11
0
        public async Task ReturnInLoadedFile()
        {
            var resolver = TestSourceReferenceResolver.Create(
                KeyValuePair.Create("a.csx", "return 42;"));
            var options = ScriptOptions.Default.WithSourceResolver(resolver);

            var script = CSharpScript.Create("#load \"a.csx\"", options);
            var result = await script.EvaluateAsync();

            Assert.Equal(42, result);

            script = CSharpScript.Create(@"
#load ""a.csx""
-1", options);
            result = await script.EvaluateAsync();

            Assert.Equal(42, result);
        }
Exemple #12
0
        public void ReturnInLoadedFileTrailingVoidExpression()
        {
            var resolver = TestSourceReferenceResolver.Create(
                KeyValuePair.Create("a.csx", @"
if (false)
{
    return 1;
}
System.Console.WriteLine(42)"));
            var options = ScriptOptions.Default.WithSourceResolver(resolver);

            var script = CSharpScript.Create("#load \"a.csx\"", options);
            var result = ScriptingTestHelpers.EvaluateScriptWithOutput(script, "42");

            Assert.Null(result);

            script = CSharpScript.Create(@"
#load ""a.csx""
2", options);
            result = ScriptingTestHelpers.EvaluateScriptWithOutput(script, "42");
            Assert.Equal(2, result);
        }
Exemple #13
0
        public async Task ReturnInLoadedFileTrailingExpression()
        {
            var resolver = TestSourceReferenceResolver.Create(
                KeyValuePair.Create("a.csx", @"
if (false)
{
    return 42;
}
1"));
            var options = ScriptOptions.Default.WithSourceResolver(resolver);

            var script = CSharpScript.Create("#load \"a.csx\"", options);
            var result = await script.EvaluateAsync();

            Assert.Null(result);

            script = CSharpScript.Create(@"
#load ""a.csx""
2", options);
            result = await script.EvaluateAsync();

            Assert.Equal(2, result);
        }
Exemple #14
0
        public async Task MultipleLoadedFilesWithReturnAndTrailingExpression()
        {
            var resolver = TestSourceReferenceResolver.Create(
                KeyValuePair.Create("a.csx", "return 1;"),
                KeyValuePair.Create("b.csx", @"
#load ""a.csx""
2"));
            var options = ScriptOptions.Default.WithSourceResolver(resolver);
            var script  = CSharpScript.Create("#load \"b.csx\"", options);
            var result  = await script.EvaluateAsync();

            Assert.Equal(1, result);

            resolver = TestSourceReferenceResolver.Create(
                KeyValuePair.Create("a.csx", "return 1;"),
                KeyValuePair.Create("b.csx", "2"));
            options = ScriptOptions.Default.WithSourceResolver(resolver);
            script  = CSharpScript.Create(@"
#load ""a.csx""
#load ""b.csx""", options);
            result  = await script.EvaluateAsync();

            Assert.Equal(1, result);

            resolver = TestSourceReferenceResolver.Create(
                KeyValuePair.Create("a.csx", "return 1;"),
                KeyValuePair.Create("b.csx", "2"));
            options = ScriptOptions.Default.WithSourceResolver(resolver);
            script  = CSharpScript.Create(@"
#load ""a.csx""
#load ""b.csx""
return 3;", options);
            result  = await script.EvaluateAsync();

            Assert.Equal(1, result);
        }
Exemple #15
0
        private void UsingsToLoadedScript()
        {
            const string scriptSource = @"
using System.Collections.Generic;
using AL = System.Collections.ArrayList;
using static System.Math;

class D { }

System.Type t;

// Previous submission
GetCommandLineArgs(); // using static not exposed
t = typeof(StringBuilder); // using not exposed
t = typeof(P); // using alias not exposed
t = typeof(B); // declaration exposed

// Current submission
GetTempPath(); // using static not exposed
t = typeof(File); // using not exposed
t = typeof(F); // using alias not exposed
t = typeof(C); // declaration exposed

// Current file - all available
Sin(1);
t = typeof(List<int>);
t = typeof(AL);
t = typeof(D);
";

            const string previousSubmissionSource = @"
using static System.Environment;
using System.Text;
using P = System.IO.Path;

class B { }
";

            const string submissionSource = @"
#load ""a.csx""

using static System.IO.Path;
using System.IO;
using F = System.IO.File;

class C { }
";

            var resolver = TestSourceReferenceResolver.Create(new Dictionary <string, string>
            {
                { "a.csx", scriptSource }
            });

            var compilation = CreateSubmission(
                submissionSource,
                options: TestOptions.DebugDll.WithSourceReferenceResolver(resolver),
                previous: CreateSubmission(previousSubmissionSource));

            compilation.VerifyDiagnostics(
                // Previous submission

                // a.csx(11,1): error CS0103: The name 'GetCommandLineArgs' does not exist in the current context
                // GetCommandLineArgs(); // using static not exposed
                Diagnostic(ErrorCode.ERR_NameNotInContext, "GetCommandLineArgs").WithArguments("GetCommandLineArgs").WithLocation(11, 1),
                // a.csx(12,12): error CS0246: The type or namespace name 'StringBuilder' could not be found (are you missing a using directive or an assembly reference?)
                // t = typeof(StringBuilder); // using not exposed
                Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "StringBuilder").WithArguments("StringBuilder").WithLocation(12, 12),
                // a.csx(13,12): error CS0246: The type or namespace name 'P' could not be found (are you missing a using directive or an assembly reference?)
                // t = typeof(P); // using alias not exposed
                Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "P").WithArguments("P").WithLocation(13, 12),

                // Current submission

                // a.csx(17,1): error CS0103: The name 'GetTempPath' does not exist in the current context
                // GetTempPath(); // using static not exposed
                Diagnostic(ErrorCode.ERR_NameNotInContext, "GetTempPath").WithArguments("GetTempPath").WithLocation(17, 1),
                // a.csx(18,12): error CS0246: The type or namespace name 'File' could not be found (are you missing a using directive or an assembly reference?)
                // t = typeof(File); // using not exposed
                Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "File").WithArguments("File").WithLocation(18, 12),
                // a.csx(19,12): error CS0246: The type or namespace name 'F' could not be found (are you missing a using directive or an assembly reference?)
                // t = typeof(F); // using alias not exposed
                Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "F").WithArguments("F").WithLocation(19, 12));
        }