コード例 #1
0
        public void ValidJsonObjectDateTimeOffsetRoundTrip()
        {
            int seed = 1;

            Log.Info("Seed: {0}", seed);
            Random rndGen = new Random(seed);

            JsonPrimitive sourceJson = new JsonPrimitive(PrimitiveCreator.CreateInstanceOfDateTimeOffset(rndGen));
            JsonPrimitive newJson    = (JsonPrimitive)JsonValue.Parse(sourceJson.ToString());

            if (!JsonValueVerifier.Compare(sourceJson, newJson))
            {
                Assert.Fail("Test failed!  The new JsonObject DateTimeOffset value does not equal to the original one.");
            }
        }
コード例 #2
0
        public void ValidJsonArrayRoundTrip()
        {
            bool oldValue = CreatorSettings.CreateDateTimeWithSubMilliseconds;

            CreatorSettings.CreateDateTimeWithSubMilliseconds = false;
            try
            {
                int seed = 1;
                Log.Info("Seed: {0}", seed);
                Random rndGen = new Random(seed);

                JsonArray sourceJson = new JsonArray(new JsonValue[]
                {
                    PrimitiveCreator.CreateInstanceOfBoolean(rndGen),
                    PrimitiveCreator.CreateInstanceOfByte(rndGen),
                    PrimitiveCreator.CreateInstanceOfDateTime(rndGen),
                    PrimitiveCreator.CreateInstanceOfDateTimeOffset(rndGen),
                    PrimitiveCreator.CreateInstanceOfDecimal(rndGen),
                    PrimitiveCreator.CreateInstanceOfDouble(rndGen),
                    PrimitiveCreator.CreateInstanceOfInt16(rndGen),
                    PrimitiveCreator.CreateInstanceOfInt32(rndGen),
                    PrimitiveCreator.CreateInstanceOfInt64(rndGen),
                    PrimitiveCreator.CreateInstanceOfSByte(rndGen),
                    PrimitiveCreator.CreateInstanceOfSingle(rndGen),
                    PrimitiveCreator.CreateInstanceOfString(rndGen),
                    PrimitiveCreator.CreateInstanceOfUInt16(rndGen),
                    PrimitiveCreator.CreateInstanceOfUInt32(rndGen),
                    PrimitiveCreator.CreateInstanceOfUInt64(rndGen)
                });

                JsonArray newJson = (JsonArray)JsonValue.Parse(sourceJson.ToString());

                Log.Info("Original JsonArray object is: {0}", sourceJson);
                Log.Info("Round-tripped JsonArray object is: {0}", newJson);

                if (!JsonValueVerifier.Compare(sourceJson, newJson))
                {
                    Assert.Fail("Test failed!  The new JsonValue does not equal to the original one.");
                }
            }
            finally
            {
                CreatorSettings.CreateDateTimeWithSubMilliseconds = oldValue;
            }
        }
コード例 #3
0
        public void ValidJsonObjectRoundTrip()
        {
            bool oldValue = CreatorSettings.CreateDateTimeWithSubMilliseconds;

            CreatorSettings.CreateDateTimeWithSubMilliseconds = false;
            try
            {
                int seed = 1;
                Log.Info("Seed: {0}", seed);
                Random rndGen = new Random(seed);

                JsonObject sourceJson = new JsonObject(new Dictionary <string, JsonValue>()
                {
                    { "Name", PrimitiveCreator.CreateInstanceOfString(rndGen) },
                    { "Age", PrimitiveCreator.CreateInstanceOfInt32(rndGen) },
                    { "DateTimeOffset", PrimitiveCreator.CreateInstanceOfDateTimeOffset(rndGen) },
                    { "Birthday", PrimitiveCreator.CreateInstanceOfDateTime(rndGen) }
                });
                sourceJson.Add("NewItem1", PrimitiveCreator.CreateInstanceOfString(rndGen));
                sourceJson.Add(new KeyValuePair <string, JsonValue>("NewItem2", PrimitiveCreator.CreateInstanceOfString(rndGen)));

                JsonObject newJson = (JsonObject)JsonValue.Parse(sourceJson.ToString());

                newJson.Remove("NewItem1");
                sourceJson.Remove("NewItem1");
                if (newJson.ContainsKey("NewItem1"))
                {
                    Assert.Fail("Test failed!  JsonObject.Remove failed to remove the targeted key value pair, the Key = NewItem1");
                }

                if (!JsonValueVerifier.Compare(sourceJson, newJson))
                {
                    Assert.Fail("Test failed!  The new JsonValue does not equal to the original one.");
                }
            }
            finally
            {
                CreatorSettings.CreateDateTimeWithSubMilliseconds = oldValue;
            }
        }
コード例 #4
0
        private bool TestPrimitiveType(string typeName)
        {
            bool retValue    = true;
            bool specialCase = false;

            int seed = 1;

            Log.Info("Seed: {0}", seed);
            Random rndGen = new Random(seed);

            JsonPrimitive sourceJson = null;
            JsonPrimitive sourceJson2;
            object        tempValue = null;

            switch (typeName.ToLower())
            {
            case "boolean":
                tempValue   = PrimitiveCreator.CreateInstanceOfBoolean(rndGen);
                sourceJson  = (JsonPrimitive)JsonValue.Parse(tempValue.ToString().ToLower());
                sourceJson2 = new JsonPrimitive((bool)tempValue);
                break;

            case "byte":
                tempValue   = PrimitiveCreator.CreateInstanceOfByte(rndGen);
                sourceJson  = (JsonPrimitive)JsonValue.Parse(tempValue.ToString());
                sourceJson2 = new JsonPrimitive((byte)tempValue);
                break;

            case "char":
                sourceJson2 = new JsonPrimitive((char)PrimitiveCreator.CreateInstanceOfChar(rndGen));
                specialCase = true;
                break;

            case "datetime":
                tempValue   = PrimitiveCreator.CreateInstanceOfDateTime(rndGen);
                sourceJson2 = new JsonPrimitive((DateTime)tempValue);
                sourceJson  = (JsonPrimitive)JsonValue.Parse(sourceJson2.ToString());
                break;

            case "decimal":
                tempValue   = PrimitiveCreator.CreateInstanceOfDecimal(rndGen);
                sourceJson  = (JsonPrimitive)JsonValue.Parse(((decimal)tempValue).ToString(NumberFormatInfo.InvariantInfo));
                sourceJson2 = new JsonPrimitive((decimal)tempValue);
                break;

            case "double":
                double tempDouble = PrimitiveCreator.CreateInstanceOfDouble(rndGen);
                sourceJson  = (JsonPrimitive)JsonValue.Parse(tempDouble.ToString("R", NumberFormatInfo.InvariantInfo));
                sourceJson2 = new JsonPrimitive(tempDouble);
                break;

            case "guid":
                sourceJson2 = new JsonPrimitive(PrimitiveCreator.CreateInstanceOfGuid(rndGen));
                specialCase = true;
                break;

            case "int16":
                tempValue   = PrimitiveCreator.CreateInstanceOfInt16(rndGen);
                sourceJson  = (JsonPrimitive)JsonValue.Parse(tempValue.ToString());
                sourceJson2 = new JsonPrimitive((short)tempValue);
                break;

            case "int32":
                tempValue   = PrimitiveCreator.CreateInstanceOfInt32(rndGen);
                sourceJson  = (JsonPrimitive)JsonValue.Parse(tempValue.ToString());
                sourceJson2 = new JsonPrimitive((int)tempValue);
                break;

            case "int64":
                tempValue   = PrimitiveCreator.CreateInstanceOfInt64(rndGen);
                sourceJson  = (JsonPrimitive)JsonValue.Parse(tempValue.ToString());
                sourceJson2 = new JsonPrimitive((long)tempValue);
                break;

            case "sbyte":
                tempValue   = PrimitiveCreator.CreateInstanceOfSByte(rndGen);
                sourceJson  = (JsonPrimitive)JsonValue.Parse(tempValue.ToString());
                sourceJson2 = new JsonPrimitive((sbyte)tempValue);
                break;

            case "single":
                float fltValue = PrimitiveCreator.CreateInstanceOfSingle(rndGen);
                sourceJson  = (JsonPrimitive)JsonValue.Parse(fltValue.ToString("R", NumberFormatInfo.InvariantInfo));
                sourceJson2 = new JsonPrimitive(fltValue);
                break;

            case "string":
                do
                {
                    tempValue = PrimitiveCreator.CreateInstanceOfString(rndGen);
                } while (tempValue == null);

                sourceJson2 = new JsonPrimitive((string)tempValue);
                sourceJson  = (JsonPrimitive)JsonValue.Parse(sourceJson2.ToString());
                break;

            case "uint16":
                tempValue   = PrimitiveCreator.CreateInstanceOfUInt16(rndGen);
                sourceJson  = (JsonPrimitive)JsonValue.Parse(tempValue.ToString());
                sourceJson2 = new JsonPrimitive((ushort)tempValue);
                break;

            case "uint32":
                tempValue   = PrimitiveCreator.CreateInstanceOfUInt32(rndGen);
                sourceJson  = (JsonPrimitive)JsonValue.Parse(tempValue.ToString());
                sourceJson2 = new JsonPrimitive((uint)tempValue);
                break;

            case "uint64":
                tempValue   = PrimitiveCreator.CreateInstanceOfUInt64(rndGen);
                sourceJson  = (JsonPrimitive)JsonValue.Parse(tempValue.ToString());
                sourceJson2 = new JsonPrimitive((ulong)tempValue);
                break;

            case "uri":
                Uri uri = null;
                do
                {
                    try
                    {
                        uri = PrimitiveCreator.CreateInstanceOfUri(rndGen);
                    }
                    catch (UriFormatException)
                    {
                    }
                } while (uri == null);

                sourceJson2 = new JsonPrimitive(uri);
                specialCase = true;
                break;

            case "null":
                sourceJson  = (JsonPrimitive)JsonValue.Parse("null");
                sourceJson2 = null;
                break;

            default:
                sourceJson  = null;
                sourceJson2 = null;
                break;
            }

            if (!specialCase)
            {
                // comparison between two constructors
                if (!JsonValueVerifier.Compare(sourceJson, sourceJson2))
                {
                    Log.Info("(JsonPrimitive)JsonValue.Parse(string) failed to match the results from default JsonPrimitive(obj)constructor for type {0}", typeName);
                    retValue = false;
                }

                if (sourceJson != null)
                {
                    // test JsonValue.Load(TextReader)
                    JsonPrimitive newJson = null;
                    using (StringReader sr = new StringReader(sourceJson.ToString()))
                    {
                        newJson = (JsonPrimitive)JsonValue.Load(sr);
                    }

                    if (!JsonValueVerifier.Compare(sourceJson, newJson))
                    {
                        Log.Info("JsonValue.Load(TextReader) failed to function properly for type {0}", typeName);
                        retValue = false;
                    }

                    // test JsonValue.Load(Stream) is located in the JObjectFromGenoTypeLib test case

                    // test JsonValue.Parse(string)
                    newJson = null;
                    newJson = (JsonPrimitive)JsonValue.Parse(sourceJson.ToString());
                    if (!JsonValueVerifier.Compare(sourceJson, newJson))
                    {
                        Log.Info("JsonValue.Parse(string) failed to function properly for type {0}", typeName);
                        retValue = false;
                    }
                }
            }
            else
            {
                // test JsonValue.Load(TextReader)
                JsonPrimitive newJson2 = null;
                using (StringReader sr = new StringReader(sourceJson2.ToString()))
                {
                    newJson2 = (JsonPrimitive)JsonValue.Load(sr);
                }

                if (!JsonValueVerifier.Compare(sourceJson2, newJson2))
                {
                    Log.Info("JsonValue.Load(TextReader) failed to function properly for type {0}", typeName);
                    retValue = false;
                }

                // test JsonValue.Load(Stream) is located in the JObjectFromGenoTypeLib test case

                // test JsonValue.Parse(string)
                newJson2 = null;
                newJson2 = (JsonPrimitive)JsonValue.Parse(sourceJson2.ToString());
                if (!JsonValueVerifier.Compare(sourceJson2, newJson2))
                {
                    Log.Info("JsonValue.Parse(string) failed to function properly for type {0}", typeName);
                    retValue = false;
                }
            }

            return(retValue);
        }
コード例 #5
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!");
                }
            }
        }
コード例 #6
0
        public void MixedJsonTypeFunctionalTest()
        {
            bool oldValue = CreatorSettings.CreateDateTimeWithSubMilliseconds;

            CreatorSettings.CreateDateTimeWithSubMilliseconds = false;
            try
            {
                int seed = 1;

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

                    JsonArray sourceJson = new JsonArray(new List <JsonValue>()
                    {
                        PrimitiveCreator.CreateInstanceOfBoolean(rndGen),
                        PrimitiveCreator.CreateInstanceOfByte(rndGen),
                        PrimitiveCreator.CreateInstanceOfDateTime(rndGen),
                        PrimitiveCreator.CreateInstanceOfDateTimeOffset(rndGen),
                        PrimitiveCreator.CreateInstanceOfDecimal(rndGen),
                        PrimitiveCreator.CreateInstanceOfDouble(rndGen),
                        PrimitiveCreator.CreateInstanceOfInt16(rndGen),
                        PrimitiveCreator.CreateInstanceOfInt32(rndGen),
                        PrimitiveCreator.CreateInstanceOfInt64(rndGen),
                        PrimitiveCreator.CreateInstanceOfSByte(rndGen),
                        PrimitiveCreator.CreateInstanceOfSingle(rndGen),
                        PrimitiveCreator.CreateInstanceOfString(rndGen),
                        PrimitiveCreator.CreateInstanceOfUInt16(rndGen),
                        PrimitiveCreator.CreateInstanceOfUInt32(rndGen),
                        PrimitiveCreator.CreateInstanceOfUInt64(rndGen),
                        new JsonObject(new Dictionary <string, JsonValue>()
                        {
                            { "Boolean", PrimitiveCreator.CreateInstanceOfBoolean(rndGen) },
                            { "Byte", PrimitiveCreator.CreateInstanceOfByte(rndGen) },
                            { "DateTime", PrimitiveCreator.CreateInstanceOfDateTime(rndGen) },
                            { "DateTimeOffset", PrimitiveCreator.CreateInstanceOfDateTimeOffset(rndGen) },
                            { "Decimal", PrimitiveCreator.CreateInstanceOfDecimal(rndGen) },
                            { "Double", PrimitiveCreator.CreateInstanceOfDouble(rndGen) },
                            { "Int16", PrimitiveCreator.CreateInstanceOfInt16(rndGen) },
                            { "Int32", PrimitiveCreator.CreateInstanceOfInt32(rndGen) },
                            { "Int64", PrimitiveCreator.CreateInstanceOfInt64(rndGen) },
                            { "SByte", PrimitiveCreator.CreateInstanceOfSByte(rndGen) },
                            { "Single", PrimitiveCreator.CreateInstanceOfSingle(rndGen) },
                            { "String", PrimitiveCreator.CreateInstanceOfString(rndGen) },
                            { "UInt16", PrimitiveCreator.CreateInstanceOfUInt16(rndGen) },
                            { "UInt32", PrimitiveCreator.CreateInstanceOfUInt32(rndGen) },
                            { "UInt64", PrimitiveCreator.CreateInstanceOfUInt64(rndGen) }
                        })
                    });

                    JsonArray newJson = (JsonArray)JsonValue.Parse(sourceJson.ToString());
                    if (!JsonValueVerifier.Compare(sourceJson, newJson))
                    {
                        Assert.Fail("MixedJsonTypeFunctionalTest failed!  The new JsonValue does not equal to the original one.");
                    }
                }
            }
            finally
            {
                CreatorSettings.CreateDateTimeWithSubMilliseconds = oldValue;
            }
        }