public void FieldConstantInLiftedOperation()
        {
            AssertCorrect(@"
public void M() {
	bool b;
	double? d = 0, d2;
	// BEGIN
	b  = d == double.PositiveInfinity;
	b  = d != double.PositiveInfinity;
	b  = d >= double.PositiveInfinity;
	b  = d <= double.PositiveInfinity;
	b  = d > double.PositiveInfinity;
	b  = d < double.PositiveInfinity;
	d2 = d + double.PositiveInfinity;
	d2 = d - double.PositiveInfinity;
	d2 = d * double.PositiveInfinity;
	d2 = d / double.PositiveInfinity;
	// END
}",
                          @"	$b = $d === {sm_Double}.$PosInf;
	$b = $d !== {sm_Double}.$PosInf;
	$b = $Lift($d >= {sm_Double}.$PosInf);
	$b = $Lift($d <= {sm_Double}.$PosInf);
	$b = $Lift($d > {sm_Double}.$PosInf);
	$b = $Lift($d < {sm_Double}.$PosInf);
	$d2 = $Lift($d + {sm_Double}.$PosInf);
	$d2 = $Lift($d - {sm_Double}.$PosInf);
	$d2 = $Lift($d * {sm_Double}.$PosInf);
	$d2 = $Lift($d / {sm_Double}.$PosInf);
", metadataImporter: new MockMetadataImporter {
                GetFieldSemantics = f => FieldScriptSemantics.Field("$PosInf")
            });
        }
 public void SetFieldSemanticsWorks()
 {
     Prepare(@"public class C { public int TheField; } public class D : C { public new int TheField; }", () => {
         Metadata.SetFieldSemantics(FindField("C.TheField"), FieldScriptSemantics.Field("__something_else__"));
     });
     Assert.AreEqual(Metadata.GetFieldSemantics(FindField("C.TheField")).Name, "__something_else__");
     Assert.AreEqual(Metadata.GetFieldSemantics(FindField("D.TheField")).Name, "theField");
 }
        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 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 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 FieldConstantInLiftedOperation()
        {
            AssertCorrect(@"
public void M() {
	bool b;
	double? d = 0;
	// BEGIN
	d += double.PositiveInfinity;
	d -= double.PositiveInfinity;
	d *= double.PositiveInfinity;
	d /= double.PositiveInfinity;
	// END
}",
                          @"	$d = $Lift($d + {sm_Double}.$PosInf);
	$d = $Lift($d - {sm_Double}.$PosInf);
	$d = $Lift($d * {sm_Double}.$PosInf);
	$d = $Lift($d / {sm_Double}.$PosInf);
", metadataImporter: new MockMetadataImporter {
                GetFieldSemantics = f => FieldScriptSemantics.Field("$PosInf")
            });
        }
Beispiel #7
0
        public void AssigningToFieldImplementedAsConstantIsAnError()
        {
            var er = new MockErrorReporter(false);

            Compile(new[] {
                @"class C {
	public int F1;
	public void M() {
		// BEGIN
		F1 = 1;
		// END
	}
}"
            }, metadataImporter: new MockMetadataImporter {
                GetFieldSemantics = f => f.Name == "F1" ? FieldScriptSemantics.NullConstant() : FieldScriptSemantics.Field(f.Name)
            }, errorReporter: er);

            Assert.That(er.AllMessagesText.Count, Is.EqualTo(1));
            Assert.That(er.AllMessagesText.Any(m => m.Contains("C.F1") && m.Contains("cannot be assigned to")));

            er = new MockErrorReporter(false);
            Compile(new[] {
                @"class C {
	public int F1;
	public void M() {
		// BEGIN
		F1 += 1;
		// END
	}
}"
            }, metadataImporter: new MockMetadataImporter {
                GetFieldSemantics = f => f.Name == "F1" ? FieldScriptSemantics.NullConstant() : FieldScriptSemantics.Field(f.Name)
            }, errorReporter: er);

            Assert.That(er.AllMessagesText.Count, Is.EqualTo(1));
            Assert.That(er.AllMessagesText.Any(m => m.Contains("C.F1") && m.Contains("cannot be assigned to")));
        }
Beispiel #8
0
        public void ReadingFieldImplementedAsConstantWorks()
        {
            AssertCorrect(
                @"
public int F1;
public int F2;
public int F3;
public int F4;

public void M() {
	// BEGIN
	var f1 = F1;
	var f2 = F2;
	var f3 = F3;
	var f4 = F4;
	// END
}",
                @"	var $f1 = null;
	var $f2 = 'abcd';
	var $f3 = 1234.5;
	var $f4 = true;
", metadataImporter: new MockMetadataImporter {
                GetFieldSemantics = f => f.Name == "F1" ? FieldScriptSemantics.NullConstant() : (f.Name == "F2" ? FieldScriptSemantics.StringConstant("abcd") : (f.Name == "F3" ? FieldScriptSemantics.NumericConstant(1234.5) : (f.Name == "F4" ? FieldScriptSemantics.BooleanConstant(true) : FieldScriptSemantics.Field(f.Name))))
            });
        }
        public void StaticFieldsAreCorrectlyImported()
        {
            var metadataImporter = new MockMetadataImporter {
                GetConstructorSemantics = c => ConstructorScriptSemantics.Unnamed(skipInInitializer: c.DeclaringType.IsKnownType(KnownTypeCode.Object)), GetFieldSemantics = f => FieldScriptSemantics.Field("$SomeProp")
            };

            Compile(new[] { "class C { public static int SomeField; }" }, metadataImporter: metadataImporter);
            FindStaticFieldInitializer("C.$SomeProp").Should().NotBeNull();
            FindClass("C").UnnamedConstructor.Body.Statements.Should().BeEmpty();
            FindClass("C").InstanceMethods.Should().BeEmpty();
            FindClass("C").StaticMethods.Should().BeEmpty();
        }
Beispiel #10
0
        public void FieldsImplementedAsConstantsWorkAsCaseLabels()
        {
            AssertCorrect(
                @"enum E { Value1, Value2 }
public void M() {
	E e = E.Value1;
	// BEGIN
	switch (e) {
		case E.Value1:
			int x = 0;
			break;
		case E.Value2:
			int y = 0;
			break;
		default:
			int z = 0;
			break;
	}
	// END
}",
                @"	switch ($e) {
		case 'Value1': {
			var $x = 0;
			break;
		}
		case 'Value2': {
			var $y = 0;
			break;
		}
		default: {
			var $z = 0;
			break;
		}
	}
", metadataImporter: new MockMetadataImporter {
                GetFieldSemantics = f => f.DeclaringType.Name == "E" ? FieldScriptSemantics.StringConstant(f.Name) : FieldScriptSemantics.Field(f.Name)
            });
        }
        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 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" });
        }