Example #1
0
        public void ProgramWithEntryPointWorks()
        {
            var type = CreateMockTypeDefinition("MyClass");
            var main = new Mock <IMethod>(MockBehavior.Strict);

            main.SetupGet(_ => _.DeclaringTypeDefinition).Returns(type);
            main.SetupGet(_ => _.Name).Returns("Main");
            main.SetupGet(_ => _.Parameters).Returns(EmptyList <IParameter> .Instance);

            AssertCorrect(
                @"////////////////////////////////////////////////////////////////////////////////
// MyClass
var $MyClass = function() {
};
$MyClass.$main = function() {
	X;
};
{Type}.registerClass(global, 'MyClass', $MyClass);
{MyClass}.$Main();
", new MockScriptSharpMetadataImporter()
            {
                GetMethodSemantics = m => MethodScriptSemantics.NormalMethod("$" + m.Name)
            },
                main.Object,
                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")))) }
            });
        }
Example #2
0
        public void AnErrorIsIssuedIfTheMainMethodHasParameters()
        {
            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(new[] { new Mock <IParameter>().Object });
            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 => MethodScriptSemantics.NormalMethod("$" + m.Name)
            },
                er,
                main.Object
                );

            Assert.That(er.AllMessages, Has.Count.EqualTo(1));
            Assert.That(er.AllMessages.Any(m => m.Code == 7800 && (string)m.Args[0] == "MyClass.Main"));
        }
Example #3
0
        public void InvokingMethodWithDynamicArgumentIsAnErrorWhenAllMethodsInTheGroupDoNotHaveTheSameScriptName()
        {
            var er = new MockErrorReporter(false);

            Compile(new[] {
                @"class X {
	public int F(int a, string b) { return 0; }
	public int F(int a, int b) { return 0; }
}
class C {
	public void M() {
		dynamic d = null;
		var x = new X();
		// BEGIN
		var a = x.F(123, d);
		// END
	}
}"
            }, metadataImporter: new MockMetadataImporter {
                GetMethodSemantics = m => MethodScriptSemantics.NormalMethod(m.Name + "$" + string.Join("$", m.Parameters.Select(p => p.Type.Name)))
            }, errorReporter: er);

            Assert.That(er.AllMessagesText.Count, Is.EqualTo(1));
            Assert.That(er.AllMessagesText.Any(m => m.StartsWith("Error:") && m.Contains("same script name")));
        }
Example #4
0
        public void GetEnumeratorWithEnumerateAsArray()
        {
            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 => MethodScriptSemantics.NormalMethod("$" + m.Name, enumerateAsArray: m.Name == "GetEnumerator")
            });
        }
 public void SetMethodSemanticsWorks()
 {
     Prepare(@"public class C { public void TheMethod(int i) {} public void TheMethod(int i, int j) {} }", () => {
         Metadata.SetMethodSemantics(FindMethod("C.TheMethod", 1), MethodScriptSemantics.NormalMethod("__something_else__"));
     });
     Assert.AreEqual(Metadata.GetMethodSemantics(FindMethod("C.TheMethod", 1)).Name, "__something_else__");
     Assert.AreEqual(Metadata.GetMethodSemantics(FindMethod("C.TheMethod", 2)).Name, "theMethod");
 }
Example #6
0
        public void StaticMethodWithGenerateCodeSetToFalseDoesNotAppearOnTheType()
        {
            var metadataImporter = new MockMetadataImporter {
                GetMethodSemantics = method => MethodScriptSemantics.NormalMethod("X", generateCode: false)
            };

            Compile(new[] { "class C { public static void M() {} }" }, metadataImporter: metadataImporter);
            FindClass("C").InstanceMethods.Should().BeEmpty();
        }
        public void AbstractIndexerHasANullDefinition()
        {
            var metadataImporter = new MockMetadataImporter {
                GetPropertySemantics = p => PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.NormalMethod("get_Item"), MethodScriptSemantics.NormalMethod("set_Item"))
            };

            Compile(new[] { "abstract class C { public abstract int this[int i] { get; set; } }" }, metadataImporter: metadataImporter);
            FindInstanceMethod("C.get_Item").Definition.Should().BeNull();
            FindInstanceMethod("C.set_Item").Definition.Should().BeNull();
        }
Example #8
0
        public void ExpandedParamArrayIsFixedAtTheTopOfMethods()
        {
            AssertCorrect(
                @"void M(int a, int b, params int[] c) {}",
                @"function($a, $b) {
	var $c = Array.prototype.slice.call(arguments, 2);
}", metadataImporter: new MockMetadataImporter {
                GetMethodSemantics = m => MethodScriptSemantics.NormalMethod("$" + m.Name, expandParams: true)
            });
        }
Example #9
0
        public void PartialMethodWithDeclarationAndDefinitionIsImported()
        {
            var metadataImporter = new MockMetadataImporter {
                GetMethodSemantics = m => MethodScriptSemantics.NormalMethod("$M")
            };

            Compile(new[] { "partial class C { partial void M(); }", "partial class C { partial void M() {} }" }, metadataImporter: metadataImporter);
            FindInstanceMethod("C.$M").Should().NotBeNull();
            FindClass("C").StaticMethods.Should().BeEmpty();
        }
        public void IndexerWithGetAndSetMethodsIsCorrectlyImported()
        {
            var metadataImporter = new MockMetadataImporter {
                GetPropertySemantics = p => PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.NormalMethod("get_Item"), MethodScriptSemantics.NormalMethod("set_Item"))
            };

            Compile(new[] { "class C { public int this[int i] { get {} set {} } }" }, metadataImporter: metadataImporter);
            FindInstanceMethod("C.get_Item").Should().NotBeNull();
            FindInstanceMethod("C.set_Item").Should().NotBeNull();
        }
Example #11
0
        public void OverloadedPartialMethodsWork()
        {
            var metadataImporter = new MockMetadataImporter {
                GetMethodSemantics = m => MethodScriptSemantics.NormalMethod("$M_" + m.Parameters.Count)
            };

            Compile(new[] { "partial class C { partial void M(); partial void M(int i); }", "partial class C { partial void M(int i) {} }" }, metadataImporter: metadataImporter);
            Assert.That(FindInstanceMethod("C.$M_0"), Is.Null);
            Assert.That(FindInstanceMethod("C.$M_1"), Is.Not.Null);
        }
        public void StaticManualEventsWithAddRemoveMethodsAreCorrectlyImported()
        {
            var metadataImporter = new MockMetadataImporter {
                GetEventSemantics = f => EventScriptSemantics.AddAndRemoveMethods(MethodScriptSemantics.NormalMethod("add_" + f.Name), MethodScriptSemantics.NormalMethod("remove_" + f.Name))
            };

            Compile(new[] { "class C { public static event System.EventHandler SomeProp { add {} remove{} } }" }, metadataImporter: metadataImporter);
            FindStaticMethod("C.add_SomeProp").Should().NotBeNull();
            FindStaticMethod("C.remove_SomeProp").Should().NotBeNull();
        }
        public void InstanceManualEventsWithAddRemoveMethodsWithNoCodeAreCorrectlyImported()
        {
            var metadataImporter = new MockMetadataImporter {
                GetEventSemantics = f => EventScriptSemantics.AddAndRemoveMethods(MethodScriptSemantics.NormalMethod("add_" + f.Name, generateCode: false), MethodScriptSemantics.NormalMethod("remove_" + f.Name, generateCode: false))
            };

            Compile(new[] { "class C { public event System.EventHandler SomeProp { add {} remove{} } }" }, metadataImporter: metadataImporter);
            FindClass("C").InstanceMethods.Should().BeEmpty();
            FindClass("C").UnnamedConstructor.Body.Statements.Should().BeEmpty();
        }
        public void IndexerAccessorsInInterfaceHaveNullDefinition()
        {
            var metadataImporter = new MockMetadataImporter {
                GetPropertySemantics = p => PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.NormalMethod("get_Item"), MethodScriptSemantics.NormalMethod("set_Item"))
            };

            Compile(new[] { "interface I { int this[int i] { get { return 0; } set {} } }" }, metadataImporter: metadataImporter);
            FindInstanceMethod("I.get_Item").Should().NotBeNull();
            FindInstanceMethod("I.set_Item").Should().NotBeNull();
        }
        public void ManuallyImplementedReadOnlyStaticPropertyWithGetAndSetMethodsIsCorrectlyImported()
        {
            var metadataImporter = new MockMetadataImporter {
                GetPropertySemantics = p => PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.NormalMethod("get_SomeProp"), null)
            };

            Compile(new[] { "class C { public static int SomeProp { get { return 0; } } }" }, metadataImporter: metadataImporter);
            FindStaticMethod("C.get_SomeProp").Should().NotBeNull();
            FindStaticMethod("C.set_SomeProp").Should().BeNull();
            FindClass("C").InstanceMethods.Should().BeEmpty();
        }
        public void IndexerWithGetAndSetMethodsWithNoCodeIsCorrectlyImported()
        {
            var metadataImporter = new MockMetadataImporter {
                GetPropertySemantics = p => PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.NormalMethod("get_Item", generateCode: false), MethodScriptSemantics.NormalMethod("set_Item", generateCode: false))
            };

            Compile(new[] { "class C { public int this[int i] { get { return 0; } set {} } }" }, metadataImporter: metadataImporter);
            FindInstanceMethod("C.get_Item").Should().BeNull();
            FindInstanceMethod("C.set_Item").Should().BeNull();
            FindClass("C").StaticMethods.Should().BeEmpty();
        }
Example #17
0
        public void GenericMethodTypeArgumentsAreIgnoredForStaticMethodsIfTheMethodImplOptionsSaySo()
        {
            var metadataImporter = new MockMetadataImporter {
                GetMethodSemantics = m => MethodScriptSemantics.NormalMethod("X", ignoreGenericArguments: true)
            };
            var namer = new MockNamer {
                GetTypeParameterName = tp => "$$" + tp.Name
            };

            Compile(new[] { "class C { public static void X<U, V>() {} }" }, metadataImporter: metadataImporter, namer: namer);
            FindStaticMethod("C.X").TypeParameterNames.Should().BeEmpty();
        }
Example #18
0
        public void ShadowingMethodsAreIncluded()
        {
            var metadataImporter = new MockMetadataImporter {
                GetMethodSemantics = m => MethodScriptSemantics.NormalMethod(m.DeclaringType.Name == "C" ? "XDerived" : m.Name)
            };

            Compile(new[] { "class B { public void X(); } class C : B { public new void X() {} }" }, metadataImporter: metadataImporter);
            var cls = FindClass("C");

            cls.InstanceMethods.Should().HaveCount(1);
            cls.InstanceMethods[0].Name.Should().Be("XDerived");
        }
        public void NormalMethodCanBeCompiled()
        {
            AssertCorrect(@"
int M(int a, string b) {
	return a;
}",
                          @"function($a, $b) {
	return $a;
}", metadataImporter: new MockMetadataImporter {
                GetMethodSemantics = m => MethodScriptSemantics.NormalMethod("$" + m.Name)
            });
        }
Example #20
0
        public void InvokingParamArrayMethodThatExpandsArgumentsInExpandedFormWorks()
        {
            AssertCorrect(
                @"public void F(int x, int y, params int[] args) {}
public void M() {
	// BEGIN
	F(4, 8, 59, 12, 4);
	// END
}",
                @"	this.$F(4, 8, 59, 12, 4);
", metadataImporter: new MockMetadataImporter {
                GetMethodSemantics = m => MethodScriptSemantics.NormalMethod("$" + m.Name, expandParams: m.Name == "F")
            });
        }
Example #21
0
        private void AssertCorrect(string csharp, string expected)
        {
            AssertCorrect(@"
using System;
using System.Collections.Generic;
using System.Linq;
class C {
	"     + csharp + @"
}", expected, referenceSystemCore: true, addSkeleton: false, metadataImporter: new MockMetadataImporter {
                GetMethodSemantics = m => MethodScriptSemantics.NormalMethod("$" + m.Name, ignoreGenericArguments: true), GetTypeSemantics = t => TypeScriptSemantics.NormalType(t.Name, ignoreGenericArguments: true)
            }, runtimeLibrary: new MockRuntimeLibrary {
                Upcast = (e, _1, _2) => e
            });
        }
Example #22
0
        public void CanPerformMethodGroupConversionOnMethodThatExpandsParamsToDelegateThatAlsoDoes()
        {
            AssertCorrect(
                @"public void F(int x, int y, params int[] args) {}
public void M() {
	System.Action<int, int, int[]> f;
	// BEGIN
	f = F;
	// END
}",
                @"	$f = $Bind(this.$F, this);
", metadataImporter: new MockMetadataImporter {
                GetMethodSemantics = m => MethodScriptSemantics.NormalMethod("$" + m.Name, expandParams: m.Name == "F"), GetDelegateSemantics = d => new DelegateScriptSemantics(expandParams: true)
            });
        }
Example #23
0
        public void MethodGroupConversionDoesNotInstantiateGenericMethodIfIgnoreGenericArgumentsIsSet()
        {
            AssertCorrect(
                @"void F<T>(T x) {}
public void M() {
	System.Action<int> f;
	// BEGIN
	f = F<int>;
	// END
}",
                @"	$f = $Bind(this.$F, this);
", metadataImporter: new MockMetadataImporter {
                GetMethodSemantics = m => MethodScriptSemantics.NormalMethod("$" + m.Name, ignoreGenericArguments: true)
            });
        }
Example #24
0
        public void GlobalStaticMethodInvocationWithArgumentsWorks()
        {
            AssertCorrect(
                @"static void F(int x, int y, int z) {}
public void M() {
	int a = 0, b = 0, c = 0;
	// BEGIN
	F(a, b, c);
	// END
}",
                @"	$F($a, $b, $c);
", metadataImporter: new MockMetadataImporter {
                GetMethodSemantics = m => MethodScriptSemantics.NormalMethod("$" + m.Name, isGlobal: m.Name == "F")
            });
        }
Example #25
0
        public void ReadingMethodGroupWithOverloadsWorks()
        {
            AssertCorrect(
                @"void F(int x) {}
void F(string x) {}
public void M() {
	System.Action<int> f;
	// BEGIN
	f = F;
	// END
}",
                @"	$f = $Bind(this.F_Int32, this);
", metadataImporter: new MockMetadataImporter {
                GetMethodSemantics = m => MethodScriptSemantics.NormalMethod(m.Parameters.Count > 0 ? m.Name + "_" + m.Parameters[0].Type.Name : m.Name)
            });
        }
Example #26
0
        public void GenericMethodInvocationWithIgnoreGenericArgumentsWorks()
        {
            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
}",
                @"	this.$F($a, $b, $c);
", metadataImporter: new MockMetadataImporter {
                GetMethodSemantics = m => MethodScriptSemantics.NormalMethod("$" + m.Name, ignoreGenericArguments: true)
            });
        }
        public void CannotAccessExpandedParamsParameter()
        {
            var er = new MockErrorReporter(false);

            Compile(new[] {
                @"class C1 {
	public void M(int i, int j, params int[] myParamArray) {
		int x = myParamArray[3];
	}
}"
            }, metadataImporter: new MockMetadataImporter {
                GetMethodSemantics = m => MethodScriptSemantics.NormalMethod("$" + m.Name, expandParams: true)
            }, errorReporter: er);

            Assert.That(er.AllMessagesText.Count, Is.EqualTo(1));
            Assert.That(er.AllMessagesText[0].Contains("myParamArray") && er.AllMessagesText[0].Contains("expand") && er.AllMessagesText[0].Contains("param array"));
        }
 public MockMetadataImporter()
 {
     GetTypeSemantics = t => {
         if (t.DeclaringTypeDefinition == null)
         {
             return(TypeScriptSemantics.NormalType(t.FullName));
         }
         else
         {
             return(TypeScriptSemantics.NormalType(GetTypeSemantics(t.DeclaringTypeDefinition).Name + "$" + t.Name));
         }
     };
     GetMethodSemantics      = m => MethodScriptSemantics.NormalMethod(m.Name);
     GetConstructorSemantics = c => {
         if (c.DeclaringType.Kind == TypeKind.Anonymous)
         {
             return(ConstructorScriptSemantics.Json(new IMember[0]));
         }
         else if (c.DeclaringType.GetConstructors().Count() == 1 || c.Parameters.Count == 0)
         {
             return(ConstructorScriptSemantics.Unnamed());
         }
         else
         {
             return(ConstructorScriptSemantics.Named("ctor$" + String.Join("$", c.Parameters.Select(p => p.Type.Name))));
         }
     };
     GetPropertySemantics = p => {
         if (p.DeclaringType.Kind == TypeKind.Anonymous || (p.DeclaringType.FullName == "System.Array" && p.Name == "Length"))
         {
             string name = p.Name.Replace("<>", "$");
             return(PropertyScriptSemantics.Field(name.StartsWith("$") ? name : ("$" + name)));
         }
         else
         {
             return(PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.NormalMethod("get_" + p.Name), MethodScriptSemantics.NormalMethod("set_" + p.Name)));
         }
     };
     GetDelegateSemantics                   = d => new DelegateScriptSemantics();
     GetAutoPropertyBackingFieldName        = p => "$" + p.Name;
     ShouldGenerateAutoPropertyBackingField = p => true;
     GetFieldSemantics                   = f => FieldScriptSemantics.Field("$" + f.Name);
     GetEventSemantics                   = e => EventScriptSemantics.AddAndRemoveMethods(MethodScriptSemantics.NormalMethod("add_" + e.Name), MethodScriptSemantics.NormalMethod("remove_" + e.Name));
     GetAutoEventBackingFieldName        = e => "$" + e.Name;
     ShouldGenerateAutoEventBackingField = e => true;
 }
Example #29
0
        public void ReadingMethodGroupWithAnotherTargetWorks()
        {
            AssertCorrect(
                @"class X { public void F(int x) {} public void F(string x) {} }
public void M() {
	Action<int> f;
	var x = new X();
	// BEGIN
	f = x.F;
	// END
}
",
                @"	$f = $Bind($x.F_Int32, $x);
", metadataImporter: new MockMetadataImporter {
                GetMethodSemantics = m => MethodScriptSemantics.NormalMethod(m.Parameters.Count > 0 ? m.Name + "_" + m.Parameters[0].Type.Name : m.Name)
            });
        }
Example #30
0
        public void CannotPerformMethodGroupConversionOnMethodThatExpandsParamsToDelegateThatDoesNot()
        {
            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 => MethodScriptSemantics.NormalMethod("$" + m.Name, expandParams: m.Name == "F")
            }, errorReporter: er);

            Assert.That(er.AllMessagesText.Count, Is.EqualTo(1));
            Assert.That(er.AllMessagesText[0].Contains("C1.F") && er.AllMessagesText[0].Contains("System.Action") && er.AllMessagesText[0].Contains("expand") && er.AllMessagesText[0].Contains("param array"));
        }