Skip to content

Commit

Permalink
Merge remote branch 'samus/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown authored and unknown committed Aug 9, 2010
2 parents f604a53 + f264954 commit 93ccee0
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 2 deletions.
Expand Up @@ -354,6 +354,20 @@ where Regex.IsMatch(p.FirstName, "Joe")
Assert.AreEqual(new Document("FirstName", new MongoRegex("Joe")), queryObject.Query);
}

[Test]
public void Regex_IsMatch_CaseInsensitive()
{
var people = from p in Collection.Linq()
where Regex.IsMatch(p.FirstName, "Joe", RegexOptions.IgnoreCase)
select p;

var queryObject = ((IMongoQueryable)people).GetQueryObject();
Assert.AreEqual(0, queryObject.Fields.Count);
Assert.AreEqual(0, queryObject.NumberToLimit);
Assert.AreEqual(0, queryObject.NumberToSkip);
Assert.AreEqual(new Document("FirstName", new MongoRegex("Joe", MongoRegexOption.IgnoreCase)), queryObject.Query);
}

[Test]
public void SingleEqualConstraint()
{
Expand Down
10 changes: 10 additions & 0 deletions source/MongoDB.Tests/IntegrationTests/Linq/MongoQueryTests.cs
Expand Up @@ -370,6 +370,16 @@ where Regex.IsMatch(p.FirstName, "Joe")
Assert.AreEqual(1, people.Count);
}

[Test]
public void Regex_IsMatch_CaseInsensitive()
{
var people = (from p in Collection.Linq()
where Regex.IsMatch(p.FirstName, "joe", RegexOptions.IgnoreCase)
select p).ToList();

Assert.AreEqual(1, people.Count);
}

[Test]
public void Single()
{
Expand Down
16 changes: 16 additions & 0 deletions source/MongoDB.Tests/UnitTests/TestMongoRegex.cs
Expand Up @@ -45,6 +45,14 @@ public void CanBeConstructedFromRegex()
Assert.AreEqual("img", regex.RawOptions);
}

[Test]
public void MongoRegexOptionFlagsAreIndependent()
{
var regex = new MongoRegex("expression", MongoRegexOption.IgnoreCase);
Assert.AreEqual("expression", regex.Expression);
Assert.AreEqual("i", regex.RawOptions);
}

[Test]
public void CanBeConstructedWithMongoRegexOption()
{
Expand All @@ -53,6 +61,14 @@ public void CanBeConstructedWithMongoRegexOption()
Assert.AreEqual("img", regex.RawOptions);
}

[Test]
public void CanBeConstructedWithRegexOptions()
{
var regex = new MongoRegex("expression", RegexOptions.IgnoreCase | RegexOptions.Multiline);
Assert.AreEqual("expression", regex.Expression);
Assert.AreEqual("im", regex.RawOptions);
}

[Test]
public void CanReadOptions()
{
Expand Down
6 changes: 5 additions & 1 deletion source/MongoDB/Linq/Translators/DocumentFormatter.cs
Expand Up @@ -225,7 +225,11 @@ protected override Expression VisitMethodCall(MethodCallExpression m)
else
throw new InvalidQueryException(string.Format("Only the static Regex.IsMatch is supported.", m.Method.Name));

AddCondition(new MongoRegex(value));
var regexOptions = RegexOptions.None;
if (m.Arguments.Count > 2)
regexOptions = EvaluateConstant<RegexOptions>(m.Arguments[2]);

AddCondition(new MongoRegex(value, regexOptions));
PopConditionScope();
return m;
}
Expand Down
10 changes: 10 additions & 0 deletions source/MongoDB/MongoRegex.cs
Expand Up @@ -38,6 +38,16 @@ public MongoRegex(string expression, MongoRegexOption options)
Options = options;
}

/// <summary>
/// Initializes a new instance of the <see cref="MongoRegex"/> class.
/// </summary>
/// <param name="expression">The Regex expression.</param>
/// <param name="options">The Regex options.</param>
public MongoRegex(string expression, RegexOptions options)
: this(new Regex(expression, options))
{
}

/// <summary>
/// Initializes a new instance of the <see cref = "MongoRegex" /> class.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion source/MongoDB/MongoRegexOption.cs
Expand Up @@ -25,6 +25,6 @@ public enum MongoRegexOption
/// <summary>
/// g - Eliminates unescaped white space from the pattern.
/// </summary>
IgnorePatternWhitespace = 3
IgnorePatternWhitespace = 4
}
}

0 comments on commit 93ccee0

Please sign in to comment.