Skip to content

Commit

Permalink
Implement generation attributes for enums and custom objects
Browse files Browse the repository at this point in the history
  • Loading branch information
haath committed Sep 6, 2018
1 parent f87ab3d commit d357d93
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions Chance.NET.Tests/Chance.NET.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="Models\Publisher.cs" />
<Compile Include="Models\Book.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Tests.cs" />
Expand Down
15 changes: 15 additions & 0 deletions Chance.NET.Tests/Models/Book.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,20 @@ public class Book

[Url]
public string Website;

[Enum(typeof(Genre))]
public Genre Genre;

[Object(typeof(Publisher))]
public Publisher Publisher;
}

public enum Genre
{
Murder = 1,
Mystery = 2,
Comedy = 3,
Erotica = 4,
Biography = 5
}
}
22 changes: 22 additions & 0 deletions Chance.NET.Tests/Models/Publisher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ChanceNET.Attributes;

namespace ChanceNET.Tests
{
public class Publisher
{
[Age(AgeRanges.Teen)]
public int Age;

[FullName]
public string Name;

[Date]
public DateTime Birthday;
}
}
8 changes: 8 additions & 0 deletions Chance.NET.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ public void SerializationTests()
Assert.IsTrue(b.Location.Latitude != 0 && b.Location.Latitude >= -90 && b.Location.Latitude <= 90);
Assert.IsTrue(b.Location.Longitude != 0 && b.Location.Longitude >= -180 && b.Location.Longitude <= 180);
Assert.IsTrue(Uri.IsWellFormedUriString(b.Website, UriKind.Absolute));
Assert.Greater((int)b.Genre, 0);

Publisher pub = b.Publisher;

Assert.IsNotNull(pub);
Assert.IsTrue(pub.Age > 0);
Assert.IsFalse(string.IsNullOrWhiteSpace(pub.Name));
Assert.IsTrue(pub.Birthday != default(DateTime));
}
}

Expand Down
22 changes: 22 additions & 0 deletions Chance.NET/Attributes/EnumAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace ChanceNET.Attributes
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class EnumAttribute : ChanceAttribute
{
Type enumType;

public EnumAttribute(Type enumType)
{
this.enumType = enumType;
}

internal override object GetValue(Chance chance)
{
return Enum.ToObject(enumType, chance.PickEnum(enumType));
}
}
}
27 changes: 27 additions & 0 deletions Chance.NET/Attributes/ObjectAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Text;

namespace ChanceNET.Attributes
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class ObjectAttribute : ChanceAttribute
{
Type objectType;

public ObjectAttribute(Type objectType)
{
this.objectType = objectType;
}

internal override object GetValue(Chance chance)
{
MethodInfo method = chance.GetType()
.GetTypeInfo()
.GetMethod("Object", new Type[] { })
.MakeGenericMethod(objectType);
return method.Invoke(chance, null);
}
}
}
13 changes: 13 additions & 0 deletions Chance.NET/Chance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1926,6 +1926,19 @@ public T PickEnum<T>() where T : struct, IConvertible
return PickOne(vals);
}


internal int PickEnum(Type type)
{
if (!type.GetTypeInfo().IsEnum)
{
throw new ArgumentException("T must be an enumerated type");
}

IEnumerable<int> vals = Enum.GetValues(type).Cast<int>();

return PickOne(vals);
}

/// <summary>
/// Generate a flags Enum with an amount of random flags set.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ for (...)
}
```

The default `Chance` instance relies on the underlying `System.Radom` which is not thread-safe by default.
To share the same generator between multiple threads use the `ConcurrentChance` class instead.

```csharp
Chance chance = new ConcurrentChance(seed);
```

## API

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

0 comments on commit d357d93

Please sign in to comment.