public static void AssertResult(bool strictValidation, params string[] sourceTexts) { ClientModel clientModel = CreateClientModel(sourceTexts, strictValidation); var documents = new StringBuilder(); var documentNames = new HashSet <string>(); documents.AppendLine("// ReSharper disable BuiltInTypeReferenceStyle"); documents.AppendLine("// ReSharper disable RedundantNameQualifier"); documents.AppendLine("// ReSharper disable ArrangeObjectCreationWhenTypeEvident"); documents.AppendLine("// ReSharper disable UnusedType.Global"); documents.AppendLine("// ReSharper disable PartialTypeWithSinglePart"); documents.AppendLine("// ReSharper disable UnusedMethodReturnValue.Local"); documents.AppendLine("// ReSharper disable ConvertToAutoProperty"); documents.AppendLine("// ReSharper disable UnusedMember.Global"); documents.AppendLine("// ReSharper disable SuggestVarOrType_SimpleTypes"); documents.AppendLine("// ReSharper disable InconsistentNaming"); documents.AppendLine(); CSharpGeneratorResult result = Generate( clientModel, @namespace: "Foo.Bar", clientName: "FooClient"); Assert.False( result.Errors.Any(), "It is expected that the result has no generator errors!"); foreach (CSharpDocument document in result.CSharpDocuments) { if (!documentNames.Add(document.Name)) { Assert.True(false, $"Document name duplicated {document.Name}"); } documents.AppendLine("// " + document.Name); documents.AppendLine(); documents.AppendLine(document.SourceText); documents.AppendLine(); } documents.ToString().MatchSnapshot(); IReadOnlyList <Diagnostic> diagnostics = CSharpCompiler.GetDiagnosticErrors(documents.ToString()); if (diagnostics.Any()) { Assert.True(false, "Diagnostic Errors: \n" + diagnostics .Select(x => $"{x.GetMessage()}" + $" (Line: {x.Location.GetLineSpan().StartLinePosition.Line})") .Aggregate((acc, val) => acc + "\n" + val)); } }
private static void AssertResult( CSharpGeneratorResult result, bool evaluateDiagnostics = true) { var content = new StringBuilder(); if (result.Errors.Any()) { content.AppendLine("// Errors:"); foreach (var error in result.Errors) { content.AppendLine(error.Message); content.AppendLine(); } } content.AppendLine("// Code:"); var documentName = new HashSet <string>(); foreach (var document in result.CSharpDocuments) { if (!documentName.Add(document.Name)) { Assert.True(false, $"Document name duplicated {document.Name}"); } content.AppendLine("// " + document.Name); content.AppendLine(); content.AppendLine(document.SourceText); content.AppendLine(); } content.ToString().MatchSnapshot(); if (!evaluateDiagnostics) { return; } IReadOnlyList <Diagnostic> diagnostics = CSharpCompiler.GetDiagnosticErrors(content.ToString()); if (diagnostics.Any()) { Assert.True(false, "Diagnostic Errors: \n" + diagnostics .Select(x => $"{x.GetMessage()} " + $"(Line: {x.Location.GetLineSpan().StartLinePosition.Line})") .Aggregate((acc, val) => acc + "\n" + val)); } }
private static void AssertResult( ClientModel clientModel, CSharpGeneratorExecutor generator, StringBuilder documents) { documents.AppendLine("// ReSharper disable BuiltInTypeReferenceStyle"); documents.AppendLine("// ReSharper disable RedundantNameQualifier"); documents.AppendLine("// ReSharper disable ArrangeObjectCreationWhenTypeEvident"); documents.AppendLine("// ReSharper disable UnusedType.Global"); documents.AppendLine("// ReSharper disable PartialTypeWithSinglePart"); documents.AppendLine("// ReSharper disable UnusedMethodReturnValue.Local"); documents.AppendLine("// ReSharper disable ConvertToAutoProperty"); documents.AppendLine("// ReSharper disable UnusedMember.Global"); documents.AppendLine("// ReSharper disable SuggestVarOrType_SimpleTypes"); documents.AppendLine("// ReSharper disable InconsistentNaming"); documents.AppendLine(); var documentName = new HashSet <string>(); foreach (CSharpDocument document in generator.Generate(clientModel, "Foo", "FooClient")) { if (!documentName.Add(document.Name)) { Assert.True(false, $"Document name duplicated {document.Name}"); } documents.AppendLine("// " + document.Name); documents.AppendLine(); documents.AppendLine(document.SourceText); documents.AppendLine(); } documents.ToString().MatchSnapshot(); IReadOnlyList <Diagnostic> diagnostics = CSharpCompiler.GetDiagnosticErrors(documents.ToString()); if (diagnostics.Any()) { Assert.True(false, "Diagnostic Errors: \n" + diagnostics .Select(x => $"{x.GetMessage()}" + $" (Line: {x.Location.GetLineSpan().StartLinePosition.Line})") .Aggregate((acc, val) => acc + "\n" + val)); } }
private static void AssertResult( ClientModel clientModel, CSharpGeneratorExecutor generator, StringBuilder documents) { var documentName = new HashSet <string>(); foreach (CSharpDocument document in generator.Generate(clientModel, "Foo", "FooClient")) { if (!documentName.Add(document.Name)) { Assert.True(false, $"Document name duplicated {document.Name}"); } documents.AppendLine("// " + document.Name); documents.AppendLine(); documents.AppendLine(document.SourceText); documents.AppendLine(); } documents.ToString().MatchSnapshot(); IReadOnlyList <Diagnostic> diagnostics = CSharpCompiler.GetDiagnosticErrors(documents.ToString()); if (diagnostics.Any()) { Assert.True(false, "Diagnostic Errors: \n" + diagnostics .Select(x => $"{x.GetMessage()}" + $" (Line: {x.Location.GetLineSpan().StartLinePosition.Line})") .Aggregate((acc, val) => acc + "\n" + val)); } }
public static void AssertResult( AssertSettings settings, bool skipWarnings, params string[] sourceTexts) { ClientModel clientModel = CreateClientModel(sourceTexts, settings.StrictValidation, settings.NoStore); var documents = new StringBuilder(); var documentNames = new HashSet <string>(); documents.AppendLine("// ReSharper disable BuiltInTypeReferenceStyle"); documents.AppendLine("// ReSharper disable RedundantNameQualifier"); documents.AppendLine("// ReSharper disable ArrangeObjectCreationWhenTypeEvident"); documents.AppendLine("// ReSharper disable UnusedType.Global"); documents.AppendLine("// ReSharper disable PartialTypeWithSinglePart"); documents.AppendLine("// ReSharper disable UnusedMethodReturnValue.Local"); documents.AppendLine("// ReSharper disable ConvertToAutoProperty"); documents.AppendLine("// ReSharper disable UnusedMember.Global"); documents.AppendLine("// ReSharper disable SuggestVarOrType_SimpleTypes"); documents.AppendLine("// ReSharper disable InconsistentNaming"); documents.AppendLine(); if (settings.Profiles.Count == 0) { settings.Profiles.Add(TransportProfile.Default); } CSharpGeneratorResult result = Generate( clientModel, new CSharpGeneratorSettings { Namespace = settings.Namespace ?? "Foo.Bar", ClientName = settings.ClientName ?? "FooClient", StrictSchemaValidation = settings.StrictValidation, RequestStrategy = settings.RequestStrategy, TransportProfiles = settings.Profiles, NoStore = settings.NoStore, InputRecords = settings.InputRecords, EntityRecords = settings.EntityRecords }); Assert.False( result.Errors.Any(), "It is expected that the result has no generator errors!"); foreach (var document in result.Documents) { if (!documentNames.Add(document.Name)) { Assert.True(false, $"Document name duplicated {document.Name}"); } if (document.Kind == SourceDocumentKind.CSharp) { documents.AppendLine("// " + document.Name); documents.AppendLine(); documents.AppendLine(document.SourceText); documents.AppendLine(); } else if (document.Kind == SourceDocumentKind.GraphQL) { documents.AppendLine("// " + document.Name); documents.AppendLine("// " + document.Hash); documents.AppendLine(); using var reader = new StringReader(document.SourceText); string?line; do { line = reader.ReadLine(); if (line is not null) { documents.AppendLine("// " + line); } } while (line is not null); documents.AppendLine(); } } if (settings.SnapshotFile is not null) { documents.ToString() .MatchSnapshot( new SnapshotFullName( settings.SnapshotFile, Snapshot.FullName().FolderPath)); } else { documents.ToString().MatchSnapshot(); } IReadOnlyList <Diagnostic> diagnostics = CSharpCompiler.GetDiagnosticErrors(documents.ToString()); if (skipWarnings) { diagnostics = diagnostics .Where(x => x.Severity == DiagnosticSeverity.Error) .ToList(); } if (diagnostics.Any()) { Assert.True(false, "Diagnostic Errors: \n" + diagnostics .Select(x => $"{x.GetMessage()}" + $" (Line: {x.Location.GetLineSpan().StartLinePosition.Line})") .Aggregate((acc, val) => acc + "\n" + val)); } }