forked from dotnet/format
/
FormattedFilesTests.cs
78 lines (63 loc) · 2.81 KB
/
FormattedFilesTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.Tools.Formatters;
using Microsoft.CodeAnalysis.Tools.Tests.Utilities;
using Microsoft.CodeAnalysis.Tools.Utilities;
using Microsoft.Extensions.Logging;
using Xunit;
namespace Microsoft.CodeAnalysis.Tools.Tests.Formatters
{
public class FormattedFilesTests : CSharpFormatterTests
{
private protected override ICodeFormatter Formatter => new FinalNewlineFormatter();
private Dictionary<string, string> EditorConfig => new Dictionary<string, string>()
{
["insert_final_newline"] = "true",
["end_of_line"] = "lf",
};
[Fact]
public async Task ReturnsItem_WhenFileFormatted()
{
var testCode = "class C\n{\n}";
var result = await TestFormattedFiles(testCode);
Assert.Single(result);
}
[Fact]
public async Task ReturnsEmptyList_WhenNoFilesFormatted()
{
var testCode = "class C\n{\n}\n";
var result = await TestFormattedFiles(testCode);
Assert.Empty(result);
}
private async Task<List<FormattedFile>> TestFormattedFiles(string testCode)
{
var text = SourceText.From(testCode, Encoding.UTF8);
TestState.Sources.Add(text);
var solution = await GetSolutionAsync(TestState.Sources.ToArray(), TestState.AdditionalFiles.ToArray(), TestState.AdditionalReferences.ToArray(), EditorConfig);
var project = solution.Projects.Single();
var document = project.Documents.Single();
var fileMatcher = SourceFileMatcher.CreateMatcher(new[] { document.FilePath }, exclude: Array.Empty<string>());
var formatOptions = new FormatOptions(
workspaceFilePath: project.FilePath,
workspaceType: WorkspaceType.Folder,
logLevel: LogLevel.Trace,
fixCategory: FixCategory.Whitespace,
codeStyleSeverity: DiagnosticSeverity.Error,
analyzerSeverity: DiagnosticSeverity.Error,
saveFormattedFiles: false,
changesAreErrors: false,
fileMatcher,
reportPath: string.Empty,
includeGeneratedFiles: false);
var pathsToFormat = GetOnlyFileToFormat(solution);
var formattedFiles = new List<FormattedFile>();
await Formatter.FormatAsync(solution, pathsToFormat, formatOptions, new TestLogger(), formattedFiles, default);
return formattedFiles;
}
}
}