Exemple #1
0
 private static bool IsPropertyType(
     Node child,
     ClassMetadata classMetadata
     )
 {
     return((child.Kind == SyntaxKind.PropertyDeclaration || child.Kind == SyntaxKind.PropertySignature) &&
            TypeIdentifier
            .Identify(
                child.Last,
                classMetadata
                ) != GenerationIdentifiedTypes.Action &&
            !IsClassBasedMethodRule.Check(child.Last));
 }
Exemple #2
0
        public void ShouldApplyRuleAndReturnFalseWitnCountZero()
        {
            // Given
            var products = new List <Product> {
                new Butter
                {
                    Price       = 0.80,
                    ProductType = ProductType.BUTTER
                }
            };

            // When
            var result = _buy2ButterGet1BreadHalfPriceRule.Check(products);

            // Then
            result.Item1.Should().BeFalse();
            result.Item2.Should().IsSameOrEqualTo(0);
        }
        public void ShouldApplyRuleAndReturnFalseWitnCountZero()
        {
            // Given
            var products = new List <Product>
            {
                new Milk
                {
                    Price       = 1.15,
                    ProductType = ProductType.MILK
                }
            };

            // When
            var result = _buy3MilkGet1FreeTestsRule.Check(products);

            // Then
            result.Item1.Should().BeFalse();
            result.Item2.Should().IsSameOrEqualTo(0);
        }
Exemple #4
0
        internal static bool Identify(
            Node parameter,
            ClassMetadata classMetadata,
            AbstractSyntaxTree ast,
            TypeOverrideDetails typeOverrideDetails,
            out TypeStatement type
            )
        {
            type = new TypeStatement
            {
                Name = GenerationIdentifiedTypes.Unknown,
            };
            if (IsUnionTypeRule.Check(parameter))
            {
                var unionNode = parameter.OfKind(
                    SyntaxKind.UnionType
                    ).FirstOrDefault()
                                .Children.Where(
                    // Make sure does not contain undefined
                    a => a.Kind != SyntaxKind.UndefinedKeyword &&
                    a.Kind != SyntaxKind.NullKeyword &&
                    a.Kind != SyntaxKind.TypeLiteral &&
                    a.Kind != SyntaxKind.LiteralType
                    ).ToList().FirstOrDefault();

                if (unionNode != null)
                {
                    type = GenericTypeIdentifier.Identify(
                        unionNode,
                        classMetadata,
                        ast,
                        typeOverrideDetails
                        );
                }

                return(true);
            }
            else if (parameter.Kind == SyntaxKind.UnionType)
            {
                type = GenericTypeIdentifier.Identify(
                    parameter.First,
                    classMetadata,
                    ast,
                    typeOverrideDetails
                    );
                return(true);
            }
            return(false);
        }
        public static TypeStatement Identify(
            Node node,
            ClassMetadata classMetadata,
            TypeScriptAST ast,
            TypeOverrideDetails typeOverrideDetails
            )
        {
            // Observer<SomeTypeData>
            //var template = "[[NAME]]<[[CHILDREN_TYPES]]>";
            var typeIdentifier = TypeIdentifier.Identify(
                node,
                classMetadata
                );
            var isLiteral = IsTypeLiteralRule.Check(
                node
                ) || TypeLiteralIdentifier.Identify(
                typeIdentifier
                );
            var genericTypes = new List <TypeStatement>();

            if (node is TypeReferenceNode referenceNode &&
                referenceNode.TypeArguments != null &&
                referenceNode.TypeArguments.Any())
            {
                foreach (var typeArgument in referenceNode.TypeArguments)
                {
                    if (typeArgument is Node typeArgumentNode)
                    {
                        genericTypes.Add(
                            Identify(
                                typeArgumentNode,
                                classMetadata,
                                ast,
                                typeOverrideDetails
                                )
                            );
                    }
                }
            }
Exemple #6
0
 public bool Available(ShoppingBasket shoppingBasket)
 {
     return(_rule == null || _rule.Check(shoppingBasket));
 }
Exemple #7
0
        public static IList <AccessorStatement> FlattenAccessorStatements(
            this IEnumerable <Node> nodes,
            AbstractSyntaxTree ast,
            ClassMetadata classMetadata,
            IDictionary <string, string> typeOverrideMap
            )
        {
            var flattenedAccessorList = nodes.Where(a => IsGetterRule.Check(a)).Select(
                accessor =>
            {
                var name                = accessor.IdentifierStr;
                var isStatic            = IsStaticRule.Check(accessor);
                var typeOverrideDetails = new TypeOverrideDetails
                {
                    IsStatic        = isStatic,
                    TypeOverrideMap = typeOverrideMap,
                };
                var type = GenericTypeIdentifier.Identify(
                    accessor.Last,
                    classMetadata,
                    ast,
                    typeOverrideDetails
                    );
                if (TypeOverrideIdentifier.Identify(
                        TypeOverrideDeclarationIdentifier.Identify(
                            classMetadata,
                            isStatic,
                            name
                            ),
                        typeOverrideMap,
                        type,
                        out var overrideType
                        ))
                {
                    type = overrideType;
                }
                return(new AccessorStatement
                {
                    Name = name,
                    Type = NormalizeLiteralTypeStatement(
                        type,
                        classMetadata,
                        ast,
                        typeOverrideDetails
                        ),
                    IsStatic = isStatic,
                    IsInterfaceResponse = InterfaceResponseTypeIdentifier.Identify(
                        type,
                        ast
                        ),
                    HasSetter = IsSetterRule.Check(accessor),
                    //IsArrayResponse = IsArrayResposneTypeRule.Check(accessor),
                    UsedClassNames = UsedClassNamesIdentifier.Identify(type),
                });
            }
                ).ToList();

            // Loop through Setters and on the setter flat as HasSetter
            foreach (var node in nodes.Where(a => IsSetterRule.Check(a)))
            {
                var getNode = flattenedAccessorList.FirstOrDefault(
                    a => a.Name == node.IdentifierStr
                    );
                if (getNode != null)
                {
                    getNode.HasSetter = true;
                }
            }
            return(flattenedAccessorList);
        }
Exemple #8
0
        public static ClassStatement Generate(
            string projectAssembly,
            string classIdentifier,
            AbstractSyntaxTree ast,
            IDictionary <string, string> typeOverrideMap
            )
        {
            var(found, className, toGenerateNode) = GetNode(
                classIdentifier,
                ast
                );
            if (!found)
            {
                return(null);
            }

            var namespaceIdentifier = string.Join(".", GetNamespace(toGenerateNode));
            var classMetadata       = new ClassMetadata
            {
                Namespace = namespaceIdentifier,
                Name      = className,
            };


            var typeOverrideDetails = new TypeOverrideDetails
            {
                IsStatic        = false,
                TypeOverrideMap = typeOverrideMap,
            };
            // Get ExtendedClassNames
            var extendedClassType = ExtendedClassTypesIdentifier.Identify(
                toGenerateNode,
                ast,
                classMetadata,
                typeOverrideDetails
                );
            // Get ImplementedInterfaces
            var implementedInterfaces = ImplementedInterfacesIdentifier.Identify(
                toGenerateNode,
                ast,
                classMetadata,
                typeOverrideDetails
                );

            // Public Properties
            var publicProperties = toGenerateNode
                                   .Children.Where(
                child => IsNotPrivate(child) && IsPropertyType(child, classMetadata)
                );

            // Public Methods/Functions
            var publicMethods = toGenerateNode
                                .Children.Where(
                child => IsNotPrivate(child) && IsMethodType(child, classMetadata)
                );

            // Get/Set Accessors
            var accessorMethods = toGenerateNode
                                  .Children.Where(
                child => IsNotPrivate(child) && IsAccessorType(child)
                );

            // Is Observer Method/Function
            var observalbleMethods = publicProperties.Where(
                a => IsObservablePropertyRule.Check(a)
                ).ToList();

            var classStatement = new ClassStatement
            {
                ProjectAssembly = projectAssembly,
                Namespace       = namespaceIdentifier,
                Name            = DotNetClassNormalizer.Normalize(
                    className
                    ),
                IsInterface = IsInterfaceRule.Check(
                    toGenerateNode
                    ),
                GenericTypes = GetGenericTypes(
                    toGenerateNode,
                    classMetadata,
                    ast,
                    new TypeOverrideDetails
                {
                    IsStatic        = false,
                    TypeOverrideMap = typeOverrideMap,
                }
                    ),
                ExtendedType          = extendedClassType,
                ImplementedInterfaces = implementedInterfaces,
                ConstructorStatement  = new ConstructorStatement
                {
                    Arguments = ConstructorArgumentIdentifier.Identify(
                        toGenerateNode,
                        classMetadata,
                        ast,
                        new TypeOverrideDetails
                    {
                        IsStatic        = false,
                        TypeOverrideMap = typeOverrideMap,
                    }
                        ),
                },
                PublicPropertyStatements = publicProperties.ToList().Select(
                    a =>
                {
                    var name                = a.IdentifierStr;
                    var isStatic            = IsStaticRule.Check(a);
                    var typeOverrideDetails = new TypeOverrideDetails
                    {
                        IsStatic        = isStatic,
                        TypeOverrideMap = typeOverrideMap,
                    };
                    var type = GenericTypeIdentifier.Identify(
                        a.Last,
                        classMetadata,
                        ast,
                        typeOverrideDetails
                        );
                    if (TypeOverrideIdentifier.Identify(
                            TypeOverrideDeclarationIdentifier.Identify(
                                classMetadata,
                                typeOverrideDetails.IsStatic,
                                name
                                ),
                            typeOverrideDetails.TypeOverrideMap,
                            type,
                            out var overrideType
                            ))
                    {
                        type = overrideType;
                    }
                    return(new PublicPropertyStatement
                    {
                        Name = name,
                        Type = NormalizeLiteralTypeStatement(
                            type,
                            classMetadata,
                            ast,
                            typeOverrideDetails
                            ),
                        IsStatic = isStatic,
                        IsInterfaceResponse = InterfaceResponseTypeIdentifier.Identify(
                            type,
                            ast
                            ),
                        //IsArrayResponse = IsArrayResposneTypeRule.Check(a),
                        IsReadonly = IsReadonlyRule.Check(a),
                        UsedClassNames = UsedClassNamesIdentifier.Identify(type),
                    });
                }
                    ).ToList(),
                PublicMethodStatements = publicMethods.ToList().Select(
                    a =>
                {
                    var name                = a.IdentifierStr;
                    var isStatic            = IsStaticRule.Check(a);
                    var typeOverrideDetails = new TypeOverrideDetails
                    {
                        IsStatic        = isStatic,
                        TypeOverrideMap = typeOverrideMap,
                    };
                    var type = GenericTypeIdentifier.Identify(
                        a.Last,
                        classMetadata,
                        ast,
                        typeOverrideDetails
                        );
                    if (TypeOverrideIdentifier.Identify(
                            TypeOverrideDeclarationIdentifier.Identify(
                                classMetadata,
                                typeOverrideDetails.IsStatic,
                                name
                                ),
                            typeOverrideDetails.TypeOverrideMap,
                            type,
                            out var overrideType
                            ))
                    {
                        type = overrideType;
                    }
                    return(new PublicMethodStatement
                    {
                        Name = name,
                        Type = NormalizeLiteralTypeStatement(
                            type,
                            classMetadata,
                            ast,
                            typeOverrideDetails
                            ),
                        GenericTypes = DeclarationGenericTypesIdentifier.Identify(
                            a
                            ),
                        Arguments = ArgumentIdentifier.Identify(
                            a,
                            classMetadata,
                            ast,
                            typeOverrideDetails
                            ),
                        IsStatic = isStatic,
                        IsInterfaceResponse = InterfaceResponseTypeIdentifier.Identify(
                            type,
                            ast
                            ),
                        UsedClassNames = UsedClassNamesIdentifier.Identify(type),
                    });
                }
                    ).Distinct().ToList(),
                AccessorStatements = accessorMethods.FlattenAccessorStatements(
                    ast,
                    classMetadata,
                    typeOverrideMap
                    ),
            };

            classStatement.ConstructorStatement.NeedsInvokableReference = InvokableReferenceIdentifier.Identify(
                classStatement
                );

            return(classStatement);
        }
Exemple #9
0
 public static Rule CompositeTwoRules(IRule rule1, IRule rule2)
 {
     return(new Rule(new Func <ShoppingBasket, bool>((ShoppingBasket basket) => rule1.Check(basket) ? rule2.Check(basket) : true)));
 }
Exemple #10
0
 public static Rule OrTwoRules(IRule rule1, IRule rule2)
 {
     return(new Rule(new Func <ShoppingBasket, bool>((ShoppingBasket basket) => rule1.Check(basket) || rule2.Check(basket))));
 }
Exemple #11
0
 public Rule OrRules(IRule additionalRule)
 {
     return(new Rule(new Func <ShoppingBasket, bool>((ShoppingBasket basket) => Check(basket) && additionalRule.Check(basket))));
 }
Exemple #12
0
        internal static IList <ArgumentStatement> Identify(
            Node node,
            ClassMetadata classMetadata,
            TypeScriptAST ast,
            TypeOverrideDetails typeOverrideDetails
            )
        {
            var methodTypeParameters = TypeParameterIdentifier.Identify(node);
            var parameters           = node.Children
                                       .Where(childNode => childNode.Kind == SyntaxKind.Parameter)
                                       .Cast <ParameterDeclaration>()
                                       .Select(parameter =>
            {
                var type = GenericTypeIdentifier.Identify(
                    parameter.Last,
                    classMetadata,
                    ast,
                    typeOverrideDetails
                    );
                if (UnionTypeIdentifier.Identify(
                        parameter,
                        classMetadata,
                        ast,
                        typeOverrideDetails,
                        out var unionType
                        ))
                {
                    type = unionType;
                }
                if (type.Name == GenerationIdentifiedTypes.CachedEntity)
                {
                    type.Name = GenerationIdentifiedTypes.Object;
                }
                if (type.Name == GenerationIdentifiedTypes.Action)
                {
                    type.GenericTypes = type.Arguments.Select(a => a.Type);
                }

                var name = parameter.IdentifierStr;
                var methodOrConstructorName = node.IdentifierStr;
                if (node.Kind == SyntaxKind.Constructor)
                {
                    methodOrConstructorName = Constants.TYPESCRIPT_CONSTRUCTOR_NAME;
                }
                if (TypeOverrideIdentifier.Identify(
                        TypeOverrideDeclarationIdentifier.IdentifyArgument(
                            classMetadata,
                            typeOverrideDetails.IsStatic,
                            methodOrConstructorName,
                            name
                            ),
                        typeOverrideDetails.TypeOverrideMap,
                        type,
                        out var overrideType
                        ))
                {
                    type = overrideType;
                }

                return(new ArgumentStatement
                {
                    Name = parameter.IdentifierStr,
                    Type = type,
                    IsOptional = IsOptionalRule.Check(
                        parameter
                        ),
                    UsedClassNames = UsedClassNamesIdentifier.Identify(
                        type
                        ),
                });
            }).ToList();

            return(parameters);
        }