示例#1
0
        public override string GetClassName(CodeNames codeNames)
        {
            PolicyScopeRunnerGenerator policyScopeRunnerGenerator =
                new PolicyScopeRunnerGenerator();

            return(policyScopeRunnerGenerator.GetClassName(codeNames) + "Mock");
        }
示例#2
0
        public string CreateClassCode(CodeNames codeNames)
        {
            PolicyScopeResultSelectorGenerator policyScopeResultSelectorGenerator =
                new PolicyScopeResultSelectorGenerator();

            StringBuilder sb = new StringBuilder();

            sb.Append(
                $@"    internal class {GetClassNameAndTemplateParamaters(codeNames)} : {policyScopeResultSelectorGenerator.GetInterfaceNameAndTemplateParamaters(codeNames)}
    {{
        internal {GetClassName(codeNames)}(IEnumerable<PolicyScopeMockConfiguration> configurations)
        {{
            Configurations = configurations;
        }}

        internal IEnumerable<PolicyScopeMockConfiguration> Configurations {{ get; }} 

");
            CodeNames codeNameWithResult = new CodeNames(codeNames.ServiceCount, true, codeNames.IsAsync);

            PolicyScopeRunnerGenerator     policyRunnerGenerator          = new PolicyScopeRunnerGenerator();
            PolicyScopeRunnerMockGenerator policyScopeRunnerMockGenerator = new PolicyScopeRunnerMockGenerator();

            sb.AppendLine(
                $@"        public {policyRunnerGenerator.GetInterfaceNameAndTemplateParamaters(codeNames)} {PolicyScopeResultSelectorGenerator.WithNoResultName}()
        {{
            var config = Configurations.FirstOrDefault(conf => conf.ReturnType == null);

            if(config == null)
            {{
                throw new PolicyScopeMockException($""No matching policy scope mock configuration was found. Found no configuration with no result type."", Configurations);
            }}

            return new {policyScopeRunnerMockGenerator.GetClassNameAndTemplateParamaters(codeNames)}(config);
        }}

        public {policyRunnerGenerator.GetInterfaceNameAndTemplateParamaters(codeNameWithResult)} {PolicyScopeResultSelectorGenerator.WithResultName}<{CodeNames.TResultName}>()
        {{
            var config = Configurations.FirstOrDefault(conf => conf.ReturnType == typeof({CodeNames.TResultName}));

            if (config == null)
            {{
                throw new PolicyScopeMockException($""No matching policy scope mock configuration was found. Found no configuration with result type {{typeof(TResult)}}."", Configurations);
            }}

            return new {policyScopeRunnerMockGenerator.GetClassNameAndTemplateParamaters(codeNameWithResult)}(config);
        }}");

            sb.AppendLine(
                $@"    }}");

            return(sb.ToString());
        }
示例#3
0
        public string CreateClassCode(CodeNames codeNames)
        {
            PolicyScopeRunnerGenerator policyScopeRunnerGenerator =
                new PolicyScopeRunnerGenerator();

            StringBuilder sb = new StringBuilder();

            sb.Append(
                $@"    internal class {GetClassNameAndTemplateParamaters(codeNames)} : {policyScopeRunnerGenerator.GetInterfaceNameAndTemplateParamaters(codeNames)}
    {{
        internal {GetClassName(codeNames)}(PolicyScopeMockConfiguration policyScopeMockConfiguration)
        {{
            PolicyScopeMockConfiguration = policyScopeMockConfiguration;
        }}

        internal PolicyScopeMockConfiguration PolicyScopeMockConfiguration {{ get; }}

");
            string retValueGrab = string.Empty;

            if (codeNames.HasReturnType)
            {
                retValueGrab = CodeNames.TResultName + " retValue = ";
            }

            if (codeNames.IsAsync)
            {
                sb.AppendLine(
                    $@"        public async Task{(codeNames.HasReturnType ? $"<{CodeNames.TResultName}>" : "")} {PolicyScopeRunnerGenerator.GetRunName(true)}({codeNames.GetFunctionDeclaration()} func)
        {{");
            }
            else
            {
                sb.AppendLine(
                    $@"        public {(codeNames.HasReturnType ? $"{CodeNames.TResultName}" : "void")} {PolicyScopeRunnerGenerator.GetRunName(false)}({codeNames.GetFunctionDeclaration()} func)
        {{");
            }

            if (codeNames.HasServices)
            {
                for (int i = 0; i < codeNames.ServiceCount; i++)
                {
                    sb.AppendLine(
                        $@"            var {codeNames.ServicesNames[i]} = ({codeNames.ServicesTypes[i]}) PolicyScopeMockConfiguration.ServiceInstances[{i}];");
                }

                sb.AppendLine();
            }

            if (codeNames.IsAsync)
            {
                sb.AppendLine(
                    $@"            {retValueGrab}await func.Invoke({string.Join(", ", codeNames.ServicesNames)}).ConfigureAwait(false);");
            }
            else
            {
                sb.AppendLine(
                    $@"            {retValueGrab} func.Invoke({string.Join(", ", codeNames.ServicesNames)});");
            }

            sb.AppendLine(
                $@"
            PolicyScopeMockConfiguration.ExecutionCount++;");

            if (codeNames.HasReturnType)
            {
                sb.AppendLine(
                    $@"
            return retValue;");
            }

            sb.AppendLine(
                $@"        }}
    }}");

            return(sb.ToString());
        }
示例#4
0
        static int Main(string[] args)
        {
            int maxServiceCount = 4;

            if (args.Length >= 1)
            {
                Common.CodeGeneratorBase.BaseDirectory = args.Last();
                Console.WriteLine("Using base directory: " + Common.CodeGeneratorBase.BaseDirectory);
            }

            int fileUpdateCount = 0;

            Console.WriteLine("Autogenerating code for main library...");

            PolicyScopeResultSelectorGenerator servicePolicyScopeBuilderGenerator
                = new PolicyScopeResultSelectorGenerator();

            fileUpdateCount += servicePolicyScopeBuilderGenerator.WriteAllFiles(maxServiceCount);

            PolicyScopeRunnerGenerator policyRunnerGenerator
                = new PolicyScopeRunnerGenerator();

            fileUpdateCount += policyRunnerGenerator.WriteAllFiles(maxServiceCount);

            PolicyScopeServiceSelectorGenerator policyScopeServiceSelectorGenerator =
                new PolicyScopeServiceSelectorGenerator();

            fileUpdateCount += policyScopeServiceSelectorGenerator.WriteAllFiles(maxServiceCount);

            Console.WriteLine();
            Console.WriteLine("Autogenerating code for mock library...");

            PolicyScopeResultSelectorMockBuilderGenerator policyScopeResultSelectorMockBuilderGenerator =
                new PolicyScopeResultSelectorMockBuilderGenerator();

            fileUpdateCount += policyScopeResultSelectorMockBuilderGenerator.WriteAllFiles(maxServiceCount);

            PolicyScopeResultSelectorMockGenerator policyScopeResultSelectorMockGenerator =
                new PolicyScopeResultSelectorMockGenerator();

            fileUpdateCount += policyScopeResultSelectorMockGenerator.WriteAllFiles(maxServiceCount);

            PolicyScopeRunnerMockGenerator policyScopeRunnerMockGenerator
                = new PolicyScopeRunnerMockGenerator();

            fileUpdateCount += policyScopeRunnerMockGenerator.WriteAllFiles(maxServiceCount);

            PolicyScopeServiceSelectorMockBuilderGenerator policyScopeServiceSelectorMockBuilderGenerator =
                new PolicyScopeServiceSelectorMockBuilderGenerator();

            fileUpdateCount += policyScopeServiceSelectorMockBuilderGenerator.WriteAllFiles(maxServiceCount);

            PolicyScopeServiceSelectorMockGenerator policyScopeServiceSelectorMockGenerator =
                new PolicyScopeServiceSelectorMockGenerator();

            fileUpdateCount += policyScopeServiceSelectorMockGenerator.WriteAllFiles(maxServiceCount);

            Console.WriteLine();
            Console.WriteLine("Autogenerating code for logic test...");

            LogicGenerator logicGenerator =
                new LogicGenerator();

            fileUpdateCount += logicGenerator.WriteAllFiles(maxServiceCount);

            WorkerGenerator workerGenerator =
                new WorkerGenerator();

            fileUpdateCount += workerGenerator.WriteAllFiles(maxServiceCount);

            Console.WriteLine();
            Console.WriteLine("Autogenerating code for test...");

            UnitTestLogicGenerator unitTestLogicGenerator =
                new UnitTestLogicGenerator();

            fileUpdateCount += unitTestLogicGenerator.WriteAllFiles(maxServiceCount);

            UnitTestResourceNotFoundGenerator unitTestResourceNotFoundGenerator =
                new UnitTestResourceNotFoundGenerator();

            fileUpdateCount += unitTestResourceNotFoundGenerator.WriteAllFiles(maxServiceCount);

            UnitTestServiceNotFoundGenerator unitTestServiceNotFoundGenerator =
                new UnitTestServiceNotFoundGenerator();

            fileUpdateCount += unitTestServiceNotFoundGenerator.WriteAllFiles(maxServiceCount);

            Console.WriteLine();
            Console.WriteLine($"Autogenerating completed. {fileUpdateCount} files updated.");

            return(fileUpdateCount);
        }