|
| 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 | +using System.Threading; |
| 12 | +using Microsoft.CodeAnalysis.Formatting; |
| 13 | + |
| 14 | +namespace XUnitConverterTests |
| 15 | +{ |
| 16 | + public class UsesXunitForTestsFormattingRuleTests |
| 17 | + { |
| 18 | + private static readonly MetadataReference s_MSTestReference = MetadataReference.CreateFromAssembly(typeof(Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute).Assembly); |
| 19 | + private static readonly MetadataReference s_XunitReference = MetadataReference.CreateFromAssembly(typeof(FactAttribute).Assembly); |
| 20 | + |
| 21 | + private CustomWorkspace CreateWorkspace() |
| 22 | + { |
| 23 | + return null; |
| 24 | + } |
| 25 | + |
| 26 | + private async Task<Solution> RunConverter(Project project, bool runFormatter) |
| 27 | + { |
| 28 | + var xunitConverter = new XUnitConverter.XUnitConverter(); |
| 29 | + var solution = await xunitConverter.ProcessAsync(project, CancellationToken.None); |
| 30 | + |
| 31 | + if (runFormatter) |
| 32 | + { |
| 33 | + foreach (var id in project.DocumentIds) |
| 34 | + { |
| 35 | + var document = solution.GetDocument(id); |
| 36 | + document = await Formatter.FormatAsync(document); |
| 37 | + solution = document.Project.Solution; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return solution; |
| 42 | + } |
| 43 | + |
| 44 | + /* |
| 45 | + protected override IEnumerable<MetadataReference> GetSolutionMetadataReferences() |
| 46 | + { |
| 47 | + return base.GetSolutionMetadataReferences() |
| 48 | + .Concat(new[] { |
| 49 | + s_MSTestReference, |
| 50 | + s_XunitReference |
| 51 | + }); |
| 52 | + } |
| 53 | + */ |
| 54 | + |
| 55 | + [Fact] |
| 56 | + public void TestUpdatesUsingStatements() |
| 57 | + { |
| 58 | + var text = @" |
| 59 | +using System; |
| 60 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 61 | +
|
| 62 | +namespace System.Composition.UnitTests |
| 63 | +{ |
| 64 | +} |
| 65 | +"; |
| 66 | + |
| 67 | + var expected = @" |
| 68 | +using System; |
| 69 | +using Xunit; |
| 70 | +
|
| 71 | +namespace System.Composition.UnitTests |
| 72 | +{ |
| 73 | +} |
| 74 | +"; |
| 75 | + Verify(text, expected); |
| 76 | + } |
| 77 | + |
| 78 | + [Fact] |
| 79 | + public void TestUpdatesUsingStatementsWithIfDefs() |
| 80 | + { |
| 81 | + var text = @" |
| 82 | +using System; |
| 83 | +#if NETFX_CORE |
| 84 | +using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; |
| 85 | +#elif PORTABLE_TESTS |
| 86 | +using Microsoft.Bcl.Testing; |
| 87 | +#else |
| 88 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 89 | +
|
| 90 | +#endif |
| 91 | +namespace System.Composition.UnitTests |
| 92 | +{ |
| 93 | +} |
| 94 | +"; |
| 95 | + |
| 96 | + var expected = @" |
| 97 | +using System; |
| 98 | +using Xunit; |
| 99 | +
|
| 100 | +namespace System.Composition.UnitTests |
| 101 | +{ |
| 102 | +} |
| 103 | +"; |
| 104 | + Verify(text, expected); |
| 105 | + } |
| 106 | + |
| 107 | + [Fact] |
| 108 | + public void TestRemovesTestClassAttributes() |
| 109 | + { |
| 110 | + var text = @" |
| 111 | +using System; |
| 112 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 113 | +
|
| 114 | +namespace System.Composition.UnitTests |
| 115 | +{ |
| 116 | + [TestClass] |
| 117 | + public class MyTestClass |
| 118 | + { |
| 119 | + } |
| 120 | +} |
| 121 | +"; |
| 122 | + |
| 123 | + var expected = @" |
| 124 | +using System; |
| 125 | +using Xunit; |
| 126 | +
|
| 127 | +namespace System.Composition.UnitTests |
| 128 | +{ |
| 129 | + public class MyTestClass |
| 130 | + { |
| 131 | + } |
| 132 | +} |
| 133 | +"; |
| 134 | + Verify(text, expected); |
| 135 | + } |
| 136 | + |
| 137 | + [Fact] |
| 138 | + public void TestUpdatesTestMethodAttributes() |
| 139 | + { |
| 140 | + var text = @" |
| 141 | +using System; |
| 142 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 143 | +
|
| 144 | +namespace System.Composition.UnitTests |
| 145 | +{ |
| 146 | + public class MyTestClass |
| 147 | + { |
| 148 | + [TestMethod] |
| 149 | + public void MyTestMethod() |
| 150 | + { |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | +"; |
| 155 | + |
| 156 | + var expected = @" |
| 157 | +using System; |
| 158 | +using Xunit; |
| 159 | +
|
| 160 | +namespace System.Composition.UnitTests |
| 161 | +{ |
| 162 | + public class MyTestClass |
| 163 | + { |
| 164 | + [Fact] |
| 165 | + public void MyTestMethod() |
| 166 | + { |
| 167 | + } |
| 168 | + } |
| 169 | +} |
| 170 | +"; |
| 171 | + Verify(text, expected); |
| 172 | + } |
| 173 | + |
| 174 | + [Fact] |
| 175 | + public void TestUpdatesAsserts() |
| 176 | + { |
| 177 | + var text = @" |
| 178 | +using System; |
| 179 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 180 | +
|
| 181 | +namespace System.Composition.UnitTests |
| 182 | +{ |
| 183 | + public class MyTestClass |
| 184 | + { |
| 185 | + public void MyTestMethod() |
| 186 | + { |
| 187 | + object obj = new object(); |
| 188 | +
|
| 189 | + Assert.AreEqual(1, 1); |
| 190 | + Assert.AreNotEqual(1, 2); |
| 191 | + Assert.IsNull(null); |
| 192 | + Assert.IsNotNull(obj); |
| 193 | + Assert.AreSame(obj, obj); |
| 194 | + Assert.AreNotSame(obj, new object()); |
| 195 | + Assert.IsTrue(true); |
| 196 | + Assert.IsFalse(false); |
| 197 | + Assert.IsInstanceOfType(string.Empty, typeof(String)); |
| 198 | + } |
| 199 | + } |
| 200 | +} |
| 201 | +"; |
| 202 | + |
| 203 | + var expected = @" |
| 204 | +using System; |
| 205 | +using Xunit; |
| 206 | +
|
| 207 | +namespace System.Composition.UnitTests |
| 208 | +{ |
| 209 | + public class MyTestClass |
| 210 | + { |
| 211 | + public void MyTestMethod() |
| 212 | + { |
| 213 | + object obj = new object(); |
| 214 | +
|
| 215 | + Assert.Equal(1, 1); |
| 216 | + Assert.NotEqual(1, 2); |
| 217 | + Assert.Null(null); |
| 218 | + Assert.NotNull(obj); |
| 219 | + Assert.Same(obj, obj); |
| 220 | + Assert.NotSame(obj, new object()); |
| 221 | + Assert.True(true); |
| 222 | + Assert.False(false); |
| 223 | + Assert.IsAssignableFrom(typeof(String), string.Empty); |
| 224 | + } |
| 225 | + } |
| 226 | +} |
| 227 | +"; |
| 228 | + Verify(text, expected); |
| 229 | + } |
| 230 | + } |
| 231 | +} |
0 commit comments