public void InstanceAutoPropertyAccessorsImplementedAsStaticMethodsAreCorrectlyCompiled()
        {
            Compile(new[] { "using System; class C { public int MyProperty { get; set; } }" }, metadataImporter: new MockMetadataImporter {
                GetPropertySemantics = p => PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("get_" + p.Name), MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("set_" + p.Name))
            });

            var getter = FindStaticMethod("C.get_MyProperty");
            var setter = FindStaticMethod("C.set_MyProperty");

            AssertCorrect(getter.Definition,
                          @"function($this) {
	return $this.$MyProperty;
}");

            AssertCorrect(setter.Definition,
                          @"function($this, $value) {
	$this.$MyProperty = $value;
}");

            AssertCorrect(FindClass("C").UnnamedConstructor,
                          @"function() {
	$Init(this, '$MyProperty', $Default({def_Int32}));
	{sm_Object}.call(this);
}");
        }
Exemple #2
0
        public void GetEnumeratorAsStaticMethodWithThisAsFirstArgumentWithEnumerateAsArray()
        {
            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.StaticMethodWithThisAsFirstArgument("$" + m.Name, enumerateAsArray: true) : MethodScriptSemantics.NormalMethod("$" + m.Name)
            });
        }
Exemple #3
0
        public void InstanceAutoEventAccessorsImplementedAsStaticMethodsAreCorrectlyCompiled()
        {
            Compile(new[] { "using System; class C { public event System.EventHandler MyEvent; }" }, metadataImporter: new MockMetadataImporter {
                GetEventSemantics = e => EventScriptSemantics.AddAndRemoveMethods(MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("add_" + e.Name), MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("remove_" + e.Name))
            });

            var adder   = FindStaticMethod("C.add_MyEvent");
            var remover = FindStaticMethod("C.remove_MyEvent");

            AssertCorrect(adder.Definition,
                          @"function($this, $value) {
	$this.$MyEvent = {sm_Delegate}.Combine($this.$MyEvent, $value);
}");

            AssertCorrect(remover.Definition,
                          @"function($this, $value) {
	$this.$MyEvent = {sm_Delegate}.Remove($this.$MyEvent, $value);
}");

            AssertCorrect(FindClass("C").UnnamedConstructor,
                          @"function() {
	this.$MyEvent = $Default({def_EventHandler});
	{sm_Object}.call(this);
}");
        }
Exemple #4
0
        public void StaticMethodWithThisAsFirstArgumentAppearsOnTheType()
        {
            var metadataImporter = new MockMetadataImporter {
                GetMethodSemantics = method => MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("X")
            };

            Compile(new[] { "class C { public static void M() {} }" }, metadataImporter: metadataImporter);
            FindClass("C").InstanceMethods.Should().BeEmpty();
            FindStaticMethod("C.X").Should().NotBeNull();
        }
Exemple #5
0
        public void ExpandedParamArrayIsFixedAtTheTopOfMethodsForStaticMethodsWithThisAsFirstArgument()
        {
            AssertCorrect(
                @"void M(int a, int b, params int[] c) {}",
                @"function($this, $a, $b) {
	var $c = Array.prototype.slice.call(arguments, 3);
}", metadataImporter: new MockMetadataImporter {
                GetMethodSemantics = m => MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("$" + m.Name, expandParams: true)
            });
        }
        public void StaticMethodWithThisAsFirstArgumentCanBeCompiled()
        {
            AssertCorrect(@"
int M(int a, string b) {
	return a;
}",
                          @"function($this, $a, $b) {
	return $a;
}", metadataImporter: new MockMetadataImporter {
                GetMethodSemantics = m => MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("$" + m.Name)
            });
        }
Exemple #7
0
		public void ExpressionLambdaThatUsesThisIsNotBoundInAStaticMethodWithThisAsFirstArgument() {
			AssertCorrect(
@"int x;
public void M() {
	// BEGIN
	Func<int> f = () => x;
	// END
}",
@"	var $f = function() {
		return $this.$x;
	};
", metadataImporter: new MockMetadataImporter { GetMethodSemantics = m => MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("$" + m.Name) });
		}
        public void ThisWorksInsideStaticMethodWithThisAsFirstArgument()
        {
            AssertCorrect(
                @"int x;
public void M() {
	// BEGIN
	int i = x;
	// END
}",
                @"	var $i = $this.$x;
", metadataImporter: new MockMetadataImporter {
                GetMethodSemantics = m => MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("$" + m.Name)
            });
        }
Exemple #9
0
        public void GenericStaticMethodWithThisAsFirstArgumentWorks()
        {
            AssertCorrect(
                @"void F<T1, T2>(T1 x, int y, T2 z) {}
public void M() {
	int a = 0, b = 0;
	string c = null;
	// BEGIN
	F(a, b, c);
	// END
}",
                @"	$InstantiateGenericMethod({sm_C}.$F, {ga_Int32}, {ga_String}).call(null, this, $a, $b, $c);
", metadataImporter: new MockMetadataImporter {
                GetMethodSemantics = m => m.Name == "F" ? MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("$" + m.Name) : MethodScriptSemantics.NormalMethod("$" + m.Name)
            });
        }
Exemple #10
0
        public void GlobalMethodWithThisAsFirstArgumentWorks()
        {
            AssertCorrect(
                @"void F(int x, int y, string z) {}
public void M() {
	int a = 0, b = 0;
	string c = null;
	// BEGIN
	F(a, b, c);
	// END
}",
                @"	$F(this, $a, $b, $c);
", metadataImporter: new MockMetadataImporter {
                GetMethodSemantics = m => m.Name == "F" ? MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("$" + m.Name, isGlobal: true) : MethodScriptSemantics.NormalMethod(m.Name)
            });
        }
        public void ThisWorksInsideAnonymousMethodInsideStaticMethodWithThisAsFirstArgument()
        {
            // It works like this because the anonymous method conversion will perform the correct bind.
            AssertCorrect(
                @"int x;
public void M() {
	Action a = () => {
		// BEGIN
		int i = x;
		// END
	};
}",
                @"		var $i = $this.$x;
", metadataImporter: new MockMetadataImporter {
                GetMethodSemantics = m => MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("$" + m.Name)
            });
        }
        public void CanPerformMethodGroupConversionOnStaticMethodWithThisAsFirstArgumentWithoutReturnValue()
        {
            AssertCorrect(
                @"public void F(int a, int b) { return 0; }
public void M() {
	// BEGIN
	Action<int, int> f = F;
	// END
}
",
                @"	var $f = $Bind(function($tmp1, $tmp2) {
		{sm_C}.$F(this, $tmp1, $tmp2);
	}, this);
", metadataImporter: new MockMetadataImporter {
                GetMethodSemantics = m => m.Name == "F" ? MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("$F") : MethodScriptSemantics.NormalMethod(m.Name)
            });
        }
Exemple #13
0
        public void StaticMethodWithThisAsFirstArgumentDeclaredInGenericTypeWorks()
        {
            AssertCorrect(
                @"class X<TX> { public void F(TX x, int y, string z) {} }
X<int> FX() { return null; }
public void M() {
	int a = 0, b = 0;
	string c = null;
	// BEGIN
	FX().F(a, b, c);
	// END
}",
                @"	sm_$InstantiateGenericType({X}, {ga_Int32}).$F(this.$FX(), $a, $b, $c);
", metadataImporter: new MockMetadataImporter {
                GetMethodSemantics = m => m.Name == "F" ? MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("$" + m.Name) : MethodScriptSemantics.NormalMethod("$" + m.Name)
            });
        }
        public void MethodGroupConversionOnStaticMethodWithThisAsFirstArgumentOnAnotherTargetWorks()
        {
            AssertCorrect(
                @"public void F(int a, int b) {}
public void M() {
	C c;
	// BEGIN
	Action<int, int> f = c.F;
	// END
}
",
                @"	var $f = $Bind(function($tmp1, $tmp2) {
		{sm_C}.$F(this, $tmp1, $tmp2);
	}, $c);
", metadataImporter: new MockMetadataImporter {
                GetMethodSemantics = m => m.Name == "F" ? MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("$F") : MethodScriptSemantics.NormalMethod(m.Name)
            });
        }
        public void CannotPerformMethodGroupConversionOnStaticMethodWithThisAsFirstArgumentThatExpandsParamsToDelegateThatDoesNot()
        {
            var er = new MockErrorReporter(false);

            Compile(new[] {
                @"class C1 {
	public void F(int x, int y, params int[] args) {}
	public void M() {
		System.Action<int, int, int[]> a = F;
	}
}"
            }, metadataImporter: new MockMetadataImporter {
                GetMethodSemantics = m => m.Name == "F" ? MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("$F", expandParams: true) : MethodScriptSemantics.NormalMethod(m.Name)
            }, errorReporter: er);

            Assert.That(er.AllMessages.Count, Is.EqualTo(1));
            Assert.That(er.AllMessages[0].FormattedMessage.Contains("C1.F") && er.AllMessages[0].FormattedMessage.Contains("System.Action") && er.AllMessages[0].FormattedMessage.Contains("expand") && er.AllMessages[0].FormattedMessage.Contains("param array"));
        }
        public void CanPerformMethodGroupConversionOnStaticMethodWithThisAsFirstArgumentInGenericType()
        {
            AssertCorrect(
                @"class C<T1, T2> {
	public int F(int a, int b) { return 0; }
	public void M() {
		// BEGIN
		System.Func<int, int, int> f = F;
		// END
	}
}
",
                @"	var $f = $Bind(function($tmp1, $tmp2) {
		return sm_$InstantiateGenericType({C}, $T1, $T2).$F(this, $tmp1, $tmp2);
	}, this);
", metadataImporter: new MockMetadataImporter {
                GetMethodSemantics = m => m.Name == "F" ? MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("$F") : MethodScriptSemantics.NormalMethod(m.Name)
            }, addSkeleton: false);
        }
Exemple #17
0
        public void StaticMethodWithThisAsFirstArgumentWorksWithReorderedAndDefaultArguments()
        {
            AssertCorrect(
                @"void F(int a = 1, int b = 2, int c = 3, int d = 4, int e = 5, int f = 6, int g = 7) {}
int F1() { return 0; }
int F2() { return 0; }
int F3() { return 0; }
int F4() { return 0; }
public void M() {
	// BEGIN
	F(d: F1(), g: F2(), f: F3(), b: F4());
	// END
}
",
                @"	var $tmp1 = this.$F1();
	var $tmp2 = this.$F2();
	var $tmp3 = this.$F3();
	{sm_C}.$F(this, 1, this.$F4(), 3, $tmp1, 5, $tmp3, $tmp2);
", metadataImporter: new MockMetadataImporter {
                GetMethodSemantics = m => m.Name == "F" ? MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("$" + m.Name) : MethodScriptSemantics.NormalMethod("$" + m.Name)
            });
        }
        public void InstanceAutoPropertiesWithGetSetMethodsStaticWithNoCodeAreCorrectlyImported()
        {
            var metadataImporter = new MockMetadataImporter {
                GetPropertySemantics            = p => PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("get_" + p.Name, generateCode: false), MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("set_" + p.Name, generateCode: false)),
                GetAutoPropertyBackingFieldName = p => { throw new InvalidOperationException("Shouldn't be called"); }
            };

            Compile(new[] { "class C { public string SomeProp { get; set; } }" }, metadataImporter: metadataImporter);

            Assert.That(FindInstanceMethod("C.get_SomeProp"), Is.Null);
            Assert.That(FindInstanceMethod("C.set_SomeProp"), Is.Null);
            Assert.That(FindStaticMethod("C.get_SomeProp"), Is.Null);
            Assert.That(FindStaticMethod("C.set_SomeProp"), Is.Null);
            FindClass("C").UnnamedConstructor.Body.Statements.Should().BeEmpty();
            Assert.That(FindClass("C").StaticInitStatements, Is.Empty);
        }
Exemple #19
0
        public void InstanceAutoPropertiesWithGetSetMethodsStaticWithNoCodeAreCorrectlyImported()
        {
            var metadataImporter = new MockMetadataImporter {
                GetConstructorSemantics = c => ConstructorScriptSemantics.Unnamed(skipInInitializer: c.DeclaringType.IsKnownType(KnownTypeCode.Object)),
                GetPropertySemantics    = p => PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("get_" + p.Name, generateCode: false), MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("set_" + p.Name, generateCode: false)),
            };

            Compile(new[] { "class C { public string SomeProp { get; set; } }" }, metadataImporter: metadataImporter);

            Assert.That(FindInstanceMethod("C.get_SomeProp"), Is.Null);
            Assert.That(FindInstanceMethod("C.set_SomeProp"), Is.Null);
            Assert.That(FindStaticMethod("C.get_SomeProp"), Is.Null);
            Assert.That(FindStaticMethod("C.set_SomeProp"), Is.Null);
            FindInstanceFieldInitializer("C.$SomeProp").Should().NotBeNull();
            Assert.That(FindClass("C").StaticInitStatements, Is.Empty);
        }
        public void CanPerformMethodGroupConversionOnStaticMethodWithThisAsFirstArgumentWhenBothTheMethodAndTheDelegateTypeExpandsParamArray()
        {
            AssertCorrect(
                @"public void F(int a, int b) { return 0; }
public void M() {
	// BEGIN
	Action<int, int> f = F;
	// END
}
",
                @"	var $f = $Bind(function() {
		{sm_C}.$F.apply(null, [this].concat(Array.prototype.slice.call(arguments)));
	}, this);
", metadataImporter: new MockMetadataImporter {
                GetDelegateSemantics = d => new DelegateScriptSemantics(expandParams: true), GetMethodSemantics = m => m.Name == "F" ? MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("$F", expandParams: true) : MethodScriptSemantics.NormalMethod(m.Name)
            });
        }
        public void CanPerformMethodGroupConversionOnStaticMethodWithThisAsFirstArgumentWhenDelegateTypeUsesBindThisToFirstParameter()
        {
            AssertCorrect(
                @"public void F(int a, int b) { return 0; }
public void M() {
	// BEGIN
	Action<int, int> f = F;
	// END
}
",
                @"	var $f = $BindFirstParameterToThis($Bind(function($tmp1, $tmp2) {
		{sm_C}.$F(this, $tmp1, $tmp2);
	}, this));
", metadataImporter: new MockMetadataImporter {
                GetDelegateSemantics = d => new DelegateScriptSemantics(bindThisToFirstParameter: true), GetMethodSemantics = m => m.Name == "F" ? MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("$F") : MethodScriptSemantics.NormalMethod(m.Name)
            });
        }