|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Linq; |
| 5 | +using System.Runtime.CompilerServices; |
| 6 | +using System.Runtime.Serialization; |
| 7 | +using System.Text.Json; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.AspNetCore.Razor; |
| 10 | +using Microsoft.AspNetCore.Razor.Language; |
| 11 | +using Microsoft.AspNetCore.Razor.Test.Common; |
| 12 | +using Microsoft.CodeAnalysis.ExternalAccess.Razor.Features; |
| 13 | +using Microsoft.CodeAnalysis.Razor.Formatting; |
| 14 | +using Microsoft.CodeAnalysis.Razor.Protocol; |
| 15 | +using Microsoft.VisualStudio.Razor.LanguageClient.Cohost; |
| 16 | +using Microsoft.VisualStudio.Razor.LanguageClient.Cohost.Formatting; |
| 17 | +using Xunit; |
| 18 | +using Xunit.Abstractions; |
| 19 | + |
| 20 | +namespace Microsoft.VisualStudio.LanguageServices.Razor.Test.Cohost.Formatting; |
| 21 | + |
| 22 | +/// <summary> |
| 23 | +/// Not tests of the formatting log, but tests that use formatting logs sent in |
| 24 | +/// by users reporting issues. |
| 25 | +/// </summary> |
| 26 | +[Collection(HtmlFormattingCollection.Name)] |
| 27 | +public class FormattingLogTest(FormattingTestContext context, HtmlFormattingFixture fixture, ITestOutputHelper testOutput) |
| 28 | + : FormattingTestBase(context, fixture.Service, testOutput), IClassFixture<FormattingTestContext> |
| 29 | +{ |
| 30 | + [Fact] |
| 31 | + [WorkItem("https://github.com/dotnet/vscode-csharp/issues/7264")] |
| 32 | + public async Task UnexpectedFalseInIndentBlockOperation() |
| 33 | + { |
| 34 | + var contents = GetResource("InitialDocument.txt"); |
| 35 | + var document = CreateProjectAndRazorDocument(contents); |
| 36 | + |
| 37 | + var optionsFile = GetResource("Options.json"); |
| 38 | + var options = (TempRazorFormattingOptions)JsonSerializer.Deserialize(optionsFile, typeof(TempRazorFormattingOptions), JsonHelpers.JsonSerializerOptions).AssumeNotNull(); |
| 39 | + |
| 40 | + var formattingService = (RazorFormattingService)OOPExportProvider.GetExportedValue<IRazorFormattingService>(); |
| 41 | + formattingService.GetTestAccessor().SetFormattingLoggerFactory(new TestFormattingLoggerFactory(TestOutputHelper)); |
| 42 | + |
| 43 | + var htmlChangesFile = GetResource("HtmlChanges.json"); |
| 44 | + var htmlChanges = JsonSerializer.Deserialize<RazorTextChange[]>(htmlChangesFile, JsonHelpers.JsonSerializerOptions); |
| 45 | + var sourceText = await document.GetTextAsync(); |
| 46 | + var htmlEdits = htmlChanges.Select(c => sourceText.GetTextEdit(c.ToTextChange())).ToArray(); |
| 47 | + |
| 48 | + await GetFormattingEditsAsync(document, htmlEdits, span: default, options.CodeBlockBraceOnNextLine, options.InsertSpaces, options.TabSize, options.ToRazorFormattingOptions().CSharpSyntaxFormattingOptions); |
| 49 | + } |
| 50 | + |
| 51 | + private string GetResource(string name, [CallerMemberName] string? testName = null) |
| 52 | + { |
| 53 | + var baselineFileName = $@"TestFiles\FormattingLog\{testName}\{name}"; |
| 54 | + |
| 55 | + var testFile = TestFile.Create(baselineFileName, GetType().Assembly); |
| 56 | + Assert.True(testFile.Exists()); |
| 57 | + |
| 58 | + return testFile.ReadAllText(); |
| 59 | + } |
| 60 | + |
| 61 | + // HACK: Temporary types for deserializing because RazorCSharpSyntaxFormattingOptions doesn't have a parameterless constructor. |
| 62 | + internal class TempRazorFormattingOptions() |
| 63 | + { |
| 64 | + [DataMember(Order = 0)] |
| 65 | + public bool InsertSpaces { get; init; } = true; |
| 66 | + [DataMember(Order = 1)] |
| 67 | + public int TabSize { get; init; } = 4; |
| 68 | + [DataMember(Order = 2)] |
| 69 | + public bool CodeBlockBraceOnNextLine { get; init; } = false; |
| 70 | + [DataMember(Order = 3)] |
| 71 | + public TempRazorCSharpSyntaxFormattingOptions? CSharpSyntaxFormattingOptions { get; init; } |
| 72 | + |
| 73 | + public RazorFormattingOptions ToRazorFormattingOptions() |
| 74 | + => new() |
| 75 | + { |
| 76 | + InsertSpaces = InsertSpaces, |
| 77 | + TabSize = TabSize, |
| 78 | + CodeBlockBraceOnNextLine = CodeBlockBraceOnNextLine, |
| 79 | + CSharpSyntaxFormattingOptions = CSharpSyntaxFormattingOptions is not null |
| 80 | + ? new RazorCSharpSyntaxFormattingOptions( |
| 81 | + CSharpSyntaxFormattingOptions.Spacing, |
| 82 | + CSharpSyntaxFormattingOptions.SpacingAroundBinaryOperator, |
| 83 | + CSharpSyntaxFormattingOptions.NewLines, |
| 84 | + CSharpSyntaxFormattingOptions.LabelPositioning, |
| 85 | + CSharpSyntaxFormattingOptions.Indentation, |
| 86 | + CSharpSyntaxFormattingOptions.WrappingKeepStatementsOnSingleLine, |
| 87 | + CSharpSyntaxFormattingOptions.WrappingPreserveSingleLine, |
| 88 | + CSharpSyntaxFormattingOptions.NamespaceDeclarations, |
| 89 | + CSharpSyntaxFormattingOptions.PreferTopLevelStatements, |
| 90 | + CSharpSyntaxFormattingOptions.CollectionExpressionWrappingLength) |
| 91 | + : RazorCSharpSyntaxFormattingOptions.Default |
| 92 | + }; |
| 93 | + } |
| 94 | + |
| 95 | + [DataContract] |
| 96 | + internal sealed record class TempRazorCSharpSyntaxFormattingOptions( |
| 97 | + [property: DataMember] RazorSpacePlacement Spacing, |
| 98 | + [property: DataMember] RazorBinaryOperatorSpacingOptions SpacingAroundBinaryOperator, |
| 99 | + [property: DataMember] RazorNewLinePlacement NewLines, |
| 100 | + [property: DataMember] RazorLabelPositionOptions LabelPositioning, |
| 101 | + [property: DataMember] RazorIndentationPlacement Indentation, |
| 102 | + [property: DataMember] bool WrappingKeepStatementsOnSingleLine, |
| 103 | + [property: DataMember] bool WrappingPreserveSingleLine, |
| 104 | + [property: DataMember] RazorNamespaceDeclarationPreference NamespaceDeclarations, |
| 105 | + [property: DataMember] bool PreferTopLevelStatements, |
| 106 | + [property: DataMember] int CollectionExpressionWrappingLength) |
| 107 | + { |
| 108 | + public TempRazorCSharpSyntaxFormattingOptions() |
| 109 | + : this( |
| 110 | + default, |
| 111 | + default, |
| 112 | + default, |
| 113 | + default, |
| 114 | + default, |
| 115 | + default, |
| 116 | + default, |
| 117 | + default, |
| 118 | + true, |
| 119 | + default) |
| 120 | + { |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments