Skip to content

Commit af4efda

Browse files
authored
Various styling refactoring (#1334)
1 parent ccf307e commit af4efda

38 files changed

+629
-755
lines changed

src/Adapter/MSTest.TestAdapter/Discovery/TypeValidator.cs

+80-80
Original file line numberDiff line numberDiff line change
@@ -52,52 +52,52 @@ internal TypeValidator(ReflectHelper reflectHelper, bool discoverInternals)
5252
/// <returns>Return true if it is a valid test class.</returns>
5353
internal virtual bool IsValidTestClass(Type type, ICollection<string> warnings)
5454
{
55-
if (type.GetTypeInfo().IsClass &&
56-
(_reflectHelper.IsAttributeDefined(type, typeof(TestClassAttribute), false) ||
57-
_reflectHelper.HasAttributeDerivedFrom(type, typeof(TestClassAttribute), false)))
55+
if (!type.GetTypeInfo().IsClass
56+
|| (!_reflectHelper.IsAttributeDefined(type, typeof(TestClassAttribute), false)
57+
&& !_reflectHelper.HasAttributeDerivedFrom(type, typeof(TestClassAttribute), false)))
5858
{
59-
// inaccessible class
60-
if (!TypeHasValidAccessibility(type.GetTypeInfo(), _discoverInternals))
61-
{
62-
var warning = string.Format(CultureInfo.CurrentCulture, Resource.UTA_ErrorNonPublicTestClass, type.FullName);
63-
warnings.Add(warning);
64-
return false;
65-
}
59+
return false;
60+
}
6661

67-
// Generic class
68-
if (type.GetTypeInfo().IsGenericTypeDefinition && !type.GetTypeInfo().IsAbstract)
69-
{
70-
// In IDE generic classes that are not abstract are treated as not runnable. Keep consistence.
71-
var warning = string.Format(CultureInfo.CurrentCulture, Resource.UTA_ErrorNonPublicTestClass, type.FullName);
72-
warnings.Add(warning);
73-
return false;
74-
}
62+
// inaccessible class
63+
if (!TypeHasValidAccessibility(type.GetTypeInfo(), _discoverInternals))
64+
{
65+
var warning = string.Format(CultureInfo.CurrentCulture, Resource.UTA_ErrorNonPublicTestClass, type.FullName);
66+
warnings.Add(warning);
67+
return false;
68+
}
7569

76-
// Class is not valid if the testContext property is incorrect
77-
if (!HasCorrectTestContextSignature(type))
78-
{
79-
var warning = string.Format(CultureInfo.CurrentCulture, Resource.UTA_ErrorInValidTestContextSignature, type.FullName);
80-
warnings.Add(warning);
81-
return false;
82-
}
70+
// Generic class
71+
if (type.GetTypeInfo().IsGenericTypeDefinition && !type.GetTypeInfo().IsAbstract)
72+
{
73+
// In IDE generic classes that are not abstract are treated as not runnable. Keep consistence.
74+
var warning = string.Format(CultureInfo.CurrentCulture, Resource.UTA_ErrorNonPublicTestClass, type.FullName);
75+
warnings.Add(warning);
76+
return false;
77+
}
8378

84-
// Abstract test classes can be base classes for derived test classes.
85-
// There is no way to see if there are derived test classes.
86-
// Thus if a test class is abstract, just ignore all test methods from it
87-
// (they will be visible in derived classes). No warnings (such as test method, deployment item,
88-
// etc attribute is defined on the class) will be generated for this class:
89-
// What we do is:
90-
// - report the class as "not valid" test class. This will cause to skip enumerating tests from it.
91-
// - Do not generate warnings/do not create NOT RUNNABLE tests.
92-
if (type.GetTypeInfo().IsAbstract)
93-
{
94-
return false;
95-
}
79+
// Class is not valid if the testContext property is incorrect
80+
if (!HasCorrectTestContextSignature(type))
81+
{
82+
var warning = string.Format(CultureInfo.CurrentCulture, Resource.UTA_ErrorInValidTestContextSignature, type.FullName);
83+
warnings.Add(warning);
84+
return false;
85+
}
9686

97-
return true;
87+
// Abstract test classes can be base classes for derived test classes.
88+
// There is no way to see if there are derived test classes.
89+
// Thus if a test class is abstract, just ignore all test methods from it
90+
// (they will be visible in derived classes). No warnings (such as test method, deployment item,
91+
// etc attribute is defined on the class) will be generated for this class:
92+
// What we do is:
93+
// - report the class as "not valid" test class. This will cause to skip enumerating tests from it.
94+
// - Do not generate warnings/do not create NOT RUNNABLE tests.
95+
if (type.GetTypeInfo().IsAbstract)
96+
{
97+
return false;
9898
}
9999

100-
return false;
100+
return true;
101101
}
102102

103103
/// <summary>
@@ -159,56 +159,56 @@ internal static bool TypeHasValidAccessibility(TypeInfo type, bool discoverInter
159159
}
160160

161161
// Either the type is not public or it is a nested class and itself or one of its containers is not public.
162-
if (type.IsNested)
162+
if (!type.IsNested)
163163
{
164-
// Assembly is CLR term for internal visibility:
165-
// Private == private,
166-
// FamilyANDAssembly == private protected,
167-
// Assembly == internal,
168-
// Family == protected,
169-
// FamilyORAssembly == protected internal,
170-
// Public == public.
171-
// So this reads IsNestedInternal || IsNestedPublic:
172-
var isNestedPublicOrInternal = type.IsNestedAssembly || type.IsNestedPublic;
173-
174-
if (!isNestedPublicOrInternal)
175-
{
176-
// This type is nested, but is not public or internal.
177-
return false;
178-
}
164+
// The type is not public and is not nested. Non-nested types can be only public or internal
165+
// so this type must be internal.
166+
return true;
167+
}
179168

180-
// The type itself is nested and is public, or internal, but could be in hierarchy of types
181-
// where some of the parent types is private (or other modifier that is not public and is not internal)
182-
// if we looked for just public types we could just look at IsVisible, but internal type nested in internal type
183-
// is not Visible, so we need to check all the parents and make sure they are all either public or internal.
184-
var parentsArePublicOrInternal = true;
185-
var declaringType = type.DeclaringType;
186-
while (declaringType != null && parentsArePublicOrInternal)
187-
{
188-
var declaringTypeIsPublicOrInternal =
169+
// Assembly is CLR term for internal visibility:
170+
// Private == private,
171+
// FamilyANDAssembly == private protected,
172+
// Assembly == internal,
173+
// Family == protected,
174+
// FamilyORAssembly == protected internal,
175+
// Public == public.
176+
// So this reads IsNestedInternal || IsNestedPublic:
177+
var isNestedPublicOrInternal = type.IsNestedAssembly || type.IsNestedPublic;
178+
179+
if (!isNestedPublicOrInternal)
180+
{
181+
// This type is nested, but is not public or internal.
182+
return false;
183+
}
189184

190-
// Declaring type is non-nested type, and we are looking for internal or public, which are the only
191-
// two valid options that non-nested type can be.
192-
!declaringType.IsNested
185+
// The type itself is nested and is public, or internal, but could be in hierarchy of types
186+
// where some of the parent types is private (or other modifier that is not public and is not internal)
187+
// if we looked for just public types we could just look at IsVisible, but internal type nested in internal type
188+
// is not Visible, so we need to check all the parents and make sure they are all either public or internal.
189+
var parentsArePublicOrInternal = true;
190+
var declaringType = type.DeclaringType;
191+
while (declaringType != null && parentsArePublicOrInternal)
192+
{
193+
var declaringTypeIsPublicOrInternal =
193194

194-
// Or the type is nested internal, or nested public type, but not any other
195-
// like nested protected internal type, or nested private type.
196-
|| declaringType.GetTypeInfo().IsNestedAssembly || declaringType.GetTypeInfo().IsNestedPublic;
195+
// Declaring type is non-nested type, and we are looking for internal or public, which are the only
196+
// two valid options that non-nested type can be.
197+
!declaringType.IsNested
197198

198-
if (!declaringTypeIsPublicOrInternal)
199-
{
200-
parentsArePublicOrInternal = false;
201-
break;
202-
}
199+
// Or the type is nested internal, or nested public type, but not any other
200+
// like nested protected internal type, or nested private type.
201+
|| declaringType.GetTypeInfo().IsNestedAssembly || declaringType.GetTypeInfo().IsNestedPublic;
203202

204-
declaringType = declaringType.DeclaringType;
203+
if (!declaringTypeIsPublicOrInternal)
204+
{
205+
parentsArePublicOrInternal = false;
206+
break;
205207
}
206208

207-
return parentsArePublicOrInternal;
209+
declaringType = declaringType.DeclaringType;
208210
}
209211

210-
// The type is not public and is not nested. Non-nested types can be only public or internal
211-
// so this type must be internal.
212-
return true;
212+
return parentsArePublicOrInternal;
213213
}
214214
}

src/Adapter/MSTest.TestAdapter/Discovery/UnitTestDiscoverer.cs

+44-39
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Discovery;
88
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel;
9-
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
109
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
1110
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
1211

@@ -114,45 +113,51 @@ internal void SendTestCases(string source, IEnumerable<UnitTestElement> testElem
114113
continue;
115114
}
116115

117-
if (shouldCollectSourceInformation)
116+
if (!shouldCollectSourceInformation)
118117
{
119-
string testSource = testElement.TestMethod.DeclaringAssemblyName ?? source;
120-
121-
if (!navigationSessions.TryGetValue(testSource, out var testNavigationSession))
122-
{
123-
testNavigationSession = PlatformServiceProvider.Instance.FileOperations.CreateNavigationSession(testSource);
124-
navigationSessions.Add(testSource, testNavigationSession);
125-
}
126-
127-
if (testNavigationSession != null)
128-
{
129-
var className = testElement.TestMethod.DeclaringClassFullName
130-
?? testElement.TestMethod.FullClassName;
131-
132-
var methodName = testElement.TestMethod.Name;
133-
134-
// If it is async test method use compiler generated type and method name for navigation data.
135-
if (!string.IsNullOrEmpty(testElement.AsyncTypeName))
136-
{
137-
className = testElement.AsyncTypeName;
138-
139-
// compiler generated method name is "MoveNext".
140-
methodName = "MoveNext";
141-
}
142-
143-
PlatformServiceProvider.Instance.FileOperations.GetNavigationData(
144-
testNavigationSession,
145-
className,
146-
methodName,
147-
out var minLineNumber,
148-
out var fileName);
149-
150-
if (!string.IsNullOrEmpty(fileName))
151-
{
152-
testCase.LineNumber = minLineNumber;
153-
testCase.CodeFilePath = fileName;
154-
}
155-
}
118+
discoverySink.SendTestCase(testCase);
119+
continue;
120+
}
121+
122+
string testSource = testElement.TestMethod.DeclaringAssemblyName ?? source;
123+
124+
if (!navigationSessions.TryGetValue(testSource, out var testNavigationSession))
125+
{
126+
testNavigationSession = PlatformServiceProvider.Instance.FileOperations.CreateNavigationSession(testSource);
127+
navigationSessions.Add(testSource, testNavigationSession);
128+
}
129+
130+
if (testNavigationSession == null)
131+
{
132+
discoverySink.SendTestCase(testCase);
133+
continue;
134+
}
135+
136+
var className = testElement.TestMethod.DeclaringClassFullName
137+
?? testElement.TestMethod.FullClassName;
138+
139+
var methodName = testElement.TestMethod.Name;
140+
141+
// If it is async test method use compiler generated type and method name for navigation data.
142+
if (!string.IsNullOrEmpty(testElement.AsyncTypeName))
143+
{
144+
className = testElement.AsyncTypeName;
145+
146+
// compiler generated method name is "MoveNext".
147+
methodName = "MoveNext";
148+
}
149+
150+
PlatformServiceProvider.Instance.FileOperations.GetNavigationData(
151+
testNavigationSession,
152+
className,
153+
methodName,
154+
out var minLineNumber,
155+
out var fileName);
156+
157+
if (!string.IsNullOrEmpty(fileName))
158+
{
159+
testCase.LineNumber = minLineNumber;
160+
testCase.CodeFilePath = fileName;
156161
}
157162

158163
discoverySink.SendTestCase(testCase);

0 commit comments

Comments
 (0)