Beispiel #1
0
        public void AddRangeParamsTest()
        {
            string key1 = AnyInstance.AnyString;
            string key2 = AnyInstance.AnyString2;
            JsonValue value1 = AnyInstance.AnyJsonValue1;
            JsonValue value2 = AnyInstance.AnyJsonValue2;

            List<KeyValuePair<string, JsonValue>> items = new List<KeyValuePair<string, JsonValue>>()
            {
                new KeyValuePair<string, JsonValue>(key1, value1),
                new KeyValuePair<string, JsonValue>(key2, value2),
            };

            JsonObject target;

            target = new JsonObject();
            target.AddRange(items[0], items[1]);
            Assert.AreEqual(2, target.Count);
            ValidateJsonObjectItems(target, key1, value1, key2, value2);

            target = new JsonObject();
            target.AddRange(items.ToArray());
            Assert.AreEqual(2, target.Count);
            ValidateJsonObjectItems(target, key1, value1, key2, value2);

            ExceptionTestHelper.ExpectException<ArgumentNullException>(delegate { new JsonObject().AddRange((KeyValuePair<string, JsonValue>[])null); });
            ExceptionTestHelper.ExpectException<ArgumentNullException>(delegate { new JsonObject().AddRange((IEnumerable<KeyValuePair<string, JsonValue>>)null); });

            items[1] = new KeyValuePair<string, JsonValue>(key2, AnyInstance.DefaultJsonValue);
            ExceptionTestHelper.ExpectException<ArgumentException>(delegate { new JsonObject().AddRange(items.ToArray()); });
            ExceptionTestHelper.ExpectException<ArgumentException>(delegate { new JsonObject().AddRange(items[0], items[1]); });
        }
        public void RemoveKVPTest()
        {
            JsonObject target = new JsonObject();
            KeyValuePair <string, JsonValue> item1 = new KeyValuePair <string, JsonValue>(AnyInstance.AnyString, AnyInstance.AnyJsonValue1);
            KeyValuePair <string, JsonValue> item2 = new KeyValuePair <string, JsonValue>(AnyInstance.AnyString2, AnyInstance.AnyJsonValue2);

            target.AddRange(item1, item2);

            Assert.Equal(2, target.Count);
            Assert.True(((ICollection <KeyValuePair <string, JsonValue> >)target).Contains(item1));
            Assert.True(((ICollection <KeyValuePair <string, JsonValue> >)target).Contains(item2));

            Assert.True(((ICollection <KeyValuePair <string, JsonValue> >)target).Remove(item1));
            Assert.Equal(1, target.Count);
            Assert.False(((ICollection <KeyValuePair <string, JsonValue> >)target).Contains(item1));
            Assert.True(((ICollection <KeyValuePair <string, JsonValue> >)target).Contains(item2));

            Assert.False(((ICollection <KeyValuePair <string, JsonValue> >)target).Remove(item1));
            Assert.Equal(1, target.Count);
            Assert.False(((ICollection <KeyValuePair <string, JsonValue> >)target).Contains(item1));
            Assert.True(((ICollection <KeyValuePair <string, JsonValue> >)target).Contains(item2));
        }
        public void JsonObjectAddRemoveFunctionalTest()
        {
            int seed = 1;

            for (int i = 0; i < iterationCount / 10; i++)
            {
                seed++;
                Log.Info("Seed: {0}", seed);
                Random rndGen   = new Random(seed);
                bool   retValue = true;

                JsonObject sourceJson = SpecialJsonValueHelper.CreateIndexPopulatedJsonObject(seed, arrayLength);

                // JsonObject.JsonType
                if (sourceJson.JsonType != JsonType.Object)
                {
                    Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonArray.JsonType failed to function properly.");
                    retValue = false;
                }

                // JsonObject.Add(KeyValuePair<string, JsonValue> item)
                // JsonObject.Add(string key, JsonValue value)
                // + various numers below so .AddRange() won't try to add an already existing value
                sourceJson.Add(SpecialJsonValueHelper.GetUniqueNonNullInstanceOfString(seed + 3, sourceJson), SpecialJsonValueHelper.GetUniqueValue(seed, sourceJson));
                KeyValuePair <string, JsonValue> kvp;
                int startingSeed = seed + 1;
                do
                {
                    kvp = SpecialJsonValueHelper.CreatePrePopulatedKeyValuePair(startingSeed);
                    startingSeed++;
                }while (sourceJson.ContainsKey(kvp.Key));

                sourceJson.Add(kvp);
                do
                {
                    kvp = SpecialJsonValueHelper.CreatePrePopulatedKeyValuePair(startingSeed);
                    startingSeed++;
                }while (sourceJson.ContainsKey(kvp.Key));

                sourceJson.Add(kvp);
                if (sourceJson.Count != arrayLength + 3)
                {
                    Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.Add() failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.Add() passed test.");
                }

                // JsonObject.Clear()
                sourceJson.Clear();
                if (sourceJson.Count > 0)
                {
                    Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.Clear() failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.Clear() passed test.");
                }

                // JsonObject.AddRange(IEnumerable<KeyValuePair<string, JsonValue>> items)
                sourceJson = SpecialJsonValueHelper.CreateIndexPopulatedJsonObject(seed, arrayLength);

                // + various numers below so .AddRange() won't try to add an already existing value
                sourceJson.AddRange(SpecialJsonValueHelper.CreatePrePopulatedListofKeyValuePair(seed + 13 + (arrayLength * 2), 5));
                if (sourceJson.Count != arrayLength + 5)
                {
                    Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.AddRange(IEnumerable<KeyValuePair<string, JsonValue>> items) failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.AddRange(IEnumerable<KeyValuePair<string, JsonValue>> items) passed test.");
                }

                // JsonObject.AddRange(params KeyValuePair<string, JsonValue>[] items)
                sourceJson = SpecialJsonValueHelper.CreateIndexPopulatedJsonObject(seed, arrayLength);

                // + various numers below so .AddRange() won't try to add an already existing value
                KeyValuePair <string, JsonValue> item1 = SpecialJsonValueHelper.CreatePrePopulatedKeyValuePair(seed + arrayLength + 41);
                KeyValuePair <string, JsonValue> item2 = SpecialJsonValueHelper.CreatePrePopulatedKeyValuePair(seed + arrayLength + 47);
                KeyValuePair <string, JsonValue> item3 = SpecialJsonValueHelper.CreatePrePopulatedKeyValuePair(seed + arrayLength + 53);
                sourceJson.AddRange(new KeyValuePair <string, JsonValue>[] { item1, item2, item3 });
                if (sourceJson.Count != arrayLength + 3)
                {
                    Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.AddRange(params KeyValuePair<string, JsonValue>[] items) failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.AddRange(params KeyValuePair<string, JsonValue>[] items) passed test.");
                }

                sourceJson.Clear();

                // JsonObject.Remove(Key)
                sourceJson = SpecialJsonValueHelper.CreateIndexPopulatedJsonObject(seed, arrayLength);
                int           count = sourceJson.Count;
                List <string> keys  = new List <string>(sourceJson.Keys);
                foreach (string key in keys)
                {
                    sourceJson.Remove(key);
                }

                if (sourceJson.Count > 0)
                {
                    Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.Remove(Key) failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.Remove(Key) passed test.");
                }

                Assert.True(retValue);
            }
        }
Beispiel #4
0
        public void RemoveKVPTest()
        {
            JsonObject target = new JsonObject();
            KeyValuePair<string, JsonValue> item1 = new KeyValuePair<string, JsonValue>(AnyInstance.AnyString, AnyInstance.AnyJsonValue1);
            KeyValuePair<string, JsonValue> item2 = new KeyValuePair<string, JsonValue>(AnyInstance.AnyString2, AnyInstance.AnyJsonValue2);
            target.AddRange(item1, item2);

            Assert.AreEqual(2, target.Count);
            Assert.IsTrue(((ICollection<KeyValuePair<string, JsonValue>>)target).Contains(item1));
            Assert.IsTrue(((ICollection<KeyValuePair<string, JsonValue>>)target).Contains(item2));

            Assert.IsTrue(((ICollection<KeyValuePair<string, JsonValue>>)target).Remove(item1));
            Assert.AreEqual(1, target.Count);
            Assert.IsFalse(((ICollection<KeyValuePair<string, JsonValue>>)target).Contains(item1));
            Assert.IsTrue(((ICollection<KeyValuePair<string, JsonValue>>)target).Contains(item2));

            Assert.IsFalse(((ICollection<KeyValuePair<string, JsonValue>>)target).Remove(item1));
            Assert.AreEqual(1, target.Count);
            Assert.IsFalse(((ICollection<KeyValuePair<string, JsonValue>>)target).Contains(item1));
            Assert.IsTrue(((ICollection<KeyValuePair<string, JsonValue>>)target).Contains(item2));
        }