Skip to content
This repository was archived by the owner on Jul 12, 2022. It is now read-only.

Commit d6ef8e4

Browse files
committed
Some improvements
Add tests for UsesXunitForTestsFormattingRule. Add command line option to specify which source files to process.
1 parent c8f3da0 commit d6ef8e4

File tree

8 files changed

+304
-40
lines changed

8 files changed

+304
-40
lines changed

src/CodeFormatter/Program.cs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System;
5-
using System.ComponentModel.Composition.Hosting;
5+
using System.Collections.Generic;
66
using System.IO;
7+
using System.Linq;
78
using System.Threading;
89
using System.Threading.Tasks;
9-
1010
using Microsoft.CodeAnalysis.MSBuild;
1111
using Microsoft.DotNet.CodeFormatting;
1212

@@ -16,9 +16,9 @@ internal static class Program
1616
{
1717
private static int Main(string[] args)
1818
{
19-
if (args.Length != 1)
19+
if (args.Length < 1)
2020
{
21-
Console.Error.WriteLine("CodeFormatter <solution>");
21+
Console.Error.WriteLine("CodeFormatter <solution> [<rule types>] [/file <filename>]");
2222
return -1;
2323
}
2424

@@ -29,22 +29,44 @@ private static int Main(string[] args)
2929
return -1;
3030
}
3131

32+
List<string> ruleTypes = new List<string>();
33+
List<string> filenames = new List<string>();
34+
35+
for (int i=1; i<args.Length; i++)
36+
{
37+
string arg = args[i];
38+
if (arg.Equals("/file", StringComparison.InvariantCultureIgnoreCase))
39+
{
40+
if (i + 1 < args.Length)
41+
{
42+
string param = args[i + 1];
43+
filenames.Add(param);
44+
i++;
45+
}
46+
}
47+
else
48+
{
49+
ruleTypes.Add(arg);
50+
}
51+
}
52+
53+
3254
var cts = new CancellationTokenSource();
3355
var ct = cts.Token;
3456

3557
Console.CancelKeyPress += delegate { cts.Cancel(); };
3658

37-
RunAsync(solutionPath, ct).Wait(ct);
59+
RunAsync(solutionPath, ruleTypes, filenames, ct).Wait(ct);
3860
Console.WriteLine("Completed formatting.");
3961
return 0;
4062
}
4163

42-
private static async Task RunAsync(string solutionFilePath, CancellationToken cancellationToken)
64+
private static async Task RunAsync(string solutionFilePath, IEnumerable<string> ruleTypes, IEnumerable<string> filenames, CancellationToken cancellationToken)
4365
{
4466
var workspace = MSBuildWorkspace.Create();
4567
await workspace.OpenSolutionAsync(solutionFilePath, cancellationToken);
4668

47-
var engine = FormattingEngine.Create();
69+
var engine = FormattingEngine.Create(ruleTypes, filenames);
4870
await engine.RunAsync(workspace, cancellationToken);
4971
}
5072
}

src/Microsoft.DotNet.CodeFormatting.Tests/CodeFormattingTestBase.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,22 @@ internal static IFormattingRule GetDefaultVSFormatter()
3232
return new Rules.IsFormattedFormattingRule();
3333
}
3434

35-
private static Solution CreateSolution(string[] sources, string language = LanguageNames.CSharp)
35+
protected virtual IEnumerable<MetadataReference> GetSolutionMetadataReferences()
36+
{
37+
yield return s_CorlibReference;
38+
yield return s_SystemCoreReference;
39+
yield return s_CodeFormatterReference;
40+
}
41+
42+
private Solution CreateSolution(string[] sources, string language = LanguageNames.CSharp)
3643
{
3744
string fileExtension = language == LanguageNames.CSharp ? CSharpFileExtension : VBFileExtension;
3845
var projectId = ProjectId.CreateNewId(TestProjectName);
3946

4047
var solution = new CustomWorkspace()
4148
.CurrentSolution
4249
.AddProject(projectId, TestProjectName, TestProjectName, language)
43-
.AddMetadataReference(projectId, s_CorlibReference)
44-
.AddMetadataReference(projectId, s_SystemCoreReference)
45-
.AddMetadataReference(projectId, s_CodeFormatterReference);
50+
.AddMetadataReferences(projectId, GetSolutionMetadataReferences());
4651

4752
int count = 0;
4853
foreach (var source in sources)
@@ -94,7 +99,7 @@ private static void AssertSolutionEqual(Solution expectedSolution, Solution actu
9499
}
95100
}
96101

97-
private static void Verify(string[] sources, string[] expected, IFormattingRule rule, bool runFormatter)
102+
private void Verify(string[] sources, string[] expected, IFormattingRule rule, bool runFormatter)
98103
{
99104
var inputSolution = CreateSolution(sources);
100105
var expectedSolution = CreateSolution(expected);

src/Microsoft.DotNet.CodeFormatting.Tests/Microsoft.DotNet.CodeFormatting.Tests.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<Reference Include="Microsoft.CodeAnalysis.Workspaces.Desktop">
6868
<HintPath>..\packages\Microsoft.CodeAnalysis.Workspaces.Common.1.0.0-beta1-20141031-01\lib\net45\Microsoft.CodeAnalysis.Workspaces.Desktop.dll</HintPath>
6969
</Reference>
70+
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
7071
<Reference Include="System" />
7172
<Reference Include="System.Collections.Immutable, Version=1.1.32.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
7273
<SpecificVersion>False</SpecificVersion>
@@ -108,6 +109,7 @@
108109
<Compile Include="Rules\HasNoNewLineBeforeEndBraceFormattingRuleTests.cs" />
109110
<Compile Include="Rules\HasPrivateAccessorOnFieldNamesFormattingRuleTests.cs" />
110111
<Compile Include="Rules\HasUnderScoreInPrivateFieldNamesFormattingRuleTests.cs" />
112+
<Compile Include="Rules\UsesXunitForTestsFormattingRuleTests.cs" />
111113
</ItemGroup>
112114
<ItemGroup>
113115
<Folder Include="Properties\" />
@@ -124,6 +126,12 @@
124126
<ItemGroup>
125127
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
126128
</ItemGroup>
129+
<ItemGroup>
130+
<Content Include="..\CodeFormatter\MSTestNamespaces.txt">
131+
<Link>MSTestNamespaces.txt</Link>
132+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
133+
</Content>
134+
</ItemGroup>
127135
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
128136
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
129137
<PropertyGroup>
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Microsoft.CodeAnalysis;
7+
using Xunit;
8+
9+
namespace Microsoft.DotNet.CodeFormatting.Tests
10+
{
11+
12+
public class UsesXunitForTestsFormattingRuleTests : CodeFormattingTestBase
13+
{
14+
[Fact]
15+
public void TestUpdatesUsingStatements()
16+
{
17+
var text = @"
18+
using System;
19+
using Microsoft.VisualStudio.TestTools.UnitTesting;
20+
21+
namespace System.Composition.UnitTests
22+
{
23+
}
24+
";
25+
26+
var expected = @"
27+
using System;
28+
using Xunit;
29+
30+
namespace System.Composition.UnitTests
31+
{
32+
}
33+
";
34+
Verify(text, expected);
35+
}
36+
37+
[Fact]
38+
public void TestUpdatesUsingStatementsWithIfDefs()
39+
{
40+
var text = @"
41+
using System;
42+
#if NETFX_CORE
43+
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
44+
#elif PORTABLE_TESTS
45+
using Microsoft.Bcl.Testing;
46+
#else
47+
using Microsoft.VisualStudio.TestTools.UnitTesting;
48+
49+
#endif
50+
namespace System.Composition.UnitTests
51+
{
52+
}
53+
";
54+
55+
var expected = @"
56+
using System;
57+
using Xunit;
58+
59+
namespace System.Composition.UnitTests
60+
{
61+
}
62+
";
63+
Verify(text, expected);
64+
}
65+
66+
[Fact]
67+
public void TestRemovesTestClassAttributes()
68+
{
69+
var text = @"
70+
using System;
71+
using Microsoft.VisualStudio.TestTools.UnitTesting;
72+
73+
namespace System.Composition.UnitTests
74+
{
75+
[TestClass]
76+
public class MyTestClass
77+
{
78+
}
79+
}
80+
";
81+
82+
var expected = @"
83+
using System;
84+
using Xunit;
85+
86+
namespace System.Composition.UnitTests
87+
{
88+
public class MyTestClass
89+
{
90+
}
91+
}
92+
";
93+
Verify(text, expected);
94+
}
95+
96+
[Fact]
97+
public void TestUpdatesTestMethodAttributes()
98+
{
99+
var text = @"
100+
using System;
101+
using Microsoft.VisualStudio.TestTools.UnitTesting;
102+
103+
namespace System.Composition.UnitTests
104+
{
105+
public class MyTestClass
106+
{
107+
[TestMethod]
108+
public void MyTestMethod()
109+
{
110+
}
111+
}
112+
}
113+
";
114+
115+
var expected = @"
116+
using System;
117+
using Xunit;
118+
119+
namespace System.Composition.UnitTests
120+
{
121+
public class MyTestClass
122+
{
123+
[Fact]
124+
public void MyTestMethod()
125+
{
126+
}
127+
}
128+
}
129+
";
130+
Verify(text, expected);
131+
}
132+
133+
[Fact]
134+
public void TestUpdatesAsserts()
135+
{
136+
var text = @"
137+
using System;
138+
using Microsoft.VisualStudio.TestTools.UnitTesting;
139+
140+
namespace System.Composition.UnitTests
141+
{
142+
public class MyTestClass
143+
{
144+
public void MyTestMethod()
145+
{
146+
object obj = new object();
147+
148+
Assert.AreEqual(1, 1);
149+
Assert.AreNotEqual(1, 2);
150+
Assert.IsNull(null);
151+
Assert.IsNotNull(obj);
152+
Assert.AreSame(obj, obj);
153+
Assert.AreNotSame(obj, new object());
154+
Assert.IsTrue(true);
155+
Assert.IsFalse(false);
156+
Assert.IsInstanceOfType(string.Empty, typeof(String));
157+
}
158+
}
159+
}
160+
";
161+
162+
var expected = @"
163+
using System;
164+
using Xunit;
165+
166+
namespace System.Composition.UnitTests
167+
{
168+
public class MyTestClass
169+
{
170+
public void MyTestMethod()
171+
{
172+
object obj = new object();
173+
174+
Assert.Equal(1, 1);
175+
Assert.NotEqual(1, 2);
176+
Assert.Null(null);
177+
Assert.NotNull(obj);
178+
Assert.Same(obj, obj);
179+
Assert.NotSame(obj, new object());
180+
Assert.True(true);
181+
Assert.False(false);
182+
Assert.IsAssignableFrom(typeof(String), string.Empty);
183+
}
184+
}
185+
}
186+
";
187+
Verify(text, expected);
188+
}
189+
190+
private static readonly MetadataReference s_MSTestReference = MetadataReference.CreateFromAssembly(typeof(VisualStudio.TestTools.UnitTesting.TestMethodAttribute).Assembly);
191+
private static readonly MetadataReference s_XunitReference = MetadataReference.CreateFromAssembly(typeof(FactAttribute).Assembly);
192+
193+
protected override IEnumerable<MetadataReference> GetSolutionMetadataReferences()
194+
{
195+
return base.GetSolutionMetadataReferences()
196+
.Concat(new[] {
197+
s_MSTestReference,
198+
s_XunitReference
199+
});
200+
}
201+
202+
internal override IFormattingRule GetFormattingRule()
203+
{
204+
return new Rules.UsesXunitForTestsFormattingRule();
205+
}
206+
}
207+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.Composition;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis;
9+
10+
namespace Microsoft.DotNet.CodeFormatting.Filters
11+
{
12+
internal sealed class FilenameFilter : IFormattingFilter
13+
{
14+
IEnumerable<string> _filenames;
15+
16+
public FilenameFilter(IEnumerable<string> filenames)
17+
{
18+
_filenames = filenames;
19+
}
20+
21+
public Task<bool> ShouldBeProcessedAsync(Document document)
22+
{
23+
if (!_filenames.Any())
24+
{
25+
return Task.FromResult(true);
26+
}
27+
28+
string docFilename = Path.GetFileName(document.FilePath);
29+
30+
foreach (var filename in _filenames)
31+
{
32+
if (filename.Equals(docFilename, StringComparison.InvariantCultureIgnoreCase))
33+
{
34+
return Task.FromResult(true);
35+
}
36+
}
37+
38+
return Task.FromResult(false);
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)