public void CompilationChain_SubmissionSlots() { var s = CSharpScript.RunAsync("using System;"). ContinueWith("using static System.Environment;"). ContinueWith("int x; x = 1;"). ContinueWith("using static System.Math;"). ContinueWith("int goo(int a) { return a + 1; } "); #if false Assert.True(session.executionState.submissions.Length >= 2, "Expected two submissions"); session.executionState.submissions.Aggregate(0, (i, sub) => { Assert.Equal(i < 2, sub != null); return(i + 1); }); #endif ScriptingTestHelpers.AssertCompilationError(s, "Version", // (1,1): error CS0229: Ambiguity between 'System.Version' and 'System.Environment.Version' Diagnostic(ErrorCode.ERR_AmbigMember, "Version").WithArguments("System.Version", "System.Environment.Version")); s = s.ContinueWith("new System.Collections.Generic.List<Version>()"); Assert.IsType <List <Version> >(s.Result.ReturnValue); s = s.ContinueWith("Environment.Version"); Assert.Equal(Environment.Version, s.Result.ReturnValue); s = s.ContinueWith("goo(x)"); Assert.Equal(2, s.Result.ReturnValue); s = s.ContinueWith("Sin(0)"); Assert.Equal(0.0, s.Result.ReturnValue); }
public void ScriptMemberAccessFromNestedClass() { var script = CSharpScript.Create(@" object field; object Property { get; set; } void Method() { } ").ContinueWith(@" class C { public void Foo() { object f = field; object p = Property; Method(); } } "); ScriptingTestHelpers.AssertCompilationError(script, // (6,20): error CS0120: An object reference is required for the non-static field, method, or property 'field' Diagnostic(ErrorCode.ERR_ObjectRequired, "field").WithArguments("field"), // (7,20): error CS0120: An object reference is required for the non-static field, method, or property 'Property' Diagnostic(ErrorCode.ERR_ObjectRequired, "Property").WithArguments("Property"), // (8,9): error CS0120: An object reference is required for the non-static field, method, or property 'Method()' Diagnostic(ErrorCode.ERR_ObjectRequired, "Method").WithArguments("Method()")); }
public void AddNamespaces_Errors() { // no immediate error, error is reported if the namespace can't be found when compiling: var options = ScriptOptions.Default.AddNamespaces("?1", "?2"); ScriptingTestHelpers.AssertCompilationError(() => CSharpScript.EvaluateAsync("1", options), // error CS0246: The type or namespace name '?1' could not be found (are you missing a using directive or an assembly reference?) Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound).WithArguments("?1"), // error CS0246: The type or namespace name '?2' could not be found (are you missing a using directive or an assembly reference?) Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound).WithArguments("?2")); options = ScriptOptions.Default.AddNamespaces(""); ScriptingTestHelpers.AssertCompilationError(() => CSharpScript.EvaluateAsync("1", options), // error CS7088: Invalid 'Usings' value: ''. Diagnostic(ErrorCode.ERR_BadCompilationOptionValue).WithArguments("Usings", "")); options = ScriptOptions.Default.AddNamespaces(".abc"); ScriptingTestHelpers.AssertCompilationError(() => CSharpScript.EvaluateAsync("1", options), // error CS7088: Invalid 'Usings' value: '.abc'. Diagnostic(ErrorCode.ERR_BadCompilationOptionValue).WithArguments("Usings", ".abc")); options = ScriptOptions.Default.AddNamespaces("a\0bc"); ScriptingTestHelpers.AssertCompilationError(() => CSharpScript.EvaluateAsync("1", options), // error CS7088: Invalid 'Usings' value: '.abc'. Diagnostic(ErrorCode.ERR_BadCompilationOptionValue).WithArguments("Usings", "a\0bc")); }
public void ObjectOverrides3() { var state0 = CSharpScript.RunAsync("", OptionsWithFacades); var src1 = @" Equals(null); GetHashCode(); ToString(); ReferenceEquals(null, null);"; ScriptingTestHelpers.AssertCompilationError(state0, src1, // (2,1): error CS0103: The name 'Equals' does not exist in the current context Diagnostic(ErrorCode.ERR_NameNotInContext, "Equals").WithArguments("Equals"), // (3,1): error CS0103: The name 'GetHashCode' does not exist in the current context Diagnostic(ErrorCode.ERR_NameNotInContext, "GetHashCode").WithArguments("GetHashCode"), // (4,1): error CS0103: The name 'ToString' does not exist in the current context Diagnostic(ErrorCode.ERR_NameNotInContext, "ToString").WithArguments("ToString"), // (5,1): error CS0103: The name 'ReferenceEquals' does not exist in the current context Diagnostic(ErrorCode.ERR_NameNotInContext, "ReferenceEquals").WithArguments("ReferenceEquals")); var src2 = @" public override string ToString() { return null; } "; ScriptingTestHelpers.AssertCompilationError(state0, src2, // (1,24): error CS0115: 'ToString()': no suitable method found to override Diagnostic(ErrorCode.ERR_OverrideNotExpected, "ToString").WithArguments("ToString()")); }
public void NestedVisibility() { var script = CSharpScript.Create(@" private class C { internal class D { internal static int foo() { return 1; } } private class E { internal static int foo() { return 1; } } public class F { internal protected static int foo() { return 1; } } internal protected class G { internal static int foo() { return 1; } } } "); Assert.Equal(1, script.ContinueWith <int>("C.D.foo()").EvaluateAsync().Result); Assert.Equal(1, script.ContinueWith <int>("C.F.foo()").EvaluateAsync().Result); Assert.Equal(1, script.ContinueWith <int>("C.G.foo()").EvaluateAsync().Result); ScriptingTestHelpers.AssertCompilationError(script.ContinueWith <int>(@"C.E.foo()"), // error CS0122: 'C.E' is inaccessible due to its protection level Diagnostic(ErrorCode.ERR_BadAccess, "E").WithArguments("C.E")); }
public void CompilationChain_SubmissionSlots() { var engine = new CSharpScriptEngine(); var session = engine.CreateSession(); session.Execute("using System;"); session.Execute("using static System.Environment;"); session.Execute("int x; x = 1;"); session.Execute("using static System.Math;"); session.Execute("int foo(int a) { return a + 1; } "); #if false Assert.True(session.executionState.submissions.Length >= 2, "Expected two submissions"); session.executionState.submissions.Aggregate(0, (i, sub) => { Assert.Equal(i < 2, sub != null); return(i + 1); }); #endif object result; // TODO (tomat): Version is a type and property, but we are not looking for a type, so can we disambiguate? ScriptingTestHelpers.AssertCompilationError(session, "Version", // (1,1): error CS0229: Ambiguity between 'System.Version' and 'System.Environment.Version' Diagnostic(ErrorCode.ERR_AmbigMember, "Version").WithArguments("System.Version", "System.Environment.Version") ); result = session.Execute("new System.Collections.Generic.List<Version>()"); Assert.True(result is List <Version>, "Expected List<Version>"); result = session.Execute("Environment.Version"); Assert.Equal(Environment.Version, result); result = session.Execute("foo(x)"); Assert.Equal(2, result); result = session.Execute("Sin(0)"); Assert.Equal(0.0, result); }
public void HostObjectBinding_PrivateClass() { var c = new PrivateClass(); ScriptingTestHelpers.AssertCompilationError(() => CSharpScript.EvaluateAsync("Z()", OptionsWithFacades, c), // (1,1): error CS0122: '<Fully Qualified Name of PrivateClass>.Z()' is inaccessible due to its protection level Diagnostic(ErrorCode.ERR_BadAccess, "Z").WithArguments(typeof(PrivateClass).FullName.Replace("+", ".") + ".Z()")); }
public void StreamWithOffset() { var resolver = new StreamOffsetResolver(); var options = ScriptOptions.Default.WithSourceResolver(resolver); var script = CSharpScript.Create(@"#load ""a.csx""", options); ScriptingTestHelpers.EvaluateScriptWithOutput(script, "Hello World!"); }
public void HostObjectBinding_PrivateMembers() { object c = new M <int>(); ScriptingTestHelpers.AssertCompilationError(() => CSharpScript.EvaluateAsync("Z()", OptionsWithFacades, c), // (1,1): error CS0103: The name 'z' does not exist in the current context Diagnostic(ErrorCode.ERR_NameNotInContext, "Z").WithArguments("Z")); }
public void TestRunVoidScript() { var state = ScriptingTestHelpers.RunScriptWithOutput( CSharpScript.Create("System.Console.WriteLine(0);"), "0"); Assert.Null(state.ReturnValue); }
public async Task CompilationChain_GlobalImportsRebinding() { var options = ScriptOptions.Default.AddImports("System.Diagnostics"); var s0 = await CSharpScript.RunAsync("", options); ScriptingTestHelpers.AssertCompilationError(s0, @"Process.GetCurrentProcess()", // (2,1): error CS0103: The name 'Process' does not exist in the current context Diagnostic(ErrorCode.ERR_NameNotInContext, "Process").WithArguments("Process")); var s1 = s0.ContinueWithAsync($"#r \"{typeof(Process).Assembly.Location}\""); var s2 = s1.ContinueWith("Process.GetCurrentProcess()"); Assert.NotNull(s2.Result); }
public async Task HostObjectBinding_Interface() { var c = new C(); var s0 = await CSharpScript.RunAsync <int>("Z()", OptionsWithFacades, c, typeof(I)); Assert.Equal(3, s0.ReturnValue); ScriptingTestHelpers.AssertCompilationError(s0, @"x + Y", // The name '{0}' does not exist in the current context Diagnostic(ErrorCode.ERR_NameNotInContext, "x").WithArguments("x"), Diagnostic(ErrorCode.ERR_NameNotInContext, "Y").WithArguments("Y")); var s1 = await s0.ContinueWithAsync <string>("N"); Assert.Equal("2", s1.ReturnValue); }
public void SearchPaths_RemoveDefault() { // remove default paths: var options = ScriptOptions.Default; var source = @" #r ""System.Data.dll"" new System.Data.DataSet() "; ScriptingTestHelpers.AssertCompilationError(() => CSharpScript.EvaluateAsync(source, options), // (2,1): error CS0006: Metadata file 'System.Data.dll' could not be found // #r "System.Data.dll" Diagnostic(ErrorCode.ERR_NoMetadataFile, @"#r ""System.Data.dll""").WithArguments("System.Data.dll"), // (3,12): error CS0234: The type or namespace name 'Data' does not exist in the namespace 'System' (are you missing an assembly reference?) // new System.Data.DataSet() Diagnostic(ErrorCode.ERR_DottedTypeNameNotFoundInNS, "Data").WithArguments("Data", "System")); }
public void CompilationChain_GlobalImportsRebinding() { var engine = new CSharpScriptEngine(); var session = engine.CreateSession(); session.ImportNamespace("System.Diagnostics"); ScriptingTestHelpers.AssertCompilationError(session, @" Process.GetCurrentProcess()", // (2,1): error CS0103: The name 'Process' does not exist in the current context Diagnostic(ErrorCode.ERR_NameNotInContext, "Process").WithArguments("Process")); session.Execute(@" #r """ + typeof(Process).Assembly.Location + @""""); session.Execute(@" Process.GetCurrentProcess()"); }
public void SearchPaths_RemoveDefault() { var engine = new CSharpScriptEngine(); var session = engine.CreateSession(); // remove default paths: session.SetReferenceSearchPaths(); ScriptingTestHelpers.AssertCompilationError(session, @" #r ""System.Data.dll"" new System.Data.DataSet() ", // (2,1): error CS0006: Metadata file 'System.Data.dll' could not be found // #r "System.Data.dll" Diagnostic(ErrorCode.ERR_NoMetadataFile, @"#r ""System.Data.dll""").WithArguments("System.Data.dll"), // (3,12): error CS0234: The type or namespace name 'Data' does not exist in the namespace 'System' (are you missing an assembly reference?) // new System.Data.DataSet() Diagnostic(ErrorCode.ERR_DottedTypeNameNotFoundInNS, "Data").WithArguments("Data", "System") ); }
public void MissingDependency() { var engine = new CSharpScriptEngine(); ScriptingTestHelpers.AssertCompilationError(engine, @" #r ""WindowsBase"" #r ""PresentationCore"" #r ""PresentationFramework"" using System.Windows; System.Collections.IEnumerable w = new Window(); ", // (7,36): error CS0012: The type 'System.ComponentModel.ISupportInitialize' is defined in an assembly that is not referenced. You must add a reference to assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. // System.Collections.IEnumerable w = new Window(); Diagnostic(ErrorCode.ERR_NoTypeDef, "new Window()").WithArguments("System.ComponentModel.ISupportInitialize", "System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"), // (7,36): error CS0012: The type 'System.Windows.Markup.IQueryAmbient' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Xaml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. // System.Collections.IEnumerable w = new Window(); Diagnostic(ErrorCode.ERR_NoTypeDef, "new Window()").WithArguments("System.Windows.Markup.IQueryAmbient", "System.Xaml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"), // (7,36): error CS0266: Cannot implicitly convert type 'System.Windows.Window' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?) // System.Collections.IEnumerable w = new Window(); Diagnostic(ErrorCode.ERR_NoImplicitConvCast, "new Window()").WithArguments("System.Windows.Window", "System.Collections.IEnumerable")); }
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); }
public async Task ReturnInNestedScopeWithTrailingVoidExpressionAsInt() { var script = CSharpScript.Create <int>(@" bool condition = false; if (condition) { return 1; } System.Console.WriteLine();"); var result = await script.EvaluateAsync(); Assert.Equal(0, result); script = CSharpScript.Create <int>(@" bool condition = false; if (condition) { return 1; } System.Console.WriteLine()"); Assert.Equal(0, ScriptingTestHelpers.EvaluateScriptWithOutput(script, "")); }
public void NoReturn() { Assert.Null(ScriptingTestHelpers.EvaluateScriptWithOutput( CSharpScript.Create("System.Console.WriteLine();"), "")); }