Browse Source

Implemented CSHARP-243. Added support for mappping between new regex options and equivalent .NET Regex options.

pull/62/merge
rstam 14 years ago
parent
commit
cac500ef1d
  1. 20
      Bson/ObjectModel/BsonRegularExpression.cs
  2. 119
      BsonUnitTests/ObjectModel/BsonValueTests.cs

20
Bson/ObjectModel/BsonRegularExpression.cs

@ -74,11 +74,19 @@ namespace MongoDB.Bson {
)
: base(BsonType.RegularExpression) {
this.pattern = regex.ToString();
// TODO: figure out how other .NET options map to JavaScript options
this.options = "";
if ((regex.Options & RegexOptions.IgnoreCase) != 0) {
this.options += "i";
}
if ((regex.Options & RegexOptions.Multiline) != 0) {
this.options += "m";
}
if ((regex.Options & RegexOptions.IgnorePatternWhitespace) != 0) {
this.options += "x";
}
if ((regex.Options & RegexOptions.Singleline) != 0) {
this.options += "s";
}
}
#endregion
@ -258,11 +266,19 @@ namespace MongoDB.Bson {
/// </summary>
/// <returns>A Regex.</returns>
public Regex ToRegex() {
// TODO: figure out how other JavaScript options map to .NET options
var options = RegexOptions.None;
if (this.options.Contains("i")) {
options |= RegexOptions.IgnoreCase;
}
if (this.options.Contains("m")) {
options |= RegexOptions.Multiline;
}
if (this.options.Contains("x")) {
options |= RegexOptions.IgnorePatternWhitespace;
}
if (this.options.Contains("s")) {
options |= RegexOptions.Singleline;
}
return new Regex(pattern, options);
}

119
BsonUnitTests/ObjectModel/BsonValueTests.cs

@ -269,7 +269,23 @@ namespace MongoDB.BsonUnitTests {
Assert.Throws<InvalidCastException>(() => { var x = s.AsObjectId; });
}
public void TestAsRegex() {
public void TestAsRegexOptionNone() {
BsonValue v = new BsonRegularExpression("xyz");
BsonValue s = "";
var r = v.AsRegex;
Assert.AreEqual(0, r.Options);
Assert.Throws<InvalidCastException>(() => { var x = s.AsRegex; });
}
public void TestAsRegexOptionAll() {
BsonValue v = new BsonRegularExpression("xyz", "imxs");
BsonValue s = "";
var r = v.AsRegex;
Assert.AreEqual(RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline, r.Options);
Assert.Throws<InvalidCastException>(() => { var x = s.AsRegex; });
}
public void TestAsRegexOptionI() {
BsonValue v = new BsonRegularExpression("xyz", "i");
BsonValue s = "";
var r = v.AsRegex;
@ -277,6 +293,30 @@ namespace MongoDB.BsonUnitTests {
Assert.Throws<InvalidCastException>(() => { var x = s.AsRegex; });
}
public void TestAsRegexOptionM() {
BsonValue v = new BsonRegularExpression("xyz", "m");
BsonValue s = "";
var r = v.AsRegex;
Assert.AreEqual(RegexOptions.Multiline, r.Options);
Assert.Throws<InvalidCastException>(() => { var x = s.AsRegex; });
}
public void TestAsRegexOptionX() {
BsonValue v = new BsonRegularExpression("xyz", "x");
BsonValue s = "";
var r = v.AsRegex;
Assert.AreEqual(RegexOptions.IgnorePatternWhitespace, r.Options);
Assert.Throws<InvalidCastException>(() => { var x = s.AsRegex; });
}
public void TestAsRegexOptionS() {
BsonValue v = new BsonRegularExpression("xyz", "s");
BsonValue s = "";
var r = v.AsRegex;
Assert.AreEqual(RegexOptions.Singleline, r.Options);
Assert.Throws<InvalidCastException>(() => { var x = s.AsRegex; });
}
public void TestAsString() {
BsonValue v = "Hello";
BsonValue i = 1;
@ -312,10 +352,30 @@ namespace MongoDB.BsonUnitTests {
Assert.AreEqual("pattern", regex.Pattern);
Assert.AreEqual("", regex.Options);
regex = BsonRegularExpression.Create(new Regex("pattern", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline));
Assert.IsInstanceOf<BsonRegularExpression>(regex);
Assert.AreEqual("pattern", regex.Pattern);
Assert.AreEqual("imxs", regex.Options);
regex = BsonRegularExpression.Create(new Regex("pattern", RegexOptions.IgnoreCase));
Assert.IsInstanceOf<BsonRegularExpression>(regex);
Assert.AreEqual("pattern", regex.Pattern);
Assert.AreEqual("i", regex.Options);
regex = BsonRegularExpression.Create(new Regex("pattern", RegexOptions.Multiline));
Assert.IsInstanceOf<BsonRegularExpression>(regex);
Assert.AreEqual("pattern", regex.Pattern);
Assert.AreEqual("m", regex.Options);
regex = BsonRegularExpression.Create(new Regex("pattern", RegexOptions.IgnorePatternWhitespace));
Assert.IsInstanceOf<BsonRegularExpression>(regex);
Assert.AreEqual("pattern", regex.Pattern);
Assert.AreEqual("x", regex.Options);
regex = BsonRegularExpression.Create(new Regex("pattern", RegexOptions.Singleline));
Assert.IsInstanceOf<BsonRegularExpression>(regex);
Assert.AreEqual("pattern", regex.Pattern);
Assert.AreEqual("s", regex.Options);
}
[Test]
@ -534,7 +594,29 @@ namespace MongoDB.BsonUnitTests {
}
[Test]
public void TestImplicitConversionFromRegex() {
public void TestImplicitConversionFromRegexOptionNone() {
BsonValue v = new Regex("xyz");
BsonValue n = (Regex) null;
Assert.IsInstanceOf<BsonRegularExpression>(v);
Assert.IsNull(n);
var r = (BsonRegularExpression) v;
Assert.AreEqual("xyz", r.Pattern);
Assert.AreEqual("", r.Options);
}
[Test]
public void TestImplicitConversionFromRegexOptionAll() {
BsonValue v = new Regex("xyz", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline);
BsonValue n = (Regex) null;
Assert.IsInstanceOf<BsonRegularExpression>(v);
Assert.IsNull(n);
var r = (BsonRegularExpression) v;
Assert.AreEqual("xyz", r.Pattern);
Assert.AreEqual("imxs", r.Options);
}
[Test]
public void TestImplicitConversionFromRegexOptionI() {
BsonValue v = new Regex("xyz", RegexOptions.IgnoreCase);
BsonValue n = (Regex) null;
Assert.IsInstanceOf<BsonRegularExpression>(v);
@ -544,6 +626,39 @@ namespace MongoDB.BsonUnitTests {
Assert.AreEqual("i", r.Options);
}
[Test]
public void TestImplicitConversionFromRegexOptionM() {
BsonValue v = new Regex("xyz", RegexOptions.Multiline);
BsonValue n = (Regex) null;
Assert.IsInstanceOf<BsonRegularExpression>(v);
Assert.IsNull(n);
var r = (BsonRegularExpression) v;
Assert.AreEqual("xyz", r.Pattern);
Assert.AreEqual("m", r.Options);
}
[Test]
public void TestImplicitConversionFromRegexOptionX() {
BsonValue v = new Regex("xyz", RegexOptions.IgnorePatternWhitespace);
BsonValue n = (Regex) null;
Assert.IsInstanceOf<BsonRegularExpression>(v);
Assert.IsNull(n);
var r = (BsonRegularExpression) v;
Assert.AreEqual("xyz", r.Pattern);
Assert.AreEqual("x", r.Options);
}
[Test]
public void TestImplicitConversionFromRegexOptionS() {
BsonValue v = new Regex("xyz", RegexOptions.Singleline);
BsonValue n = (Regex) null;
Assert.IsInstanceOf<BsonRegularExpression>(v);
Assert.IsNull(n);
var r = (BsonRegularExpression) v;
Assert.AreEqual("xyz", r.Pattern);
Assert.AreEqual("s", r.Options);
}
[Test]
public void TestImplicitConversionFromString() {
BsonValue v = "xyz";

Loading…
Cancel
Save