/
Faker.cs
183 lines (153 loc) · 5.29 KB
/
Faker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Bogus.DataSets;
using System = Bogus.DataSets.System;
namespace Bogus
{
/// <summary>
/// A hub of all the categories merged into a single class to ease fluent syntax API.
/// </summary>
public class Faker : ILocaleAware
{
/// <summary>
/// The default mode to use when generating objects. Strict mode ensures that all properties have rules.
/// </summary>
public static bool DefaultStrictMode = false;
/// <summary>
/// Create a Faker with a specific locale.
/// </summary>
public Faker(string locale = "en")
{
Locale = locale;
this.Address = new Address(locale);
this.Company = new Company(locale);
this.Date = new Date {Locale = locale};
this.Finance = new Finance {Locale = locale};
this.Hacker = new Hacker();
this.Image = new Images(locale);
this.Internet = new Internet(locale);
this.Lorem = new Lorem(locale);
this.Name = new Name(locale);
this.Phone = new PhoneNumbers(locale);
this.System = new DataSets.System(locale);
this.Random = new Randomizer();
this.Hashids = new Hashids();
}
/// <summary>
/// Can parse a handle bar expression like "{{name.lastName}}, {{name.firstName}} {{name.suffix}}".
/// </summary>
public string Parse(string str)
{
return Tokenizer.Parse(str,
this.Address,
this.Company,
this.Date,
this.Finance,
this.Hacker,
this.Image,
this.Internet,
this.Lorem,
this.Name,
this.Phone,
this.System);
}
private Person person;
/// <summary>
/// A contextually relevant fields of a person.
/// </summary>
public Person Person => person ?? (person = new Person(this.Locale));
/// <summary>
/// Creates hacker gibberish.
/// </summary>
public Hacker Hacker { get; set; }
/// <summary>
/// Generate Phone Numbers
/// </summary>
public PhoneNumbers Phone { get; set; }
/// <summary>
/// Generate Names
/// </summary>
public Name Name { get; set; }
/// <summary>
/// Generate Words
/// </summary>
public Lorem Lorem { get; set; }
/// <summary>
/// Generate Image URL Links
/// </summary>
public Images Image { get; set; }
/// <summary>
/// Generate Finance Items
/// </summary>
public Finance Finance { get; set; }
/// <summary>
/// Generate Addresses
/// </summary>
public Address Address { get; set; }
/// <summary>
/// Generate Dates
/// </summary>
public Date Date { get; set; }
/// <summary>
/// Generates company names, titles and BS.
/// </summary>
public Company Company { get; set; }
/// <summary>
/// Generate Internet stuff like Emails and UserNames.
/// </summary>
public Internet Internet { get; set; }
/// <summary>
/// Generates fake data for many computer systems properties
/// </summary>
public DataSets.System System { get; set; }
/// <summary>
/// Generate numbers, booleans, and decimals.
/// </summary>
public Randomizer Random { get; set; }
/// <summary>
/// Helper method to pick a random element.
/// </summary>
public T PickRandom<T>(IEnumerable<T> items)
{
return this.Random.ArrayElement(items.ToArray());
}
/// <summary>
/// Picks a random Enum of T. Works only with Enums.
/// </summary>
/// <typeparam name="T">Must be an Enum</typeparam>
public T PickRandom<T>() where T : struct
{
return this.Random.Enum<T>();
}
/// <summary>
/// The current locale for the dataset.
/// </summary>
/// <value>The locale.</value>
public string Locale { get; set; }
/// <summary>
/// Triggers a new generation context
/// </summary>
internal void NewContext()
{
person = null;
Interlocked.Increment(ref GlobalUniqueIndex);
}
/// <summary>
/// A global variable that is automatically incremented on every
/// new object created by Bogus. Useful for composing property values that require
/// uniqueness.
/// </summary>
public static int GlobalUniqueIndex = 0;
/// <summary>
/// A global variable that is automatically incremented on every
/// new object created by Bogus. Useful for composing property values that require
/// uniqueness.
/// </summary>
public int UniqueIndex => GlobalUniqueIndex;
/// <summary>
/// HashID generator with default (string.Empty) salt. See: https://github.com/ullmark/hashids.net
/// </summary>
public Hashids Hashids { get; set; }
}
}