public void InlineCodeImplementationOfGetResultWorks() { try { StatementCompiler.DisableStateMachineRewriteTestingUseOnly = true; AssertCorrect(@" using System; public class MyAwaiter : System.Runtime.CompilerServices.INotifyCompletion { public bool IsCompleted { get { return false; } } public void OnCompleted(Action continuation) {} public int GetResult() {} } public class Awaitable { public MyAwaiter GetAwaiter() { return null; } } public class C { public async void M() { Awaitable x = null; // BEGIN await x; // END } } ", @" var $tmp1 = $x.$GetAwaiter(); await $tmp1:$OnCompleted; _GetResult($tmp1)._; ", addSkeleton: false, metadataImporter: new MockMetadataImporter { GetMethodSemantics = m => m.Name == "GetResult" ? MethodScriptSemantics.InlineCode("_GetResult({this})._") : MethodScriptSemantics.NormalMethod("$" + m.Name) }); } finally { StatementCompiler.DisableStateMachineRewriteTestingUseOnly = false; } }
public void AnErrorIsIssuedIfTheMainMethodIsNotImplementedAsANormalMethod() { var type = CreateMockTypeDefinition("MyClass"); var main = new Mock <IMethod>(MockBehavior.Strict); main.SetupGet(_ => _.DeclaringTypeDefinition).Returns(type); main.SetupGet(_ => _.Name).Returns("Main"); main.SetupGet(_ => _.FullName).Returns("MyClass.Main"); main.SetupGet(_ => _.Parameters).Returns(EmptyList <IParameter> .Instance); main.SetupGet(_ => _.Region).Returns(DomRegion.Empty); var er = new MockErrorReporter(); Process( new[] { new JsClass(type, JsClass.ClassTypeEnum.Class, null, null, null) { StaticMethods = { new JsMethod(main.Object, "$Main", new string[0], JsExpression.FunctionDefinition(new string[0], new JsExpressionStatement(JsExpression.Identifier("X")))) } } }, new MockScriptSharpMetadataImporter() { GetMethodSemantics = m => ReferenceEquals(m, main.Object) ? MethodScriptSemantics.InlineCode("X") : MethodScriptSemantics.NormalMethod("$" + m.Name) }, er, main.Object ); Assert.That(er.AllMessages, Has.Count.EqualTo(1)); Assert.That(er.AllMessages.Any(m => m.Code == 7801 && (string)m.Args[0] == "MyClass.Main")); }
public void InvokingMethodWithExpandedParamArrayWorksForNewExpressions() { AssertCorrect( @"public void F(string p1, int p2, params string[] p3) {} public void M() { // BEGIN F(""x"", 4); F(""x"", 4, ""y""); F(""x"", 4, ""y"", ""z""); // END }", @" 'x' * new 4(); 'x' * new 4('y'); 'x' * new 4('y', 'z'); ", metadataImporter: new MockMetadataImporter { GetMethodSemantics = m => m.Name == "F" ? MethodScriptSemantics.InlineCode("{p1}* new {p2}({*p3})") : MethodScriptSemantics.NormalMethod(m.Name) }); AssertCorrect( @"public void F(string p1, int p2, params string[] p3) {} public void M() { // BEGIN F(""x"", 4); F(""x"", 4, ""y""); F(""x"", 4, ""y"", ""z""); // END }", @" 'x' * new 4(A); 'x' * new 4(A, 'y'); 'x' * new 4(A, 'y', 'z'); ", metadataImporter: new MockMetadataImporter { GetMethodSemantics = m => m.Name == "F" ? MethodScriptSemantics.InlineCode("{p1}* new {p2}(A, {*p3})") : MethodScriptSemantics.NormalMethod(m.Name) }); }
public void InlineCodeImplementationOfOnCompletedIsAnError() { var er = new MockErrorReporter(); Compile(new[] { @" using System; public class MyAwaiter : System.Runtime.CompilerServices.INotifyCompletion { public bool IsCompleted { get { return false; } } public void OnCompleted(Action continuation) {} public int GetResult() {} } public class Awaitable { public MyAwaiter GetAwaiter() { return null; } } public class C { public async void M() { Awaitable x = null; await x; } }" }, metadataImporter: new MockMetadataImporter { GetMethodSemantics = m => m.Name == "OnCompleted" ? MethodScriptSemantics.InlineCode("_OnCompleted({this})._") : MethodScriptSemantics.NormalMethod("$" + m.Name) }, errorReporter: er); Assert.That(er.AllMessages.Count, Is.EqualTo(1)); Assert.That(er.AllMessages.Any(e => e.FormattedMessage.Contains("OnCompleted") && e.FormattedMessage.Contains("normal method") && e.FormattedMessage.Contains("await"))); }
public void InvokingMethodWithExpandedParamArrayWorksForArrayLiterals() { AssertCorrect( @"public void F(string p1, int p2, params string[] p3) {} public void M() { // BEGIN F(""x"", 4); F(""x"", 4, ""y""); F(""x"", 4, ""y"", ""z""); // END }", @" 'x' * 4 + []; 'x' * 4 + ['y']; 'x' * 4 + ['y', 'z']; ", metadataImporter: new MockMetadataImporter { GetMethodSemantics = m => m.Name == "F" ? MethodScriptSemantics.InlineCode("{p1}*{p2} + [{*p3}]") : MethodScriptSemantics.NormalMethod(m.Name) }); AssertCorrect( @"public void F(string p1, int p2, params string[] p3) {} public void M() { // BEGIN F(""x"", 4); F(""x"", 4, ""y""); F(""x"", 4, ""y"", ""z""); // END }", @" 'x' * 4 + [A]; 'x' * 4 + [A, 'y']; 'x' * 4 + [A, 'y', 'z']; ", metadataImporter: new MockMetadataImporter { GetMethodSemantics = m => m.Name == "F" ? MethodScriptSemantics.InlineCode("{p1}*{p2} + [A, {*p3}]") : MethodScriptSemantics.NormalMethod(m.Name) }); }
public void GetEnumeratorAsInlineCodeWithEnumerateAsArray() { AssertCorrect( @"public class X { class MyEnumerable { public MyEnumerator GetEnumerator() { return null; } } sealed class MyEnumerator { public int Current { get { return 0; } } public bool MoveNext() {} } public void M() { var enm = new MyEnumerable(); // BEGIN foreach (var item in enm) { int x = 0; } // END }", @" for (var $tmp1 = 0; $tmp1 < $enm.$Length; $tmp1++) { var $item = $enm[$tmp1]; var $x = 0; } ", metadataImporter: new MockMetadataImporter { GetMethodSemantics = m => m.Name == "GetEnumerator" ? MethodScriptSemantics.InlineCode("X", enumerateAsArray: true) : MethodScriptSemantics.NormalMethod("$" + m.Name) }); }
public void MethodImplementedAsInlineCodeDoesNotAppearOnTheType() { var metadataImporter = new MockMetadataImporter { GetMethodSemantics = method => MethodScriptSemantics.InlineCode("X") }; Compile(new[] { "class C { public static void M() {} }" }, metadataImporter: metadataImporter); FindClass("C").InstanceMethods.Should().BeEmpty(); }
public void MethodImplementedAsInlineCodeWithGeneratedMethodNameDoesAppearOnTheType() { var metadataImporter = new MockMetadataImporter { GetMethodSemantics = method => MethodScriptSemantics.InlineCode("X", generatedMethodName: "someMethod") }; Compile(new[] { "class C { public static void M() {} }" }, metadataImporter: metadataImporter); var m = FindInstanceMethod("C.someMethod"); m.Definition.Should().NotBeNull(); }
public void UsingTypeReferenceFromLiteralCodeWorks() { AssertCorrect( @"public void F() {} public void M() { // BEGIN F(); // END }", @" [{sm_String}, {sm_Int32}]; ", metadataImporter: new MockMetadataImporter { GetMethodSemantics = m => m.Name == "F" ? MethodScriptSemantics.InlineCode("[ {$System.String}, {$System.Int32} ]") : MethodScriptSemantics.NormalMethod(m.Name) }); }
public void UsingLiteralStringParameterToUseAsIdentifierFromLiteralCodeWorks() { AssertCorrect( @"public void F(string s) {} public void M() { // BEGIN F(""X""); // END }", @" invoke_X; ", metadataImporter: new MockMetadataImporter { GetMethodSemantics = m => m.Name == "F" ? MethodScriptSemantics.InlineCode("invoke_{@s}") : MethodScriptSemantics.NormalMethod(m.Name) }); }
public void UsingMethodTypeArgumentsFromLiteralCodeWorks() { AssertCorrect( @"public object F<T1, T2>(T1 arg1, T2 arg2) { return null; } public void M() { // BEGIN F(45, ""test""); // END }", @" ({ item1: {ga_Int32}, item2: {ga_String} }); ", metadataImporter: new MockMetadataImporter { GetMethodSemantics = m => m.Name == "F" ? MethodScriptSemantics.InlineCode("{{ item1: {T1}, item2: {T2} }}") : MethodScriptSemantics.NormalMethod(m.Name) }); }
public void UsingTextWithBracesInLiteralCodeWorks() { AssertCorrect( @"public object F(int arg1, string arg2) {} public void M() { // BEGIN F(45, ""test""); // END }", @" ({}); ", metadataImporter: new MockMetadataImporter { GetMethodSemantics = m => m.Name == "F" ? MethodScriptSemantics.InlineCode("{{ }}") : MethodScriptSemantics.NormalMethod(m.Name) }); }
public void ReadingPropertyWithGetMethodImplementedAsInlineCodeWorks() { AssertCorrect( @"int P { get; set; } public void M() { // BEGIN int i = P; // END }", @" var $i = get_(this); ", metadataImporter: new MockMetadataImporter { GetPropertySemantics = p => PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.InlineCode("get_({this})"), MethodScriptSemantics.InlineCode("set_({this})._({value})")) }); }
public void UsingParametersFromLiteralCodeWorks() { AssertCorrect( @"public object F(int arg1, string arg2) { return null; } public void M() { // BEGIN F(45, ""test""); // END }", @" ({ item1: 45, item2: 'test' }); ", metadataImporter: new MockMetadataImporter { GetMethodSemantics = m => m.Name == "F" ? MethodScriptSemantics.InlineCode("{{ item1: {arg1}, item2: {arg2} }}") : MethodScriptSemantics.NormalMethod(m.Name) }); }
public void CompoundAssigningToPropertyWithSetMethodImplementedAsLiteralCodeWorks() { AssertCorrectForBulkOperators( @"int P { get; set; } public void M() { int i = 0; // BEGIN P += i; // END }", @" set_(this)._(get_(this) + $i); ", metadataImporter: new MockMetadataImporter { GetPropertySemantics = p => PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.InlineCode("get_({this})"), MethodScriptSemantics.InlineCode("set_({this})._({value})")) }); }
public void AnErrorIsIssuedIfTheMainMethodIsNotImplementedAsANormalMethod() { var er = new MockErrorReporter(); var invoker = new OOPEmulatorInvoker(new MockOOPEmulator(), new MockMetadataImporter { GetMethodSemantics = m => m.Name == "Main" ? MethodScriptSemantics.InlineCode("X") : MethodScriptSemantics.NormalMethod(m.Name) }, er); var cu = new CSharpParser().Parse(@"class MyClass { public void Main() { } }", "file.cs").ToTypeSystem(); var compilation = new CSharpProjectContent().AddOrUpdateFiles(new IUnresolvedFile[] { cu }).AddAssemblyReferences(new[] { MinimalCorlib.Instance }).CreateCompilation(); var typeResolveContext = new SimpleTypeResolveContext(compilation.MainAssembly); invoker.Process(cu.GetAllTypeDefinitions().Select(t => new JsClass(t.Resolve(typeResolveContext).GetDefinition())).ToList <JsType>(), compilation.FindType(new FullTypeName("MyClass")).GetMethods().Single(m => m.Name == "Main")); Assert.That(er.AllMessages, Has.Count.EqualTo(1)); Assert.That(er.AllMessages.Any(m => m.Code == 7801 && (string)m.Args[0] == "MyClass.Main")); }
public void AssigningToPropertyWithSetMethodImplementedAsInlineCodeWorksStruct() { AssertCorrect( @"int P { get; set; } public void M() { int i = 0; // BEGIN P = i; // END }", @" set_(this)._($Clone($i, {to_Int32})); ", metadataImporter: new MockMetadataImporter { GetPropertySemantics = p => PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.InlineCode("get_({this})"), MethodScriptSemantics.InlineCode("set_({this})._({value})")), GetTypeSemantics = t => TypeScriptSemantics.MutableValueType(t.Name) }); }
public void UsingThisInInlineCodeForInstanceMethodWorks() { AssertCorrect( @"class C1 { public void F() {} } public void M() { var c = new C1(); // BEGIN c.F(); // END }", @" [$c]; ", metadataImporter: new MockMetadataImporter { GetMethodSemantics = m => m.Name == "F" ? MethodScriptSemantics.InlineCode("[ {this} ]") : MethodScriptSemantics.NormalMethod(m.Name) }); }
public void PostfixForPropertyWithSetMethodImplementedAsInlineCodeWorks() { AssertCorrect( @"int P { get; set; } public void M() { int i = 0; // BEGIN ++P; // END }", @" set_(this)._(get_(this) + 1); ", metadataImporter: new MockMetadataImporter { GetPropertySemantics = p => PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.InlineCode("get_({this})"), MethodScriptSemantics.InlineCode("set_({this})._({value})")) }); }
public void UsingEventRemoveAccessorImplementedAsInlineCodeWorks() { AssertCorrect( @"event System.EventHandler MyEvent; public void M() { System.EventHandler h = null; // BEGIN MyEvent -= h; // END }", @" remove_(this)._($h); ", metadataImporter: new MockMetadataImporter() { GetEventSemantics = e => EventScriptSemantics.AddAndRemoveMethods(MethodScriptSemantics.InlineCode("add_({this})._({value})"), MethodScriptSemantics.InlineCode("remove_({this})._({value})")) }); }
public void MethodGroupConversionOnInlineCodeStaticMethodPrefixedWithClassNameWorks() { AssertCorrect( @"static void F(int x) {} public void M() { System.Action<int> f; // BEGIN f = C.F; // END }", @" $f = function($tmp1) { $tmp1; }; ", metadataImporter: new MockMetadataImporter { GetMethodSemantics = m => m.Name == "F" ? MethodScriptSemantics.InlineCode("{x}") : MethodScriptSemantics.NormalMethod(m.Name) }); }
public void CanPerformMethodGroupConversionOnInlineCodeMethodWithoutReturnValue() { AssertCorrect( @"private int i; public void F<T>(int a, int b) {} public void M() { // BEGIN Action<int, int> f = F<string>; // END } ", @" var $f = function($tmp1, $tmp2) { _($tmp1)._($tmp2)._({ga_String}); }; ", metadataImporter: new MockMetadataImporter { GetMethodSemantics = m => m.Name == "F" ? MethodScriptSemantics.InlineCode("_({a})._({b})._({T})") : MethodScriptSemantics.NormalMethod(m.Name) }); }
public void CanPerformMethodGroupConversionOnInlineCodeMethodThatUsesThis() { AssertCorrect( @"private int i; public int F<T>(int a, int b) { return 0; } public void M() { // BEGIN Func<int, int, int> f = F<string>; // END } ", @" var $f = $Bind(function($tmp1, $tmp2) { return _(this)._($tmp1)._($tmp2)._({ga_String}); }, this); ", metadataImporter: new MockMetadataImporter { GetMethodSemantics = m => m.Name == "F" ? MethodScriptSemantics.InlineCode("_({this})._({a})._({b})._({T})") : MethodScriptSemantics.NormalMethod(m.Name) }); }
public void InvokingMethodImplementedAsInlineCodeWorks() { AssertCorrect( @"class X<T1> { public class Y<T2> { public void F<T3>(T1 x, T2 y, T3 z) {} } } public void M() { X<int>.Y<byte> o = null; int a = 0; byte b = 0; string c = null; // BEGIN o.F(a, b, c); // END }", @" _({ga_Int32})._({ga_Byte})._({ga_String})._($o)._($a)._($b)._($c); ", metadataImporter: new MockMetadataImporter { GetMethodSemantics = m => m.Name == "F" ? MethodScriptSemantics.InlineCode("_({T1})._({T2})._({T3})._({this})._({x})._({y})._({z})") : MethodScriptSemantics.NormalMethod("$" + m.Name) }); }
public void CannotPerformMethodGroupConversionOnInlineCodeMethodThatIncludesAParameterAsLiteralText() { var er = new MockErrorReporter(false); Compile(new[] { @"class C1 { public int F1(string a) { return 0; } public void M() { System.Func<string, int> f = F1; } } " }, metadataImporter: new MockMetadataImporter { GetMethodSemantics = m => m.Name == "F1" ? MethodScriptSemantics.InlineCode("_({@a})") : MethodScriptSemantics.NormalMethod(m.Name) }, errorReporter: er); Assert.That(er.AllMessages.Count, Is.EqualTo(1)); Assert.That(er.AllMessages.Any(msg => msg.Severity == MessageSeverity.Error && msg.Code == 7523 && msg.FormattedMessage.Contains("C1.F1") && msg.FormattedMessage.Contains("literal string as code"))); }
public void InvokingInlineCodeMethodWithTypeParameterAsTypeArgumentWorks() { AssertCorrect( @"public class C2<T1> { public void F<T2>() {} } public class C<T3> { public void M<T4>() { var c = new C2<T3>(); // BEGIN c.F<T4>(); // END } }", @" $T3._($T4); ", addSkeleton: false, metadataImporter: new MockMetadataImporter { GetMethodSemantics = m => m.Name == "F" ? MethodScriptSemantics.InlineCode("{T1}._({T2})") : MethodScriptSemantics.NormalMethod(m.Name) }); }
public void MethodGroupConversionOnInlineCodeMethodOnAnotherTargetWorks() { AssertCorrect( @"private int i; public int F<T>(int a, int b) { return 0; } public void M() { C c = null; // BEGIN Func<int, int, int> f = c.F<string>; // END } ", @" var $f = function($tmp1, $tmp2) { return _($c)._($tmp1)._($tmp2)._({ga_String}); }; ", metadataImporter: new MockMetadataImporter { GetMethodSemantics = m => m.Name == "F" ? MethodScriptSemantics.InlineCode("_({this})._({a})._({b})._({T})") : MethodScriptSemantics.NormalMethod(m.Name) }); }
public void MethodGroupConversionOnInlineCodeMethodUsesNonExpandedFormPattern() { AssertCorrect( @"class C1 { public int F1(params object[] a) { return 0; } public void M() { // BEGIN System.Func<object[], int> f = F1; // END } } ", @" var $f = function($tmp1) { return _2($tmp1); }; ", metadataImporter: new MockMetadataImporter { GetMethodSemantics = m => m.Name == "F1" ? MethodScriptSemantics.InlineCode("_({*a})", nonExpandedFormLiteralCode: "_2({a})") : MethodScriptSemantics.NormalMethod(m.Name) }); }
public void InlineCodeWithSyntaxErrorIsAnError() { var er = new MockErrorReporter(false); Compile(new[] { @"class C { public void F(string p1, int p2, params string[] p3) {} public void M() { string[] args = null; // BEGIN F(""x"", 1, ""y"", ""z""); // END } }" }, metadataImporter: new MockMetadataImporter { GetMethodSemantics = m => m.Name == "F" ? MethodScriptSemantics.InlineCode("{p1}*{p2}+") : MethodScriptSemantics.NormalMethod(m.Name) }, errorReporter: er); Assert.That(er.AllMessages.Count, Is.EqualTo(1)); Assert.That(er.AllMessages.Any(m => m.FormattedMessage.Contains("syntax error"))); }
public void InvokingMethodThatExpectsLiteralStringWithSomethingElseIsAnError() { var er = new MockErrorReporter(false); Compile(new[] { @"class C { public void F(string myParameter) {} public void M() { string s = ""X""; // BEGIN F(s); // END } }" }, metadataImporter: new MockMetadataImporter { GetMethodSemantics = m => m.Name == "F" ? MethodScriptSemantics.InlineCode("invoke_{@myParameter}") : MethodScriptSemantics.NormalMethod(m.Name) }, errorReporter: er); Assert.That(er.AllMessages.Count, Is.EqualTo(1)); Assert.That(er.AllMessages.Any(m => m.FormattedMessage.Contains("myParameter") && m.FormattedMessage.Contains("literal string"))); }