Beispiel #1
0
        public void ChainingToGlobalStaticMethodConstructorFromAnotherStaticMethodConstructorWorks()
        {
            AssertCorrect(
                @"class C {
	static int F1() { return 0; }
	static int F2() { return 0; }
	static int F3() { return 0; }
	static int F4() { return 0; }

	public void M() {}

	public C(int a = 1, int b = 2, int c = 3, int d = 4, int e = 5, int f = 6, int g = 7) {
	}

	[System.Runtime.CompilerServices.CompilerGenerated]
	public C() : this(d: F1(), g: F2(), f: F3(), b: F4()) {
		M();
	}
}",
                @"function() {
	var $tmp1 = {sm_C}.F1();
	var $tmp2 = {sm_C}.F2();
	var $tmp3 = {sm_C}.F3();
	var $this = ctor$7(1, {sm_C}.F4(), 3, $tmp1, 5, $tmp3, $tmp2);
	$this.M();
	return $this;
}", metadataImporter: new MockMetadataImporter {
                GetConstructorSemantics = c => ConstructorScriptSemantics.StaticMethod("ctor$" + c.Parameters.Count.ToString(CultureInfo.InvariantCulture), isGlobal: true)
            });
        }
Beispiel #2
0
        public void ChainingToAnonymousConstructorFromStaticMethodConstructorWorks()
        {
            AssertCorrect(
                @"class C {
	static int F1() { return 0; }
	static int F2() { return 0; }
	static int F3() { return 0; }
	static int F4() { return 0; }

	public void M() {}

	public C(int a = 1, int b = 2, int c = 3, int d = 4, int e = 5, int f = 6, int g = 7) {
	}

	[System.Runtime.CompilerServices.CompilerGenerated]
	public C() : this(d: F1(), g: F2(), f: F3(), b: F4()) {
		M();
	}
}",
                @"function() {
	var $tmp1 = {sm_C}.F1();
	var $tmp2 = {sm_C}.F2();
	var $tmp3 = {sm_C}.F3();
	var $this = new {inst_C}(1, {sm_C}.F4(), 3, $tmp1, 5, $tmp3, $tmp2);
	$this.M();
	return $this;
}", metadataImporter: new MockMetadataImporter {
                GetConstructorSemantics = c => c.Parameters.Count == 0 ? ConstructorScriptSemantics.StaticMethod("ctor") : ConstructorScriptSemantics.Unnamed()
            });
        }
        public void CanCallStaticMethodConstructorWithReorderedAndDefaultArguments()
        {
            AssertCorrect(
                @"class X {
	public X(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() {
	int a = 0, b = 0, c = 0;
	// BEGIN
	var x = new X(d: F1(), g: F2(), f: F3(), b: F4());
	// END
}",
                @"	var $tmp1 = this.$F1();
	var $tmp2 = this.$F2();
	var $tmp3 = this.$F3();
	var $x = {sm_X}.create_X(1, this.$F4(), 3, $tmp1, 5, $tmp3, $tmp2);
", metadataImporter: new MockMetadataImporter {
                GetConstructorSemantics = c => ConstructorScriptSemantics.StaticMethod("create_" + c.DeclaringType.Name), GetMethodSemantics = m => MethodScriptSemantics.NormalMethod("$" + m.Name)
            });
        }
Beispiel #4
0
        public void ChainingToStaticMethodParamArrayConstructorThatExpandsArgumentsInNonExpandedFormWorks()
        {
            AssertCorrect(
                @"class C1 {
	static int[] args = new[] { 59, 12, 4 };
	public C1(int x, int y, params int[] args) {}

	[System.Runtime.CompilerServices.CompilerGenerated]
	public C1() : this(4, 8, args) {}
}",
                @"function() {
	var $this = {sm_C1}.ctor$3.apply(null, [4, 8].concat({sm_C1}.$args));
	return $this;
}", metadataImporter: new MockMetadataImporter {
                GetConstructorSemantics = c => ConstructorScriptSemantics.StaticMethod("ctor$" + c.Parameters.Count.ToString(CultureInfo.InvariantCulture), expandParams: true)
            });

            AssertCorrect(
                @"class C1 {
	public C1(int x, int y, params int[] args) {}

	[System.Runtime.CompilerServices.CompilerGenerated]
	public C1() : this(4, 8, new[] { 59, 12, 4 }) {}
}",
                @"function() {
	var $this = {sm_C1}.ctor$3(4, 8, 59, 12, 4);
	return $this;
}", metadataImporter: new MockMetadataImporter {
                GetConstructorSemantics = c => ConstructorScriptSemantics.StaticMethod("ctor$" + c.Parameters.Count.ToString(CultureInfo.InvariantCulture), expandParams: true)
            });
        }
Beispiel #5
0
        public void ChainingToJsonConstructorFromStaticMethodConstructorWorks()
        {
            AssertCorrect(
                @"class C {
	static int F1() { return 0; }
	static int F2() { return 0; }
	static int F3() { return 0; }
	static int F4() { return 0; }

	public int A, B, C, D, E, F, G;

	public void M() {}

	public C(int a = 1, int b = 2, int c = 3, int d = 4, int e = 5, int f = 6, int g = 7) {
	}

	[System.Runtime.CompilerServices.CompilerGenerated]
	public C() : this(d: F1(), g: F2(), f: F3(), b: F4()) {
		M();
	}
}",
                @"function() {
	var $this = { $D: {sm_C}.F1(), $G: {sm_C}.F2(), $F: {sm_C}.F3(), $B: {sm_C}.F4(), $A: 1, $C: 3, $E: 5 };
	$this.M();
	return $this;
}", metadataImporter: new MockMetadataImporter {
                GetConstructorSemantics = c => c.Parameters.Count == 0 ? ConstructorScriptSemantics.StaticMethod("X") : ConstructorScriptSemantics.Json(c.Parameters.Select(p => c.DeclaringType.GetFields().Single(x => x.Name.Equals(p.Name, StringComparison.InvariantCultureIgnoreCase))))
            });
        }
Beispiel #6
0
        public void ReturningFromAStaticMethodConstructorReturnsTheCreatedObject()
        {
            AssertCorrect(
                @"class C {
	public void M() {}

	[System.Runtime.CompilerServices.CompilerGenerated]
	public D() {
		if (false) {
			System.Func<int, int> a = i => i + 1;
			return;
		}
		this.M();
		return;
	}
}",
                @"function() {
	var $this = {};
	if (false) {
		var $a = function($i) {
			return $i + 1;
		};
		return $this;
	}
	$this.M();
	return $this;
}", metadataImporter: new MockMetadataImporter {
                GetConstructorSemantics = c => ConstructorScriptSemantics.StaticMethod("ctor")
            });
        }
        public void DefaultConstructorImplementedAsStaticMethodWorks()
        {
            var metadataImporter = new MockMetadataImporter {
                GetConstructorSemantics = ctor => ConstructorScriptSemantics.StaticMethod("X")
            };

            Compile(new[] { "class C { }" }, metadataImporter: metadataImporter);
            FindStaticMethod("C.X").Should().NotBeNull();
            FindNamedConstructor("C.X").Should().BeNull();
        }
        public void ConstructorImplementedAsStaticMethodGetsAddedToTheStaticMethodsCollectionAndNotTheConstructors()
        {
            var metadataImporter = new MockMetadataImporter {
                GetConstructorSemantics = ctor => ConstructorScriptSemantics.StaticMethod("X")
            };

            Compile(new[] { "class C { public C() {} }" }, metadataImporter: metadataImporter);
            FindStaticMethod("C.X").Should().NotBeNull();
            FindNamedConstructor("C.X").Should().BeNull();
        }
        public void InvokingStaticMethodParamArrayConstructorThatExpandsArgumentsInExpandedFormWorks()
        {
            AssertCorrect(
                @"class C1 { public C1(int x, int y, params int[] args) {} }
public void M() {
	// BEGIN
	var c = new C1(4, 8, 59, 12, 4);
	// END
}",
                @"	var $c = {sm_C1}.X(4, 8, 59, 12, 4);
", metadataImporter: new MockMetadataImporter {
                GetConstructorSemantics = c => ConstructorScriptSemantics.StaticMethod("X", expandParams: true)
            });
        }
        public void CanCallStaticMethodConstructorWithNoArguments()
        {
            AssertCorrect(
                @"class X {
}
public void M() {
	// BEGIN
	var c = new X();
	// END
}",
                @"	var $c = {sm_X}.create_X();
", metadataImporter: new MockMetadataImporter {
                GetConstructorSemantics = c => ConstructorScriptSemantics.StaticMethod("create_" + c.DeclaringType.Name)
            });
        }
Beispiel #11
0
        public void ChainingToStaticMethodParamArrayConstructorThatDoesNotExpandArgumentsInExpandedFormWorks()
        {
            AssertCorrect(
                @"class C1 {
	public C1(int x, int y, params int[] args) {}

	[System.Runtime.CompilerServices.CompilerGenerated]
	public C1() : this(4, 8, 59, 12, 4) {}
}",
                @"function() {
	var $this = {sm_C1}.ctor$3(4, 8, [59, 12, 4]);
	return $this;
}", metadataImporter: new MockMetadataImporter {
                GetConstructorSemantics = c => ConstructorScriptSemantics.StaticMethod("ctor$" + c.Parameters.Count.ToString(CultureInfo.InvariantCulture))
            });
        }
        public void CanCallStaticMethodConstructorWithArguments()
        {
            AssertCorrect(
                @"class X {
	public X(int x, int y, int z) {}
}
public void M() {
	int a = 0, b = 0, c = 0;
	// BEGIN
	var t = new X(a, b, c);
	// END
}",
                @"	var $t = {sm_X}.create_X($a, $b, $c);
", metadataImporter: new MockMetadataImporter {
                GetConstructorSemantics = c => ConstructorScriptSemantics.StaticMethod("create_" + c.DeclaringType.Name)
            });
        }
        public void CreatingObjectWithDynamicArgumentGivesAnErrorWhenNamesDifferBetweenApplicableMethods()
        {
            var er = new MockErrorReporter();

            Compile(new[] {
                @"public class C1 {
	public C1(int x) {}
	public C1(string x) {}
}

public class C {
	public void M() {
		dynamic d = null;
		// BEGIN
		var c = new C1(d);
		// END
	}
}"
            }, metadataImporter: new MockMetadataImporter {
                GetConstructorSemantics = c => c.Parameters.Count > 0 ? ConstructorScriptSemantics.Named("C$" + c.Parameters[0].Type.Name) : ConstructorScriptSemantics.Unnamed()
            }, errorReporter: er);
            Assert.That(er.AllMessages.Count, Is.EqualTo(1));
            Assert.That(er.AllMessages.Any(m => m.Code == 7531));

            er = new MockErrorReporter();
            Compile(new[] {
                @"public class C1 {
	public C1(int x) {}
	public C1(string x) {}
}

public class C {
	public void M() {
		dynamic d = null;
		// BEGIN
		var c = new C1(d);
		// END
	}
}"
            }, metadataImporter: new MockMetadataImporter {
                GetConstructorSemantics = c => c.Parameters.Count > 0 ? ConstructorScriptSemantics.StaticMethod("C$" + c.Parameters[0].Type.Name) : ConstructorScriptSemantics.Unnamed()
            }, errorReporter: er);
            Assert.That(er.AllMessages.Count, Is.EqualTo(1));
            Assert.That(er.AllMessages.Any(m => m.Code == 7531));
        }
        public void CreatingObjectWithDynamicArgumentWorksWhenAllCandidatesAreStaticMethodsWithTheSameName()
        {
            AssertCorrect(
                @"public class C1 {
	public C1(int x) {}
	public C1(string x) {}
}

public void M() {
	dynamic d = null;
	// BEGIN
	var c = new C1(d);
	// END
}",
                @"	var $c = {sm_C1}.X($d);
", metadataImporter: new MockMetadataImporter {
                GetConstructorSemantics = c => ConstructorScriptSemantics.StaticMethod("X", generateCode: false)
            });
        }
Beispiel #15
0
        public void SimpleStaticMethodConstructorWithoutBaseCallWorks()
        {
            AssertCorrect(
                @"class C {
	public void M() {}

	[System.Runtime.CompilerServices.CompilerGenerated]
	public C() {
		this.M();
	}
}",
                @"function() {
	var $this = {};
	$this.M();
	return $this;
}", metadataImporter: new MockMetadataImporter {
                GetConstructorSemantics = c => ConstructorScriptSemantics.StaticMethod("ctor")
            });
        }
        public void InvokingStaticMethodParamArrayConstructorThatExpandsArgumentsInNonExpandedFormWorks()
        {
            AssertCorrect(
                @"public C(int x, int y, params int[] args) {}
public void M() {
	var args = new[] { 59, 12, 4 };
	// BEGIN
	var c1 = new C(4, 8, args);
	var c2 = new C(4, 8, new[] { 59, 12, 4 });
	// END
}",
                @"	var $c1 = {sm_C}.X.apply(null, [4, 8].concat($args));
	var $c2 = {sm_C}.X(4, 8, 59, 12, 4);
", metadataImporter: new MockMetadataImporter {
                GetConstructorSemantics = c => ConstructorScriptSemantics.StaticMethod("X", expandParams: true)
            });

            AssertCorrect(
                @"public C(int x, params int[] args) {}
public void M() {
	var args = new[] { 59, 12, 4 };
	// BEGIN
	var c = new C(4, args);
	// END
}",
                @"	var $c = {sm_C}.X.apply(null, [4].concat($args));
", metadataImporter: new MockMetadataImporter {
                GetConstructorSemantics = c => ConstructorScriptSemantics.StaticMethod("X", expandParams: true)
            });

            AssertCorrect(
                @"public C(params int[] args) {}
public void M() {
	var args = new[] { 59, 12, 4 };
	// BEGIN
	var c = new C(args);
	// END
}",
                @"	var $c = {sm_C}.X.apply(null, $args);
", metadataImporter: new MockMetadataImporter {
                GetConstructorSemantics = c => ConstructorScriptSemantics.StaticMethod("X", expandParams: true)
            });
        }
Beispiel #17
0
        public void InvokingStaticMethodBaseConstructorMarkedAsSkipInInitializerCreatesAnEmptyObjectLiteral()
        {
            AssertCorrect(
                @"class B {
}
class D : B {
	public void M() {}

	[System.Runtime.CompilerServices.CompilerGenerated]
	public D() : base() {
		this.M();
	}
}",
                @"function() {
	var $this = {};
	$this.M();
	return $this;
}", metadataImporter: new MockMetadataImporter {
                GetConstructorSemantics = c => ConstructorScriptSemantics.StaticMethod("construct_" + c.DeclaringType.Name, skipInInitializer: c.DeclaringType.Name == "B")
            });
        }
Beispiel #18
0
        public void FieldInitializationWorksForStaticMethodConstructors()
        {
            AssertCorrect(
                @"class C {
	int x = 1;
	public void M() {}

	[System.Runtime.CompilerServices.CompilerGenerated]
	public C() {
		M();
	}
}",
                @"function() {
	var $this = {};
	$this.$x = 1;
	$this.M();
	return $this;
}", metadataImporter: new MockMetadataImporter {
                GetConstructorSemantics = c => ConstructorScriptSemantics.StaticMethod("ctor")
            });
        }
Beispiel #19
0
        public void ChainingToConstructorImplementedAsInlineCodeFromStaticMethodConstructorWorks()
        {
            AssertCorrect(
                @"class C {
	public void M() {}

	[System.Runtime.CompilerServices.CompilerGenerated]
	public C() : this(0, ""X"") {
		M();
	}

	public C(int x, string s) {
	}
}",
                @"function() {
	var $this = __Literal_(0)._X__;
	$this.M();
	return $this;
}", metadataImporter: new MockMetadataImporter {
                GetConstructorSemantics = c => c.Parameters.Count == 0 ? ConstructorScriptSemantics.StaticMethod("M") : ConstructorScriptSemantics.InlineCode("__Literal_({x})._{@s}__")
            });
        }
        public void FieldInitializationWorksForStaticMethodConstructorsWhenCallingBase()
        {
            AssertCorrect(
                @"class B {
}
class D : B {
	int x = 1;
	public void M() {}

	[System.Runtime.CompilerServices.CompilerGenerated]
	public D() {
		this.M();
	}
}",
                @"function() {
	var $this = {sm_B}.ctor();
	$Init($this, '$x', 1);
	$this.M();
	return $this;
}", metadataImporter: new MockMetadataImporter {
                GetConstructorSemantics = c => ConstructorScriptSemantics.StaticMethod("ctor")
            });
        }
Beispiel #21
0
        public void ChainingToStaticMethodConstructorFromAnotherTypeOfConstructorWorks()
        {
            AssertCorrect(
                @"class C {
	public static int P { get; set; }
	[System.Runtime.CompilerServices.CompilerGenerated]
	public C() : this(P = 0) {
	}
	public C(int x) {
	}
}",
                @"function() {
	{sm_C}.set_P(0);
	$ShallowCopy({sm_C}.ctor(0), this);
}", metadataImporter: new MockMetadataImporter {
                GetConstructorSemantics = c => c.Parameters.Count == 0 ? ConstructorScriptSemantics.Unnamed() : ConstructorScriptSemantics.StaticMethod("ctor")
            });
        }
Beispiel #22
0
        public void InvokingBaseStaticMethodConstructorFromAnotherTypeOfConstructorWorks()
        {
            AssertCorrect(
                @"class B {
	public B(int x) {}
}
class D : B {
	public static int P { get; set; }
	[System.Runtime.CompilerServices.CompilerGenerated]
	public D() : base(P = 1) {}
}",
                @"function() {
	{sm_D}.set_P(1);
	$ShallowCopy({sm_B}.ctor(1), this);
}", metadataImporter: new MockMetadataImporter {
                GetConstructorSemantics = c => c.DeclaringType.Name == "D" ? ConstructorScriptSemantics.Unnamed() : ConstructorScriptSemantics.StaticMethod("ctor")
            });
        }
Beispiel #23
0
        public void InvokingBaseStaticMethodConstructorFromAnotherTypeOfConstructorIsAnError()
        {
            var rpt = new MockErrorReporter(false);

            Compile(new[] {
                @"class B {
	public B() {}
}
class D : B {
	public D() {}
}"
            }, errorReporter: rpt, metadataImporter: new MockMetadataImporter {
                GetConstructorSemantics = c => c.DeclaringType.Name == "D" ? ConstructorScriptSemantics.Unnamed() : ConstructorScriptSemantics.StaticMethod("ctor")
            });
            Assert.That(rpt.AllMessagesText.Any(msg => msg.StartsWith("Error", StringComparison.InvariantCultureIgnoreCase) && msg.IndexOf("static method", StringComparison.InvariantCultureIgnoreCase) >= 0));
        }
Beispiel #24
0
        public void ChainingToStaticMethodConstructorFromAnotherTypeOfConstructorIsAnError()
        {
            var rpt = new MockErrorReporter(false);

            Compile(new[] {
                @"class C {
	public C() : this(0) {
	}
	public C(int x) {
	}
}"
            }, errorReporter: rpt, metadataImporter: new MockMetadataImporter {
                GetConstructorSemantics = c => c.Parameters.Count == 0 ? ConstructorScriptSemantics.Unnamed() : ConstructorScriptSemantics.StaticMethod("ctor")
            });
            Assert.That(rpt.AllMessagesText.Any(msg => msg.StartsWith("Error", StringComparison.InvariantCultureIgnoreCase) && msg.IndexOf("static method", StringComparison.InvariantCultureIgnoreCase) >= 0));
        }
        public void ConstructorsCanBeOverloadedWithDifferentImplementations()
        {
            var metadataImporter = new MockMetadataImporter {
                GetConstructorSemantics = ctor => ctor.Parameters[0].Type.Name == "String" ? ConstructorScriptSemantics.Named("StringCtor") : ConstructorScriptSemantics.StaticMethod("IntCtor")
            };

            Compile(new[] { "class C { C(int i) {} C(string s) {} }" }, metadataImporter: metadataImporter);
            FindClass("C").NamedConstructors.Should().HaveCount(1);
            FindClass("C").StaticMethods.Should().HaveCount(1);
            FindNamedConstructor("C.StringCtor").Should().NotBeNull();
            FindStaticMethod("C.IntCtor").Should().NotBeNull();
        }