Skip to content

Commit d357d93

Browse files
committed
Implement generation attributes for enums and custom objects
1 parent f87ab3d commit d357d93

File tree

8 files changed

+115
-0
lines changed

8 files changed

+115
-0
lines changed

Chance.NET.Tests/Chance.NET.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<Reference Include="System.Core" />
5454
</ItemGroup>
5555
<ItemGroup>
56+
<Compile Include="Models\Publisher.cs" />
5657
<Compile Include="Models\Book.cs" />
5758
<Compile Include="Properties\AssemblyInfo.cs" />
5859
<Compile Include="Tests.cs" />

Chance.NET.Tests/Models/Book.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,20 @@ public class Book
3737

3838
[Url]
3939
public string Website;
40+
41+
[Enum(typeof(Genre))]
42+
public Genre Genre;
43+
44+
[Object(typeof(Publisher))]
45+
public Publisher Publisher;
46+
}
47+
48+
public enum Genre
49+
{
50+
Murder = 1,
51+
Mystery = 2,
52+
Comedy = 3,
53+
Erotica = 4,
54+
Biography = 5
4055
}
4156
}

Chance.NET.Tests/Models/Publisher.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
using ChanceNET.Attributes;
8+
9+
namespace ChanceNET.Tests
10+
{
11+
public class Publisher
12+
{
13+
[Age(AgeRanges.Teen)]
14+
public int Age;
15+
16+
[FullName]
17+
public string Name;
18+
19+
[Date]
20+
public DateTime Birthday;
21+
}
22+
}

Chance.NET.Tests/Tests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ public void SerializationTests()
5656
Assert.IsTrue(b.Location.Latitude != 0 && b.Location.Latitude >= -90 && b.Location.Latitude <= 90);
5757
Assert.IsTrue(b.Location.Longitude != 0 && b.Location.Longitude >= -180 && b.Location.Longitude <= 180);
5858
Assert.IsTrue(Uri.IsWellFormedUriString(b.Website, UriKind.Absolute));
59+
Assert.Greater((int)b.Genre, 0);
60+
61+
Publisher pub = b.Publisher;
62+
63+
Assert.IsNotNull(pub);
64+
Assert.IsTrue(pub.Age > 0);
65+
Assert.IsFalse(string.IsNullOrWhiteSpace(pub.Name));
66+
Assert.IsTrue(pub.Birthday != default(DateTime));
5967
}
6068
}
6169

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace ChanceNET.Attributes
6+
{
7+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
8+
public class EnumAttribute : ChanceAttribute
9+
{
10+
Type enumType;
11+
12+
public EnumAttribute(Type enumType)
13+
{
14+
this.enumType = enumType;
15+
}
16+
17+
internal override object GetValue(Chance chance)
18+
{
19+
return Enum.ToObject(enumType, chance.PickEnum(enumType));
20+
}
21+
}
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Reflection;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace ChanceNET.Attributes
7+
{
8+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
9+
public class ObjectAttribute : ChanceAttribute
10+
{
11+
Type objectType;
12+
13+
public ObjectAttribute(Type objectType)
14+
{
15+
this.objectType = objectType;
16+
}
17+
18+
internal override object GetValue(Chance chance)
19+
{
20+
MethodInfo method = chance.GetType()
21+
.GetTypeInfo()
22+
.GetMethod("Object", new Type[] { })
23+
.MakeGenericMethod(objectType);
24+
return method.Invoke(chance, null);
25+
}
26+
}
27+
}

Chance.NET/Chance.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,6 +1926,19 @@ public T PickEnum<T>() where T : struct, IConvertible
19261926
return PickOne(vals);
19271927
}
19281928

1929+
1930+
internal int PickEnum(Type type)
1931+
{
1932+
if (!type.GetTypeInfo().IsEnum)
1933+
{
1934+
throw new ArgumentException("T must be an enumerated type");
1935+
}
1936+
1937+
IEnumerable<int> vals = Enum.GetValues(type).Cast<int>();
1938+
1939+
return PickOne(vals);
1940+
}
1941+
19291942
/// <summary>
19301943
/// Generate a flags Enum with an amount of random flags set.
19311944
/// </summary>

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ for (...)
6161
}
6262
```
6363

64+
The default `Chance` instance relies on the underlying `System.Radom` which is not thread-safe by default.
65+
To share the same generator between multiple threads use the `ConcurrentChance` class instead.
66+
67+
```csharp
68+
Chance chance = new ConcurrentChance(seed);
69+
```
70+
6471
## API
6572

6673
Almost every function that is implemented in [Chance.js](http://chancejs.com).

0 commit comments

Comments
 (0)