Skip to content

Commit 8dd2d9f

Browse files
committed
Fixed GetEnum<T>
Was getting System.IndexOutOfRangeException. The fix for that is to make the max index value values.length - 1. Also added a unit test.
1 parent a392570 commit 8dd2d9f

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

Source/RandomData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public static T GetEnum<T>() {
159159
throw new ArgumentException("T must be an enum type.");
160160

161161
Array values = Enum.GetValues(typeof(T));
162-
return (T)values.GetValue(GetInt(0, values.Length));
162+
return (T)values.GetValue(GetInt(0, values.Length - 1));
163163
}
164164

165165
public static string GetIp4Address() {

Source/Tests/Exceptioless.RandomData.Tests.csproj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="..\..\packages\xunit.runner.visualstudio.2.0.0\build\net20\xunit.runner.visualstudio.props" Condition="Exists('..\..\packages\xunit.runner.visualstudio.2.0.0\build\net20\xunit.runner.visualstudio.props')" />
34
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
45
<PropertyGroup>
56
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -11,6 +12,7 @@
1112
<AssemblyName>c</AssemblyName>
1213
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
1314
<FileAlignment>512</FileAlignment>
15+
<NuGetPackageImportStamp>ff297c97</NuGetPackageImportStamp>
1416
</PropertyGroup>
1517
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1618
<DebugSymbols>true</DebugSymbols>
@@ -54,7 +56,16 @@
5456
<Name>Exceptionless.RandomData</Name>
5557
</ProjectReference>
5658
</ItemGroup>
59+
<ItemGroup>
60+
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
61+
</ItemGroup>
5762
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
63+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
64+
<PropertyGroup>
65+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
66+
</PropertyGroup>
67+
<Error Condition="!Exists('..\..\packages\xunit.runner.visualstudio.2.0.0\build\net20\xunit.runner.visualstudio.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\xunit.runner.visualstudio.2.0.0\build\net20\xunit.runner.visualstudio.props'))" />
68+
</Target>
5869
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
5970
Other similar extension points exist, see Microsoft.Common.targets.
6071
<Target Name="BeforeBuild">

Source/Tests/RandomDataTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ public void CanGenerateRandomData() {
1313
Assert.InRange(value, 1, 3);
1414
}
1515

16+
[Fact]
17+
public void GetEnumWithOneValueTest()
18+
{
19+
var result = RandomData.GetEnum<_days>();
20+
21+
Assert.Equal<_days>(_days.Monday, result);
22+
}
23+
1624
private int[] _numbers = new[] {1, 2, 3};
25+
26+
private enum _days
27+
{
28+
Monday
29+
}
1730
}
1831
}

Source/Tests/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="xunit" version="1.9.2" targetFramework="net45" />
4+
<package id="xunit.runner.visualstudio" version="2.0.0" targetFramework="net45" />
45
</packages>

0 commit comments

Comments
 (0)