Example #1
0
 public void TestKeyHoles()
 {
     BsonArray ba = new BsonArray();
     ba.Add(1,"A");
     ba.Add(3,"C");
     Assert.IsNull(ba[2]);
 }
Example #2
0
 public static BsonType Create(BsonDataType type)
 {
     BsonType ret = null;
     if(type == BsonDataType.Number){
         ret = new BsonNumber();
     }else if(type == BsonDataType.Number){
         throw new NotImplementedException();
     }else if(type == BsonDataType.String){
         ret = new BsonString();
     }else if(type == BsonDataType.Obj){
         ret = new BsonDocument();
     }else if(type == BsonDataType.Array){
         ret = new BsonArray();
     }else if(type == BsonDataType.Integer){
         ret = new BsonInteger();
     }else if(type == BsonDataType.Long){
         ret = new BsonLong();
     }else if(type == BsonDataType.Boolean){
         ret = new BsonBoolean();
     }else if(type == BsonDataType.Oid){
         ret = new BsonOid();
     }else if(type == BsonDataType.Date){
         ret = new BsonDate();
     }else if(type == BsonDataType.Regex){
         ret = new BsonRegex();
     }else{
         throw new ArgumentOutOfRangeException("Type: " + type + " not recognized");
     }
     return ret;
 }
Example #3
0
        public void TestElementsSameType()
        {
            BsonArray ba = new BsonArray();
            ba.Add(1,"A");
            ba.Add(2,"B");
            ba.Add(3,"C");

            Assert.IsTrue(ba.ElementsSameType());
        }
Example #4
0
        public void TestKeyOrdering()
        {
            BsonArray ba = new BsonArray();
            ba.Add(1,"A");
            ba.Add(2,"B");
            ba.Add(5,"E");
            ba.Add(3,"C");
            ba.Add(4,"D");

            int ikey = 1;
            foreach(string key in ba.Keys){
                Assert.AreEqual(ikey, int.Parse(key));
                ikey++;
            }
        }
Example #5
0
        public void TestOnlyNumericKeys()
        {
            bool thrown = false;
            BsonArray ba = new BsonArray();
            ba.Add("1","A");
            Assert.IsNotNull(ba["1"]);
            try {
                ba.Add("A","A");
            } catch (ArgumentOutOfRangeException) {
                thrown = true;
            }
            if(thrown != true) Assert.Fail("Exception should have been thrown");

            thrown = false;
            try {
                ba["A"] = new BsonElement("A", new BsonString("A"));
            } catch (ArgumentOutOfRangeException) {
                thrown = true;
            }
            if(thrown != true) Assert.Fail("Exception should have been thrown");
        }
Example #6
0
 public static BsonType Create(BsonDataType type)
 {
     BsonType ret = null;
     if(type == BsonDataType.Number){
         ret = new BsonNumber();
     }else if(type == BsonDataType.Number){
         throw new NotImplementedException();
     }else if(type == BsonDataType.String){
         ret = new BsonString();
     }else if(type == BsonDataType.Obj){
         ret = new BsonDocument();
     }else if(type == BsonDataType.Array){
         ret = new BsonArray();
     }else if(type == BsonDataType.Integer){
         ret = new BsonInteger();
     }else if(type == BsonDataType.Long){
         ret = new BsonLong();
     }else if(type == BsonDataType.Boolean){
         ret = new BsonBoolean();
     }else if(type == BsonDataType.Oid){
         ret = new BsonOid();
     }else if(type == BsonDataType.Date){
         ret = new BsonDate();
     }else if(type == BsonDataType.Regex){
         ret = new BsonRegex();
     }else if(type == BsonDataType.Undefined){
         ret = new BsonUndefined();
     }else if(type == BsonDataType.Null){
         ret = new BsonNull();
     }else if(type == BsonDataType.Code){
         ret = new BsonCode();
     }else if(type == BsonDataType.CodeWScope){
         ret = new BsonCodeWScope();
     }else{
         string typename = Enum.GetName(typeof(BsonDataType), type);
         throw new ArgumentOutOfRangeException("type",typename + "is not recognized");
     }
     return ret;
 }
Example #7
0
 public void TestToNativeProducesArrayWithAllValuesTheSameType()
 {
     Document[] songs = new[] {
         new Document().Append("title", "Let The Music Set You Free").Append("length", "5:15"),
         new Document().Append("title", "Sally Likes to Run").Append("length", "4:06"),
         new Document().Append("title", "Deliveries After Dark").Append("length", "4:17"),
         new Document().Append("title", "Theme From The Godfather").Append("length", "3:06"),
         new Document().Append("title", "Grown Man Crying Blues").Append("length", "8:09"),
     };
     BsonArray ba = new BsonArray();
     ba.Add(1,"A");
     ba.Add(2,"B");
     ba.Add(3,"C");
     Object obj = ba.ToNative();
     Assert.AreEqual(typeof(string[]),obj.GetType());
 }
Example #8
0
 public void TestKeysCanBeInts()
 {
     BsonArray ba = new BsonArray();
     ba.Add(1,"A");
     Assert.AreEqual("A", ba[1].Val.ToNative());
 }
Example #9
0
        public static BsonArray From(IEnumerable val)
        {
            BsonArray array = new BsonArray();

            int num = 0;
            foreach (object obj in val){
                array.Add(num++, From(obj));
            }
            return array;
        }