Esempio n. 1
0
 /// <summary>
 ///     Determines whether [contains] [the specified instance].
 /// </summary>
 /// <param name="instance">The instance.</param>
 /// <returns><c>true</c> if [contains] [the specified instance]; otherwise, <c>false</c>.</returns>
 /// <inheritdoc />
 public bool Contains(object instance)
 {
     lock (WeakMap)
     {
         return(WeakMap.TryGetValue(instance, out var throwAway));
     }
 }
Esempio n. 2
0
        public void Constructor(RuntimeFunction ctor)
        {
            IsConstructorWLength(ctor, "WeakMap", 0, WeakMap.Prototype);
            That(GlobalThis, Has.OwnProperty("WeakMap", ctor, EcmaPropertyAttributes.Writable | EcmaPropertyAttributes.Configurable));

            It("must be called as constructor", () => {
                That(() => WeakMap.Call(This), Throws.TypeError);
                That(() => WeakMap.Call(This, EcmaArray.Of()), Throws.TypeError);
            });

            It("should return a new WeakMap object if iterable is undefined or null", () => {
                That(Object.Invoke("getPrototypeOf", WeakMap.Construct()), Is.EqualTo(WeakMap.Prototype));
                That(Object.Invoke("getPrototypeOf", WeakMap.Construct(Undefined)), Is.EqualTo(WeakMap.Prototype));
                That(Object.Invoke("getPrototypeOf", WeakMap.Construct(Null)), Is.EqualTo(WeakMap.Prototype));
            });

            It("should throw a TypeError if object is not iterable", () => {
                That(() => WeakMap.Construct(Object.Construct()), Throws.TypeError);
            });

            It("should throw a TypeError if iterable items are not Objects", () => {
                That(() => WeakMap.Construct(EcmaArray.Of(Undefined, 1)), Throws.TypeError);
                That(() => WeakMap.Construct(EcmaArray.Of(Null, 1)), Throws.TypeError);
                That(() => WeakMap.Construct(EcmaArray.Of(1, 1)), Throws.TypeError);
                That(() => WeakMap.Construct(EcmaArray.Of("", 1)), Throws.TypeError);
                That(() => WeakMap.Construct(EcmaArray.Of(true, 1)), Throws.TypeError);
                That(() => WeakMap.Construct(EcmaArray.Of(new Symbol(), 1)), Throws.TypeError);
                That(() => WeakMap.Construct(EcmaArray.Of(EcmaArray.Of("a", 1), 2)), Throws.TypeError);
            });

            It("should throw TypeError if set is not callable only when iterable is not undefined or null", () => {
                using (TempProperty(WeakMap.Prototype, "set", Null)) {
                    That(() => WeakMap.Construct(EcmaArray.Of()), Throws.TypeError);
                    That(() => WeakMap.Construct(), Throws.Nothing);
                    That(() => WeakMap.Construct(Undefined), Throws.Nothing);
                    That(() => WeakMap.Construct(Null), Throws.Nothing);
                }
            });

            It("should return abrupt from getting set mehod only when iterable is not undefined or null", () => {
                using (TempProperty(WeakMap.Prototype, "set", EcmaPropertyDescriptor.FromValue(CreateObject(("get", ThrowTest262Exception), ("configurable", true))))) {
                    That(() => WeakMap.Construct(EcmaArray.Of()), Throws.Test262);
                    That(() => WeakMap.Construct(), Throws.Nothing);
                    That(() => WeakMap.Construct(Undefined), Throws.Nothing);
                    That(() => WeakMap.Construct(Null), Throws.Nothing);
                }
            });
Esempio n. 3
0
        /// <summary>
        ///     Gets the or add.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="instance">The instance.</param>
        /// <param name="factoryMethod">The factory method.</param>
        /// <param name="setupFunction">The setup function.</param>
        /// <returns>T.</returns>
        /// <inheritdoc />
        public T GetOrAdd <T>(object instance, Func <T> factoryMethod, Action setupFunction = null)
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance), "Key cannot be null");
            }

            lock (WeakMap)
            {
                if (WeakMap.TryGetValue(instance, out var value))
                {
                    return((T)value);
                }

                var created = factoryMethod();
                WeakMap.Add(instance, created);
                setupFunction?.Invoke();
                return(created);
            }
        }
Esempio n. 4
0
        public void GettingSettingAndDeletingWorks()
        {
            // AppVeyor Chutzpah engine adjustment
            if (Bridge.ClientTest.Utilities.BrowserHelper.IsPhantomJs())
            {
                Assert.True(true, "Not running WeapMap tests as not supported in PhantomJS. See https://github.com/ariya/phantomjs/issues/13652");
                return;
            }

            var someValue = new SomeCustomClass
            {
                SomeProperty = 456
            };

            var someKey      = new SomeCustomClass();
            var someOtherKey = new SomeCustomClass();

            Assert.AreEqual(someKey, someOtherKey, "Keys sanity check"); //sanity check

            var amap = new WeakMap();

            amap.Set(someKey, someValue);

            Assert.True(amap.Has(someKey), "Has someKey");
            Assert.False(amap.Has(someOtherKey), "Does not have someOtherKey");
            var v = amap.Get(someKey);

            Assert.NotNull(v, "Get not null");
            var typedV = v as SomeCustomClass;

            Assert.NotNull(typedV, "Get not null SomeCustomClass");
            Assert.AreEqual(typedV.SomeProperty, 456, "Check SomeProperty");
            Assert.AreEqual(someValue, v, "Check references");

            Assert.True(amap.Delete(someKey), "Delete someKey");
            Assert.False(amap.Delete(someKey), "Another delete someKey");
            Assert.False(amap.Has(someKey), "Check if has deleted someKey");

            Assert.AreEqual(Script.Undefined, amap.Get(someKey), "Get deleted someKey");
        }
Esempio n. 5
0
 /// <summary>
 /// Clear all elements from Dictionary
 /// </summary>
 public void Clear()
 {
     //as WeakMap.clear() is not widely implemented it uses workaround
     _impl = new WeakMap();
 }
Esempio n. 6
0
 public WeakDictionary(IEnumerable <KeyValuePair <TKey, TValue> > items)
 {
     _impl = new WeakMap(
         items.Select(x => new object[] { x.Key, x.Value }).ToArray()
         );
 }
Esempio n. 7
0
 public WeakDictionary()
 {
     _impl = new WeakMap();
 }