فهرست منبع

Implementing Enum name support

With this commit you are able to use the Enum type and Name
Florian 11 سال پیش
والد
کامیت
cc77e90380
2فایلهای تغییر یافته به همراه69 افزوده شده و 2 حذف شده
  1. 52 1
      Jint.Tests/Runtime/InteropTests.cs
  2. 17 1
      Jint/Runtime/Interop/TypeReference.cs

+ 52 - 1
Jint.Tests/Runtime/InteropTests.cs

@@ -601,9 +601,60 @@ namespace Jint.Tests.Runtime
                 list.Add(1);
                 assert(2 === list.Count);
             ");
+        }
+
+        [Fact]
+        public void EnumComparesByName()
+        {
+            var o = new
+            {
+                r = Colors.Red,
+                b = Colors.Blue,
+                g = Colors.Green,
+                b2 = Colors.Red
+            };
+
+            _engine.SetValue("o", o);
+            _engine.SetValue("assertFalse", new Action<bool>(Assert.False));
+
+            RunTest(@"
+                var domain = importNamespace('Jint.Tests.Runtime.Domain');
+                var colors = domain.Colors;
+                assert(o.r === colors.Red);
+                assert(o.g === colors.Green);
+                assert(o.b === colors.Blue);
+                assertFalse(o.b2 === colors.Blue);
+            ");
+        }
+
+        [Fact]
+        public void ShouldSetEnumProperty()
+        {
+            var s = new Circle
+            {
+                Color = Colors.Red,
+            };
+
+            _engine.SetValue("s", s);
+
+            RunTest(@"
+                var domain = importNamespace('Jint.Tests.Runtime.Domain');
+                var colors = domain.Colors;
+                
+                s.Color = colors.Blue;
+                assert(s.Color === colors.Blue);
+            ");
+
+            _engine.SetValue("s", s);
+
+            RunTest(@"
+                s.Color = colors.Blue | colors.Green;
+                assert(s.Color === colors.Blue | colors.Green);
+            ");
+
+            Assert.Equal(Colors.Blue | Colors.Green, s.Color);
         }
 
-
         [Fact]
         public void EnumIsConvertedToNumber()
         {

+ 17 - 1
Jint/Runtime/Interop/TypeReference.cs

@@ -109,7 +109,23 @@ namespace Jint.Runtime.Interop
 
         public override PropertyDescriptor GetOwnProperty(string propertyName)
         {
-            // todo: cache members locally
+            // todo: cache members locally
+
+            if (Type.IsEnum)
+            {
+                Array enumValues = Enum.GetValues(Type);
+                Array enumNames = Enum.GetNames(Type);
+
+                for (int i = 0; i < enumValues.Length; i++)
+                {
+                    if (enumNames.GetValue(i) as string == propertyName)
+                    {
+                        return new PropertyDescriptor((int)enumValues.GetValue(i), false, false, false);
+                    }
+                }
+                return PropertyDescriptor.Undefined;
+            }
+
             var propertyInfo = Type.GetProperty(propertyName);
             if (propertyInfo != null)
             {