Skip to content

Commit 82b8bc0

Browse files
authored
Global code refactor (#167)
***NO_CI***
1 parent e9e4ebe commit 82b8bc0

File tree

9 files changed

+281
-230
lines changed

9 files changed

+281
-230
lines changed

poc/TestOfTestFrameworkByReference/NFUnitTestByReference.nfproj

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<ItemGroup>
4-
<ProjectCapability Include="TestContainer" />
5-
</ItemGroup>
63
<PropertyGroup Label="Globals">
74
<NanoFrameworkProjectSystemPath>$(MSBuildExtensionsPath)\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
85
</PropertyGroup>
@@ -43,10 +40,10 @@
4340
</ItemGroup>
4441
<ItemGroup>
4542
<Reference Include="mscorlib">
46-
<HintPath>..\packages\nanoFramework.CoreLibrary.1.12.0\lib\mscorlib.dll</HintPath>
43+
<HintPath>..\..\packages\nanoFramework.CoreLibrary.1.12.0\lib\mscorlib.dll</HintPath>
4744
</Reference>
4845
<Reference Include="nanoFramework.Runtime.Native">
49-
<HintPath>..\packages\nanoFramework.Runtime.Native.1.5.4\lib\nanoFramework.Runtime.Native.dll</HintPath>
46+
<HintPath>..\..\packages\nanoFramework.Runtime.Native.1.5.4\lib\nanoFramework.Runtime.Native.dll</HintPath>
5047
</Reference>
5148
</ItemGroup>
5249
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />

poc/TestOfTestFrameworkByReference/nano.runsettings

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
<MaxCpuCount>1</MaxCpuCount>
66
<ResultsDirectory>.\TestResults</ResultsDirectory><!-- Path relative to solution directory -->
77
<TestSessionTimeout>120000</TestSessionTimeout><!-- Milliseconds -->
8-
<TargetFrameworkVersion>Framework40</TargetFrameworkVersion>
8+
<TargetFrameworkVersion>net48</TargetFrameworkVersion>
9+
<TargetPlatform>x64</TargetPlatform>
910
<!-- Comment the following line or adjust it if you want to test the TestAdapter. Path has to be absolute or relative to the build folder of the test dll -->
1011
<TestAdaptersPaths>E:\GitHub\nf-nanoFramework.TestFramework\source\TestAdapter\bin\Debug\net4.8</TestAdaptersPaths>
1112
</RunConfiguration>

source/TestAdapter/Discover.cs

Lines changed: 70 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -117,41 +117,34 @@ public static List<TestCase> FindTestCases(string source)
117117
AppDomain.CurrentDomain.AssemblyResolve += App_AssemblyResolve;
118118
AppDomain.CurrentDomain.Load(test.GetName());
119119

120-
Type[] allTypes = test.GetTypes();
120+
var allTypes = test.GetTypes().Where(x=> x.IsClass);
121121
foreach (var type in allTypes)
122122
{
123-
if (type.IsClass)
123+
if (!type.IsClass)
124124
{
125-
var typeAttribs = type.GetCustomAttributes(true);
126-
foreach (var typeAttrib in typeAttribs)
125+
continue;
126+
}
127+
128+
var typeAttribs = type.GetCustomAttributes(true)
129+
.Where(x => x.GetType().FullName == typeof(TestClassAttribute).GetType().FullName);
130+
foreach (var typeAttrib in typeAttribs)
131+
{
132+
var methods = type.GetMethods();
133+
// First we look at Setup
134+
foreach (var method in methods)
127135
{
128-
if (typeof(TestClassAttribute).FullName == typeAttrib.GetType().FullName)
136+
var attribs = method.GetCustomAttributes(true);
137+
attribs = Helper.RemoveTestMethodIfDataRowExists(attribs);
138+
var attribsToItterate = attribs.Where(x => IsTestMethod(x)).ToArray();
139+
for (int i = 0; i < attribsToItterate.Length; i++)
129140
{
130-
var methods = type.GetMethods();
131-
// First we look at Setup
132-
foreach (var method in methods)
133-
{
134-
var attribs = method.GetCustomAttributes(true);
135-
attribs = Helper.RemoveTestMethodIfDataRowExists(attribs);
136-
for (int i = 0; i < attribs.Length; i++)
137-
{
138-
var attrib = attribs[i];
139-
140-
if (attrib.GetType().FullName == typeof(SetupAttribute).FullName ||
141-
attrib.GetType().FullName == typeof(TestMethodAttribute).FullName ||
142-
attrib.GetType().FullName == typeof(CleanupAttribute).FullName ||
143-
attrib.GetType().FullName == typeof(DataRowAttribute).FullName)
144-
{
145-
var testCase = GetFileNameAndLineNumber(allCsFils, type, method, attrib, i);
146-
testCase.Source = source;
147-
testCase.ExecutorUri = new Uri(TestsConstants.NanoExecutor);
148-
testCase.FullyQualifiedName = $"{type.FullName}.{testCase.DisplayName}";
149-
testCase.Traits.Add(new Trait("Type", attrib.GetType().Name.Replace("Attribute", "")));
150-
testCases.Add(testCase);
151-
}
152-
}
153-
}
154-
141+
var attrib = attribsToItterate[i];
142+
var testCase = GetFileNameAndLineNumber(allCsFils, type, method, attrib, i);
143+
testCase.Source = source;
144+
testCase.ExecutorUri = new Uri(TestsConstants.NanoExecutor);
145+
testCase.FullyQualifiedName = $"{type.FullName}.{testCase.DisplayName}";
146+
testCase.Traits.Add(new Trait("Type", attrib.GetType().Name.Replace("Attribute", "")));
147+
testCases.Add(testCase);
155148
}
156149
}
157150
}
@@ -160,6 +153,33 @@ public static List<TestCase> FindTestCases(string source)
160153
return testCases;
161154
}
162155

156+
private static bool IsTestMethod(object attrib)
157+
{
158+
var attributeName = attrib.GetType().FullName;
159+
160+
if (attributeName == typeof(SetupAttribute).FullName)
161+
{
162+
return true;
163+
}
164+
165+
if (attributeName == typeof(TestMethodAttribute).FullName)
166+
{
167+
return true;
168+
}
169+
170+
if (attributeName == typeof(CleanupAttribute).FullName)
171+
{
172+
return true;
173+
}
174+
175+
if (attributeName == typeof(DataRowAttribute).FullName)
176+
{
177+
return true;
178+
}
179+
180+
return false;
181+
}
182+
163183
private static Assembly App_AssemblyResolve(object sender, ResolveEventArgs args)
164184
{
165185
try
@@ -233,25 +253,29 @@ private static TestCase GetFileNameAndLineNumber(string[] csFiles, Type classNam
233253
{
234254
StreamReader sr = new StreamReader(csFile);
235255
var allFile = sr.ReadToEnd();
236-
if (allFile.Contains($"class {clName}"))
256+
if (!allFile.Contains($"class {clName}"))
257+
{
258+
continue;
259+
}
260+
261+
if (!allFile.Contains($" {methodName}("))
262+
{
263+
continue;
264+
}
265+
266+
// We found it!
267+
int lineNum = 1;
268+
foreach (var line in allFile.Split('\r'))
237269
{
238-
if (allFile.Contains($" {methodName}("))
270+
if (line.Contains($" {methodName}("))
239271
{
240-
// We found it!
241-
int lineNum = 1;
242-
foreach (var line in allFile.Split('\r'))
243-
{
244-
if (line.Contains($" {methodName}("))
245-
{
246-
flret.CodeFilePath = csFile;
247-
flret.LineNumber = lineNum;
248-
flret.DisplayName = Helper.GetTestDisplayName(method, attribute, attributeIndex);
249-
return flret;
250-
}
251-
252-
lineNum++;
253-
}
272+
flret.CodeFilePath = csFile;
273+
flret.LineNumber = lineNum;
274+
flret.DisplayName = Helper.GetTestDisplayName(method, attribute, attributeIndex);
275+
return flret;
254276
}
277+
278+
lineNum++;
255279
}
256280
}
257281

source/TestAdapter/Executor.cs

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -138,43 +138,45 @@ public void RunTests(IEnumerable<TestCase> tests, IRunContext runContext, IFrame
138138

139139
private void InitializeLogger(IRunContext runContext, IFrameworkHandle frameworkHandle)
140140
{
141-
if (_logger == null)
141+
if (_logger != null)
142142
{
143-
var settingsProvider = runContext.RunSettings.GetSettings(TestsConstants.SettingsName) as SettingsProvider;
143+
return;
144+
}
145+
146+
var settingsProvider = runContext.RunSettings.GetSettings(TestsConstants.SettingsName) as SettingsProvider;
144147

145-
_logger = new LogMessenger(frameworkHandle, settingsProvider);
148+
_logger = new LogMessenger(frameworkHandle, settingsProvider);
146149

147-
if (settingsProvider != null)
150+
if (settingsProvider != null)
151+
{
152+
// get TestSessionTimeout from runsettings
153+
var xml = new XmlDocument();
154+
xml.LoadXml(runContext.RunSettings.SettingsXml);
155+
var timeout = xml.SelectSingleNode("RunSettings//RunConfiguration//TestSessionTimeout");
156+
if (timeout != null && timeout.NodeType == XmlNodeType.Element)
148157
{
149-
// get TestSessionTimeout from runsettings
150-
var xml = new XmlDocument();
151-
xml.LoadXml(runContext.RunSettings.SettingsXml);
152-
var timeout = xml.SelectSingleNode("RunSettings//RunConfiguration//TestSessionTimeout");
153-
if (timeout != null && timeout.NodeType == XmlNodeType.Element)
154-
{
155-
int.TryParse(timeout.InnerText, out _testSessionTimeout);
156-
}
158+
int.TryParse(timeout.InnerText, out _testSessionTimeout);
159+
}
157160

158-
_settings = settingsProvider.Settings;
161+
_settings = settingsProvider.Settings;
159162

160-
_logger.LogMessage(
161-
"Getting ready to run tests...",
162-
Settings.LoggingLevel.Detailed);
163+
_logger.LogMessage(
164+
"Getting ready to run tests...",
165+
Settings.LoggingLevel.Detailed);
163166

164-
_logger.LogMessage(
165-
"Settings parsed",
166-
Settings.LoggingLevel.Verbose);
167-
}
168-
else
169-
{
170-
_logger.LogMessage(
171-
"Getting ready to run tests...",
172-
Settings.LoggingLevel.Detailed);
167+
_logger.LogMessage(
168+
"Settings parsed",
169+
Settings.LoggingLevel.Verbose);
170+
}
171+
else
172+
{
173+
_logger.LogMessage(
174+
"Getting ready to run tests...",
175+
Settings.LoggingLevel.Detailed);
173176

174-
_logger.LogMessage(
175-
"No settings for nanoFramework adapter",
176-
Settings.LoggingLevel.Verbose);
177-
}
177+
_logger.LogMessage(
178+
"No settings for nanoFramework adapter",
179+
Settings.LoggingLevel.Verbose);
178180
}
179181
}
180182

source/TestAdapter/TestObjectHelper.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@ public static string GetNanoClrLocation()
2828
{
2929
return nanoClrFullPath;
3030
}
31-
else
32-
{
33-
var inititialDir = new DirectoryInfo(Path.GetDirectoryName(thisAssemblyDir));
34-
return FindNanoClr(inititialDir);
35-
}
31+
32+
var inititialDir = new DirectoryInfo(Path.GetDirectoryName(thisAssemblyDir));
33+
return FindNanoClr(inititialDir);
3634
}
3735

3836
private static string FindNanoClr(DirectoryInfo initialPath)
@@ -45,13 +43,10 @@ private static string FindNanoClr(DirectoryInfo initialPath)
4543
{
4644
return findnanoClr.First().FullName;
4745
}
48-
else
49-
{
50-
return FindNanoClr(dir);
51-
}
46+
return FindNanoClr(dir);
5247
}
5348

54-
return string.Empty;
49+
throw new FileNotFoundException($"Unable to find nanoCLR.");
5550
}
5651
}
5752
}

0 commit comments

Comments
 (0)