private SourceText GenerateSource(AdditionalText additionalFile, string scalarFieldTypeMappingProviderTypeName) { var configurationOptions = new Dictionary <string, string> { { "build_property.GraphQlClientGenerator_ClassPrefix", "SourceGenerated" }, { "build_property.GraphQlClientGenerator_ClassSuffix", "V2" }, { "build_property.GraphQlClientGenerator_IncludeDeprecatedFields", "true" }, { "build_property.GraphQlClientGenerator_CommentGeneration", "CodeSummary" }, { "build_property.GraphQlClientGenerator_FloatTypeMapping", "Double" }, { "build_property.GraphQlClientGenerator_BooleanTypeMapping", "Boolean" }, { "build_property.GraphQlClientGenerator_IdTypeMapping", "String" }, { "build_property.GraphQlClientGenerator_JsonPropertyGeneration", "Always" }, { "build_property.GraphQlClientGenerator_CustomClassMapping", "Query:Tibber|RootMutation:TibberMutation Consumption:ConsumptionEntry;Production:ProductionEntry" }, { "build_property.GraphQlClientGenerator_Headers", "Authorization:Basic XXX|X-REQUEST-ID:123456789" }, { "build_property.GraphQlClientGenerator_HttpMethod", "GET" }, { "build_property.GraphQlClientGenerator_EnumValueNaming", "CSharp" } }; if (scalarFieldTypeMappingProviderTypeName is not null) { configurationOptions.Add("build_property.GraphQlClientGenerator_ScalarFieldTypeMappingProvider", scalarFieldTypeMappingProviderTypeName); } var compilerAnalyzerConfigOptionsProvider = new CompilerAnalyzerConfigOptionsProvider(new CompilerAnalyzerConfigOptions(configurationOptions)); var compilation = CompilationHelper.CreateCompilation(null, "SourceGeneratorTestAssembly"); var additionalFiles = new List <AdditionalText> { _fileGraphQlSchema }; if (additionalFile is not null) { additionalFiles.Add(additionalFile); } var sourceGenerator = new GraphQlClientSourceGenerator(); var driver = CSharpGeneratorDriver.Create(new [] { sourceGenerator }, additionalFiles, optionsProvider: compilerAnalyzerConfigOptionsProvider); var csharpDriver = driver.RunGenerators(compilation); var runResult = csharpDriver.GetRunResult(); var results = runResult.Results; results.Length.ShouldBe(1); results[0].GeneratedSources.Length.ShouldBe(1); return(results[0].GeneratedSources[0].SourceText); }
private SourceText GenerateSource(AdditionalText additionalFile, string scalarFieldTypeMappingProviderTypeName) { var sourceGenerator = new GraphQlClientSourceGenerator(); sourceGenerator.Initialize(new GeneratorInitializationContext()); var configurationOptions = new Dictionary <string, string> { { "build_property.GraphQlClientGenerator_ClassPrefix", "SourceGenerated" }, { "build_property.GraphQlClientGenerator_ClassSuffix", "V2" }, { "build_property.GraphQlClientGenerator_IncludeDeprecatedFields", "true" }, { "build_property.GraphQlClientGenerator_CommentGeneration", "CodeSummary" }, { "build_property.GraphQlClientGenerator_FloatTypeMapping", "Double" }, { "build_property.GraphQlClientGenerator_BooleanTypeMapping", "Boolean" }, { "build_property.GraphQlClientGenerator_IdTypeMapping", "String" }, { "build_property.GraphQlClientGenerator_JsonPropertyGeneration", "Always" }, { "build_property.GraphQlClientGenerator_CustomClassMapping", "Query:Tibber|RootMutation:TibberMutation Consumption:ConsumptionEntry;Production:ProductionEntry" }, { "build_property.GraphQlClientGenerator_Headers", "Authorization:Basic XXX|X-REQUEST-ID:123456789" } }; if (scalarFieldTypeMappingProviderTypeName != null) { configurationOptions.Add("build_property.GraphQlClientGenerator_ScalarFieldTypeMappingProvider", scalarFieldTypeMappingProviderTypeName); } var compilerAnalyzerConfigOptionsProvider = new CompilerAnalyzerConfigOptionsProvider(new CompilerAnalyzerConfigOptions(configurationOptions)); var compilation = CompilationHelper.CreateCompilation(null, "SourceGeneratorTestAssembly"); var generatorExecutionContextType = typeof(GeneratorExecutionContext); var constructorInfo = generatorExecutionContextType.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)[0]; var additionalFiles = new List <AdditionalText> { _fileGraphQlSchema }; if (additionalFile != null) { additionalFiles.Add(additionalFile); } var additionalSourcesCollectionType = Type.GetType("Microsoft.CodeAnalysis.AdditionalSourcesCollection, Microsoft.CodeAnalysis"); var additionalSourcesCollection = additionalSourcesCollectionType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { typeof(string) }, null).Invoke(new object[] { ".cs" }); var executionContext = (GeneratorExecutionContext)constructorInfo.Invoke( new[] { compilation, new CSharpParseOptions(LanguageVersion.CSharp9), additionalFiles.ToImmutableArray(), compilerAnalyzerConfigOptionsProvider, null, additionalSourcesCollection, CancellationToken.None }); var additionalSourceFiles = generatorExecutionContextType.GetField("_additionalSources", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(executionContext); sourceGenerator.Execute(executionContext); var sourcesAdded = ((IEnumerable)additionalSourceFiles.GetType().GetField("_sourcesAdded", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(additionalSourceFiles)).GetEnumerator(); sourcesAdded.MoveNext().ShouldBeTrue(); var sourceText = (SourceText)sourcesAdded.Current.GetType().GetProperty("Text").GetValue(sourcesAdded.Current); sourcesAdded.MoveNext().ShouldBeFalse(); return(sourceText); }