Esempio n. 1
0
        public void JsonObjectItemsFunctionalTest()
        {
            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[key].set_Item
                sourceJson["1"] = new JsonPrimitive(true);
                if (sourceJson["1"].ToString() != "true")
                {
                    Log.Info("[JsonObjectItemsFunctionalTest] JsonObject[key].set_Item failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonObjectItemsFunctionalTest] JsonObject[key].set_Item passed test.");
                }

                // ICollection<KeyValuePair<string, JsonValue>>.Contains(KeyValuePair<string, JsonValue> item)
                KeyValuePair <string, System.Json.JsonValue> kp = new KeyValuePair <string, JsonValue>("5", sourceJson["5"]);
                if (!((ICollection <KeyValuePair <string, JsonValue> >)sourceJson).Contains(kp))
                {
                    Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.Contains(KeyValuePair<string, JsonValue> item) failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.Contains(KeyValuePair<string, JsonValue> item) passed test.");
                }

                // ICollection<KeyValuePair<string, JsonValue>>.IsReadOnly
                if (((ICollection <KeyValuePair <string, JsonValue> >)sourceJson).IsReadOnly)
                {
                    Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.IsReadOnly failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.IsReadOnly passed test.");
                }

                // ICollection<KeyValuePair<string, JsonValue>>.Add(KeyValuePair<string, JsonValue> item)
                kp = new KeyValuePair <string, JsonValue>("100", new JsonPrimitive(100));
                ((ICollection <KeyValuePair <string, JsonValue> >)sourceJson).Add(kp);
                if (sourceJson.Count != arrayLength + 1)
                {
                    Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.Add(KeyValuePair<string, JsonValue> item) failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.Add(KeyValuePair<string, JsonValue> item) passed test.");
                }

                // ICollection<KeyValuePair<string, JsonValue>>.Remove(KeyValuePair<string, JsonValue> item)
                ((ICollection <KeyValuePair <string, JsonValue> >)sourceJson).Remove(kp);
                if (sourceJson.Count != arrayLength)
                {
                    Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.Remove(KeyValuePair<string, JsonValue> item) failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.Remove(KeyValuePair<string, JsonValue> item) passed test.");
                }

                // ICollection<KeyValuePair<string, JsonValue>>.GetEnumerator()
                JsonObject jo = new JsonObject {
                    { "member 1", 123 }, { "member 2", new JsonArray {
                                               1, 2, 3
                                           } }
                };
                List <string> expected = new List <string> {
                    "member 1 - 123", "member 2 - [1,2,3]"
                };
                expected.Sort();
                IEnumerator <KeyValuePair <string, JsonValue> > ko = ((ICollection <KeyValuePair <string, JsonValue> >)jo).GetEnumerator();
                List <string> actual = new List <string>();
                ko.Reset();
                ko.MoveNext();
                do
                {
                    actual.Add(String.Format("{0} - {1}", ko.Current.Key, ko.Current.Value));
                    Log.Info("added one item: {0}", String.Format("{0} - {1}", ko.Current.Key, ko.Current.Value));
                    ko.MoveNext();
                }while (ko.Current.Value != null);

                actual.Sort();
                if (!JsonValueVerifier.CompareStringLists(expected, actual))
                {
                    Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.GetEnumerator() failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonObjectItemsFunctionalTest] ICollection<KeyValuePair<string, JsonValue>>.GetEnumerator() passed test.");
                }

                // JsonObject.Values
                sourceJson = SpecialJsonValueHelper.CreateIndexPopulatedJsonObject(seed, arrayLength);
                JsonValue[] manyValues = SpecialJsonValueHelper.CreatePrePopulatedJsonValueArray(seed, arrayLength);
                JsonObject  jov        = new JsonObject();
                for (int j = 0; j < manyValues.Length; j++)
                {
                    jov.Add("member" + j, manyValues[j]);
                }

                List <string> expectedList = new List <string>();
                foreach (JsonValue v in manyValues)
                {
                    expectedList.Add(v.ToString());
                }

                expectedList.Sort();
                List <string> actualList = new List <string>();
                foreach (JsonValue v in jov.Values)
                {
                    actualList.Add(v.ToString());
                }

                actualList.Sort();
                if (!JsonValueVerifier.CompareStringLists(expectedList, actualList))
                {
                    Log.Info("[JsonObjectItemsFunctionalTest] JsonObject.Values failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonObjectItemsFunctionalTest] JsonObject.Values passed test.");
                }

                for (int j = 0; j < sourceJson.Count; j++)
                {
                    // JsonObject.Contains(Key)
                    if (!sourceJson.ContainsKey(j.ToString()))
                    {
                        Log.Info("[JsonObjectItemsFunctionalTest] JsonObject.Contains(Key) failed to function properly.");
                        retValue = false;
                    }
                    else
                    {
                        Log.Info("[JsonObjectItemsFunctionalTest] JsonObject.Contains(Key) passed test.");
                    }

                    // JsonObject.TryGetValue(String, out JsonValue)
                    JsonValue retJson;
                    if (!sourceJson.TryGetValue(j.ToString(), out retJson))
                    {
                        Log.Info("[JsonObjectItemsFunctionalTest] JsonObject.TryGetValue(String, out JsonValue) failed to function properly.");
                        retValue = false;
                    }
                    else if (retJson != sourceJson[j.ToString()])
                    {
                        // JsonObjectthis[string key]
                        Log.Info("[JsonObjectItemsFunctionalTest] JsonObject[string key] or JsonObject.TryGetValue(String, out JsonValue) failed to function properly.");
                        retValue = false;
                    }
                    else
                    {
                        Log.Info("[JsonObjectItemsFunctionalTest] JsonObject.TryGetValue(String, out JsonValue) & JsonObject[string key] passed test.");
                    }
                }

                if (!retValue)
                {
                    Assert.Fail("[JsonObjectItemsFunctionalTest] Test failed!");
                }
            }
        }
Esempio n. 2
0
        public void JsonArrayAddRemoveFunctionalTest()
        {
            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;

                JsonArray   sourceJson = SpecialJsonValueHelper.CreatePrePopulatedJsonArray(seed, arrayLength);
                JsonValue[] cloneJson  = SpecialJsonValueHelper.CreatePrePopulatedJsonValueArray(seed, 3);

                // JsonArray.AddRange(JsonValue[])
                sourceJson.AddRange(cloneJson);
                if (sourceJson.Count != arrayLength + cloneJson.Length)
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.AddRange(JsonValue[]) failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.AddRange(JsonValue[]) passed test.");
                }

                // JsonArray.RemoveAt(int)
                int count = sourceJson.Count;
                for (int j = 0; j < count; j++)
                {
                    sourceJson.RemoveAt(0);
                }

                if (sourceJson.Count > 0)
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.RemoveAt(int) failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.RemoveAt(int) passed test.");
                }

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

                // JsonArray.Clear()
                sourceJson = SpecialJsonValueHelper.CreatePrePopulatedJsonArray(seed, arrayLength);
                sourceJson.Clear();
                if (sourceJson.Count > 0)
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.Clear() failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.Clear() passed test.");
                }

                // JsonArray.AddRange(JsonValue)
                sourceJson = SpecialJsonValueHelper.CreatePrePopulatedJsonArray(seed, arrayLength);

                // adding one additional value to the array
                sourceJson.AddRange(SpecialJsonValueHelper.GetRandomJsonPrimitives(seed));
                if (sourceJson.Count != arrayLength + 1)
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.AddRange(JsonValue) failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.AddRange(JsonValue) passed test.");
                }

                // JsonArray.AddRange(IEnumerable<JsonValue> items)
                sourceJson = SpecialJsonValueHelper.CreatePrePopulatedJsonArray(seed, arrayLength);
                MyJsonValueCollection <JsonValue> myCols = new MyJsonValueCollection <JsonValue>();
                myCols.Add(new JsonPrimitive(PrimitiveCreator.CreateInstanceOfUInt32(rndGen)));
                string str;
                do
                {
                    str = PrimitiveCreator.CreateInstanceOfString(rndGen);
                } while (str == null);

                myCols.Add(new JsonPrimitive(str));
                myCols.Add(new JsonPrimitive(PrimitiveCreator.CreateInstanceOfDateTime(rndGen)));

                // adding 3 additional value to the array
                sourceJson.AddRange(myCols);
                if (sourceJson.Count != arrayLength + 3)
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.AddRange(IEnumerable<JsonValue> items) failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.AddRange(IEnumerable<JsonValue> items) passed test.");
                }

                // JsonArray[index].set_Item
                sourceJson = SpecialJsonValueHelper.CreatePrePopulatedJsonArray(seed, arrayLength);
                string temp;
                do
                {
                    temp = PrimitiveCreator.CreateInstanceOfString(rndGen);
                } while (temp == null);

                sourceJson[1] = temp;
                if ((string)sourceJson[1] != temp)
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray[index].set_Item failed to function properly.");
                    retValue = false;
                }
                else
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray[index].set_Item passed test.");
                }

                // JsonArray.Remove(JsonValue)
                count = sourceJson.Count;
                for (int j = 0; j < count; j++)
                {
                    sourceJson.Remove(sourceJson[0]);
                }

                if (sourceJson.Count > 0)
                {
                    Log.Info("[JsonArrayAddRemoveFunctionalTest] JsonArray.Remove(JsonValue) failed to function properly.");
                    retValue = false;
                }

                if (!retValue)
                {
                    Assert.Fail("[JsonArrayAddRemoveFunctionalTest] Test failed!");
                }
            }
        }