Skip to content

Commit

Permalink
Enable construction of MongoRegex from Regex.
Browse files Browse the repository at this point in the history
  • Loading branch information
lanwin committed Jun 1, 2010
1 parent 3af0514 commit 5ae43be
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
13 changes: 13 additions & 0 deletions source/MongoDB.Tests/UnitTests/TestMongoRegex.cs
@@ -1,5 +1,6 @@
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
using NUnit.Framework;

Expand Down Expand Up @@ -32,6 +33,18 @@ public void CanBeConstructedWithOption()
Assert.AreEqual("options",regex.RawOptions);
}

[Test]
public void CanBeConstructedFromRegex()
{
const RegexOptions options = RegexOptions.IgnoreCase |
RegexOptions.IgnorePatternWhitespace |
RegexOptions.Multiline;

var regex = new MongoRegex(new Regex("expression", options));
Assert.AreEqual("expression", regex.Expression);
Assert.AreEqual("img", regex.RawOptions);
}

[Test]
public void CanBeConstructedWithMongoRegexOption()
{
Expand Down
17 changes: 17 additions & 0 deletions source/MongoDB/MongoRegex.cs
@@ -1,4 +1,5 @@
using System;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
Expand Down Expand Up @@ -37,6 +38,22 @@ public MongoRegex(string expression,MongoRegexOption options)
Options = options;
}

/// <summary>
/// Initializes a new instance of the <see cref="MongoRegex"/> class.
/// </summary>
/// <param name="regex">The regex.</param>
public MongoRegex(Regex regex)
{
if(regex == null)
throw new ArgumentNullException("regex");

Expression = regex.ToString();

ToggleOption("i", (regex.Options & RegexOptions.IgnoreCase) != 0);
ToggleOption("m", (regex.Options & RegexOptions.Multiline) != 0);
ToggleOption("g", (regex.Options & RegexOptions.IgnorePatternWhitespace) != 0);
}

/// <summary>
/// Initializes a new instance of the <see cref = "MongoRegex" /> class.
/// </summary>
Expand Down

0 comments on commit 5ae43be

Please sign in to comment.