Exemplo n.º 1
0
        private static ClassDeclarationSyntax CreateClass(
            ClassDeclarationSyntax parentClass,
            ConstructorDeclarationSyntax constructor)
        {
            ObjectCreationExpressionSyntax CreateMoq(TypeSyntax moqType) =>
            EGH.CreateObject(
                moqType,
                SF.Argument(
                    EGH.MemberAccess(
                        GH.Identifier("MockBehavior"),
                        GH.Identifier("Strict"))));

            var mi = new MethodInspector(constructor);

            var recordBuilder = new RecordBuilder($"{mi.Name}Test")
                                .AddModifiers(Modifiers.Public);

            foreach (var p in mi.Parameters)
            {
                var mockedType = GetMockedType(p.Type);
                var moqType    = GH.GenericName("Mock", mockedType);
                recordBuilder.AddField(moqType, MockName(p.Name), CreateMoq(moqType));
            }

            recordBuilder.AddMethod(GenerateCreateSut(parentClass, mi));

            return(recordBuilder.Build());
        }
Exemplo n.º 2
0
        private static MethodDeclarationSyntax GenerateCreateSut(
            ClassDeclarationSyntax classDeclaration,
            MethodInspector constructorInspector)
        {
            var methodBuilder = new MethodBuilder(GH.IdentifierToken("CreateSut"))
                                .Modifiers(Modifiers.Private);

            var constructorParameters = constructorInspector
                                        .Parameters
                                        .Select(p => SF.Argument(
                                                    EGH.MemberAccess(
                                                        EGH.ThisMemberAccess(
                                                            GH.Identifier(MockName(p.Name))
                                                            ),
                                                        GH.Identifier("Object")
                                                        )
                                                    )
                                                );

            methodBuilder.ArrowBody(
                EGH.Arrow(
                    EGH.CreateObject(
                        SF.IdentifierName(classDeclaration.Identifier),
                        constructorParameters.ToArray())));

            return(methodBuilder.Build());
        }