-
Notifications
You must be signed in to change notification settings - Fork 337
/
RegularExpressionGenerator.cs
80 lines (71 loc) · 2.53 KB
/
RegularExpressionGenerator.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
using System;
using System.Text.RegularExpressions;
using AutoFixture.Kernel;
using Fare;
namespace AutoFixture
{
/// <summary>
/// Creates a string that is guaranteed to match a RegularExpressionRequest.
/// </summary>
public class RegularExpressionGenerator : ISpecimenBuilder
{
private readonly Random random;
private readonly object syncRoot;
/// <summary>
/// Initializes a new instance of the <see cref="RegularExpressionGenerator"/> class.
/// </summary>
public RegularExpressionGenerator()
{
this.random = new Random();
this.syncRoot = new object();
}
/// <summary>
/// Creates a string that is guaranteed to match a RegularExpressionRequest.
/// </summary>
/// <param name="request">The request that describes what to create.</param>
/// <param name="context">A context that can be used to create other specimens.</param>
/// <returns>
/// The requested specimen if possible; otherwise a <see cref="NoSpecimen"/> instance.
/// </returns>
public object Create(object request, ISpecimenContext context)
{
if (request == null) return new NoSpecimen();
var regularExpressionRequest = request as RegularExpressionRequest;
if (regularExpressionRequest == null)
{
return new NoSpecimen();
}
return this.GenerateRegularExpression(regularExpressionRequest);
}
private object GenerateRegularExpression(RegularExpressionRequest request)
{
string pattern = request.Pattern;
try
{
// Use the Xeger constructor overload that that takes an instance of Random.
// Otherwise identically strings can be generated, if regex are generated within short time.
string regex = new Xeger(pattern, new Random(this.GenerateSeed())).Generate();
if (Regex.IsMatch(regex, pattern))
{
return regex;
}
}
catch (InvalidOperationException)
{
return new NoSpecimen();
}
catch (ArgumentException)
{
return new NoSpecimen();
}
return new NoSpecimen();
}
private int GenerateSeed()
{
lock (this.syncRoot)
{
return this.random.Next();
}
}
}
}