Ejemplo n.º 1
0
        public void UsingdoOnMethodWithGenericReturnValue()
        {
            IGenericType <object> mock = MockRepository.Mock <IGenericType <object> >();

            mock.Expect(x => x.MyMethod())
            .DoInstead((MyDelegate) delegate { return(new object()); });
        }
Ejemplo n.º 2
0
 protected IType ResolveType(IType targetType)
 {
     if (Parent is IGenericType)
     {
         IGenericType parent = ((IGenericType)(this.Parent));
         if (parent.IsGenericConstruct)
         {
             if (!parent.IsGenericDefinition)
             {
                 if (this.IsGenericConstruct && this.GenericReplacementsImpl != null)
                 {
                     return(targetType.Disambiguify(parent.GenericParameters, this.GenericReplacementsImpl, TypeParameterSources.Both));
                 }
                 else
                 {
                     return(targetType.Disambiguify(parent.GenericParameters, null, TypeParameterSources.Type));
                 }
             }
         }
         else if (this.IsGenericConstruct && this.GenericReplacementsImpl != null)
         {
             return(targetType.Disambiguify(null, this.GenericReplacementsImpl, TypeParameterSources.Method));
         }
     }
     else if (this.IsGenericConstruct && this.GenericReplacementsImpl != null)
     {
         return(targetType.Disambiguify(null, this.GenericReplacementsImpl, TypeParameterSources.Method));
     }
     return(targetType);
 }
Ejemplo n.º 3
0
 public override bool Equals(IType other)
 {
     if (other == null)
     {
         return(false);
     }
     if (base.Equals(other))
     {
         return(true);
     }
     if (this.IsGenericConstruct && other.IsGenericConstruct)
     {
         if (!(other is IGenericType))
         {
             return(false);
         }
         IGenericType otherGeneric = (IGenericType)other;
         if (this.GenericParameters.Count != otherGeneric.GenericParameters.Count)
         {
             return(false);
         }
         if (otherGeneric.IsGenericDefinition)
         {
             return(false);
         }
         return(otherGeneric.GenericParameters.SequenceEqual(this.GenericParameters));
     }
     return(false);
 }
Ejemplo n.º 4
0
        public void UsingdoOnMethodWithGenericReturnValue()
        {
            MockRepository          mocks         = new MockRepository();
            IGenericType <object>   mock          = mocks.StrictMock <IGenericType <object> >();
            IMethodOptions <object> methodOptions = Expect.Call(mock.MyMethod());

            methodOptions.Do((MyDelegate) delegate { return(new object()); });
        }
        public void UsingdoOnMethodWithGenericReturnValue()
        {
            IGenericType <object> mock = MockRepository.Mock <IGenericType <object> >();

            mock.SetUnexpectedBehavior(UnexpectedCallBehaviors.BaseOrDefault);

            mock.Expect(x => x.MyMethod())
            .DoInstead((MyDelegate) delegate { return(new object()); });
        }
Ejemplo n.º 6
0
        private void GenericHelper <T>(T param, string expected)
        {
            dynamic tNew = new ExpandoObject();

            tNew.Funct = new Func <T, string>(it => it.ToString());
            IGenericType <T> tActsLike = Impromptu.ActLike <IGenericType <T> >(tNew);

            Assert.AreEqual(expected, tActsLike.Funct(param));
        }
Ejemplo n.º 7
0
 public void RegisterGenericClosure(IGenericType targetType, ILockedTypeCollection typeParameters)
 {
     lock (this.SyncObject)
     {
         if (this.genericCache == null)
         {
             this.genericCache = new GenericTypeCache();
         }
         this.genericCache.RegisterGenericType(targetType, typeParameters);
     }
 }
Ejemplo n.º 8
0
 public bool TryObtainGenericClosure(ILockedTypeCollection typeParameters, out IGenericType genericClosure)
 {
     lock (this.SyncObject)
     {
         if (this.genericCache == null)
         {
             genericClosure = null;
             return(false);
         }
         return(this.genericCache.TryObtainGenericClosure(typeParameters, out genericClosure));
     }
 }
Ejemplo n.º 9
0
        /// <summary>
        /// Applies the rule.
        /// </summary>
        /// <param name="node">The node instance to modify.</param>
        /// <param name="data">Private data from CheckConsistency().</param>
        public override void Apply(IAssignmentTypeArgument node, object data)
        {
            IIdentifier ParameterIdentifier = (IIdentifier)node.ParameterIdentifier;

            Debug.Assert(ParameterIdentifier.ValidText.IsAssigned);
            string ValidText = ParameterIdentifier.ValidText.Item;

            IGenericType ParentGenericType = (IGenericType)node.ParentSource;
            IObjectType  TypeToResolve     = (IObjectType)node.Source;

            node.ResolvedSourceTypeName.Item = TypeToResolve.ResolvedTypeName.Item;
            node.ResolvedSourceType.Item     = TypeToResolve.ResolvedType.Item;
            ParentGenericType.ArgumentIdentifierTable.Add(ValidText, ParameterIdentifier);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Given generic type and list of arguments in the expression like
        /// Mapping[T1, int, ...] or Mapping[str, int] where Mapping inherits from Generic[K,T] creates generic class base
        /// (if the former) on specific type (if the latter).
        /// </summary>
        private IMember CreateSpecificTypeFromIndex(IGenericType gt, IReadOnlyList <IMember> args, Expression expr)
        {
            // TODO: move check to GenericClassBase. This requires extensive changes to SpecificTypeConstructor.
            if (gt.Name.EqualsOrdinal("Generic"))
            {
                var genericTypeArgs = args.OfType <IGenericTypeParameter>().ToArray();
                if (!GenericClassParameterValid(genericTypeArgs, args, expr))
                {
                    return(UnknownType);
                }
                // Generic[T1, T2, ...] expression. Create generic base for the class.
                return(new GenericClassBase(genericTypeArgs, Module.Interpreter));
            }

            // For other types just use supplied arguments
            return(args.Count > 0 ? gt.CreateSpecificType(new ArgumentSet(args, expr, this)) : UnknownType);
        }
Ejemplo n.º 11
0
        private ITypeParent OnGetParentImpl()
        {
            ITypeParent declParent = this.Original.Parent;

            if (declParent == null)
            {
                return(null);
            }
            else
            {
                if (this.Original.IsGenericConstruct)
                {
                    if (declParent is IGenericType)
                    {
                        IGenericType genericParent = ((IGenericType)(declParent));
                        if (genericParent.IsGenericConstruct)
                        {
                            if (!genericParent.IsGenericDefinition)
                            {
                                genericParent = (IGenericType)genericParent.ElementType;
                            }
                            return((ITypeParent)genericParent.MakeGenericClosure(this.GenericParameters.Take(genericParent.GenericParameters.Count).ToCollection()));
                        }
                        else
                        {
                            return((ITypeParent)genericParent);
                        }
                    }
                    else if (declParent is IMethodMember)
                    {
                        IMethodMember genericParent = ((IMethodMember)(declParent));
                        if (genericParent.IsGenericConstruct)
                        {
                            if (!genericParent.IsGenericDefinition)
                            {
                                genericParent = (IMethodMember)genericParent.GetGenericDefinition();
                            }
                            return((ITypeParent)genericParent.MakeGenericClosure(this.GenericParameters.Take(genericParent.GenericParameters.Count).ToCollection()));
                        }
                    }
                }
                return(declParent);
            }
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Checks for errors before applying a rule.
        /// </summary>
        /// <param name="node">The node instance to check.</param>
        /// <param name="dataList">Optional data collected during inspection of sources.</param>
        /// <param name="data">Private data to give to Apply() upon return.</param>
        /// <returns>True if an error occured.</returns>
        public override bool CheckConsistency(IAssignmentTypeArgument node, IDictionary <ISourceTemplate, object> dataList, out object data)
        {
            bool Success = true;

            data = null;

            IIdentifier ParameterIdentifier = (IIdentifier)node.ParameterIdentifier;

            Debug.Assert(ParameterIdentifier.ValidText.IsAssigned);
            string ValidText = ParameterIdentifier.ValidText.Item;

            IGenericType ParentGenericType = (IGenericType)node.ParentSource;

            if (ParentGenericType.ArgumentIdentifierTable.ContainsKey(ValidText))
            {
                AddSourceError(new ErrorIdentifierAlreadyListed(ParameterIdentifier, ValidText));
                Success = false;
            }

            return(Success);
        }
        private async Task <IMember> CreateSpecificFromGenericAsync(IGenericType gen, IndexExpression expr, CancellationToken cancellationToken = default)
        {
            var args = new List <IPythonType>();

            if (expr.Index is TupleExpression tex)
            {
                foreach (var item in tex.Items)
                {
                    var e = await GetValueFromExpressionAsync(item, cancellationToken);

                    args.Add(e?.GetPythonType() ?? UnknownType);
                }
            }
            else
            {
                var index = await GetValueFromExpressionAsync(expr.Index, cancellationToken);

                args.Add(index?.GetPythonType() ?? UnknownType);
            }
            return(gen.CreateSpecificType(args, Module, GetLoc(expr)));
        }
        /// <summary>
        /// Given generic type and list of arguments in the expression like
        /// Mapping[T1, int, ...] or Mapping[str, int] where Mapping inherits from Generic[K,T] creates generic class base
        /// (if the former) on specific type (if the latter).
        /// </summary>
        private IMember CreateSpecificTypeFromIndex(IGenericType gt, IReadOnlyList <IMember> args, Expression expr)
        {
            if (gt.Name.EqualsOrdinal("Generic"))
            {
                var genericTypeArgs = args.OfType <IGenericTypeParameter>().ToArray();
                if (!GenericClassParameterValid(genericTypeArgs, args, expr))
                {
                    return(UnknownType);
                }

                // Generic[T1, T2, ...] expression. Create generic base for the class.
                return(new GenericClassParameter(genericTypeArgs, Module));
            }

            // For other types just use supplied arguments
            if (args.Count > 0)
            {
                return(gt.CreateSpecificType(new ArgumentSet(args, expr, this)));
            }

            return(UnknownType);
        }
        /// <summary>
        /// Given generic type and list of indices in the expression like
        /// Generic[T1, T2, ...] or List[str] creates generic class base
        /// (if the former) on specific type (if the latter).
        /// </summary>
        private IMember CreateSpecificTypeFromIndex(IGenericType gt, IReadOnlyList <IMember> indices, Expression expr)
        {
            // See which ones are generic parameters as defined by TypeVar()
            // and which are specific types. Normally there should not be a mix.
            var genericTypeArgs = indices.OfType <IGenericTypeDefinition>().ToArray();
            var specificTypes   = indices.Where(i => !(i is IGenericTypeDefinition)).OfType <IPythonType>().ToArray();

            if (genericTypeArgs.Length > 0 && genericTypeArgs.Length != indices.Count)
            {
                // TODO: report that some type arguments are not declared with TypeVar.
            }
            if (specificTypes.Length > 0 && specificTypes.Length != indices.Count)
            {
                // TODO: report that arguments are not specific types or are not declared.
            }

            if (gt.Name.EqualsOrdinal("Generic"))
            {
                // Generic[T1, T2, ...] expression. Create generic base for the class.
                if (genericTypeArgs.Length > 0)
                {
                    return(new GenericClassParameter(genericTypeArgs, Module));
                }
                else
                {
                    // TODO: report too few type arguments for Generic[].
                    return(UnknownType);
                }
            }

            // For other types just use supplied arguments
            if (indices.Count > 0)
            {
                return(gt.CreateSpecificType(new ArgumentSet(indices)));
            }
            // TODO: report too few type arguments for the generic expression.
            return(UnknownType);
        }
Ejemplo n.º 16
0
 private ITypeParent OnGetParentImpl()
 {
     ITypeParent declType = this.Original.Parent;
     if (declType == null)
         return null;
     else
     {
         if (this.Original.IsGenericConstruct && declType is IGenericType)
         {
             IGenericType genericParent = ((IGenericType)(declType));
             if (genericParent.IsGenericConstruct)
             {
                 if (!genericParent.IsGenericDefinition)
                     genericParent = (IGenericType)genericParent.ElementType;
                 return (ITypeParent)genericParent.MakeGenericClosure(this.GenericParameters.Take(genericParent.GenericParameters.Count).ToCollection());
             }
             else
                 return (ITypeParent)genericParent;
         }
         else
             return declType;
     }
 }
Ejemplo n.º 17
0
 private static void Method(IGenericType <B> gen)
 {
 }
Ejemplo n.º 18
0
 public NestedGeneric(IGenericType <TestType> genericType)
 {
     GenericType = genericType;
 }
Ejemplo n.º 19
0
 internal _ClassTypesBase(_FullTypesBase master, IClassTypeDictionary originalSet, IGenericType parent)
     : base(master, originalSet, parent)
 {
 }
Ejemplo n.º 20
0
        public async Task TestCases(IGenericType <ClassWithNewCtor, string> remote)
        {
            var local = new GenericTypeImpl <ClassWithNewCtor, string>();

            {
                const string expected = "first";
                string       actual   = remote.First(new[] { expected, "other" });
                Assert.Equal(expected, actual);
            }

            await Assert.ThrowsAsync <TestException>(async() => await remote.TestAsync());

            {
                ClassWithNewCtor.InitialName = Guid.NewGuid().ToString();
                var actual = await remote.TestAsync(string.Empty);

                Assert.Equal(ClassWithNewCtor.InitialName, actual.Name);
            }

            {
                string expected = local.TestTypeOf <string, BuildInType>();
                string actual   = remote.TestTypeOf <string, BuildInType>();

                Assert.Equal(expected, actual);
            }

            {
                var expected = new ClassWithNewCtor
                {
                    Id   = Guid.NewGuid().ToString(),
                    Name = Guid.NewGuid().ToString()
                };
                var actual = remote.TestGenericConstraint(expected);

                Assert.Equal(expected.Id, actual.Id);
                Assert.Equal(expected.Name, actual.Name);
            }

            {
                var expected = new ClassWithNewCtor
                {
                    Id   = Guid.NewGuid().ToString(),
                    Name = Guid.NewGuid().ToString()
                };
                var actual = remote.ReturnOriginalValue(expected);

                Assert.Equal(expected.Id, actual.Id);
                Assert.Equal(expected.Name, actual.Name);
            }

            {
                var actual = remote.Flatten(new List <List <List <string> > >
                {
                    new()
                    {
                        new() { "1", "2", "3", },
                        new() { "4", "5", "6", },
                    },
                    new()
                    {
                        new() { "7", "8", "9", },
                        new() { "10", "11", "12", },
                    },
                });
Ejemplo n.º 21
0
 internal _DelegateTypesBase(_FullTypesBase master, IDelegateTypeDictionary originalSet, IGenericType parent)
     : base(master, originalSet, parent)
 {
 }
Ejemplo n.º 22
0
 public static Foo <T> CreateInstance(IGenericType <T> instance)
 {
     return(new Foo <T>(instance));
 }
Ejemplo n.º 23
0
 public _Types(_FullTypesBase master, TTypes originalSet, IGenericType parent)
     : base(master, originalSet, parent)
 {
 }
 /// <summary>
 /// Determines if this <see cref="GenericTypeWrapper"/> object equals the given <see cref="IGenericType"/> object.
 /// </summary>
 /// <param name="other">The <see cref="IGenericType"/> object to compare with this object.</param>
 /// <returns>
 /// Returns true if this object object is equal to the other object, otherwise false.
 /// </returns>
 public bool Equals(IGenericType other)
 {
     return other != null &&
         this.Name.Equals(other.Name) &&
         this.Access == other.Access &&
         this.IsAbstract == other.IsAbstract &&
         this.IsClass == other.IsClass &&
         this.IsStruct == other.IsStruct &&
         this.IsInterface == other.IsInterface &&
         this.TypeParameters.SequenceEqual(other.TypeParameters);
 }
Ejemplo n.º 25
0
 internal _InterfaceTypesBase(_FullTypesBase master, IInterfaceTypeDictionary originalSet, IGenericType parent)
     : base(master, originalSet, parent)
 {
 }