public void InstanceAutoEventAccessorsImplementedAsStaticMethodsAreCorrectlyCompiled() { Compile(new[] { "using System; class C { public event System.EventHandler MyEvent; }" }, metadataImporter: new MockMetadataImporter { GetEventSemantics = e => EventScriptSemantics.AddAndRemoveMethods(MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("add_" + e.Name), MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("remove_" + e.Name)) }); var adder = FindStaticMethod("C.add_MyEvent"); var remover = FindStaticMethod("C.remove_MyEvent"); AssertCorrect(adder.Definition, @"function($this, $value) { $this.$MyEvent = {sm_Delegate}.Combine($this.$MyEvent, $value); }"); AssertCorrect(remover.Definition, @"function($this, $value) { $this.$MyEvent = {sm_Delegate}.Remove($this.$MyEvent, $value); }"); AssertCorrect(FindClass("C").UnnamedConstructor, @"function() { this.$MyEvent = $Default({def_EventHandler}); {sm_Object}.call(this); }"); }
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 SetEventSemanticsWorks() { Prepare(@"public class C { public event System.EventHandler TheEvent; } public class D : C { public new event System.EventHandler TheEvent; }", () => { Metadata.SetEventSemantics(FindEvent("C.TheEvent"), EventScriptSemantics.AddAndRemoveMethods(MethodScriptSemantics.NormalMethod("addIt"), MethodScriptSemantics.NormalMethod("removeIt"))); }); Assert.AreEqual(Metadata.GetEventSemantics(FindEvent("C.TheEvent")).Type, EventScriptSemantics.ImplType.AddAndRemoveMethods); Assert.AreEqual(Metadata.GetEventSemantics(FindEvent("C.TheEvent")).AddMethod.Name, "addIt"); Assert.AreEqual(Metadata.GetEventSemantics(FindEvent("D.TheEvent")).Type, EventScriptSemantics.ImplType.AddAndRemoveMethods); Assert.AreEqual(Metadata.GetEventSemantics(FindEvent("D.TheEvent")).AddMethod.Name, "add_theEvent"); }
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 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 InstanceAutoEventsWithAddRemoveMethodsWithNoCodeAreCorrectlyImported() { var metadataImporter = new MockMetadataImporter { GetEventSemantics = e => EventScriptSemantics.AddAndRemoveMethods(MethodScriptSemantics.NormalMethod("add_" + e.Name, generateCode: false), MethodScriptSemantics.NormalMethod("remove_" + e.Name, generateCode: false)), GetAutoEventBackingFieldName = e => { throw new InvalidOperationException(); } }; Compile(new[] { "class C { public event System.EventHandler SomeProp; }" }, metadataImporter: metadataImporter); FindClass("C").InstanceMethods.Should().BeEmpty(); FindClass("C").UnnamedConstructor.Body.Statements.Should().BeEmpty(); }
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(); }
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 UsingEventRemoveAccessorImplementedAsInlineCodeWorks() { AssertCorrect( @"event System.EventHandler MyEvent; public void M() { System.EventHandler h = null; // BEGIN MyEvent -= h; // END }", @" remove_(this)._($h); ", metadataImporter: new MockMetadataImporter() { GetEventSemantics = e => EventScriptSemantics.AddAndRemoveMethods(MethodScriptSemantics.InlineCode("add_({this})._({value})"), MethodScriptSemantics.InlineCode("remove_({this})._({value})")) }); }
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 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; }
protected void AssertCorrect(string csharp, string expected, IMetadataImporter metadataImporter = null, IRuntimeLibrary runtimeLibrary = null, bool addSkeleton = true, IEnumerable <IAssemblyReference> references = null, string methodName = "M", bool mutableValueTypes = false) { CompileMethod(csharp, metadataImporter: metadataImporter ?? new MockMetadataImporter { GetPropertySemantics = p => { if (p.DeclaringType.Kind == TypeKind.Anonymous || new Regex("^F[0-9]*$").IsMatch(p.Name) || (p.DeclaringType.FullName == "System.Array" && p.Name == "Length")) { return(PropertyScriptSemantics.Field("$" + p.Name)); } else { return(PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.NormalMethod("get_$" + p.Name), MethodScriptSemantics.NormalMethod("set_$" + p.Name))); } }, GetMethodSemantics = m => MethodScriptSemantics.NormalMethod("$" + m.Name), GetEventSemantics = e => EventScriptSemantics.AddAndRemoveMethods(MethodScriptSemantics.NormalMethod("add_$" + e.Name), MethodScriptSemantics.NormalMethod("remove_$" + e.Name)), GetTypeSemantics = t => { return(mutableValueTypes && t.Kind == TypeKind.Struct ? TypeScriptSemantics.MutableValueType(t.FullName) : TypeScriptSemantics.NormalType(t.FullName)); } }, runtimeLibrary: runtimeLibrary, methodName: methodName, addSkeleton: addSkeleton, references: references); string actual = OutputFormatter.Format(CompiledMethod, true); int begin = actual.IndexOf("// BEGIN"); if (begin > -1) { while (begin < (actual.Length - 1) && actual[begin - 1] != '\n') { begin++; } actual = actual.Substring(begin); } int end = actual.IndexOf("// END"); if (end >= 0) { while (end >= 0 && actual[end] != '\n') { end--; } actual = actual.Substring(0, end + 1); } Assert.That(actual.Replace("\r\n", "\n"), Is.EqualTo(expected.Replace("\r\n", "\n")), "Expected:\n" + expected + "\n\nActual:\n" + actual); }