public void IndexerThatIsNotUsableFromScriptIsNotImported()
 {
     var metadataImporter = new MockMetadataImporter { GetPropertySemantics = p => PropertyScriptSemantics.NotUsableFromScript() };
     Compile(new[] { "class C { public int this[int i] { get {} set {} } }" }, metadataImporter: metadataImporter);
     FindClass("C").InstanceMethods.Should().BeEmpty();
     FindClass("C").StaticMethods.Should().BeEmpty();
 }
        public void CannotUseNotUsableTypeInATypeOfExpression()
        {
            var metadataImporter = new MockMetadataImporter {
                GetTypeSemantics = t => t.Name == "C1" ? TypeScriptSemantics.NotUsableFromScript() : TypeScriptSemantics.NormalType(t.Name)
            };
            var er = new MockErrorReporter(false);

            Compile(new[] {
                @"class C1 {}
class C {
	public void M() {
		var t = typeof(C1);
	}
}"
            }, metadataImporter: metadataImporter, errorReporter: er);

            Assert.That(er.AllMessagesText.Count, Is.EqualTo(1));
            Assert.That(er.AllMessagesText[0].Contains("not usable from script") && er.AllMessagesText[0].Contains("typeof") && er.AllMessagesText[0].Contains("C1"));

            er = new MockErrorReporter(false);
            Compile(new[] {
                @"class C1 {}
interface I1<T> {}
class C {
	public void M() {
		var t= typeof(I1<I1<C1>>);
	}
}"
            }, metadataImporter: metadataImporter, errorReporter: er);
            Assert.That(er.AllMessagesText.Count, Is.EqualTo(1));
            Assert.That(er.AllMessagesText[0].Contains("not usable from script") && er.AllMessagesText[0].Contains("typeof") && er.AllMessagesText[0].Contains("C1"));
        }
        public void CannotUseNotUsableTypeAsAGenericArgument()
        {
            var nc = new MockMetadataImporter { GetTypeSemantics = t => t.Name == "C1" ? TypeScriptSemantics.NotUsableFromScript() : TypeScriptSemantics.NormalType(t.Name) };
            var er = new MockErrorReporter(false);

            Compile(new[] {
            @"class C1 {}
            class C {
            public void F1<T>() {}
            public void M() {
            F1<C1>();
            }
            }" }, metadataImporter: nc, errorReporter: er);

            Assert.That(er.AllMessagesText.Count, Is.EqualTo(1));
            Assert.That(er.AllMessagesText[0].Contains("not usable from script") && er.AllMessagesText[0].Contains("generic argument") && er.AllMessagesText[0].Contains("C1") && er.AllMessagesText[0].Contains("F1"));

            er = new MockErrorReporter(false);
            Compile(new[] {
            @"class C1 {}
            interface I1<T> {}
            class C {
            public void F1<T>() {}
            public void M() {
            F1<I1<I1<C1>>>();
            }
            }" }, metadataImporter: nc, errorReporter: er);
            Assert.That(er.AllMessagesText.Count, Is.EqualTo(1));
            Assert.That(er.AllMessagesText[0].Contains("not usable from script") && er.AllMessagesText[0].Contains("generic argument") && er.AllMessagesText[0].Contains("C1") && er.AllMessagesText[0].Contains("F1"));
        }
        public void StaticFieldsWithoutInitializersAreInitializedToDefault()
        {
            var metadataImporter = new MockMetadataImporter {
                GetConstructorSemantics = ctor => { if (ctor.IsStatic)
                                                    {
                                                        throw new InvalidOperationException();
                                                    }
                                                    else
                                                    {
                                                        return(ConstructorScriptSemantics.Unnamed());
                                                    } }
            };

            Compile(new[] {
                @"class C<T> {
    static T x;
    static int y;
	static string z;
}"
            }, metadataImporter: metadataImporter);

            var cctor = FindClass("C").StaticInitStatements.Aggregate("", (s, st) => s + OutputFormatter.Format(st, true));

            cctor.Replace("\r\n", "\n").Should().Be(
                @"sm_$InstantiateGenericType({C}, ga_$T).$x = $Default(def_$T);
sm_$InstantiateGenericType({C}, ga_$T).$y = 0;
sm_$InstantiateGenericType({C}, ga_$T).$z = null;
".Replace("\r\n", "\n"));
        }
		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 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 CannotUseNotUsableTypeAsATypeArgument()
        {
            var nc = new MockMetadataImporter {
                GetTypeSemantics = t => t.Name == "C1" ? TypeScriptSemantics.NotUsableFromScript() : TypeScriptSemantics.NormalType(t.Name)
            };
            var er = new MockErrorReporter(false);

            Compile(new[] {
                @"class C1 {}
class C {
	public void M() {
		var c = new C1();
	}
}"
            }, metadataImporter: nc, errorReporter: er);

            Assert.That(er.AllMessagesText.Count, Is.EqualTo(1));
            Assert.That(er.AllMessagesText[0].Contains("not usable from script") && er.AllMessagesText[0].Contains("instance") && er.AllMessagesText[0].Contains("C1"));

            er = new MockErrorReporter(false);
            Compile(new[] {
                @"class C1 {}
class C2<T> {}
class C {
	public void M() {
		var x = new C2<C2<C1>>();
	}
}"
            }, metadataImporter: nc, errorReporter: er);
            Assert.That(er.AllMessagesText.Count, Is.EqualTo(1));
            Assert.That(er.AllMessagesText[0].Contains("not usable from script") && er.AllMessagesText[0].Contains("type argument") && er.AllMessagesText[0].Contains("C1") && er.AllMessagesText[0].Contains("C2"));
        }
        public void StaticConstructorBodyGetsAddedLastInTheStaticInitStatements()
        {
            var metadataImporter = new MockMetadataImporter {
                GetConstructorSemantics = ctor => { if (ctor.IsStatic)
                                                    {
                                                        throw new InvalidOperationException();
                                                    }
                                                    else
                                                    {
                                                        return(ConstructorScriptSemantics.Unnamed());
                                                    } }
            };

            Compile(new[] {
                @"class C {
    static int x = 0;
    static C() {
        int z = 2;
    }
    static int y = 1;
}"
            }, metadataImporter: metadataImporter);

            var cctor = FindClass("C").StaticInitStatements.Aggregate("", (s, st) => s + OutputFormatter.Format(st, true));

            cctor.Replace("\r\n", "\n").Should().Be(
                @"{sm_C}.$x = 0;
{sm_C}.$y = 1;
var $z = 2;
".Replace("\r\n", "\n"));
        }
		public void InstanceAutoPropertiesThatShouldBeInstanceFieldsAreCorrectlyImported() {
			var metadataImporter = new MockMetadataImporter { GetPropertySemantics = p => PropertyScriptSemantics.Field("$" + p.Name) };
			Compile(new[] { "class C { public string SomeProp { get; set; } }" }, metadataImporter: metadataImporter);
			FindClass("C").InstanceMethods.Should().BeEmpty();
			FindClass("C").StaticMethods.Should().BeEmpty();
			FindInstanceFieldInitializer("C.$SomeProp").Should().NotBeNull();
		}
Exemple #10
0
        public void StaticFieldsWithInitializersInGenericTypeAreInitialized()
        {
            var metadataImporter = new MockMetadataImporter {
                GetConstructorSemantics = ctor => { if (ctor.IsStatic)
                                                    {
                                                        throw new InvalidOperationException();
                                                    }
                                                    else
                                                    {
                                                        return(ConstructorScriptSemantics.Unnamed());
                                                    } }
            };

            Compile(new[] {
                @"class C<T> {
	static T x = default(T);
	static int y = 42;
	static string z = ""X"";
}"
            }, metadataImporter: metadataImporter);

            var cctor = OutputFormatter.Format(FindClass("C").StaticInitStatements, allowIntermediates: true);

            cctor.Replace("\r\n", "\n").Should().Be(
                @"$Init(sm_$InstantiateGenericType({C}, $T), '$x', $Default($T));
$Init(sm_$InstantiateGenericType({C}, $T), '$y', 42);
$Init(sm_$InstantiateGenericType({C}, $T), '$z', 'X');
".Replace("\r\n", "\n"));
        }
 public void ReadOnlyNativeIndexerIsCorrectlyImported()
 {
     var metadataImporter = new MockMetadataImporter { GetPropertySemantics = p => PropertyScriptSemantics.NativeIndexer() };
     Compile(new[] { "class C { public int this[int i] { get { return 0; } } }" }, metadataImporter: metadataImporter);
     FindClass("C").InstanceMethods.Should().BeEmpty();
     FindClass("C").StaticMethods.Should().BeEmpty();
 }
		public void WriteOnlyIndexerWithGetAndSetMethodsIsCorrectlyImported() {
			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] { set {} } }" }, metadataImporter: metadataImporter);
			FindInstanceMethod("C.get_Item").Should().BeNull();
			FindInstanceMethod("C.set_Item").Should().NotBeNull();
			FindClass("C").StaticMethods.Should().BeEmpty();
		}
Exemple #13
0
        public void StaticConstructorBodyGetsAddedLastInTheStaticInitStatements()
        {
            var metadataImporter = new MockMetadataImporter {
                GetConstructorSemantics = ctor => { if (ctor.IsStatic)
                                                    {
                                                        throw new InvalidOperationException();
                                                    }
                                                    else
                                                    {
                                                        return(ConstructorScriptSemantics.Unnamed());
                                                    } }
            };

            Compile(new[] {
                @"class C {
	static int x = 0;
	static C() {
		int z = 2;
	}
	static int y = 1;
}"
            }, metadataImporter: metadataImporter);

            var cctor = OutputFormatter.Format(FindClass("C").StaticInitStatements, allowIntermediates: true);

            cctor.Replace("\r\n", "\n").Should().Be(
                @"$Init({sm_C}, '$x', 0);
$Init({sm_C}, '$y', 1);
var $z = 2;
".Replace("\r\n", "\n"));
        }
        public void CannotUseMutableValueTypeAsATypeArgumentInATypeOfExpression()
        {
            var metadataImporter = new MockMetadataImporter {
                GetTypeSemantics = t => t.Name == "C1" ? TypeScriptSemantics.MutableValueType(t.Name) : TypeScriptSemantics.NormalType(t.Name)
            };
            var er = new MockErrorReporter(false);

            Compile(new[] {
                @"struct C1 {}
class C {
	public void M() {
		var t = typeof(C1);
	}
}"
            }, metadataImporter: metadataImporter, errorReporter: er);

            Assert.That(er.AllMessages.Count, Is.EqualTo(0));

            er = new MockErrorReporter(false);
            Compile(new[] {
                @"struct C1 {}
interface I1<T> {}
class C {
	public void M() {
		var t= typeof(I1<I1<C1>>);
	}
}"
            }, metadataImporter: metadataImporter, errorReporter: er);
            Assert.That(er.AllMessages.Count, Is.EqualTo(1));
            Assert.That(er.AllMessages[0].Code == 7539 && er.AllMessages[0].FormattedMessage.Contains("mutable value type") && er.AllMessages[0].FormattedMessage.Contains("C1"));
        }
		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();
		}
        public void CannotUseNotUsableTypeInATypeOfExpression()
        {
            var metadataImporter = new MockMetadataImporter { GetTypeSemantics = t => t.Name == "C1" ? TypeScriptSemantics.NotUsableFromScript() : TypeScriptSemantics.NormalType(t.Name) };
            var er = new MockErrorReporter(false);

            Compile(new[] {
            @"class C1 {}
            class C {
            public void M() {
            var t = typeof(C1);
            }
            }" }, metadataImporter: metadataImporter, errorReporter: er);

            Assert.That(er.AllMessagesText.Count, Is.EqualTo(1));
            Assert.That(er.AllMessagesText[0].Contains("not usable from script") && er.AllMessagesText[0].Contains("typeof") && er.AllMessagesText[0].Contains("C1"));

            er = new MockErrorReporter(false);
            Compile(new[] {
            @"class C1 {}
            interface I1<T> {}
            class C {
            public void M() {
            var t= typeof(I1<I1<C1>>);
            }
            }" }, metadataImporter: metadataImporter, errorReporter: er);
            Assert.That(er.AllMessagesText.Count, Is.EqualTo(1));
            Assert.That(er.AllMessagesText[0].Contains("not usable from script") && er.AllMessagesText[0].Contains("typeof") && er.AllMessagesText[0].Contains("C1"));
        }
 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();
 }
 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();
 }
 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 CannotUseMutableValueTypeAsTypeArgument()
        {
            var md = new MockMetadataImporter {
                GetTypeSemantics = t => t.Kind == TypeKind.Struct ? TypeScriptSemantics.MutableValueType(t.Name) : TypeScriptSemantics.NormalType(t.Name)
            };
            var er = new MockErrorReporter(false);

            Compile(new[] {
                @"struct S1 {}
class C1<T> {}
class C {
	public void M() {
		var c = new C1<S1>();
	}
}"
            }, metadataImporter: md, errorReporter: er);

            Assert.That(er.AllMessages.Count, Is.EqualTo(1));
            Assert.That(er.AllMessages[0].Code == 7539 && er.AllMessages[0].FormattedMessage.Contains("mutable value type") && er.AllMessages[0].FormattedMessage.Contains("S1"));

            er = new MockErrorReporter(false);

            Compile(new[] {
                @"struct S1 {}
class C1<T> {}
class C {
	public void M() {
		var c = new C1<C1<S1>>();
	}
}"
            }, metadataImporter: md, errorReporter: er);

            Assert.That(er.AllMessages.Count, Is.EqualTo(1));
            Assert.That(er.AllMessages[0].Code == 7539 && er.AllMessages[0].FormattedMessage.Contains("mutable value type") && er.AllMessages[0].FormattedMessage.Contains("S1"));
        }
Exemple #21
0
        public void CannotUseNotUsableTypeAsAGenericArgument()
        {
            var nc = new MockMetadataImporter {
                GetTypeSemantics = t => t.Name == "C1" ? TypeScriptSemantics.NotUsableFromScript() : TypeScriptSemantics.NormalType(t.Name)
            };
            var er = new MockErrorReporter(false);

            Compile(new[] {
                @"class C1 {}
class C {
	public void F1<T>() {}
	public void M() {
		F1<C1>();
	}
}"
            }, metadataImporter: nc, errorReporter: er);

            Assert.That(er.AllMessagesText.Count, Is.EqualTo(1));
            Assert.That(er.AllMessagesText[0].Contains("not usable from script") && er.AllMessagesText[0].Contains("generic argument") && er.AllMessagesText[0].Contains("C1") && er.AllMessagesText[0].Contains("F1"));

            er = new MockErrorReporter(false);
            Compile(new[] {
                @"class C1 {}
interface I1<T> {}
class C {
	public void F1<T>() {}
	public void M() {
		F1<I1<I1<C1>>>();
	}
}"
            }, metadataImporter: nc, errorReporter: er);
            Assert.That(er.AllMessagesText.Count, Is.EqualTo(1));
            Assert.That(er.AllMessagesText[0].Contains("not usable from script") && er.AllMessagesText[0].Contains("generic argument") && er.AllMessagesText[0].Contains("C1") && er.AllMessagesText[0].Contains("F1"));
        }
		public void InstanceFieldsAreCorrectlyImported() {
			var metadataImporter = new MockMetadataImporter { GetFieldSemantics = f => FieldScriptSemantics.Field("$SomeProp") };
			Compile(new[] { "class C { public int SomeField; }" }, metadataImporter: metadataImporter);
			FindInstanceFieldInitializer("C.$SomeProp").Should().NotBeNull();
			FindClass("C").StaticInitStatements.Should().BeEmpty();
			FindClass("C").InstanceMethods.Should().BeEmpty();
			FindClass("C").StaticMethods.Should().BeEmpty();
		}
		public void StaticFieldsAreCorrectlyImported() {
			var metadataImporter = new MockMetadataImporter { GetConstructorSemantics = c => ConstructorScriptSemantics.Unnamed(skipInInitializer: c.DeclaringType.IsKnownType(KnownTypeCode.Object)) };
			Compile(new[] { "class C { public static int SomeField; }" }, metadataImporter: metadataImporter);
			FindStaticFieldInitializer("C.$SomeField").Should().NotBeNull();
			FindClass("C").UnnamedConstructor.Body.Statements.Should().BeEmpty();
			FindClass("C").InstanceMethods.Should().BeEmpty();
			FindClass("C").StaticMethods.Should().BeEmpty();
		}
		public void FieldsThatAreNotUsableFromScriptAreNotImported() {
			var metadataImporter = new MockMetadataImporter { GetConstructorSemantics = c => ConstructorScriptSemantics.Unnamed(skipInInitializer: c.DeclaringType.IsKnownType(KnownTypeCode.Object)), GetFieldSemantics = f => FieldScriptSemantics.NotUsableFromScript() };
			Compile(new[] { "class C { public int SomeField; }" }, metadataImporter: metadataImporter);
			FindClass("C").UnnamedConstructor.Body.Statements.Should().BeEmpty();
			FindClass("C").StaticInitStatements.Should().BeEmpty();
			FindClass("C").InstanceMethods.Should().BeEmpty();
			FindClass("C").StaticMethods.Should().BeEmpty();
		}
		public void InstanceAutoEventsWithAddRemoveMethodsWithNoCodeAreCorrectlyImported() {
			var metadataImporter = new MockMetadataImporter { GetConstructorSemantics = c => ConstructorScriptSemantics.Unnamed(skipInInitializer: c.DeclaringType.IsKnownType(KnownTypeCode.Object)),
			                                                  GetEventSemantics = e => EventScriptSemantics.AddAndRemoveMethods(MethodScriptSemantics.NormalMethod("add_" + e.Name, generateCode: false), MethodScriptSemantics.NormalMethod("remove_" + e.Name, generateCode: false)),
			                                                };
			Compile(new[] { "class C { public event System.EventHandler SomeProp; }" }, metadataImporter: metadataImporter);
			FindClass("C").InstanceMethods.Should().BeEmpty();
			FindInstanceFieldInitializer("C.$SomeProp").Should().NotBeNull();
		}
		public void ConstructorsCanBeOverloadedWithDifferentImplementations() {
			var metadataImporter = new MockMetadataImporter { GetConstructorSemantics = ctor => ctor.DeclaringType.IsKnownType(KnownTypeCode.Object) ? ConstructorScriptSemantics.Unnamed(skipInInitializer: true) : (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();
		}
        public void EnumValueWhichIsNotUsableFromScriptIsNotImported()
        {
            var md = new MockMetadataImporter {
                GetFieldSemantics = f => f.Name == "Member2" ? FieldScriptSemantics.NotUsableFromScript() : FieldScriptSemantics.Field("$" + f.Name)
            };

            Compile(new[] { "enum MyEnum { Member1, Member2, Member3 }" }, metadataImporter: md);
            FindEnum("MyEnum").Values.Select(v => v.Name).Should().BeEquivalentTo(new[] { "$Member1", "$Member3" });
        }
Exemple #28
0
        public void IgnoreGenericArgumentsInTheTypeSemanticsRemovesGenericArgumentsFromTheType()
        {
            var metadataImporter = new MockMetadataImporter {
                GetTypeSemantics = t => TypeScriptSemantics.NormalType(t.Name, ignoreGenericArguments: true)
            };

            Compile(new[] { "class C1<T1, T2> {}" }, metadataImporter: metadataImporter);
            FindClass("C1").TypeArgumentNames.Should().BeEmpty();
        }
		public void ImportingMultipleFieldsInTheSameDeclarationWorks() {
			var metadataImporter = new MockMetadataImporter { GetFieldSemantics = f => FieldScriptSemantics.Field("$" + f.Name) };
			Compile(new[] { "class C { public int Field1, Field2; }" }, metadataImporter: metadataImporter);
			FindInstanceFieldInitializer("C.$Field1").Should().NotBeNull();
			FindInstanceFieldInitializer("C.$Field2").Should().NotBeNull();
			FindClass("C").StaticInitStatements.Should().BeEmpty();
			FindClass("C").InstanceMethods.Should().BeEmpty();
			FindClass("C").StaticMethods.Should().BeEmpty();
		}
		public void InstanceAutoEventsThatShouldNotGenerateBackingFieldsDoNotGenerateBackingFields() {
			var metadataImporter = new MockMetadataImporter { GetConstructorSemantics = c => ConstructorScriptSemantics.Unnamed(skipInInitializer: c.DeclaringType.IsKnownType(KnownTypeCode.Object)),
			                                                  ShouldGenerateAutoEventBackingField = e => false,
			                                                };
			Compile(new[] { "class C { public event System.EventHandler SomeProp; }" }, metadataImporter: metadataImporter);
			FindInstanceMethod("C.add_SomeProp").Should().NotBeNull();
			FindInstanceMethod("C.remove_SomeProp").Should().NotBeNull();
			FindClass("C").UnnamedConstructor.Body.Statements.Should().BeEmpty();
		}
Exemple #31
0
        public void MethodImplementedAsNotUsableFromScriptDoesNotAppearOnTheType()
        {
            var metadataImporter = new MockMetadataImporter {
                GetMethodSemantics = method => MethodScriptSemantics.NotUsableFromScript()
            };

            Compile(new[] { "class C { public static void M() {} }" }, metadataImporter: metadataImporter);
            FindClass("C").InstanceMethods.Should().BeEmpty();
        }
 public void FieldsThatAreNotUsableFromScriptAreNotImported()
 {
     var metadataImporter = new MockMetadataImporter { GetFieldSemantics = f => FieldScriptSemantics.NotUsableFromScript() };
     Compile(new[] { "class C { public int SomeField; }" }, metadataImporter: metadataImporter);
     FindClass("C").UnnamedConstructor.Body.Statements.Should().BeEmpty();
     FindClass("C").StaticInitStatements.Should().BeEmpty();
     FindClass("C").InstanceMethods.Should().BeEmpty();
     FindClass("C").StaticMethods.Should().BeEmpty();
 }
        public void EnumMemberImplementedAsStringConstantIsNotImported()
        {
            var md = new MockMetadataImporter {
                GetFieldSemantics = f => f.Name == "Member2" ? FieldScriptSemantics.StringConstant("a", "$" + f.Name) : FieldScriptSemantics.Field("$" + f.Name)
            };

            Compile(new[] { "enum MyEnum { Member1, Member2, Member3 }" }, metadataImporter: md);
            FindEnum("MyEnum").Values.Select(v => v.Name).Should().BeEquivalentTo(new[] { "$Member1", "$Member3" });
        }
		public void InstanceAutoPropertiesWithGetSetMethodsWithNoCodeAreCorrectlyImported() {
			var metadataImporter = new MockMetadataImporter { GetConstructorSemantics = c => ConstructorScriptSemantics.Unnamed(skipInInitializer: c.DeclaringType.IsKnownType(KnownTypeCode.Object)),
			                                                  GetPropertySemantics = p => PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.NormalMethod("get_" + p.Name, generateCode: false), MethodScriptSemantics.NormalMethod("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);
			FindClass("C").InstanceMethods.Should().BeEmpty();
			FindClass("C").UnnamedConstructor.Body.Statements.Should().BeEmpty();
		}
Exemple #35
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 UsingUnusableClassAsABaseClassForAnotherUnusableClassIsNotAnError()
        {
            var metadataImporter = new MockMetadataImporter {
                GetTypeSemantics = t => TypeScriptSemantics.NotUsableFromScript()
            };

            Compile(new[] { "class B {} class D : B {}" }, metadataImporter: metadataImporter);
            // No errors is good enough
        }
        public void ConstructorImplementedAsInlineCodeDoesNotAppearOnTheType()
        {
            var metadataImporter = new MockMetadataImporter {
                GetConstructorSemantics = ctor => ConstructorScriptSemantics.InlineCode("X")
            };

            Compile(new[] { "class C { public C() {} }" }, metadataImporter: metadataImporter);
            FindClass("C").UnnamedConstructor.Should().BeNull();
        }
		public void InstanceAutoPropertiesWithGetSetMethodsWithNoCodeAreCorrectlyImported() {
			var metadataImporter = new MockMetadataImporter { GetConstructorSemantics = c => ConstructorScriptSemantics.Unnamed(skipInInitializer: c.DeclaringType.IsKnownType(KnownTypeCode.Object)),
			                                                  GetPropertySemantics = p => PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.NormalMethod("get_" + p.Name, generateCode: false), MethodScriptSemantics.NormalMethod("set_" + p.Name, generateCode: false)),
			                                                };

			Compile(new[] { "class C { public string SomeProp { get; set; } }" }, metadataImporter: metadataImporter);
			FindClass("C").InstanceMethods.Should().BeEmpty();
			FindInstanceFieldInitializer("C.$SomeProp").Should().NotBeNull();
		}
		public void DefaultConstructorIsNotInsertedIfOtherConstructorIsDefined() {
			var metadataImporter = new MockMetadataImporter() { GetConstructorSemantics = c => c.Parameters.Count == 0 ? ConstructorScriptSemantics.Unnamed() : ConstructorScriptSemantics.Named("ctor$" + string.Join("$", c.Parameters.Select(p => p.Type.Name))) };
			Compile(new[] { "class C { C(int i) {} }" }, metadataImporter: metadataImporter);
			var cls = FindClass("C");
			cls.UnnamedConstructor.Should().BeNull();
			cls.NamedConstructors.Should().HaveCount(1);
			cls.NamedConstructors[0].Name.Should().Be("ctor$Int32");
			cls.NamedConstructors[0].Definition.Should().NotBeNull();
		}
		public void StaticAutoEventsWithAddRemoveMethodsAreCorrectlyImported() {
			var metadataImporter = new MockMetadataImporter { GetEventSemantics = e => EventScriptSemantics.AddAndRemoveMethods(MethodScriptSemantics.NormalMethod("add_" + e.Name), MethodScriptSemantics.NormalMethod("remove_" + e.Name)),
			                                                  GetAutoEventBackingFieldName = e => "$" + e.Name
			                                                };

			Compile(new[] { "class C { public static event System.EventHandler SomeProp; }" }, metadataImporter: metadataImporter);
			FindStaticMethod("C.add_SomeProp").Should().NotBeNull();
			FindStaticMethod("C.remove_SomeProp").Should().NotBeNull();
			FindStaticFieldInitializer("C.$SomeProp").Should().NotBeNull();
		}
Exemple #41
0
        public void PartialMethodWithoutDefinitionIsNotImported()
        {
            var metadataImporter = new MockMetadataImporter {
                GetMethodSemantics = m => { throw new InvalidOperationException(); }
            };

            Compile(new[] { "partial class C { private partial void M(); }" }, metadataImporter: metadataImporter);
            FindClass("C").InstanceMethods.Should().BeEmpty();
            FindClass("C").StaticMethods.Should().BeEmpty();
        }
Exemple #42
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);
        }
Exemple #43
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();
        }
Exemple #44
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();
        }
		public void InstanceAutoPropertiesAreCorrectlyImported() {
			var metadataImporter = new MockMetadataImporter { GetPropertySemantics = p => PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.NormalMethod("get_" + p.Name), MethodScriptSemantics.NormalMethod("set_" + p.Name)),
			                                                  GetAutoPropertyBackingFieldName = p => "$" + p.Name
			                                                };
			Compile(new[] { "class C { public string SomeProp { get; set; } }" }, metadataImporter: metadataImporter);
			FindInstanceMethod("C.get_SomeProp").Should().NotBeNull();
			FindInstanceMethod("C.set_SomeProp").Should().NotBeNull();
			FindInstanceFieldInitializer("C.$SomeProp").Should().NotBeNull();
			FindClass("C").StaticInitStatements.Should().BeEmpty();
		}
        public void ReadOnlyNativeIndexerIsCorrectlyImported()
        {
            var metadataImporter = new MockMetadataImporter {
                GetPropertySemantics = p => PropertyScriptSemantics.NativeIndexer()
            };

            Compile(new[] { "class C { public int this[int i] { get { return 0; } } }" }, metadataImporter: metadataImporter);
            FindClass("C").InstanceMethods.Should().BeEmpty();
            FindClass("C").StaticMethods.Should().BeEmpty();
        }
        public void InstanceManualEventsWithAddRemoveMethodsWithNoCodeAreCorrectlyImported()
        {
            var metadataImporter = new MockMetadataImporter {
                GetConstructorSemantics = c => ConstructorScriptSemantics.Unnamed(skipInInitializer: c.DeclaringType.IsKnownType(KnownTypeCode.Object)), 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 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 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();
        }
        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 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();
        }
        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 IndexerThatIsNotUsableFromScriptIsNotImported()
        {
            var metadataImporter = new MockMetadataImporter {
                GetPropertySemantics = p => PropertyScriptSemantics.NotUsableFromScript()
            };

            Compile(new[] { "class C { public int this[int i] { get {} set {} } }" }, metadataImporter: metadataImporter);
            FindClass("C").InstanceMethods.Should().BeEmpty();
            FindClass("C").StaticMethods.Should().BeEmpty();
        }
        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 InstanceAutoPropertiesThatShouldNotGenerateBackingFieldsDoNotGenerateBackingFields() {
			var metadataImporter = new MockMetadataImporter { GetConstructorSemantics = c => ConstructorScriptSemantics.Unnamed(skipInInitializer: c.DeclaringType.IsKnownType(KnownTypeCode.Object)),
			                                                  ShouldGenerateAutoPropertyBackingField = p => false,
			                                                };
			Compile(new[] { "class C { public string SomeProp { get; set; } }" }, metadataImporter: metadataImporter);

			Assert.That(FindInstanceMethod("C.get_SomeProp"), Is.Not.Null);
			Assert.That(FindInstanceMethod("C.set_SomeProp"), Is.Not.Null);
			FindClass("C").UnnamedConstructor.Body.Statements.Should().BeEmpty();
			Assert.That(FindClass("C").StaticInitStatements, Is.Empty);
		}
        public void InstanceAutoPropertiesThatShouldBeInstanceFieldsAreCorrectlyImported()
        {
            var metadataImporter = new MockMetadataImporter {
                GetPropertySemantics = p => PropertyScriptSemantics.Field("$" + p.Name)
            };

            Compile(new[] { "class C { public string SomeProp { get; set; } }" }, metadataImporter: metadataImporter);
            FindClass("C").InstanceMethods.Should().BeEmpty();
            FindClass("C").StaticMethods.Should().BeEmpty();
            FindInstanceFieldInitializer("C.$SomeProp").Should().NotBeNull();
        }
        public void AbstractEventIsNotAnAutoEvent()
        {
            var metadataImporter = new MockMetadataImporter { GetEventSemantics = e => EventScriptSemantics.AddAndRemoveMethods(MethodScriptSemantics.NormalMethod("add_" + e.Name), MethodScriptSemantics.NormalMethod("remove_" + e.Name)),
                                                              GetAutoEventBackingFieldName = e => "$" + e.Name
                                                            };

            Compile(new[] { "abstract class C { public abstract event System.EventHandler SomeProp; }" }, metadataImporter: metadataImporter);
            FindInstanceMethod("C.add_SomeProp").Should().NotBeNull();
            FindInstanceMethod("C.add_SomeProp").Definition.Should().BeNull();
            FindInstanceMethod("C.remove_SomeProp").Should().NotBeNull();
            FindInstanceMethod("C.remove_SomeProp").Definition.Should().BeNull();
            FindInstanceFieldInitializer("C.$SomeProp").Should().BeNull();
        }
		public void ImportingMultipleEventsInTheSameDeclarationWorks() {
			var metadataImporter = new MockMetadataImporter { GetEventSemantics = f => EventScriptSemantics.AddAndRemoveMethods(MethodScriptSemantics.NormalMethod("add_" + f.Name), MethodScriptSemantics.NormalMethod("remove_" + f.Name)),
			                                                  GetAutoEventBackingFieldName = f => "$" + f.Name
			                                                };
			Compile(new[] { "class C { public event System.EventHandler Event1, Event2; }" }, metadataImporter: metadataImporter);
			FindInstanceFieldInitializer("C.$Event1").Should().NotBeNull();
			FindInstanceFieldInitializer("C.$Event2").Should().NotBeNull();
			FindInstanceMethod("C.add_Event1").Should().NotBeNull();
			FindInstanceMethod("C.remove_Event1").Should().NotBeNull();
			FindInstanceMethod("C.add_Event2").Should().NotBeNull();
			FindInstanceMethod("C.remove_Event2").Should().NotBeNull();
			FindClass("C").StaticInitStatements.Should().BeEmpty();
			FindClass("C").StaticMethods.Should().BeEmpty();
		}
		public void StaticConstructorBodyGetsAddedLastInTheStaticInitStatements() {
			var metadataImporter = new MockMetadataImporter { GetConstructorSemantics = ctor => { if (ctor.IsStatic) throw new InvalidOperationException(); else return ConstructorScriptSemantics.Unnamed(); } };
			Compile(new[] {
@"class C {
	static int x = 0;
	static C() {
		int z = 2;
	}
	static int y = 1;
}" }, metadataImporter: metadataImporter);

			var cctor = OutputFormatter.Format(FindClass("C").StaticInitStatements, allowIntermediates: true);
			cctor.Replace("\r\n", "\n").Should().Be(
@"{sm_C}.$x = 0;
{sm_C}.$y = 1;
var $z = 2;
".Replace("\r\n", "\n"));
		}
		public void CannotUseMutableValueTypeAsAGenericArgument() {
			var md = new MockMetadataImporter { GetTypeSemantics = t => t.Name == "C1" ? TypeScriptSemantics.MutableValueType(t.Name) : TypeScriptSemantics.NormalType(t.Name) };
			var er = new MockErrorReporter(false);

			Compile(new[] {
@"struct C1 {}
class C {
	public void F1<T>() {}
	public void M() {
		F1<C1>();
	}
}" }, metadataImporter: md, errorReporter: er);

			Assert.That(er.AllMessages.Count, Is.EqualTo(1));
			Assert.That(er.AllMessages[0].Code == 7539 && er.AllMessages[0].FormattedMessage.Contains("mutable value type") && er.AllMessages[0].FormattedMessage.Contains("C1"));

			er = new MockErrorReporter(false);
			Compile(new[] {
@"struct C1 {}
interface I1<T> {}
class C {
	public void F1<T>() {}
	public void M() {
		F1<I1<I1<C1>>>();
	}
}" }, metadataImporter: md, errorReporter: er);

			Assert.That(er.AllMessages.Count, Is.EqualTo(1));
			Assert.That(er.AllMessages[0].Code == 7539 && er.AllMessages[0].FormattedMessage.Contains("mutable value type") && er.AllMessages[0].FormattedMessage.Contains("C1"));
		}