Skip to content

Commit 68308d9

Browse files
gokTyBalDgokTyBalD
authored andcommitted
Merge branch 'master' into FEATURE_Enumerator_Plugin
2 parents e7e333e + f4f23c8 commit 68308d9

File tree

6 files changed

+188
-39
lines changed

6 files changed

+188
-39
lines changed

ObjectFiller.Test/EnumTest.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using Tynamix.ObjectFiller;
4+
5+
namespace ObjectFiller.Test
6+
{
7+
[TestClass]
8+
public class EnumTest
9+
{
10+
[TestMethod]
11+
public void Must_support_enums_out_of_the_box()
12+
{
13+
var filler = new Filler<MyClass>();
14+
filler.Setup()
15+
.OnProperty(x => x.Manual).Use(() => ManualSetupEnum.B)
16+
.OnProperty(x => x.Ignored).IgnoreIt();
17+
18+
for (int n = 0; n < 1000; n++)
19+
{
20+
var c = filler.Create();
21+
22+
Assert.IsTrue(
23+
c.Standard == StandardEnum.A ||
24+
c.Standard == StandardEnum.B ||
25+
c.Standard == StandardEnum.C);
26+
27+
Assert.IsTrue(
28+
c.Numbered == NumberedEnum.A ||
29+
c.Numbered == NumberedEnum.B ||
30+
c.Numbered == NumberedEnum.C);
31+
32+
Assert.IsTrue(
33+
c.Flags == FlagsEnum.A ||
34+
c.Flags == FlagsEnum.B ||
35+
c.Flags == FlagsEnum.C);
36+
37+
Assert.IsTrue((int)c.Nasty == 0);
38+
39+
Assert.IsTrue(c.Manual == ManualSetupEnum.B);
40+
41+
Assert.IsTrue((int)c.Ignored == 0);
42+
}
43+
}
44+
45+
public enum StandardEnum
46+
{
47+
A, B, C
48+
}
49+
50+
public enum NumberedEnum
51+
{
52+
A=1,B=3,C=5
53+
}
54+
55+
[Flags]
56+
public enum FlagsEnum
57+
{
58+
A = 0x01,
59+
B = 0x02,
60+
C = A | B,
61+
}
62+
63+
[Flags]
64+
public enum NastyEmptyEnum
65+
{
66+
}
67+
68+
public enum ManualSetupEnum
69+
{
70+
A, B, C
71+
}
72+
73+
public enum IgnoredEnum
74+
{
75+
A, B, C
76+
}
77+
78+
public class MyClass
79+
{
80+
public StandardEnum Standard { get; set; }
81+
public NumberedEnum Numbered { get; set; }
82+
public FlagsEnum Flags { get; set; }
83+
public NastyEmptyEnum Nasty { get; set; }
84+
public ManualSetupEnum Manual { get; set; }
85+
public IgnoredEnum Ignored { get; set; }
86+
}
87+
}
88+
}

ObjectFiller.Test/LibraryFillingTest.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Linq;
3+
using System.Security.Cryptography.X509Certificates;
24
using Microsoft.VisualStudio.TestTools.UnitTesting;
35
using ObjectFiller.Test.TestPoco.Library;
46
using Tynamix.ObjectFiller;
@@ -62,6 +64,22 @@ public void TestFillLibraryWithPocoOfABook()
6264
Assert.AreEqual(1, filledLib.Books.Count);
6365
}
6466

67+
[TestMethod]
68+
public void TestFillLibraryWithConfiguredPocoOfABook()
69+
{
70+
Filler<LibraryConstructorPoco> lib = new Filler<LibraryConstructorPoco>();
71+
lib.Setup()
72+
.OnProperty(x => x.Books).IgnoreIt()
73+
.SetupFor<Book>()
74+
.OnProperty(x => x.Name).Use(() => "ABook");
75+
76+
77+
var l = lib.Create();
78+
79+
Assert.AreEqual("ABook", ((Book)l.Books.ToList()[0]).Name);
80+
81+
}
82+
6583
[TestMethod]
6684
public void TestFillLibraryWithDictionary()
6785
{

ObjectFiller.Test/ObjectFiller.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
</ItemGroup>
4747
<ItemGroup>
4848
<Compile Include="AddressFillingTest.cs" />
49+
<Compile Include="EnumTest.cs" />
4950
<Compile Include="LibraryFillingTest.cs" />
5051
<Compile Include="ListFillingTest.cs" />
5152
<Compile Include="LoremIpsumPluginTest.cs" />

ObjectFiller/Filler.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,33 @@ private object GetFilledObject(Type type, ObjectFillerSetup currentSetup)
254254
return GetFilledPoco(type, currentSetup);
255255
}
256256

257+
if (TypeIsEnum(type))
258+
{
259+
return GetRandomEnumValue(type);
260+
}
261+
257262
object newValue = GetRandomValue(type, currentSetup);
258263
return newValue;
259264
}
260265

261-
private object GetFilledPoco(Type type, ObjectFillerSetup currentSetup)
266+
267+
268+
private object GetRandomEnumValue(Type type)
269+
{
270+
// performance: Enum.GetValues() is slow due to reflection, should cache it
271+
Array values = Enum.GetValues(type);
272+
if (values.Length > 0)
273+
{
274+
int index = Random.Next() % values.Length;
275+
return values.GetValue(index);
276+
}
277+
else
278+
{
279+
return 0;
280+
}
281+
}
282+
283+
private object GetFilledPoco(Type type, ObjectFillerSetup currentSetup)
262284
{
263285
object result = CreateInstanceOfType(type, currentSetup);
264286

@@ -430,5 +452,10 @@ private static bool TypeIsList(Type type)
430452
&& (type.GetGenericTypeDefinition() == typeof(IEnumerable<>)
431453
|| type.GetInterfaces().Any(x => x == typeof(IEnumerable)));
432454
}
433-
}
455+
456+
private static bool TypeIsEnum(Type type)
457+
{
458+
return type.IsEnum;
459+
}
460+
}
434461
}

ObjectFiller/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
3232
// übernehmen, indem Sie "*" eingeben:
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("1.2.1.0")]
35-
[assembly: AssemblyFileVersion("1.2.1.0")]
34+
[assembly: AssemblyVersion("1.2.2.0")]
35+
[assembly: AssemblyFileVersion("1.2.2.0")]

0 commit comments

Comments
 (0)