public void AddEdmToClrMap_Cached_OnlyOneInstance()
        {
            // Arrange
            TypeCacheItem cache   = new TypeCacheItem();
            IEdmType      edmType = new Mock <IEdmType>().Object;

            Action cacheCallAndVerify = () =>
            {
                cache.AddEdmToClrMap(edmType, true, typeof(int?));
                cache.AddEdmToClrMap(edmType, false, typeof(int));

                KeyValuePair <IEdmType, (Type, Type)> item = Assert.Single(cache.EdmToClrTypeCache);
                Assert.Same(edmType, item.Key);
                Assert.Equal(typeof(int), item.Value.Item1);
                Assert.Equal(typeof(int?), item.Value.Item2);
            };
Exemplo n.º 2
0
        private static void CacheTypes()
        {
            var baseType = typeof(Instance);

            var asm = baseType.Assembly;

            foreach (var type in asm.GetTypes().Where(x => baseType.IsAssignableFrom(x) && !x.IsAbstract))
            {
                TypeProblem     problem = TypeProblem.NoProblem;
                Func <Instance> creator = null;

                if (type.IsAbstract)
                {
                    problem = TypeProblem.NotCreatable;
                }
                else if (typeof(Service).IsAssignableFrom(type))
                {
                    problem = TypeProblem.IsAService;
                }
                else if (type.GetConstructor(Type.EmptyTypes) == null)
                {
                    problem = TypeProblem.RequiresParameters;
                }
                else if (type.GetCustomAttributes(typeof(UncreatableAttribute), false).Any())
                {
                    problem = TypeProblem.NotCreatable;
                }

                if (problem == TypeProblem.NoProblem)
                {
                    var constructor = type.GetConstructor(Type.EmptyTypes);
                    Debug.Assert(constructor != null, "constructor != null");
                    creator =
                        Expression.Lambda <Func <Instance> >(Expression.New(constructor))
                        .Compile();
                }

                var item = new TypeCacheItem(type, creator)
                {
                    Problem = problem
                };

                _typeCache.TryAdd(type.Name, item);
            }
        }
        public void AddAndFindEdmType_Returns_CachedInstance(Type testType)
        {
            // Arrange
            IEdmTypeReference edmType = new Mock <IEdmTypeReference>().Object;
            TypeCacheItem     cache   = new TypeCacheItem();

            // Act
            bool found = cache.TryFindEdmType(testType, out _);

            Assert.False(found); // not found

            cache.AddClrToEdmMap(testType, edmType);
            found = cache.TryFindEdmType(testType, out IEdmTypeReference acutal);

            // Assert
            Assert.True(found);
            Assert.Same(edmType, acutal);
        }
        public void AddAndGetClrType_Returns_CorrectType(Type testType)
        {
            // Arrange
            IEdmType      edmType = new Mock <IEdmType>().Object;
            TypeCacheItem cache   = new TypeCacheItem();

            // Act
            bool found = cache.TryFindClrType(edmType, true, out _);

            Assert.False(found);

            cache.AddEdmToClrMap(edmType, true, testType);

            found = cache.TryFindClrType(edmType, true, out Type actualType);

            // Act & Assert
            Assert.True(found);
            Assert.Same(testType, actualType);
        }
        public void AddClrToEdmMap_Cached_OnlyOneInstance()
        {
            // Arrange
            TypeCacheItem cache = new TypeCacheItem();
            Action        cacheCallAndVerify = () =>
            {
                IEdmTypeReference edmType = new Mock <IEdmTypeReference>().Object;
                cache.AddClrToEdmMap(typeof(TypeCacheItemTests), edmType);
                Assert.Single(cache.ClrToEdmTypeCache);
            };

            // Act & Assert
            cacheCallAndVerify();

            // 5 is a magic number, it doesn't matter, just want to call it multiple times.
            for (int i = 0; i < 5; i++)
            {
                cacheCallAndVerify();
            }

            cacheCallAndVerify();
        }