Skip to content

Commit 21fa7f2

Browse files
committed
Merge pull request #35 from Tynamix/bugfix_missing_default_mappings
added missing mappings for primitive types (unit, decimal?, ...)
2 parents 23e91e0 + 8863f00 commit 21fa7f2

File tree

3 files changed

+82
-4
lines changed

3 files changed

+82
-4
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Linq;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using Tynamix.ObjectFiller;
6+
7+
namespace ObjectFiller.Test
8+
{
9+
[TestClass]
10+
public class DefaultDatatypeMappingsTest
11+
{
12+
[TestMethod]
13+
public void Ensure_that_each_primitive_datatype_is_mapped_by_default()
14+
{
15+
var filler = new Filler<MyClass>();
16+
var myClasses = filler.Create(100).ToArray();
17+
foreach (var myClass in myClasses)
18+
{
19+
Assert.AreNotEqual(default(Guid), myClass._Guid);
20+
Assert.AreNotEqual(default(decimal), myClass._Decimal);
21+
}
22+
}
23+
24+
public class MyClass
25+
{
26+
public byte _byte { get; set; }
27+
public char _char { get; set; }
28+
public Int16 _i16 { get; set; }
29+
public Int32 _i32 { get; set; }
30+
public Int64 _i64 { get; set; }
31+
public UInt16 _u16 { get; set; }
32+
public UInt32 _u32 { get; set; }
33+
public UInt64 _u64 { get; set; }
34+
public float _float { get; set; }
35+
public double _double { get; set; }
36+
public decimal _Decimal { get; set; }
37+
public IntPtr _IntPtr { get; set; }
38+
public DateTime _DateTime { get; set; }
39+
public TimeSpan _TimeSpan { get; set; }
40+
public Guid _Guid { get; set; }
41+
public string _String { get; set; }
42+
43+
public byte? _n_byte { get; set; }
44+
public char? _n_char { get; set; }
45+
public Int16? _n_i16 { get; set; }
46+
public Int32? _n_i32 { get; set; }
47+
public Int64? _n_i64 { get; set; }
48+
public UInt16? _n_u16 { get; set; }
49+
public UInt32? _n_u32 { get; set; }
50+
public UInt64? _n_u64 { get; set; }
51+
public float? _n_float { get; set; }
52+
public double? _n_double { get; set; }
53+
public decimal? _n_Decimal { get; set; }
54+
public IntPtr? _n_IntPtr { get; set; }
55+
public DateTime? _n_DateTime { get; set; }
56+
public TimeSpan? _n_TimeSpan { get; set; }
57+
public Guid? _n_Guid { get; set; }
58+
}
59+
}
60+
}

ObjectFiller.Test/ObjectFiller.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<ItemGroup>
4848
<Compile Include="AddressFillingTest.cs" />
4949
<Compile Include="CreateInstanceTest.cs" />
50+
<Compile Include="DefaultDatatypeMappingsTest.cs" />
5051
<Compile Include="EnumTest.cs" />
5152
<Compile Include="HashStackTests.cs" />
5253
<Compile Include="LibraryFillingTest.cs" />

ObjectFiller/Setup/FillerSetupItem.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,35 @@ private void SetDefaultRandomizer()
3131
TypeToRandomFunc[typeof(string)] = mnemonic.GetValue;
3232
TypeToRandomFunc[typeof(bool)] = () => Random.Next(0, 2) == 1;
3333
TypeToRandomFunc[typeof(short)] = () => (short)Random.Next(-32767, 32767);
34+
TypeToRandomFunc[typeof(short?)] = () => (short)Random.Next(-32767, 32767);
3435
TypeToRandomFunc[typeof(int)] = () => Random.Next();
3536
TypeToRandomFunc[typeof(int?)] = () => Random.Next();
36-
TypeToRandomFunc[typeof(long)] = () => Random.Next();
37-
TypeToRandomFunc[typeof(long?)] = () => Random.Next();
37+
TypeToRandomFunc[typeof(long)] = () => (long)Random.Next();
38+
TypeToRandomFunc[typeof(long?)] = () => (long)Random.Next();
39+
TypeToRandomFunc[typeof(float)] = () => (float)doublePlugin.GetValue();
40+
TypeToRandomFunc[typeof(float?)] = () => (float?)doublePlugin.GetValue();
3841
TypeToRandomFunc[typeof(double)] = () => doublePlugin.GetValue();
3942
TypeToRandomFunc[typeof(double?)] = () => doublePlugin.GetValue();
40-
TypeToRandomFunc[typeof(decimal)] = () => doublePlugin.GetValue();
43+
TypeToRandomFunc[typeof(decimal)] = () => (decimal)Random.Next();
44+
TypeToRandomFunc[typeof(decimal?)] = () => (decimal)Random.Next();
4145
TypeToRandomFunc[typeof(Guid)] = () => Guid.NewGuid();
4246
TypeToRandomFunc[typeof(Guid?)] = () => Guid.NewGuid();
4347
TypeToRandomFunc[typeof(System.DateTime)] = () => dateTimeRandomizer.GetValue();
4448
TypeToRandomFunc[typeof(System.DateTime?)] = () => dateTimeRandomizer.GetValue();
45-
49+
TypeToRandomFunc[typeof(byte)] = () => (byte)Random.Next();
50+
TypeToRandomFunc[typeof(byte?)] = () => (byte?)Random.Next();
51+
TypeToRandomFunc[typeof(char)] = () => (char)Random.Next();
52+
TypeToRandomFunc[typeof(char?)] = () => (char)Random.Next();
53+
TypeToRandomFunc[typeof(ushort)] = () => (ushort)Random.Next();
54+
TypeToRandomFunc[typeof(ushort?)] = () => (ushort)Random.Next();
55+
TypeToRandomFunc[typeof(uint)] = () => (uint)Random.Next();
56+
TypeToRandomFunc[typeof(uint?)] = () => (uint)Random.Next();
57+
TypeToRandomFunc[typeof(ulong)] = () => (ulong)Random.Next();
58+
TypeToRandomFunc[typeof(ulong?)] = () => (ulong)Random.Next();
59+
TypeToRandomFunc[typeof(IntPtr)] = () => default(IntPtr);
60+
TypeToRandomFunc[typeof(IntPtr?)] = () => default(IntPtr);
61+
TypeToRandomFunc[typeof(TimeSpan)] = () => new TimeSpan(Random.Next());
62+
TypeToRandomFunc[typeof(TimeSpan?)] = () => new TimeSpan(Random.Next());
4663
}
4764

4865
/// <summary>

0 commit comments

Comments
 (0)