public string CreateClassCode(CodeNames codeNameBase, int maxServiceCount) { PolicyScopeResultSelectorMockGenerator policyScopeResultSelectorMockGenerator = new PolicyScopeResultSelectorMockGenerator(); PolicyScopeServiceSelectorGenerator policyScopeServiceSelectorGenerator = new PolicyScopeServiceSelectorGenerator(); PolicyScopeResultSelectorGenerator servicePolicyScopeBuilderGenerator = new PolicyScopeResultSelectorGenerator(); StringBuilder sb = new StringBuilder(); sb.Append( $@" internal class {GetClassNameAndTemplateParamaters(codeNameBase)} : {policyScopeServiceSelectorGenerator.GetInterfaceNameAndTemplateParamaters(codeNameBase)} {{ internal {GetClassName(codeNameBase)}(IEnumerable<PolicyScopeMockConfiguration> configurations) {{ Configurations = configurations; }} internal IEnumerable<PolicyScopeMockConfiguration> Configurations {{ get; }} "); for (int serviceCount = 0; serviceCount <= maxServiceCount; serviceCount++) { if (serviceCount != 0) { sb.AppendLine(); } CodeNames codeNames = new CodeNames(serviceCount, false, codeNameBase.IsAsync); sb.Append( $@" public {servicePolicyScopeBuilderGenerator.GetInterfaceNameAndTemplateParamaters(codeNames)} {codeNames.FunctionWithServicesName}{codeNames.TemplateParameters}() {{ var configurations = Configurations .Where(conf => conf.ServiceTypes.Count() == {codeNames.ServiceCount})"); for (int i = 0; i < codeNames.ServiceCount; i++) { sb.Append( $@" .Where(conf => typeof({codeNames.ServicesTypes[i]}).IsAssignableFrom(conf.ServiceTypes[{i}]))" ); } sb.AppendLine( $@"; if(!configurations.Any()) {{ string errorMessage = ""Found no configuration with {codeNames.ServiceCount} {(codeNames.ServiceCount == 1 ? "service" : "services")}"";"); for (int i = 0; i < codeNames.ServiceCount; i++) { sb.AppendLine( $@" errorMessage += Environment.NewLine + "" and service {i} is "" + typeof({codeNames.ServicesTypes[i]});" ); } sb.AppendLine( $@" errorMessage += "".""; throw new PolicyScopeMockException(errorMessage, Configurations); }} return new {policyScopeResultSelectorMockGenerator.GetClassNameAndTemplateParamaters(codeNames)}(configurations); }}"); } sb.AppendLine( $@" }}"); return(sb.ToString()); }
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()); }