Skip to content

Commit 8ca9b17

Browse files
authored
Adding tests, fixing TwinCollection (#88)
1 parent 9190107 commit 8ca9b17

File tree

7 files changed

+216
-22
lines changed

7 files changed

+216
-22
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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: AssemblyDescription("")]
9+
[assembly: AssemblyConfiguration("")]
10+
[assembly: AssemblyCompany("")]
11+
[assembly: AssemblyCopyright("Copyright (c) 2021 nanoFramework contributors")]
12+
[assembly: AssemblyTrademark("")]
13+
[assembly: AssemblyCulture("")]
14+
15+
// Setting ComVisible to false makes the types in this assembly not visible
16+
// to COM components. If you need to access a type in this assembly from
17+
// COM, set the ComVisible attribute to true on that type.
18+
[assembly: ComVisible(false)]
19+
20+
// Version information for an assembly consists of the following four values:
21+
//
22+
// Major Version
23+
// Minor Version
24+
// Build Number
25+
// Revision
26+
//
27+
// You can specify all the values or you can default the Build and Revision Numbers
28+
// by using the '*' as shown below:
29+
// [assembly: AssemblyVersion("1.0.*")]
30+
[assembly: AssemblyVersion("1.0.0.0")]
31+
[assembly: AssemblyFileVersion("1.0.0.0")]

Tests/TwinTests/TwinTest.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 nanoFramework.Azure.Devices.Shared;
5+
using nanoFramework.TestFramework;
6+
using System;
7+
using System.Collections;
8+
using System.Diagnostics;
9+
10+
namespace TwinTests
11+
{
12+
[TestClass]
13+
public class TwinTest
14+
{
15+
[TestMethod]
16+
public void TestContains()
17+
{
18+
Hashtable twinHash = new()
19+
{
20+
{ "something", "22" },
21+
{ "else", 42 },
22+
{ "null", null }
23+
};
24+
TwinCollection twin = new(twinHash);
25+
26+
Assert.True(twin.Contains("something"), "something");
27+
Assert.Equal("22", (string)twin["something"]);
28+
Assert.True(twin.Contains("else"), "else");
29+
Assert.Equal(42, (int)twin["else"]);
30+
Assert.True(twin.Contains("null"), "null");
31+
var nullresult = twin["null"];
32+
Assert.Null(nullresult);
33+
34+
Assert.False(twin.Contains("nothing"), "nothing");
35+
Assert.Null(twin["nothing"]);
36+
}
37+
38+
[TestMethod]
39+
public void TestEnumeration()
40+
{
41+
Hashtable twinHash = new()
42+
{
43+
{ "something", "22" },
44+
{ "else", 42 },
45+
{ "null", null }
46+
};
47+
TwinCollection twin = new(twinHash);
48+
49+
int count = 0;
50+
foreach (DictionaryEntry coll in twin)
51+
{
52+
if (coll.Key.ToString() == "something")
53+
{
54+
Assert.Equal("22", coll.Value.ToString());
55+
}
56+
57+
if (coll.Key.ToString() == "else")
58+
{
59+
Assert.Equal(42, (int)coll.Value);
60+
}
61+
62+
if (coll.Key.ToString() == "null")
63+
{
64+
Assert.Null(coll.Value);
65+
}
66+
67+
count++;
68+
}
69+
70+
Assert.Equal(3, count);
71+
}
72+
}
73+
}

Tests/TwinTests/TwinTests.nfproj

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Label="Globals">
4+
<NanoFrameworkProjectSystemPath>$(MSBuildExtensionsPath)\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
5+
</PropertyGroup>
6+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
7+
<ItemGroup>
8+
<ProjectCapability Include="TestContainer" />
9+
</ItemGroup>
10+
<PropertyGroup>
11+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
12+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
13+
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
14+
<ProjectGuid>50bed153-ff67-4207-adb0-2651ddadc2a3</ProjectGuid>
15+
<OutputType>Library</OutputType>
16+
<AppDesignerFolder>Properties</AppDesignerFolder>
17+
<FileAlignment>512</FileAlignment>
18+
<RootNamespace>TwinTests</RootNamespace>
19+
<AssemblyName>NFUnitTest</AssemblyName>
20+
<IsCodedUITest>False</IsCodedUITest>
21+
<IsTestProject>true</IsTestProject>
22+
<TestProjectType>UnitTest</TestProjectType>
23+
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
24+
</PropertyGroup>
25+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
26+
<PropertyGroup>
27+
<RunSettingsFilePath>$(MSBuildProjectDirectory)\nano.runsettings</RunSettingsFilePath>
28+
</PropertyGroup>
29+
<ItemGroup>
30+
<Compile Include="TwinTest.cs" />
31+
<Compile Include="Properties\AssemblyInfo.cs" />
32+
</ItemGroup>
33+
<ItemGroup>
34+
<Reference Include="mscorlib">
35+
<HintPath>..\..\packages\nanoFramework.CoreLibrary.1.12.0-preview.1\lib\mscorlib.dll</HintPath>
36+
</Reference>
37+
<Reference Include="nanoFramework.System.Collections">
38+
<HintPath>..\..\packages\nanoFramework.System.Collections.1.4.0-preview.7\lib\nanoFramework.System.Collections.dll</HintPath>
39+
</Reference>
40+
<Reference Include="nanoFramework.TestFramework">
41+
<HintPath>..\..\packages\nanoFramework.TestFramework.1.0.172\lib\nanoFramework.TestFramework.dll</HintPath>
42+
</Reference>
43+
<Reference Include="nanoFramework.UnitTestLauncher">
44+
<HintPath>..\..\packages\nanoFramework.TestFramework.1.0.172\lib\nanoFramework.UnitTestLauncher.exe</HintPath>
45+
</Reference>
46+
</ItemGroup>
47+
<ItemGroup>
48+
<None Include="nano.runsettings" />
49+
<None Include="packages.config" />
50+
</ItemGroup>
51+
<ItemGroup>
52+
<ProjectReference Include="..\..\nanoFramework.Azure.Devices.Client\Azure.Devices.Client.nfproj" />
53+
</ItemGroup>
54+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
55+
<!-- MANUAL UPDATE HERE -->
56+
<Import Project="..\packages\nanoFramework.TestFramework.1.0.166\build\nanoFramework.TestFramework.targets" Condition="Exists('..\packages\nanoFramework.TestFramework.1.0.166\build\nanoFramework.TestFramework.targets')" />
57+
<ProjectExtensions>
58+
<ProjectCapabilities>
59+
<ProjectConfigurationsDeclaredAsItems />
60+
</ProjectCapabilities>
61+
</ProjectExtensions>
62+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
63+
<PropertyGroup>
64+
<WarningText>Update the Import path in nfproj to the correct nanoFramework.TestFramework NuGet package folder.</WarningText>
65+
</PropertyGroup>
66+
<Warning Condition="!Exists('..\packages\nanoFramework.TestFramework.1.0.166\build\nanoFramework.TestFramework.targets')" Text="'$(WarningText)'" />
67+
</Target>
68+
</Project>

Tests/TwinTests/nano.runsettings

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RunSettings>
3+
<!-- Configurations that affect the Test Framework -->
4+
<RunConfiguration>
5+
<MaxCpuCount>1</MaxCpuCount>
6+
<ResultsDirectory>.\TestResults</ResultsDirectory><!-- Path relative to solution directory -->
7+
<TestSessionTimeout>120000</TestSessionTimeout><!-- Milliseconds -->
8+
<TargetFrameworkVersion>Framework40</TargetFrameworkVersion>
9+
</RunConfiguration>
10+
<nanoFrameworkAdapter>
11+
<Logging>None</Logging>
12+
<IsRealHardware>False</IsRealHardware>
13+
</nanoFrameworkAdapter>
14+
</RunSettings>

Tests/TwinTests/packages.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="nanoFramework.CoreLibrary" version="1.12.0-preview.1" targetFramework="netnanoframework10" />
4+
<package id="nanoFramework.System.Collections" version="1.4.0-preview.7" targetFramework="netnanoframework10" />
5+
<package id="nanoFramework.TestFramework" version="1.0.172" targetFramework="netnanoframework10" developmentDependency="true" />
6+
</packages>

nanoFramework.Azure.Devices.Client.sln

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Microsoft Visual Studio Solution File, Format Version 12.00
2-
# Visual Studio Version 16
3-
VisualStudioVersion = 16.0.31321.278
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.0.32014.148
44
MinimumVisualStudioVersion = 10.0.40219.1
55
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "Azure.Devices.Client", "nanoFramework.Azure.Devices.Client\Azure.Devices.Client.nfproj", "{9EDF2863-99AC-4CA1-93FE-F48ED3C6D45F}"
66
EndProject
@@ -12,6 +12,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Azure Certificates", "Azure
1212
AzureCertificates\DigiCertGlobalRootG2.der = AzureCertificates\DigiCertGlobalRootG2.der
1313
EndProjectSection
1414
EndProject
15+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{439F8827-FED8-45E0-9DA0-9DE277A29722}"
16+
EndProject
17+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "TwinTests", "Tests\TwinTests\TwinTests.nfproj", "{50BED153-FF67-4207-ADB0-2651DDADC2A3}"
18+
EndProject
1519
Global
1620
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1721
Debug|Any CPU = Debug|Any CPU
@@ -24,10 +28,19 @@ Global
2428
{9EDF2863-99AC-4CA1-93FE-F48ED3C6D45F}.Release|Any CPU.ActiveCfg = Release|Any CPU
2529
{9EDF2863-99AC-4CA1-93FE-F48ED3C6D45F}.Release|Any CPU.Build.0 = Release|Any CPU
2630
{9EDF2863-99AC-4CA1-93FE-F48ED3C6D45F}.Release|Any CPU.Deploy.0 = Release|Any CPU
31+
{50BED153-FF67-4207-ADB0-2651DDADC2A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
32+
{50BED153-FF67-4207-ADB0-2651DDADC2A3}.Debug|Any CPU.Build.0 = Debug|Any CPU
33+
{50BED153-FF67-4207-ADB0-2651DDADC2A3}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
34+
{50BED153-FF67-4207-ADB0-2651DDADC2A3}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{50BED153-FF67-4207-ADB0-2651DDADC2A3}.Release|Any CPU.Build.0 = Release|Any CPU
36+
{50BED153-FF67-4207-ADB0-2651DDADC2A3}.Release|Any CPU.Deploy.0 = Release|Any CPU
2737
EndGlobalSection
2838
GlobalSection(SolutionProperties) = preSolution
2939
HideSolutionNode = FALSE
3040
EndGlobalSection
41+
GlobalSection(NestedProjects) = preSolution
42+
{50BED153-FF67-4207-ADB0-2651DDADC2A3} = {439F8827-FED8-45E0-9DA0-9DE277A29722}
43+
EndGlobalSection
3144
GlobalSection(ExtensibilityGlobals) = postSolution
3245
SolutionGuid = {C967FC56-9F2D-40AD-8B89-52B96DFD776B}
3346
EndGlobalSection

nanoFramework.Azure.Devices.Client/TwinCollection.cs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,7 @@ public int Count
8282

8383
public object this[string propertyName]
8484
{
85-
get
86-
{
87-
try
88-
{
89-
return _twin[propertyName];
90-
}
91-
catch
92-
{
93-
return null;
94-
}
95-
}
85+
get => _twin[propertyName];
9686

9787
set => _twin[propertyName] = value;
9888
}
@@ -126,19 +116,18 @@ public void Add(string property, object value)
126116
/// <returns>True if the specified property is present; otherwise, false.</returns>
127117
public bool Contains(string propertyName)
128118
{
129-
try
130-
{
131-
var obj = _twin[propertyName];
132-
return true;
133-
}
134-
catch
119+
foreach (string key in _twin.Keys)
135120
{
136-
// That means it doesn't exist
137-
return false;
121+
if (key == propertyName)
122+
{
123+
return true;
124+
}
138125
}
126+
127+
return false;
139128
}
140129

141130
/// <inheritdoc />
142-
public IEnumerator GetEnumerator() => _twin as IEnumerator;
131+
public IEnumerator GetEnumerator() => _twin.GetEnumerator();
143132
}
144133
}

0 commit comments

Comments
 (0)