Exemplo n.º 1
0
        public void WriteSingleRegularExpression()
        {
            using (var stream = new MemoryStream())
              using (var reader = new BinaryReader(stream))
              {
            var root = new BSONDocument();
            var pattern = @"^[-.\w]+@(?:[a-z\d]{2,}\.)+[a-z]{2,6}$";
            var options = BSONRegularExpressionOptions.I | BSONRegularExpressionOptions.M |BSONRegularExpressionOptions.U;
            var regex = new BSONRegularExpression(pattern, options);
            root.Set(new BSONRegularExpressionElement("email", regex));
            root.WriteAsBSON(stream);

            Assert.AreEqual(stream.Position, 55); // ensure document length is 55 bytes

            stream.Seek(0, SeekOrigin.Begin);

            CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(55));       // ensure content length is 55
            Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.RegularExpression);     // ensure element type is RegularExpression 0x0b
            CollectionAssert.AreEqual(reader.ReadBytes(5), Encoding.UTF8.GetBytes("email")); // ensure element name is 'email'
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                  // ensure string name terminator 0x00 is present

            CollectionAssert.AreEqual(reader.ReadBytes(38), Encoding.UTF8.GetBytes(pattern)); // ensure element value is pattern
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                   // ensure string value terminator 0x00 is present

            CollectionAssert.AreEqual(reader.ReadBytes(3), Encoding.UTF8.GetBytes("imu")); // ensure element value is options in BSON format
            Assert.AreEqual(reader.ReadByte(), (byte)0x00);                                // ensure string value terminator 0x00 is present

            Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00

            Assert.AreEqual(stream.Position, 55); // ensure whole document readed
              }
        }
Exemplo n.º 2
0
 /// <summary>
 /// { email: <pattern='^[-.\w]+@(?:[a-z\d]{2,}\.)+[a-z]{2,6}$' options=I,M,U> }
 /// </summary>
 public void WriteSingleRegularExpression(Stream stream)
 {
     var root = new BSONDocument();
       var pattern = @"^[-.\w]+@(?:[a-z\d]{2,}\.)+[a-z]{2,6}$";
       var options = BSONRegularExpressionOptions.I | BSONRegularExpressionOptions.M |BSONRegularExpressionOptions.U;
       var regex = new BSONRegularExpression(pattern, options);
       root.Set(new BSONRegularExpressionElement("email", regex));
       root.WriteAsBSON(stream);
 }