Ejemplo n.º 1
0
        public static void AssertStarWarsResult(
            AssertSettings settings,
            params string[] sourceTexts)
        {
            var source = new string[sourceTexts.Length + 2];

            source[0] = FileResource.Open("Schema.graphql");
            source[1] = FileResource.Open("Schema.extensions.graphql");

            Array.Copy(
                sourceTexts,
                sourceIndex: 0,
                source,
                destinationIndex: 2,
                length: sourceTexts.Length);

            AssertResult(settings, true, source);
        }
Ejemplo n.º 2
0
        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));
            }
        }
Ejemplo n.º 3
0
 public static void AssertResult(
     AssertSettings settings,
     params string[] sourceTexts)
 {
     AssertResult(settings, false, sourceTexts);
 }
Ejemplo n.º 4
0
        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,
                RazorComponents = settings.RazorComponents
            });

            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}.{document.Kind}"))
                {
                    Assert.True(false, $"Document name duplicated {document.Name}");
                }

                if (document.Kind is SourceDocumentKind.CSharp or SourceDocumentKind.Razor)
                {
                    documents.AppendLine("// " + document.Name);
                    documents.AppendLine();
                    documents.AppendLine(document.SourceText);
                    documents.AppendLine();
                }