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

Commit 8479c0f

Browse files
committed
Merge pull request #63 from dotnet/xunit
Move xunit code into custom converter
2 parents c9490f1 + a82be6c commit 8479c0f

22 files changed

+644
-79
lines changed

src/CodeFormatter.sln

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 2013
4-
VisualStudioVersion = 12.0.30723.0
4+
VisualStudioVersion = 12.0.31101.0
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.DotNet.CodeFormatting", "Microsoft.DotNet.CodeFormatting\Microsoft.DotNet.CodeFormatting.csproj", "{D535641F-A2D7-481C-930D-96C02F052B95}"
77
EndProject
@@ -14,6 +14,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{34034F
1414
.nuget\packages.config = .nuget\packages.config
1515
EndProjectSection
1616
EndProject
17+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XUnitConverter", "XUnitConverter\XUnitConverter.csproj", "{81B0FF57-C128-4F6B-83C7-94DBAF261582}"
18+
EndProject
19+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XUnitConverter.Tests", "XUnitConverter.Tests\XUnitConverter.Tests.csproj", "{BA4C1700-8A72-4F33-AF67-0E60F324E521}"
20+
EndProject
1721
Global
1822
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1923
Debug|Any CPU = Debug|Any CPU
@@ -32,6 +36,14 @@ Global
3236
{D4D6FF88-0586-43C7-BDE4-D336EB25E7AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
3337
{D4D6FF88-0586-43C7-BDE4-D336EB25E7AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
3438
{D4D6FF88-0586-43C7-BDE4-D336EB25E7AA}.Release|Any CPU.Build.0 = Release|Any CPU
39+
{81B0FF57-C128-4F6B-83C7-94DBAF261582}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
40+
{81B0FF57-C128-4F6B-83C7-94DBAF261582}.Debug|Any CPU.Build.0 = Debug|Any CPU
41+
{81B0FF57-C128-4F6B-83C7-94DBAF261582}.Release|Any CPU.ActiveCfg = Release|Any CPU
42+
{81B0FF57-C128-4F6B-83C7-94DBAF261582}.Release|Any CPU.Build.0 = Release|Any CPU
43+
{BA4C1700-8A72-4F33-AF67-0E60F324E521}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
44+
{BA4C1700-8A72-4F33-AF67-0E60F324E521}.Debug|Any CPU.Build.0 = Debug|Any CPU
45+
{BA4C1700-8A72-4F33-AF67-0E60F324E521}.Release|Any CPU.ActiveCfg = Release|Any CPU
46+
{BA4C1700-8A72-4F33-AF67-0E60F324E521}.Release|Any CPU.Build.0 = Release|Any CPU
3547
EndGlobalSection
3648
GlobalSection(SolutionProperties) = preSolution
3749
HideSolutionNode = FALSE

src/CodeFormatter/CodeFormatter.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,6 @@
103103
<Name>Microsoft.DotNet.CodeFormatting</Name>
104104
</ProjectReference>
105105
</ItemGroup>
106-
<ItemGroup>
107-
<Content Include="MSTestNamespaces.txt" />
108-
</ItemGroup>
109106
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
110107
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
111108
Other similar extension points exist, see Microsoft.Common.targets.
@@ -114,6 +111,5 @@
114111
<Target Name="AfterBuild">
115112
<Copy SourceFiles="$(ProjectDir)IllegalHeaders.md" DestinationFolder="$(OutDir)" ContinueOnError="true" />
116113
<Copy SourceFiles="$(ProjectDir)CopyrightHeader.md" DestinationFolder="$(OutDir)" ContinueOnError="true" />
117-
<Copy SourceFiles="$(ProjectDir)MSTestNamespaces.txt" DestinationFolder="$(OutDir)" ContinueOnError="true" />
118114
</Target>
119115
</Project>

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,6 @@
114114
<Compile Include="Rules\HasNewLineBeforeFirstUsingFormattingRuleTests.cs" />
115115
<Compile Include="Rules\PrivateFieldNamingRuleTests.cs" />
116116
<Compile Include="Rules\NonAsciiCharactersAreEscapedInLiteralsRuleTests.cs" />
117-
<Compile Include="Rules\TestAssertTrueOrFalseRuleTests.cs" />
118-
<Compile Include="Rules\UsesXunitForTestsFormattingRuleTests.cs" />
119117
</ItemGroup>
120118
<ItemGroup>
121119
<Folder Include="Properties\" />
@@ -138,12 +136,6 @@
138136
<ItemGroup>
139137
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
140138
</ItemGroup>
141-
<ItemGroup>
142-
<Content Include="..\CodeFormatter\MSTestNamespaces.txt">
143-
<Link>MSTestNamespaces.txt</Link>
144-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
145-
</Content>
146-
</ItemGroup>
147139
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
148140
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
149141
<PropertyGroup>

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@
9999
<Compile Include="Rules\NonAsciiCharactersAreEscapedInLiteralsRule.cs" />
100100
<Compile Include="Rules\ExplicitThisRule.cs" />
101101
<Compile Include="Rules\RuleOrder.cs" />
102-
<Compile Include="Rules\TestAssertTrueOrFalseRule.cs" />
103-
<Compile Include="Rules\UsesXunitForTestsFormattingRule.cs" />
104102
<Compile Include="RuleTypeConstants.cs" />
105103
</ItemGroup>
106104
<ItemGroup>

src/Microsoft.DotNet.CodeFormatting/Rules/RuleOrder.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ internal static class SyntaxRuleOrder
1919
public const int HasNewLineBeforeFirstNamespaceFormattingRule = 5;
2020
public const int BraceNewLineRule = 6;
2121
public const int NonAsciiChractersAreEscapedInLiterals = 7;
22-
public const int TestAssertTrueOrFalseRule = 8;
2322
}
2423

2524
// Please keep these values sorted by number, not rule name.
@@ -35,7 +34,6 @@ internal static class LocalSemanticRuleOrder
3534
internal static class GlobalSemanticRuleOrder
3635
{
3736
public const int PrivateFieldNamingRule = 1;
38-
public const int UsesXunitForTestsFormattingRule = 2;
3937

4038
}
4139
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.Formatting;
3+
using Microsoft.CodeAnalysis.Text;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
using Xunit;
11+
using XUnitConverter;
12+
13+
namespace XUnitConverter.Tests
14+
{
15+
public abstract class ConverterTestBase
16+
{
17+
private static readonly MetadataReference s_CorlibReference = MetadataReference.CreateFromAssembly(typeof(object).Assembly);
18+
private static readonly MetadataReference s_SystemCoreReference = MetadataReference.CreateFromAssembly(typeof(Enumerable).Assembly);
19+
private static readonly MetadataReference s_MSTestReference = MetadataReference.CreateFromAssembly(typeof(Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute).Assembly);
20+
private static readonly MetadataReference s_XunitReference = MetadataReference.CreateFromAssembly(typeof(FactAttribute).Assembly);
21+
22+
protected abstract ConverterBase CreateConverter();
23+
24+
private async Task<Project> RunConverter(Project project, bool runFormatter)
25+
{
26+
var converter = CreateConverter();
27+
var solution = await converter.ProcessAsync(project, CancellationToken.None);
28+
29+
if (runFormatter)
30+
{
31+
foreach (var id in project.DocumentIds)
32+
{
33+
var document = solution.GetDocument(id);
34+
document = await Formatter.FormatAsync(document);
35+
solution = document.Project.Solution;
36+
}
37+
}
38+
39+
return solution.GetProject(project.Id); ;
40+
}
41+
42+
private Project CreateSolution(string source)
43+
{
44+
var testProjectName = "Test";
45+
var projectId = ProjectId.CreateNewId(testProjectName);
46+
47+
var references = new[]
48+
{
49+
s_CorlibReference,
50+
s_SystemCoreReference,
51+
s_MSTestReference,
52+
s_XunitReference
53+
};
54+
55+
var solution = new CustomWorkspace()
56+
.CurrentSolution
57+
.AddProject(projectId, testProjectName, testProjectName, LanguageNames.CSharp)
58+
.AddMetadataReferences(projectId, references);
59+
60+
var fileName = "File.cs";
61+
var documentId = DocumentId.CreateNewId(projectId, fileName);
62+
solution = solution.AddDocument(documentId, fileName, SourceText.From(source));
63+
return solution.GetProject(projectId);
64+
}
65+
66+
protected async Task Verify(string text, string expected, bool runFormatter = true)
67+
{
68+
var project = CreateSolution(text);
69+
project = await RunConverter(project, runFormatter);
70+
var actual = await project.Documents.Single().GetTextAsync(CancellationToken.None);
71+
Assert.Equal(expected, actual.ToString());
72+
}
73+
}
74+
}
Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@
88
using System.Threading.Tasks;
99
using Microsoft.CodeAnalysis;
1010
using Xunit;
11+
using System.Threading;
12+
using Microsoft.CodeAnalysis.Formatting;
13+
using Microsoft.CodeAnalysis.Text;
1114

12-
namespace Microsoft.DotNet.CodeFormatting.Tests
15+
namespace XUnitConverter.Tests
1316
{
14-
public class UsesXunitForTestsFormattingRuleTests : GlobalSemanticRuleTestBase
17+
public class MSTestToXUnitConverterTests : ConverterTestBase
1518
{
16-
internal override IGlobalSemanticFormattingRule Rule
19+
protected override XUnitConverter.ConverterBase CreateConverter()
1720
{
18-
get { return new Rules.UsesXunitForTestsFormattingRule(new Options()); }
21+
return new XUnitConverter.MSTestToXUnitConverter();
1922
}
2023

2124
[Fact]
22-
public void TestUpdatesUsingStatements()
25+
public async Task TestUpdatesUsingStatements()
2326
{
2427
var text = @"
2528
using System;
@@ -38,11 +41,11 @@ namespace System.Composition.UnitTests
3841
{
3942
}
4043
";
41-
Verify(text, expected);
44+
await Verify(text, expected);
4245
}
4346

4447
[Fact]
45-
public void TestUpdatesUsingStatementsWithIfDefs()
48+
public async Task TestUpdatesUsingStatementsWithIfDefs()
4649
{
4750
var text = @"
4851
using System;
@@ -67,11 +70,11 @@ namespace System.Composition.UnitTests
6770
{
6871
}
6972
";
70-
Verify(text, expected);
73+
await Verify(text, expected);
7174
}
7275

7376
[Fact]
74-
public void TestRemovesTestClassAttributes()
77+
public async Task TestRemovesTestClassAttributes()
7578
{
7679
var text = @"
7780
using System;
@@ -97,11 +100,11 @@ public class MyTestClass
97100
}
98101
}
99102
";
100-
Verify(text, expected);
103+
await Verify(text, expected);
101104
}
102105

103106
[Fact]
104-
public void TestUpdatesTestMethodAttributes()
107+
public async Task TestUpdatesTestMethodAttributes()
105108
{
106109
var text = @"
107110
using System;
@@ -134,11 +137,11 @@ public void MyTestMethod()
134137
}
135138
}
136139
";
137-
Verify(text, expected);
140+
await Verify(text, expected);
138141
}
139142

140143
[Fact]
141-
public void TestUpdatesAsserts()
144+
public async Task TestUpdatesAsserts()
142145
{
143146
var text = @"
144147
using System;
@@ -191,19 +194,7 @@ public void MyTestMethod()
191194
}
192195
}
193196
";
194-
Verify(text, expected);
195-
}
196-
197-
private static readonly MetadataReference s_MSTestReference = MetadataReference.CreateFromAssembly(typeof(VisualStudio.TestTools.UnitTesting.TestMethodAttribute).Assembly);
198-
private static readonly MetadataReference s_XunitReference = MetadataReference.CreateFromAssembly(typeof(FactAttribute).Assembly);
199-
200-
protected override IEnumerable<MetadataReference> GetSolutionMetadataReferences()
201-
{
202-
return base.GetSolutionMetadataReferences()
203-
.Concat(new[] {
204-
s_MSTestReference,
205-
s_XunitReference
206-
});
197+
await Verify(text, expected);
207198
}
208199
}
209200
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("XUnitConverter.Tests")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("XUnitConverter.Tests")]
13+
[assembly: AssemblyCopyright("Copyright © 2015")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("53b17378-8f9a-4669-ac68-94b0ae5e4e24")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

src/Microsoft.DotNet.CodeFormatting.Tests/Rules/TestAssertTrueOrFalseRuleTests.cs renamed to src/XUnitConverter.Tests/TestAssertTrueOrFalseConverterTests.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99
using Microsoft.CodeAnalysis;
1010
using Xunit;
1111

12-
namespace Microsoft.DotNet.CodeFormatting.Tests
12+
namespace XUnitConverter.Tests
1313
{
14-
public class TestAssertTrueOrFalseRuleTests : SyntaxRuleTestBase
14+
public class TestAssertTrueOrFalseConverterTests : ConverterTestBase
1515
{
16-
internal override ISyntaxFormattingRule Rule
16+
protected override XUnitConverter.ConverterBase CreateConverter()
1717
{
18-
get { return new Rules.TestAssertTrueOrFalseRule(); }
18+
return new XUnitConverter.TestAssertTrueOrFalseConverter();
1919
}
2020

2121
[Fact]
22-
public void TestAssertEqualNotEqual()
22+
public async Task TestAssertEqualNotEqual()
2323
{
2424
var text = @"
2525
using System;
@@ -64,11 +64,11 @@ public void MyTest()
6464
}
6565
}
6666
";
67-
Verify(text, expected);
67+
await Verify(text, expected);
6868
}
6969

7070
[Fact]
71-
public void TestAssertEqualNotEqualNull()
71+
public async Task TestAssertEqualNotEqualNull()
7272
{
7373
var text = @"
7474
using System;
@@ -107,11 +107,11 @@ public void MyTest()
107107
}
108108
}
109109
";
110-
Verify(text, expected);
110+
await Verify(text, expected);
111111
}
112112

113113
[Fact]
114-
public void TestAssertEqualNotEqualTrueOrFalse()
114+
public async Task TestAssertEqualNotEqualTrueOrFalse()
115115
{
116116
var text = @"
117117
using System;
@@ -158,11 +158,11 @@ public void MyTest()
158158
}
159159
}
160160
";
161-
Verify(text, expected);
161+
await Verify(text, expected);
162162
}
163163

164164
[Fact]
165-
public void TestAssertEqualNotEqualNegation()
165+
public async Task TestAssertEqualNotEqualNegation()
166166
{
167167
var text = @"
168168
using System;
@@ -195,7 +195,7 @@ public void MyTest()
195195
}
196196
}
197197
";
198-
Verify(text, expected);
198+
await Verify(text, expected);
199199
}
200200
}
201201
}

0 commit comments

Comments
 (0)