Skip to content

Commit 13e42ef

Browse files
committed
Add some more thorough testing
1 parent 24e1cea commit 13e42ef

File tree

5 files changed

+89
-37
lines changed

5 files changed

+89
-37
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
<ItemGroup>
5656
<Compile Include="Models\Book.cs" />
5757
<Compile Include="Properties\AssemblyInfo.cs" />
58-
<Compile Include="SerializationTests.cs" />
58+
<Compile Include="Tests.cs" />
5959
</ItemGroup>
6060
<ItemGroup>
6161
<None Include="packages.config" />

Chance.NET.Tests/Models/Book.cs

+15
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,20 @@ public class Book
2222

2323
[Person]
2424
public Person Author;
25+
26+
[Location]
27+
public Location Location;
28+
29+
[Guid]
30+
public string Guid;
31+
32+
[Double(0, 100)]
33+
public double Price;
34+
35+
[Color]
36+
public string CoverColor;
37+
38+
[Url]
39+
public string Website;
2540
}
2641
}

Chance.NET.Tests/SerializationTests.cs

-33
This file was deleted.

Chance.NET.Tests/Tests.cs

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Text;
3+
using System.Collections.Generic;
4+
5+
using NUnit.Framework;
6+
7+
namespace ChanceNET.Tests
8+
{
9+
/// <summary>
10+
/// Summary description for SerializationTests
11+
/// </summary>
12+
[TestFixture]
13+
public class Tests
14+
{
15+
Chance chance = new Chance();
16+
17+
static IEnumerable<int> LoopTestSource()
18+
{
19+
for (int i = 0; i < 10000; i++)
20+
{
21+
yield return i;
22+
}
23+
}
24+
25+
[Test, TestCaseSource("LoopTestSource")]
26+
public void LocationTests(int i)
27+
{
28+
Location loc = chance.Location();
29+
30+
Assert.IsTrue(loc.Latitude != 0 && loc.Latitude >= -90 && loc.Latitude <= 90);
31+
Assert.IsTrue(loc.Longitude != 0 && loc.Longitude >= -180 && loc.Longitude <= 180);
32+
33+
double distance = loc.DistanceTo(loc);
34+
35+
Assert.AreEqual(0, distance);
36+
37+
Location other = chance.Location(loc, 1000);
38+
Assert.IsTrue(loc.DistanceTo(other) < 1000);
39+
}
40+
41+
[Test, TestCaseSource("LoopTestSource")]
42+
public void SerializationTests(int i)
43+
{
44+
Book b = chance.Object<Book>();
45+
46+
Assert.IsTrue(b.Year > 0);
47+
Assert.IsTrue(b.Price >= 0 && b.Price < 100);
48+
Assert.IsTrue(b.Release != default(DateTime));
49+
Assert.IsFalse(string.IsNullOrWhiteSpace(b.Title));
50+
Assert.IsFalse(string.IsNullOrWhiteSpace(b.Guid));
51+
Assert.IsFalse(string.IsNullOrWhiteSpace(b.CoverColor));
52+
Assert.IsTrue(b.CoverColor.StartsWith("#"));
53+
Assert.IsFalse(string.IsNullOrWhiteSpace(b.Author.Email));
54+
Assert.IsFalse(string.IsNullOrWhiteSpace(b.Author.FirstName));
55+
Assert.IsFalse(string.IsNullOrWhiteSpace(b.Author.FullName()));
56+
Assert.IsFalse(string.IsNullOrWhiteSpace(b.Author.LastName));
57+
Assert.IsTrue(b.Location.Latitude != 0 && b.Location.Latitude >= -90 && b.Location.Latitude <= 90);
58+
Assert.IsTrue(b.Location.Longitude != 0 && b.Location.Longitude >= -180 && b.Location.Longitude <= 180);
59+
Assert.IsTrue(Uri.IsWellFormedUriString(b.Website, UriKind.Absolute));
60+
}
61+
62+
[TearDown]
63+
public void Reset()
64+
{
65+
chance = chance.New();
66+
}
67+
}
68+
}

Chance.NET/Chance.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ public byte Byte()
410410
/// <summary>
411411
/// Pick a random human age (0-100) in the given range.
412412
/// </summary>
413-
/// <param name="range"></param>
413+
/// <param name="range">The range to limit the age to. Default is any.</param>
414414
/// <returns></returns>
415415
public int Age(AgeRanges range = AgeRanges.Any)
416416
{
@@ -429,11 +429,13 @@ public int Age(AgeRanges range = AgeRanges.Any)
429429
}
430430
if (range.HasFlag(AgeRanges.Adult))
431431
{
432-
ranges.Add(Integer(18, 65), 47);
432+
int min = range.HasFlag(AgeRanges.Teen) ? 20 : 18;
433+
ranges.Add(Integer(min, 66), 47);
433434
}
434435
if (range.HasFlag(AgeRanges.Senior))
435436
{
436-
ranges.Add(Integer(60, 100), 40);
437+
int min = range.HasFlag(AgeRanges.Adult) ? 66 : 65;
438+
ranges.Add(Integer(min, 101), 40);
437439
}
438440

439441
return Weighted(ranges);

0 commit comments

Comments
 (0)