Skip to content

Commit bb9174f

Browse files
committed
IpAddress Plugin whritten
1 parent a22985f commit bb9174f

File tree

7 files changed

+258
-2
lines changed

7 files changed

+258
-2
lines changed

Tynamix.ObjectFiller.Test/AddressFillingTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class AddressFillingTest
1313
public void FillAllAddressProperties()
1414
{
1515
Filler<Address> addressFiller = new Filler<Address>();
16-
Address a = addressFiller.Create();
16+
Address a = addressFiller.Create();
1717

1818
Assert.NotNull(a.City);
1919
Assert.NotNull(a.Country);

Tynamix.ObjectFiller.Test/CollectionizerTest.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Linq;
45
using Tynamix.ObjectFiller;
56
using Xunit;
@@ -45,6 +46,23 @@ public void TestMnemonicStringPlugin()
4546
Assert.True(collection.MnemonicStrings.All(x => x.Length >= 20 && x.Length <= 25));
4647
}
4748

49+
[Fact]
50+
public void TestMnemonicStringPluginTest()
51+
{
52+
var filler = new Filler<CollectionizerPoco>();
53+
54+
filler.Setup()
55+
.OnProperty(x => x.MnemonicStrings).Use<Collectionizer<string, MnemonicString>>();
56+
57+
var collection = filler.Create();
58+
59+
Assert.NotNull(collection);
60+
Assert.NotNull(collection.MnemonicStrings);
61+
Assert.NotEmpty(collection.MnemonicStrings);
62+
63+
collection.MnemonicStrings.ToList().ForEach(s => Debug.WriteLine(s));
64+
}
65+
4866
[Fact]
4967
public void TestIntRangePlugin()
5068
{
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
namespace ObjectFiller.Test
2+
{
3+
using System.Security.Policy;
4+
using Tynamix.ObjectFiller;
5+
using Xunit;
6+
7+
public class IpAddressPluginTest
8+
{
9+
10+
internal class IpAddressClass
11+
{
12+
public string IpAddress { get; set; }
13+
}
14+
15+
[Fact]
16+
public void TestIpAddressGenerator()
17+
{
18+
Filler<IpAddressClass> filler = new Filler<IpAddressClass>();
19+
20+
filler.Setup()
21+
.OnProperty(x => x.IpAddress).Use<IpAddress>();
22+
23+
var result = filler.Create();
24+
25+
var ipAddresses = result.IpAddress.Split('.');
26+
27+
Assert.Collection(ipAddresses,
28+
(s) => Assert.True(int.Parse(s) > 0 && int.Parse(s) <= 255),
29+
(s) => Assert.True(int.Parse(s) > 0 && int.Parse(s) <= 255),
30+
(s) => Assert.True(int.Parse(s) > 0 && int.Parse(s) <= 255),
31+
(s) => Assert.True(int.Parse(s) > 0 && int.Parse(s) <= 255));
32+
}
33+
34+
[Fact]
35+
public void TestIpAddressGeneratorWithWrongNumbersAndFirstSegmentMaximumOf10()
36+
{
37+
Filler<IpAddressClass> filler = new Filler<IpAddressClass>();
38+
39+
filler.Setup().OnProperty(x => x.IpAddress).Use(new IpAddress(10, 300, 300, 300));
40+
41+
var result = filler.Create();
42+
43+
var ipAddresses = result.IpAddress.Split('.');
44+
45+
Assert.Collection(ipAddresses,
46+
(s) => Assert.True(int.Parse(s) > 0 && int.Parse(s) <= 10),
47+
(s) => Assert.True(int.Parse(s) > 0 && int.Parse(s) <= 255),
48+
(s) => Assert.True(int.Parse(s) > 0 && int.Parse(s) <= 255),
49+
(s) => Assert.True(int.Parse(s) > 0 && int.Parse(s) <= 255));
50+
}
51+
52+
[Fact]
53+
public void TestIpAddressGeneratorWithSegmentSetup()
54+
{
55+
Filler<IpAddressClass> filler = new Filler<IpAddressClass>();
56+
57+
filler.Setup().OnProperty(x => x.IpAddress).Use(new IpAddress(10, 10, 10, 10));
58+
59+
var result = filler.Create();
60+
61+
var ipAddresses = result.IpAddress.Split('.');
62+
63+
Assert.Collection(ipAddresses,
64+
(s) => Assert.True(int.Parse(s) > 0 && int.Parse(s) <= 10),
65+
(s) => Assert.True(int.Parse(s) > 0 && int.Parse(s) <= 10),
66+
(s) => Assert.True(int.Parse(s) > 0 && int.Parse(s) <= 10),
67+
(s) => Assert.True(int.Parse(s) > 0 && int.Parse(s) <= 10));
68+
}
69+
70+
[Fact]
71+
public void TestIpAddressGeneratorWithFirstTwoSegmentSetup()
72+
{
73+
Filler<IpAddressClass> filler = new Filler<IpAddressClass>();
74+
75+
filler.Setup().OnProperty(x => x.IpAddress).Use(new IpAddress(10, 10));
76+
77+
var result = filler.Create();
78+
79+
var ipAddresses = result.IpAddress.Split('.');
80+
81+
Assert.Collection(ipAddresses,
82+
(s) => Assert.True(int.Parse(s) > 0 && int.Parse(s) <= 10),
83+
(s) => Assert.True(int.Parse(s) > 0 && int.Parse(s) <= 10),
84+
(s) => Assert.True(int.Parse(s) > 0 && int.Parse(s) <= 255),
85+
(s) => Assert.True(int.Parse(s) > 0 && int.Parse(s) <= 255));
86+
}
87+
}
88+
}

Tynamix.ObjectFiller.Test/ObjectFillerTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public void TestFillPerson()
1818
filler.Setup()
1919
.OnType<IAddress>().CreateInstanceOf<Address>()
2020
.OnType<string>().Use(new MnemonicString(10))
21-
.OnProperty(person => person.FirstName).Use(new MnemonicString(1))
21+
.OnProperty(person => person.FirstName).Use<MnemonicString>()
2222
.OnProperty(person => person.LastName).Use(new RandomListItem<string>("Maik", "Tom", "Anton"))
2323
.OnProperty(person => person.Age).Use(() => Tynamix.ObjectFiller.Random.Next(12, 83))
2424
.SetupFor<Address>()
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
namespace Tynamix.ObjectFiller
2+
{
3+
/// <summary>
4+
/// Generates an IP address
5+
/// </summary>
6+
public class IpAddress : IRandomizerPlugin<string>
7+
{
8+
/// <summary>
9+
/// The maximum of the first IP part (max 255)
10+
/// </summary>
11+
private int firstIpPartMax;
12+
13+
/// <summary>
14+
/// The maximum of the second IP part (max 255)
15+
/// </summary>
16+
private int secondIpPartMax;
17+
18+
/// <summary>
19+
/// The maximum of the third IP part (max 255)
20+
/// </summary>
21+
private int thirdIpPartMax;
22+
23+
/// <summary>
24+
/// The maximum of the last IP part (max 255)
25+
/// </summary>
26+
private int lastIpPartMax;
27+
28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="IpAddress"/> class.
30+
/// </summary>
31+
public IpAddress()
32+
: this(255, 255, 255, 255)
33+
{
34+
}
35+
36+
/// <summary>
37+
/// Initializes a new instance of the <see cref="IpAddress"/> class.
38+
/// </summary>
39+
/// <param name="firstIpPartMax">
40+
/// The maximum of the first IP part (max 255)
41+
/// </param>
42+
public IpAddress(uint firstIpPartMax)
43+
: this(firstIpPartMax, 255, 255, 255)
44+
{
45+
}
46+
47+
/// <summary>
48+
/// Initializes a new instance of the <see cref="IpAddress"/> class.
49+
/// </summary>
50+
/// <param name="firstIpPartMax">
51+
/// The maximum of the first IP part (max 255)
52+
/// </param>
53+
/// <param name="secondIpPartMax">
54+
/// The maximum of the second IP part (max 255)
55+
/// </param>
56+
public IpAddress(
57+
uint firstIpPartMax,
58+
uint secondIpPartMax)
59+
: this(firstIpPartMax, secondIpPartMax, 255, 255)
60+
61+
{
62+
}
63+
64+
/// <summary>
65+
/// Initializes a new instance of the <see cref="IpAddress"/> class.
66+
/// </summary>
67+
/// <param name="firstIpPartMax">
68+
/// The maximum of the first IP part (max 255)
69+
/// </param>
70+
/// <param name="secondIpPartMax">
71+
/// The maximum of the second IP part (max 255)
72+
/// </param>
73+
/// <param name="thirdIpPartMax">
74+
/// The maximum of the third IP part (max 255)
75+
/// </param>
76+
public IpAddress(
77+
uint firstIpPartMax,
78+
uint secondIpPartMax,
79+
uint thirdIpPartMax)
80+
: this(firstIpPartMax, secondIpPartMax, thirdIpPartMax, 255)
81+
{
82+
}
83+
84+
/// <summary>
85+
/// Initializes a new instance of the <see cref="IpAddress"/> class.
86+
/// </summary>
87+
/// <param name="firstIpPartMax">
88+
/// The maximum of the first IP part (max 255)
89+
/// </param>
90+
/// <param name="secondIpPartMax">
91+
/// The maximum of the second IP part (max 255)
92+
/// </param>
93+
/// <param name="thirdIpPartMax">
94+
/// The maximum of the third IP part (max 255)
95+
/// </param>
96+
/// <param name="lastIpPartMax">
97+
/// The maximum of the last IP part (max 255)
98+
/// </param>
99+
public IpAddress(
100+
uint firstIpPartMax,
101+
uint secondIpPartMax,
102+
uint thirdIpPartMax,
103+
uint lastIpPartMax)
104+
{
105+
this.firstIpPartMax = (int)(firstIpPartMax > 255 ? 255 : firstIpPartMax);
106+
this.secondIpPartMax = (int)(secondIpPartMax > 255 ? 255 : secondIpPartMax);
107+
this.thirdIpPartMax = (int)(thirdIpPartMax > 255 ? 255 : thirdIpPartMax);
108+
this.lastIpPartMax = (int)(lastIpPartMax > 255 ? 255 : lastIpPartMax);
109+
}
110+
111+
/// <summary>
112+
/// Gets random IP address
113+
/// </summary>
114+
/// <returns>An random IP address</returns>
115+
public string GetValue()
116+
{
117+
int firstSegment = Randomizer<int>.Create(new IntRange(1, this.firstIpPartMax));
118+
int secondSegment = Randomizer<int>.Create(new IntRange(1, this.secondIpPartMax));
119+
int thirdSegment = Randomizer<int>.Create(new IntRange(1, this.thirdIpPartMax));
120+
int lastSegment = Randomizer<int>.Create(new IntRange(1, this.lastIpPartMax));
121+
122+
return string.Format("{0}.{1}.{2}.{3}", firstSegment, secondSegment, thirdSegment, lastSegment);
123+
}
124+
}
125+
}

Tynamix.ObjectFiller/Setup/FluentPropertyApi.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,19 @@ public FluentFillerApi<TTargetObject> Use(IRandomizerPlugin<TTargetType> randomi
132132
return this.Use(randomizerPlugin.GetValue);
133133
}
134134

135+
136+
/// <summary>
137+
/// Defines which implementation of the <see cref="IRandomizerPlugin{T}"/> interface will be used to generate a value for the given <see cref="TTargetType"/>
138+
/// </summary>
139+
/// <typeparam name="TRandomizerPlugin">A <see cref="IRandomizerPlugin{TTargetType}"/> which will be used to generate a value of the <see cref="TTargetType"/></param>
140+
/// <returns>Main FluentFiller API</returns>
141+
public FluentFillerApi<TTargetObject> Use<TRandomizerPlugin>()
142+
where TRandomizerPlugin : IRandomizerPlugin<TTargetType>, new()
143+
{
144+
this.Use(new TRandomizerPlugin());
145+
return this.callback;
146+
}
147+
135148
/// <summary>
136149
/// Use this function if you want to use an IEnumerable for the data generation.
137150
/// With that you can generate random data in a specific order, with include, exclude and all the other stuff

Tynamix.ObjectFiller/Setup/FluentTypeApi.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ public FluentFillerApi<TTargetObject> Use(IRandomizerPlugin<TTargetType> randomi
7777
return this.callback;
7878
}
7979

80+
/// <summary>
81+
/// Defines which implementation of the <see cref="IRandomizerPlugin{T}"/> interface will be used to generate a value for the given <see cref="TTargetType"/>
82+
/// </summary>
83+
/// <typeparam name="TRandomizerPlugin">A <see cref="IRandomizerPlugin{TTargetType}"/> which will be used to generate a value of the <see cref="TTargetType"/></param>
84+
/// <returns>Main FluentFiller API</returns>
85+
public FluentFillerApi<TTargetObject> Use<TRandomizerPlugin>()
86+
where TRandomizerPlugin : IRandomizerPlugin<TTargetType>, new()
87+
{
88+
this.Use(new TRandomizerPlugin());
89+
return this.callback;
90+
}
91+
8092
/// <summary>
8193
/// Use this function if you want to use an IEnumerable for the data generation.
8294
/// With that you can generate random data in a specific order, with include, exclude and all the other stuff

0 commit comments

Comments
 (0)