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

Commit 659a740

Browse files
committed
Ported Assert rules to xunit converter
1 parent b9cbfdb commit 659a740

9 files changed

+502
-81
lines changed
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 XUnitConverterTests
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: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
using Microsoft.CodeAnalysis;
10+
using Xunit;
11+
12+
namespace XUnitConverterTests
13+
{
14+
public class TestAssertTrueOrFalseRuleTests : ConverterTestBase
15+
{
16+
protected override XUnitConverter.ConverterBase CreateConverter()
17+
{
18+
return new XUnitConverter.TestAssertTrueOrFalseConverter();
19+
}
20+
21+
[Fact]
22+
public async Task TestAssertEqualNotEqual()
23+
{
24+
var text = @"
25+
using System;
26+
using Xunit;
27+
28+
namespace System.UnitTests
29+
{
30+
public class Testing
31+
{
32+
[Fact]
33+
public void MyTest()
34+
{
35+
int x1 = 123, x2 = 456;
36+
Assert.True(x1 == x2);
37+
Assert.True(x2 != x1, ""Message"");
38+
39+
Assert.False(x2 == x1, ""Message"");
40+
Assert.False(x1 != x2);
41+
}
42+
}
43+
}
44+
";
45+
46+
var expected = @"
47+
using System;
48+
using Xunit;
49+
50+
namespace System.UnitTests
51+
{
52+
public class Testing
53+
{
54+
[Fact]
55+
public void MyTest()
56+
{
57+
int x1 = 123, x2 = 456;
58+
Assert.Equal(x1, x2);
59+
Assert.NotEqual(x2, x1);
60+
61+
Assert.NotEqual(x2, x1);
62+
Assert.Equal(x1, x2);
63+
}
64+
}
65+
}
66+
";
67+
await Verify(text, expected);
68+
}
69+
70+
[Fact]
71+
public async Task TestAssertEqualNotEqualNull()
72+
{
73+
var text = @"
74+
using System;
75+
using Xunit;
76+
77+
public class Testing
78+
{
79+
[Fact]
80+
public void MyTest()
81+
{
82+
string s1 = ""A"", s2 = null;
83+
Assert.True(s1 == null);
84+
Assert.True(null != s2, ""Message"");
85+
86+
Assert.False(null == s1, ""Message"");
87+
Assert.False(s2 != null);
88+
}
89+
}
90+
";
91+
92+
var expected = @"
93+
using System;
94+
using Xunit;
95+
96+
public class Testing
97+
{
98+
[Fact]
99+
public void MyTest()
100+
{
101+
string s1 = ""A"", s2 = null;
102+
Assert.Null(s1);
103+
Assert.NotNull(s2);
104+
105+
Assert.NotNull(s1);
106+
Assert.Null(s2);
107+
}
108+
}
109+
";
110+
await Verify(text, expected);
111+
}
112+
113+
[Fact]
114+
public async Task TestAssertEqualNotEqualTrueOrFalse()
115+
{
116+
var text = @"
117+
using System;
118+
using Xunit;
119+
120+
public class Testing
121+
{
122+
[Fact]
123+
public void MyTest()
124+
{
125+
bool b1 = true, b2 = false;
126+
Assert.True(b1 == true);
127+
Assert.True(true != b2);
128+
Assert.True(b2 == false);
129+
Assert.True(false != b1);
130+
131+
Assert.False(b1 == true);
132+
Assert.False(true != b2);
133+
Assert.False(b2 == false);
134+
Assert.False(false != b1);
135+
}
136+
}
137+
";
138+
139+
var expected = @"
140+
using System;
141+
using Xunit;
142+
143+
public class Testing
144+
{
145+
[Fact]
146+
public void MyTest()
147+
{
148+
bool b1 = true, b2 = false;
149+
Assert.True(b1);
150+
Assert.False(b2);
151+
Assert.False(b2);
152+
Assert.True(b1);
153+
154+
Assert.False(b1);
155+
Assert.True(b2);
156+
Assert.True(b2);
157+
Assert.False(b1);
158+
}
159+
}
160+
";
161+
await Verify(text, expected);
162+
}
163+
164+
[Fact]
165+
public async Task TestAssertEqualNotEqualNegation()
166+
{
167+
var text = @"
168+
using System;
169+
using Xunit;
170+
171+
public class Testing
172+
{
173+
[Fact]
174+
public void MyTest()
175+
{
176+
bool b1 = true, b2 = false;
177+
Assert.True(!b1);
178+
Assert.False(!b2);
179+
}
180+
}
181+
";
182+
183+
var expected = @"
184+
using System;
185+
using Xunit;
186+
187+
public class Testing
188+
{
189+
[Fact]
190+
public void MyTest()
191+
{
192+
bool b1 = true, b2 = false;
193+
Assert.False(b1);
194+
Assert.True(b2);
195+
}
196+
}
197+
";
198+
await Verify(text, expected);
199+
}
200+
}
201+
}

src/XUnitConverter.Tests/UsesXunitForTestsFormattingRuleTests.cs

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -14,61 +14,11 @@
1414

1515
namespace XUnitConverterTests
1616
{
17-
public class UsesXunitForTestsFormattingRuleTests
17+
public class UsesXunitForTestsFormattingRuleTests : ConverterTestBase
1818
{
19-
private static readonly MetadataReference s_CorlibReference = MetadataReference.CreateFromAssembly(typeof(object).Assembly);
20-
private static readonly MetadataReference s_SystemCoreReference = MetadataReference.CreateFromAssembly(typeof(Enumerable).Assembly);
21-
private static readonly MetadataReference s_MSTestReference = MetadataReference.CreateFromAssembly(typeof(Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute).Assembly);
22-
private static readonly MetadataReference s_XunitReference = MetadataReference.CreateFromAssembly(typeof(FactAttribute).Assembly);
23-
24-
private async Task<Project> RunConverter(Project project, bool runFormatter)
25-
{
26-
var xunitConverter = new XUnitConverter.XUnitConverter();
27-
var solution = await xunitConverter.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-
private async Task Verify(string text, string expected, bool runFormatter = true)
19+
protected override XUnitConverter.ConverterBase CreateConverter()
6720
{
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());
21+
return new XUnitConverter.XUnitConverter();
7222
}
7323

7424
[Fact]

src/XUnitConverter.Tests/XUnitConverter.Tests.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@
103103
</Reference>
104104
</ItemGroup>
105105
<ItemGroup>
106+
<Compile Include="ConverterTestBase.cs" />
106107
<Compile Include="Properties\AssemblyInfo.cs" />
108+
<Compile Include="TestAssertTrueOrFalseRuleTests.cs" />
107109
<Compile Include="UsesXunitForTestsFormattingRuleTests.cs" />
108110
</ItemGroup>
109111
<ItemGroup>
@@ -136,4 +138,4 @@
136138
<Target Name="AfterBuild">
137139
</Target>
138140
-->
139-
</Project>
141+
</Project>

src/XUnitConverter/ConverterBase.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Microsoft.CodeAnalysis;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
9+
namespace XUnitConverter
10+
{
11+
public abstract class ConverterBase
12+
{
13+
public async Task<Solution> ProcessAsync(Project project, CancellationToken cancellationToken)
14+
{
15+
var solution = project.Solution;
16+
foreach (var id in project.DocumentIds)
17+
{
18+
var document = solution.GetDocument(id);
19+
var syntaxNode = await document.GetSyntaxRootAsync(cancellationToken);
20+
if (syntaxNode == null)
21+
{
22+
continue;
23+
}
24+
25+
solution = await ProcessAsync(document, syntaxNode, cancellationToken);
26+
}
27+
28+
return solution;
29+
}
30+
31+
protected abstract Task<Solution> ProcessAsync(Document document, SyntaxNode syntaxNode, CancellationToken cancellationToken);
32+
}
33+
}

0 commit comments

Comments
 (0)