public void InstanceAutoPropertyAccessorsImplementedAsStaticMethodsAreCorrectlyCompiled() { Compile(new[] { "using System; class C { public int MyProperty { get; set; } }" }, metadataImporter: new MockMetadataImporter { GetPropertySemantics = p => PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("get_" + p.Name), MethodScriptSemantics.StaticMethodWithThisAsFirstArgument("set_" + p.Name)) }); var getter = FindStaticMethod("C.get_MyProperty"); var setter = FindStaticMethod("C.set_MyProperty"); AssertCorrect(getter.Definition, @"function($this) { return $this.$MyProperty; }"); AssertCorrect(setter.Definition, @"function($this, $value) { $this.$MyProperty = $value; }"); AssertCorrect(FindClass("C").UnnamedConstructor, @"function() { $Init(this, '$MyProperty', $Default({def_Int32})); {sm_Object}.call(this); }"); }
public void ReadingNotUsablePropertyGivesAnError() { var er = new MockErrorReporter(false); Compile(new[] { "class Class { int UnusableProperty { get; set; } public void M() { int i = UnusableProperty; } }" }, metadataImporter: new MockMetadataImporter { GetPropertySemantics = p => PropertyScriptSemantics.NotUsableFromScript() }, errorReporter: er); Assert.That(er.AllMessagesText.Any(m => m.StartsWith("Error:") && m.Contains("Class.UnusableProperty"))); }
public void UsingPropertyThatIsNotUsableFromScriptGivesAnError() { var er = new MockErrorReporter(false); Compile(new[] { "class Class { int UnusableProperty { get; set; } public void M() { UnusableProperty += 0; } }" }, metadataImporter: new MockMetadataImporter { GetPropertySemantics = p => PropertyScriptSemantics.NotUsableFromScript() }, errorReporter: er); Assert.That(er.AllMessages.Any(msg => msg.Severity == MessageSeverity.Error && msg.FormattedMessage.Contains("Class.UnusableProperty"))); }
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 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 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 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 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 SetPropertySemanticsWorks() { Prepare(@"public class C { public int TheProperty { get; set; } } public class D : C { public new int TheProperty { get; set; } }", () => { Metadata.SetPropertySemantics(FindProperty("C.TheProperty"), PropertyScriptSemantics.Field("__something_else__")); }); Assert.AreEqual(Metadata.GetPropertySemantics(FindProperty("C.TheProperty")).Type, PropertyScriptSemantics.ImplType.Field); Assert.AreEqual(Metadata.GetPropertySemantics(FindProperty("C.TheProperty")).FieldName, "__something_else__"); Assert.AreEqual(Metadata.GetPropertySemantics(FindProperty("D.TheProperty")).Type, PropertyScriptSemantics.ImplType.GetAndSetMethods); Assert.AreEqual(Metadata.GetPropertySemantics(FindProperty("D.TheProperty")).GetMethod.Name, "get_theProperty"); }
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 InstanceAutoPropertiesWithGetSetMethodsWithNoCodeAreCorrectlyImported() { var metadataImporter = new MockMetadataImporter { 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(); }
public void ManuallyImplementedWriteOnlyStaticPropertyWithGetAndSetMethodsIsCorrectlyImported() { var metadataImporter = new MockMetadataImporter { GetPropertySemantics = p => PropertyScriptSemantics.GetAndSetMethods(null, MethodScriptSemantics.NormalMethod("set_SomeProp")) }; Compile(new[] { "class C { public static int SomeProp { set {} } }" }, metadataImporter: metadataImporter); FindStaticMethod("C.get_SomeProp").Should().BeNull(); FindStaticMethod("C.set_SomeProp").Should().NotBeNull(); FindClass("C").InstanceMethods.Should().BeEmpty(); }
public void ManuallyImplementedWriteOnlyStaticPropertyThatShouldBeAFieldIsCorrectlyImported() { var metadataImporter = new MockMetadataImporter { GetPropertySemantics = p => PropertyScriptSemantics.Field("$SomeProp") }; Compile(new[] { "class C { public static int SomeProp { set {} } }" }, metadataImporter: metadataImporter); FindStaticFieldInitializer("C.$SomeProp").Should().NotBeNull(); FindClass("C").InstanceMethods.Should().BeEmpty(); FindClass("C").StaticMethods.Should().BeEmpty(); }
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 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 StaticAutoPropertiesWithGetSetMethodsAndFieldAreCorrectlyImported() { 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 static string SomeProp { get; set; } }" }, metadataImporter: metadataImporter); FindStaticMethod("C.get_SomeProp").Should().NotBeNull(); FindStaticMethod("C.set_SomeProp").Should().NotBeNull(); FindStaticFieldInitializer("C.$SomeProp").Should().NotBeNull(); }
public void AssigningToPropertyWithSetMethodImplementedAsInlineCodeWorks() { AssertCorrect( @"int P { get; set; } public void M() { int i = 0; // BEGIN P = i; // END }", @" set_(this)._($i); ", metadataImporter: new MockMetadataImporter { GetPropertySemantics = p => PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.InlineCode("get_({this})"), MethodScriptSemantics.InlineCode("set_({this})._({value})")) }); }
public void AssigningToPropertyWithSetMethodImplementedAsInlineCodeWorksStruct() { AssertCorrect( @"int P { get; set; } public void M() { int i = 0; // BEGIN P = i; // END }", @" set_(this)._($Clone($i, {to_Int32})); ", metadataImporter: new MockMetadataImporter { GetPropertySemantics = p => PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.InlineCode("get_({this})"), MethodScriptSemantics.InlineCode("set_({this})._({value})")), GetTypeSemantics = t => TypeScriptSemantics.MutableValueType(t.Name) }); }
public void ReadingPropertyWithGetMethodImplementedAsInlineCodeWorks() { AssertCorrect( @"int P { get; set; } public void M() { // BEGIN int i = P; // END }", @" var $i = get_(this); ", metadataImporter: new MockMetadataImporter { GetPropertySemantics = p => PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.InlineCode("get_({this})"), MethodScriptSemantics.InlineCode("set_({this})._({value})")) }); }
public void AbstractPropertyIsNotAnAutoProperty() { var metadataImporter = new MockMetadataImporter { 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; } }" }, metadataImporter: metadataImporter); 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 ReadingPropertyImplementedAsNativeIndexerWorks() { AssertCorrect( @"int this[int x] { get { return 0; } set {} } public void M() { int a = 0, b = 0; // BEGIN int i = this[a]; // END }", @" var $i = this[$a]; ", metadataImporter: new MockMetadataImporter { GetPropertySemantics = p => p.IsIndexer ? PropertyScriptSemantics.NativeIndexer() : PropertyScriptSemantics.Field(p.Name) }); }
public void PostfixForPropertyImplementedAsNativeIndexerWorks() { AssertCorrectForBoth( @"int this[int x] { get { return 0; } set {} } public void M() { int i = 0, j = 1; // BEGIN j = this[i]++; // END }", @" $j = this[$i]++; ", metadataImporter: new MockMetadataImporter { GetPropertySemantics = p => p.IsIndexer ? PropertyScriptSemantics.NativeIndexer() : PropertyScriptSemantics.Field(p.Name) }); }
public void LiftedPrefixForPropertyImplementedAsNativeIndexerWorksWhenUsingTheReturnValue() { AssertCorrectForBoth( @"int? this[int x] { get { return 0; } set {} } public void M() { int i = 0; // BEGIN var x = ++this[i]; // END }", @" var $x = this[$i] = $Lift(this[$i] + 1); ", metadataImporter: new MockMetadataImporter { GetPropertySemantics = p => p.IsIndexer ? PropertyScriptSemantics.NativeIndexer() : PropertyScriptSemantics.Field(p.Name) }); }
public void CompoundAssigningToPropertyImplementedAsNativeIndexerWorks() { AssertCorrectForBulkOperators( @"int this[int x] { get { return 0; } set {} } public void M() { int i = 0, j = 1, k = 2, l; // BEGIN l = this[i] += k; // END }", @" $l = this[$i] += $k; ", metadataImporter: new MockMetadataImporter { GetPropertySemantics = p => p.IsIndexer ? PropertyScriptSemantics.NativeIndexer() : PropertyScriptSemantics.Field(p.Name) }); }
public void CompoundAssigningToIndexerImplementedAsInlineCodeWorks() { AssertCorrectForBulkOperators( @"int this[int x, int y] { get { return 0; } set {} } public void M() { int i = 0, j = 1, k = 2; // BEGIN this[i, j] += k; // END }", @" set_(this)._($i)._($j)._(get_(this)._($i)._($j) + $k); ", metadataImporter: new MockMetadataImporter { GetPropertySemantics = p => p.IsIndexer ? PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.InlineCode("get_({this})._({x})._({y})"), MethodScriptSemantics.InlineCode("set_({this})._({x})._({y})._({value})")) : PropertyScriptSemantics.Field(p.Name) }); }
public void AssigningToIndexerImplementedAsInlineCodeWorksStruct() { AssertCorrect( @"int this[int x, int y] { get { return 0; } set {} } public void M() { int i = 0, j = 1, k = 2; // BEGIN this[i, j] = k; // END }", @" set_(this)._($Clone($i, {to_Int32}))._($Clone($j, {to_Int32}))._($Clone($k, {to_Int32})); ", metadataImporter: new MockMetadataImporter { GetPropertySemantics = p => p.IsIndexer ? PropertyScriptSemantics.GetAndSetMethods(MethodScriptSemantics.InlineCode("get_({this})._({x})._({y})"), MethodScriptSemantics.InlineCode("set_({this})._({x})._({y})._({value})")) : PropertyScriptSemantics.Field(p.Name), GetTypeSemantics = t => TypeScriptSemantics.MutableValueType(t.Name) }); }
public void AssigningToPropertyImplementedAsNativeIndexerWorksStruct() { AssertCorrect( @"int this[int x] { get { return 0; } set {} } public void M() { int i = 0, j = 1, k = 2, l; // BEGIN l = this[i] = k; // END }", @" this[$i] = $Clone($k, {to_Int32}); $l = $Clone(this[$i], {to_Int32}); ", metadataImporter: new MockMetadataImporter { GetPropertySemantics = p => p.IsIndexer ? PropertyScriptSemantics.NativeIndexer() : PropertyScriptSemantics.Field(p.Name), GetTypeSemantics = t => TypeScriptSemantics.MutableValueType(t.Name) }); }
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; }
public void InstanceAutoPropertiesWithGetSetMethodsStaticWithNoCodeAreCorrectlyImported() { var metadataImporter = new MockMetadataImporter { GetConstructorSemantics = c => ConstructorScriptSemantics.Unnamed(skipInInitializer: c.DeclaringType.IsKnownType(KnownTypeCode.Object)), 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; } }" }, metadataImporter: metadataImporter); 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 DivisionCompoundAssignmentWorksForFloatingPointPropertiesImplementedAsNativeIndexers() { DoForAllFloatingPointTypes(type => AssertCorrect( @"public type this[int x] { get { return 0; } set {} } public void M() { int i = 0; type j = 0; // BEGIN this[i] /= j; // END } ".Replace("type", type), @" this[$i] /= $j; ", metadataImporter: new MockMetadataImporter { GetPropertySemantics = p => p.IsIndexer ? PropertyScriptSemantics.NativeIndexer() : PropertyScriptSemantics.Field(p.Name) })); }