public override string Generate(Random random) { int numRepeat; StringBuilder buffer = new StringBuilder(); if (this == RegexCompiler.InvalidNode) { //randomly choose to repeat more or less than the given range int repeatMore = random.Next(2); if ((mMaxRepeat != -1 && 1 == repeatMore) || mMinRepeat == 0) { //repeat more than the given range checked { numRepeat = random.Next(mMaxRepeat + 1, mMaxRepeat + 11); } } else { //repeat less than the given range numRepeat = random.Next(0, mMinRepeat); } } else { //repeat for some number inside the given range checked { int maxRepeat = (mMaxRepeat == -1) ? mMinRepeat + extraRepetitions : mMaxRepeat; //don't repeat zero times if the repeated node is on the invalidating path int minRepeat = (mMinRepeat == 0 && mRefNode == mReservedPath) ? 1 : mMinRepeat; numRepeat = (minRepeat < maxRepeat) ? random.Next(minRepeat, maxRepeat + 1) : minRepeat; } } string childStr; if (mRefNode is RegexTextNode) //If the referenced node is text node, only repeat the last character { childStr = mRefNode.Generate(random); buffer.Append(childStr.Substring(0, childStr.Length - 1)); childStr = childStr[childStr.Length - 1].ToString(); //Get last character mSameValue = true; } else { childStr = mRefNode.Generate(random); } for (int i = 0; i < numRepeat; i++) { buffer.Append(mSameValue ? childStr : mRefNode.Generate(random)); } return(buffer.ToString()); }
public override string Generate(Random random) { if (mReservedPath != null) { //call the reserved path return(mReservedPath.Generate(random)); } else { //call a random path return(Children[random.Next(Children.Count)].Generate(random)); } }
/// <summary> /// Returns a random string, conforming to the provided regular expression pattern. /// </summary> /// <param name="regex">The regular expression, which the generated string should conform to.</param> /// <param name="seed">The random number generator seed.</param> /// <returns>A string, conforming to the provided regular expression pattern.</returns> /// /// <example> /// The following example demonstrates how to generate a random string from a Regex pattern: /// <code lang="C#" > /// // Using a custom regex pattern... /// Regex emailAddress1 = new Regex(@"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$"); /// string s1 = StringFactory.GenerateRandomString(emailAddress1, 1234); /// /// // Using a standard regex pattern... /// Regex emailAddress2 = CommonRegexPatterns.EmailAddress; /// string s2 = StringFactory.GenerateRandomString(emailAddress2, 1234); /// </code> /// </example> public static string GenerateRandomString(Regex regex, int seed) { Random random = new Random(seed); //reset the static variables RegexCompiler.IsInvalidSection = false; RegexCompiler.InvalidNode = null; RegexCompiler.InvalidableNodes.Clear(); //construct the RegEx tree RegexCompiler compiler = new RegexCompiler(); RegexNode node = compiler.Compile(regex.ToString()); //search for a signal to invalidate a node if (regex.ToString().IndexOf("\\i") != -1) { //something should have been invalidated //select a node to invalidate if (RegexCompiler.InvalidableNodes.Count == 0) { throw new ArgumentException("Asked to generate invalid: Impossible to invalidate"); } RegexCompiler.InvalidNode = RegexCompiler.InvalidableNodes[random.Next(RegexCompiler.InvalidableNodes.Count)]; //Mark REOrNodes and RERepeatNodes to ensure that the invalid node will be part of the string RegexCompiler.InvalidNode.ReservePath(null); } //generate and return the string string result = node.Generate(random); if (RegexCompiler.InvalidNode != null) { //confirm that the generated string is invalid (e.g. [a-z]|[^a-z] will always fail) Regex compare = new Regex("^" + regex.Replace("\\i", "") + "$"); if (compare.IsMatch(result)) { throw new ArgumentException(regex + ": Did not generate invalid string: " + result); } } return(result); }
public override string Generate(Random random) { return(mRefNode.Generate(random)); }