public void IndexerThatIsNotUsableFromScriptIsNotImported() { var namingConvention = new MockNamingConventionResolver { GetPropertySemantics = p => PropertyScriptSemantics.NotUsableFromScript() }; Compile(new[] { "class C { public int this[int i] { get {} set {} } }" }, namingConvention: namingConvention); FindClass("C").InstanceMethods.Should().BeEmpty(); FindClass("C").StaticMethods.Should().BeEmpty(); }
public void ConstructorImplementedAsStaticMethodGetsAddedToTheStaticMethodsCollectionAndNotTheConstructors() { var namingConvention = new MockNamingConventionResolver { GetConstructorSemantics = ctor => ConstructorScriptSemantics.StaticMethod("X") }; Compile(new[] { "class C { public C() {} }" }, namingConvention: namingConvention); FindStaticMethod("C.X").Should().NotBeNull(); FindNamedConstructor("C.X").Should().BeNull(); }
public void AbstractIndexerHasANullDefinition() { var namingConvention = new MockNamingConventionResolver { 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; } }" }, namingConvention: namingConvention); FindInstanceMethod("C.get_Item").Definition.Should().BeNull(); FindInstanceMethod("C.set_Item").Definition.Should().BeNull(); }
public void IndexerAccessorsInInterfaceHaveNullDefinition() { var namingConvention = new MockNamingConventionResolver { GetPropertySemantics = p => PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.NormalMethod("get_Item"), MethodScriptSemantics.NormalMethod("set_Item")) }; Compile(new[] { "interface I { int this[int i] { get { return 0; } set {} } }" }, namingConvention: namingConvention); FindInstanceMethod("I.get_Item").Should().NotBeNull(); FindInstanceMethod("I.set_Item").Should().NotBeNull(); }
public void CannotUseNotUsableTypeInATypeOfExpression() { var nc = new MockNamingConventionResolver { 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); } }" }, namingConvention: 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("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>>); } }" }, namingConvention: 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("typeof") && er.AllMessagesText[0].Contains("C1")); }
public void DefaultConstructorImplementedAsStaticMethodWorks() { var namingConvention = new MockNamingConventionResolver { GetConstructorSemantics = ctor => ConstructorScriptSemantics.StaticMethod("X") }; Compile(new[] { "class C { }" }, namingConvention: namingConvention); FindStaticMethod("C.X").Should().NotBeNull(); FindNamedConstructor("C.X").Should().BeNull(); }
public void ReadOnlyNativeIndexerIsCorrectlyImported() { var namingConvention = new MockNamingConventionResolver { GetPropertySemantics = p => PropertyScriptSemantics.NativeIndexer() }; Compile(new[] { "class C { public int this[int i] { get { return 0; } } }" }, namingConvention: namingConvention); FindClass("C").InstanceMethods.Should().BeEmpty(); FindClass("C").StaticMethods.Should().BeEmpty(); }
public void IndexerWithGetAndSetMethodsIsCorrectlyImported() { var namingConvention = new MockNamingConventionResolver { GetPropertySemantics = p => PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.NormalMethod("get_Item"), MethodScriptSemantics.NormalMethod("set_Item")) }; Compile(new[] { "class C { public int this[int i] { get {} set {} } }" }, namingConvention: namingConvention); FindInstanceMethod("C.get_Item").Should().NotBeNull(); FindInstanceMethod("C.set_Item").Should().NotBeNull(); }
public void IndexerWithGetAndSetMethodsWithNoCodeIsCorrectlyImported() { var namingConvention = new MockNamingConventionResolver { 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 {} } }" }, namingConvention: namingConvention); FindInstanceMethod("C.get_Item").Should().BeNull(); FindInstanceMethod("C.set_Item").Should().BeNull(); FindClass("C").StaticMethods.Should().BeEmpty(); }
public void InstanceAutoPropertiesThatShouldBeInstanceFieldsAreCorrectlyImported() { var namingConvention = new MockNamingConventionResolver { GetPropertySemantics = p => PropertyScriptSemantics.Field("$" + p.Name) }; Compile(new[] { "class C { public string SomeProp { get; set; } }" }, namingConvention: namingConvention); FindClass("C").InstanceMethods.Should().BeEmpty(); FindClass("C").StaticMethods.Should().BeEmpty(); FindInstanceFieldInitializer("C.$SomeProp").Should().NotBeNull(); }
public void ConstructorsCanBeOverloadedWithDifferentImplementations() { var namingConvention = new MockNamingConventionResolver { GetConstructorSemantics = ctor => ctor.Parameters[0].Type.Name == "String" ? ConstructorScriptSemantics.Named("StringCtor") : ConstructorScriptSemantics.StaticMethod("IntCtor") }; Compile(new[] { "class C { C(int i) {} C(string s) {} }" }, namingConvention: namingConvention); FindClass("C").NamedConstructors.Should().HaveCount(1); FindClass("C").StaticMethods.Should().HaveCount(1); FindNamedConstructor("C.StringCtor").Should().NotBeNull(); FindStaticMethod("C.IntCtor").Should().NotBeNull(); }
public void FieldsThatAreNotUsableFromScriptAreNotImported() { var namingConvention = new MockNamingConventionResolver { GetFieldSemantics = f => FieldScriptSemantics.NotUsableFromScript() }; Compile(new[] { "class C { public int SomeField; }" }, namingConvention: namingConvention); FindClass("C").UnnamedConstructor.Body.Statements.Should().BeEmpty(); FindClass("C").StaticInitStatements.Should().BeEmpty(); FindClass("C").InstanceMethods.Should().BeEmpty(); FindClass("C").StaticMethods.Should().BeEmpty(); }
public void ImportingMultipleFieldsInTheSameDeclarationWorks() { var namingConvention = new MockNamingConventionResolver { GetFieldSemantics = f => FieldScriptSemantics.Field("$" + f.Name) }; Compile(new[] { "class C { public int Field1, Field2; }" }, namingConvention: namingConvention); 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 DefaultConstructorIsNotInsertedIfOtherConstructorIsDefined() { var namingConvention = new MockNamingConventionResolver() { 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) {} }" }, namingConvention: namingConvention); 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 InstanceAutoPropertiesWithGetSetMethodsAndFieldAreCorrectlyImported() { var namingConvention = new MockNamingConventionResolver { 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; } }" }, namingConvention: namingConvention); FindInstanceMethod("C.get_SomeProp").Should().NotBeNull(); FindInstanceMethod("C.set_SomeProp").Should().NotBeNull(); FindInstanceFieldInitializer("C.$SomeProp").Should().NotBeNull(); }
public void InstanceAutoEventsWithAddRemoveMethodsAreCorrectlyImported() { var namingConvention = new MockNamingConventionResolver { GetEventSemantics = e => EventScriptSemantics.AddAndRemoveMethods(MethodScriptSemantics.NormalMethod("add_" + e.Name), MethodScriptSemantics.NormalMethod("remove_" + e.Name)), GetAutoEventBackingFieldName = e => "$" + e.Name }; Compile(new[] { "class C { public event System.EventHandler SomeProp; }" }, namingConvention: namingConvention); FindInstanceMethod("C.add_SomeProp").Should().NotBeNull(); FindInstanceMethod("C.remove_SomeProp").Should().NotBeNull(); FindInstanceFieldInitializer("C.$SomeProp").Should().NotBeNull(); }
public void AbstractEventIsNotAnAutoEvent() { var namingConvention = new MockNamingConventionResolver { 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; }" }, namingConvention: namingConvention); 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 AbstractPropertyIsNotAnAutoProperty() { var namingConvention = new MockNamingConventionResolver { GetPropertySemantics = p => PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.NormalMethod("get_" + p.Name), MethodScriptSemantics.NormalMethod("set_" + p.Name)), GetAutoPropertyBackingFieldName = p => "$" + p.Name }; Compile(new[] { "abstract class C { public abstract string SomeProp { get; set; } }" }, namingConvention: namingConvention); FindInstanceMethod("C.get_SomeProp").Should().NotBeNull(); FindInstanceMethod("C.get_SomeProp").Definition.Should().BeNull(); FindInstanceMethod("C.set_SomeProp").Should().NotBeNull(); FindInstanceMethod("C.set_SomeProp").Definition.Should().BeNull(); FindInstanceFieldInitializer("C.$SomeProp").Should().BeNull(); }
public void InstanceAutoPropertiesWithGetSetMethodsStaticWithNoCodeAreCorrectlyImported() { var namingConvention = new MockNamingConventionResolver { 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; } }" }, namingConvention: namingConvention); 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); }
public void ImportingMultipleEventsInTheSameDeclarationWorks() { var namingConvention = new MockNamingConventionResolver { 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; }" }, namingConvention: namingConvention); 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 CannotUseNotUsableTypeAsATypeArgument() { var nc = new MockNamingConventionResolver { 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(); } }" }, namingConvention: 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>>(); } }" }, namingConvention: 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 namingConvention = new MockNamingConventionResolver { 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; }" }, namingConvention: namingConvention); var cctor = FindClass("C").StaticInitStatements.Aggregate("", (s, st) => s + OutputFormatter.Format(st, true)); cctor.Replace("\r\n", "\n").Should().Be( @"{C}.$x = 0; {C}.$y = 1; var $z = 2; ".Replace("\r\n", "\n")); }
public void ShadowingMethodsAreIncluded() { var namingConvention = new MockNamingConventionResolver { 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() {} }" }, namingConvention: namingConvention); var cls = FindClass("C"); cls.InstanceMethods.Should().HaveCount(1); cls.InstanceMethods[0].Name.Should().Be("XDerived"); }
public void ConstructorImplementedAsNotUsableFromScriptDoesNotAppearOnTheType() { var namingConvention = new MockNamingConventionResolver { GetConstructorSemantics = ctor => ConstructorScriptSemantics.NotUsableFromScript() }; Compile(new[] { "class C { public C() {} }" }, namingConvention: namingConvention); FindClass("C").UnnamedConstructor.Should().BeNull(); }
public void StaticMethodWithThisAsFirstArgumentAppearsOnTheType() { var namingConvention = new MockNamingConventionResolver { GetMethodSemantics = method => MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("X") }; Compile(new[] { "class C { public static void M() {} }" }, namingConvention: namingConvention); FindClass("C").InstanceMethods.Should().BeEmpty(); FindStaticMethod("C.X").Should().NotBeNull(); }
public void StaticFieldsWithoutInitializersAreInitializedToDefault() { var namingConvention = new MockNamingConventionResolver { 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; }" }, namingConvention: namingConvention); var cctor = FindClass("C").StaticInitStatements.Aggregate("", (s, st) => s + OutputFormatter.Format(st, true)); cctor.Replace("\r\n", "\n").Should().Be( @"$InstantiateGenericType({C}, $T).$x = $Default($T); $InstantiateGenericType({C}, $T).$y = 0; $InstantiateGenericType({C}, $T).$z = null; ".Replace("\r\n", "\n")); }
public void MethodImplementedAsNotUsableFromScriptDoesNotAppearOnTheType() { var namingConvention = new MockNamingConventionResolver { GetMethodSemantics = method => MethodScriptSemantics.NotUsableFromScript() }; Compile(new[] { "class C { public static void M() {} }" }, namingConvention: namingConvention); FindClass("C").InstanceMethods.Should().BeEmpty(); }
public void InstanceMethodWithGenerateCodeSetToFalseDoesNotAppearOnTheType() { var namingConvention = new MockNamingConventionResolver { GetMethodSemantics = method => MethodScriptSemantics.NormalMethod("X", generateCode: false) }; Compile(new[] { "class C { public static void M() {} }" }, namingConvention: namingConvention); FindClass("C").InstanceMethods.Should().BeEmpty(); }
public void GenericMethodTypeArgumentsAreIncludedForStaticMethods() { var namingConvention = new MockNamingConventionResolver { GetMethodSemantics = m => MethodScriptSemantics.NormalMethod("X"), GetTypeParameterName = tp => "$$" + tp.Name }; Compile(new[] { "class C { public static void X<U, V>() {} }" }, namingConvention: namingConvention); FindStaticMethod("C.X").TypeParameterNames.Should().Equal(new[] { "$$U", "$$V" }); }
public void GenericMethodTypeArgumentsAreIgnoredForStaticMethodsIfTheMethodImplOptionsSaySo() { var namingConvention = new MockNamingConventionResolver { GetMethodSemantics = m => MethodScriptSemantics.NormalMethod("X", ignoreGenericArguments: true), GetTypeParameterName = tp => "$$" + tp.Name }; Compile(new[] { "class C { public static void X<U, V>() {} }" }, namingConvention: namingConvention); FindStaticMethod("C.X").TypeParameterNames.Should().BeEmpty(); }