Exemple #1
0
        private IFixtureBuilder[] GetFixtureBuilderAttributes(ITypeInfo?typeInfo)
        {
            IFixtureBuilder[] attrs = new IFixtureBuilder[0];

            while (typeInfo != null && !typeInfo.IsType(typeof(object)))
            {
                attrs = typeInfo.GetCustomAttributes <IFixtureBuilder>(false);

                if (attrs.Length > 0)
                {
                    // We want to eliminate duplicates that have no args.
                    // If there is just one, no duplication is possible.
                    if (attrs.Length == 1)
                    {
                        return(attrs);
                    }

                    // Count how many have arguments
                    int withArgs = 0;
                    foreach (var attr in attrs)
                    {
                        if (HasArguments(attr))
                        {
                            withArgs++;
                        }
                    }

                    // If all have args, just return them
                    if (withArgs == attrs.Length)
                    {
                        return(attrs);
                    }

                    // If none of them have args, return the first one
                    if (withArgs == 0)
                    {
                        return new IFixtureBuilder[] { attrs[0] }
                    }
                    ;

                    // Some of each - extract those with args
                    var result = new IFixtureBuilder[withArgs];
                    int count  = 0;
                    foreach (var attr in attrs)
                    {
                        if (HasArguments(attr))
                        {
                            result[count++] = attr;
                        }
                    }

                    return(result);
                }

                typeInfo = typeInfo.BaseType;
            }

            return(attrs);
        }
Exemple #2
0
 /// <summary>
 /// return;          <br/>
 ///                  <br/>
 /// OR               <br/>
 ///                  <br/>
 /// return (T) ...;
 /// </summary>
 protected internal ReturnStatementSyntax ReturnResult(ITypeInfo?returnType, ExpressionSyntax result) => ReturnStatement
 (
     expression: returnType?.IsVoid == true
         ? null
         : returnType != null
             ? CastExpression
     (
         type: CreateType(returnType),
         expression: result
     )
             : result
 );
Exemple #3
0
        public static TestCollection TestCollection(Assembly?assembly = null, ITypeInfo?definition = null, string?displayName = null)
        {
            if (assembly == null)
            {
                assembly = typeof(Mocks).GetTypeInfo().Assembly;
            }
            if (displayName == null)
            {
                displayName = "Mock test collection for " + assembly.CodeBase;
            }

            return(new TestCollection(TestAssembly(assembly), definition, displayName));
        }
Exemple #4
0
        public static ITestClass TestClass(
            ITestCollection?testCollection = null,
            ITypeInfo?classType            = null)
        {
            testCollection ??= TestCollection();
            classType ??= TypeInfo();

            var result = Substitute.For <ITestClass, InterfaceProxy <ITestClass> >();

            result.Class.Returns(classType);
            result.TestCollection.Returns(testCollection);
            return(result);
        }
Exemple #5
0
        public static ITestCollection TestCollection(
            ITestAssembly?assembly = null,
            ITypeInfo?definition   = null,
            string?displayName     = null)
        {
            assembly ??= TestAssembly();
            displayName ??= "Mock test collection";

            var result = Substitute.For <ITestCollection, InterfaceProxy <ITestCollection> >();

            result.CollectionDefinition.Returns(definition);
            result.DisplayName.Returns(displayName);
            result.TestAssembly.Returns(assembly);
            result.UniqueID.Returns(OneGuid);
            return(result);
        }
Exemple #6
0
            protected override IEnumerable <MemberDeclarationSyntax> BuildMembers(CancellationToken cancellation)
            {
                foreach (IEventInfo ifaceEvt in Context.InterfaceType.Events)
                {
                    cancellation.ThrowIfCancellationRequested();

                    IEventInfo targetEvt = GetTargetMember(ifaceEvt, Context.TargetType.Events);

                    //
                    // Ellenorizzuk h az esemeny lathato e a legeneralando szerelvenyunk szamara.
                    //

                    Visibility.Check
                    (
                        targetEvt,
                        Context.ContainingAssembly,
                        checkAdd: ifaceEvt.AddMethod is not null,
                        checkRemove: ifaceEvt.RemoveMethod is not null
                    );

                    IMethodInfo accessor = ifaceEvt.AddMethod ?? ifaceEvt.RemoveMethod !;

                    ITypeInfo?castTargetTo = accessor.AccessModifiers == AccessModifiers.Explicit
                        ? accessor.DeclaringInterfaces.Single() // explicit esemenyhez biztosan csak egy deklaralo interface tartozik
                        : null;

                    yield return(DeclareEvent
                                 (
                                     ifaceEvt,
                                     addBody: CreateBody(register: true),
                                     removeBody: CreateBody(register: false),
                                     forceInlining: true
                                 ));

                    ArrowExpressionClauseSyntax CreateBody(bool register) => ArrowExpressionClause
                    (
                        expression: RegisterEvent
                        (
                            targetEvt,
                            MemberAccess(null, TARGET),
                            register,
                            IdentifierName(Value),
                            castTargetTo
                        )
                    );
                }
            }
Exemple #7
0
        private Test(string?pathName, string name, ITypeInfo?typeInfo, IMethodInfo?method)
        {
            Guard.ArgumentNotNullOrEmpty(name, nameof(name));

            Id       = GetNextId();
            Name     = name;
            FullName = !string.IsNullOrEmpty(pathName)
                ? pathName + '.' + name
                : name;

            TypeInfo        = typeInfo;
            Method          = method;
            Properties      = new PropertyBag();
            RunState        = RunState.Runnable;
            SetUpMethods    = new MethodInfo[0];
            TearDownMethods = new MethodInfo[0];
        }
 /// <summary>
 /// target.Property           <br/>
 ///                           <br/>
 /// OR                        <br/>
 ///                           <br/>
 /// target.Propery[index]
 /// </summary>
 protected internal ExpressionSyntax PropertyAccess(IPropertyInfo property, ExpressionSyntax?target, ITypeInfo?castTargetTo = null, IEnumerable <ArgumentSyntax>?indices = null) => !property.Indices.Any()
     ? MemberAccess
 (
     target,
     property,
     castTargetTo
 )
     : (ExpressionSyntax)ElementAccessExpression
 /// <summary>
 /// target.Property           <br/>
 ///                           <br/>
 /// OR                        <br/>
 ///                           <br/>
 /// target.Propery[index]
 /// </summary>
 protected internal ExpressionSyntax PropertyAccess(IPropertyInfo property, ExpressionSyntax?target, ITypeInfo?castTargetTo = null) => PropertyAccess
 (
     property,
     target,
     castTargetTo,
     property
     .Indices
     .Select(param => Argument(IdentifierName(param.Name)))
 );
Exemple #10
0
 /// <summary>
 /// return;             <br/>
 ///                     <br/>
 /// OR                  <br/>
 ///                     <br/>
 /// return (T) result;
 /// </summary>
 protected internal ReturnStatementSyntax ReturnResult(ITypeInfo?returnType, LocalDeclarationStatementSyntax result) =>
 ReturnResult(returnType, ToIdentifierName(result));
        protected MemberAccessExpressionSyntax MethodAccess(ExpressionSyntax?target, IMethodInfo method, ITypeInfo?castTargetTo = null)
        {
            SimpleNameSyntax identifier = IdentifierName(method.Name);

            if (method is IGenericMethodInfo genericMethod)
            {
                identifier = GenericName(identifier.Identifier).WithTypeArgumentList
                             (
                    typeArgumentList: TypeArgumentList
                    (
                        arguments: genericMethod.GenericArguments.ToSyntaxList(ResolveType)
                    )
                             );
            }

            return(SimpleMemberAccess
                   (
                       AmendTarget(target, method, castTargetTo),
                       identifier
                   ));
        }
        protected InvocationExpressionSyntax InvokeMethod(IMethodInfo method, ExpressionSyntax?target, ITypeInfo?castTargetTo = null, params ArgumentSyntax[] arguments)
        {
            IReadOnlyList <IParameterInfo> paramz = method.Parameters;

            Debug.Assert(arguments.Length == paramz.Count);

            return(InvocationExpression
                   (
                       expression: MethodAccess
                       (
                           target,
                           method,
                           castTargetTo
                       )
                   )
                   .WithArgumentList
                   (
                       argumentList: ArgumentList
                       (
                           arguments.ToSyntaxList
                           (
                               (arg, i) => paramz[i].Kind switch
            {
                ParameterKind.In => arg.WithRefKindKeyword
                (
                    refKindKeyword: Token(SyntaxKind.InKeyword)
                ),
                ParameterKind.Out => arg.WithRefKindKeyword
                (
                    refKindKeyword: Token(SyntaxKind.OutKeyword)
                ),
                ParameterKind.Ref => arg.WithRefKindKeyword
                (
                    refKindKeyword: Token(SyntaxKind.RefKeyword)
                ),
                _ => arg
            }
        private ExpressionSyntax AmendTarget(ExpressionSyntax?target, IMemberInfo member, ITypeInfo?castTargetTo)
        {
            target ??= member.IsStatic ? ResolveType(member.DeclaringType) : (ExpressionSyntax)ThisExpression();

            if (castTargetTo is not null)
            {
                Debug.Assert(!member.IsStatic);

                target = ParenthesizedExpression
                         (
                    CastExpression(ResolveType(castTargetTo), target)
                         );
            }

            return(target);
        }
 /// <summary>
 /// target.Event [+|-]= ...;
 /// </summary>
 protected internal AssignmentExpressionSyntax RegisterEvent(IEventInfo @event, ExpressionSyntax?target, bool add, ExpressionSyntax right, ITypeInfo?castTargetTo = null) => AssignmentExpression
 (
     kind: add ? SyntaxKind.AddAssignmentExpression : SyntaxKind.SubtractAssignmentExpression,
     left: MemberAccess
     (
         target,
         @event,
         castTargetTo
     ),
     right: right
 );
        /// <summary>
        /// [[(Type)] target | [(Type)] this | Namespace.Type].Method[...](...)
        /// </summary>
        protected internal MemberAccessExpressionSyntax MethodAccess(ExpressionSyntax?target, IMethodInfo method, ITypeInfo?castTargetTo = null) => SimpleMemberAccess
        (
            AmendTarget(target, method, castTargetTo),

            method is not IGenericMethodInfo genericMethod
 /// <summary>
 /// [[(Type)] target | [(Type)] this | Namespace.Type].Member
 /// </summary>
 protected internal MemberAccessExpressionSyntax MemberAccess(ExpressionSyntax?target, IMemberInfo member, ITypeInfo?castTargetTo = null) => SimpleMemberAccess
 (
     AmendTarget(target, member, castTargetTo),
     member.Name
 );