Exemplo n.º 1
0
        static JObject CreateJObject(Random rndGen, int depth)
        {
            const string keyChars = "abcdefghijklmnopqrstuvwxyz0123456789";
            int          size     = rndGen.Next(CreatorSettings.MaxArrayLength);

            if (CreatorSettings.NullValueProbability == 0 && size == 0)
            {
                size++;
            }

            JObject result = new JObject();

            for (int i = 0; i < size; i++)
            {
                string key;
                do
                {
                    key = PrimitiveCreator.CreateInstanceOfString(rndGen, 10, keyChars);
                } while (result.Count > 0 && ((IDictionary <string, JToken>)result).ContainsKey(key));

                result.Add(key, CreateJToken(rndGen, depth + 1));
            }

            return(result);
        }
Exemplo n.º 2
0
 public override object CreateInstanceOf(Type type, Random rndGen)
 {
     if (type == typeof(float))
     {
         float result;
         do
         {
             result = PrimitiveCreator.CreateInstanceOfSingle(rndGen);
         }while (float.IsInfinity(result));
         return(result);
     }
     else if (type == typeof(double))
     {
         double result;
         do
         {
             result = PrimitiveCreator.CreateInstanceOfDouble(rndGen);
         }while (double.IsInfinity(result));
         return(result);
     }
     else
     {
         return(new DerivedType(rndGen));
     }
 }
        public void JsonValueRoundTripCastTests()
        {
            int seed = 1;

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

            this.DoRoundTripCasting(String.Empty, typeof(string));
            this.DoRoundTripCasting("null", typeof(string));
            string str;

            do
            {
                str = PrimitiveCreator.CreateInstanceOfString(rndGen);
            } while (str == null);

            this.DoRoundTripCasting(str, typeof(string));
            this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfInt16(rndGen), typeof(int));
            this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfInt32(rndGen), typeof(int));
            this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfInt64(rndGen), typeof(int));
            this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfUInt16(rndGen), typeof(int));
            this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfUInt32(rndGen), typeof(int));
            this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfUInt64(rndGen), typeof(int));
            this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfGuid(rndGen), typeof(Guid));
            this.DoRoundTripCasting(new Uri("http://bug/test?param=hello%0a"), typeof(Uri));
            this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfChar(rndGen), typeof(char));
            this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfBoolean(rndGen), typeof(bool));
            this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfDateTime(rndGen), typeof(DateTime));
            this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfDateTimeOffset(rndGen), typeof(DateTimeOffset));
            this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfDouble(rndGen), typeof(double));
            this.DoRoundTripCasting(PrimitiveCreator.CreateInstanceOfDouble(rndGen), typeof(float));
            this.DoRoundTripCasting(0.12345f, typeof(double));
            this.DoRoundTripCasting(0.12345f, typeof(float));
        }
        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");

                Assert.False(newJson.ContainsKey("NewItem1"));

                Assert.False(!JsonValueVerifier.Compare(sourceJson, newJson));
            }
            finally
            {
                CreatorSettings.CreateDateTimeWithSubMilliseconds = oldValue;
            }
        }
Exemplo n.º 5
0
        public void JLinqSimpleCreationQueryTest()
        {
            int    seed   = 1;
            Random rndGen = new Random(seed);

            JsonArray sourceJson = new JsonArray
            {
                new JsonObject {
                    { "Name", "Alex" }, { "Age", 18 }, { "Birthday", PrimitiveCreator.CreateInstanceOfDateTime(rndGen) }
                },
                new JsonObject {
                    { "Name", "Joe" }, { "Age", 19 }, { "Birthday", DateTime.MinValue }
                },
                new JsonObject {
                    { "Name", "Chris" }, { "Age", 20 }, { "Birthday", DateTime.Now }
                },
                new JsonObject {
                    { "Name", "Jeff" }, { "Age", 21 }, { "Birthday", DateTime.MaxValue }
                },
                new JsonObject {
                    { "Name", "Carlos" }, { "Age", 22 }, { "Birthday", PrimitiveCreator.CreateInstanceOfDateTime(rndGen) }
                },
                new JsonObject {
                    { "Name", "Mohammad" }, { "Age", 23 }, { "Birthday", PrimitiveCreator.CreateInstanceOfDateTime(rndGen) }
                },
                new JsonObject {
                    { "Name", "Sara" }, { "Age", 24 }, { "Birthday", new DateTime(1998, 3, 20) }
                },
                new JsonObject {
                    { "Name", "Tomasz" }, { "Age", 25 }, { "Birthday", PrimitiveCreator.CreateInstanceOfDateTime(rndGen) }
                },
                new JsonObject {
                    { "Name", "Suwat" }, { "Age", 26 }, { "Birthday", new DateTime(1500, 12, 20) }
                },
                new JsonObject {
                    { "Name", "Eugene" }, { "Age", 27 }, { "Birthday", PrimitiveCreator.CreateInstanceOfDateTime(rndGen) }
                }
            };

            var adults = from JsonValue adult in sourceJson
                         where (int)adult["Age"] > 21
                         select adult;

            Log.Info("Team contains: ");
            int count = 0;

            foreach (JsonValue adult in adults)
            {
                count++;
                Log.Info((string)adult["Name"]);
            }

            if (count != 6)
            {
                Assert.Fail("There should be 6 adults, but JLinq query only located " + count);
            }
        }
Exemplo n.º 6
0
        static JToken CreateJsonPrimitive(Random rndGen)
        {
            switch (rndGen.Next(17))
            {
            case 0:
                return(PrimitiveCreator.CreateInstanceOfChar(rndGen));

            case 1:
                return(new JValue(PrimitiveCreator.CreateInstanceOfByte(rndGen)));

            case 2:
                return(PrimitiveCreator.CreateInstanceOfSByte(rndGen));

            case 3:
                return(PrimitiveCreator.CreateInstanceOfInt16(rndGen));

            case 4:
                return(PrimitiveCreator.CreateInstanceOfUInt16(rndGen));

            case 5:
                return(PrimitiveCreator.CreateInstanceOfInt32(rndGen));

            case 6:
                return(PrimitiveCreator.CreateInstanceOfUInt32(rndGen));

            case 7:
                return(PrimitiveCreator.CreateInstanceOfInt64(rndGen));

            case 8:
                return(PrimitiveCreator.CreateInstanceOfUInt64(rndGen));

            case 9:
                return(PrimitiveCreator.CreateInstanceOfDecimal(rndGen));

            case 10:
                return(PrimitiveCreator.CreateInstanceOfDouble(rndGen));

            case 11:
                return(PrimitiveCreator.CreateInstanceOfSingle(rndGen));

            case 12:
                return(PrimitiveCreator.CreateInstanceOfDateTime(rndGen));

            case 13:
                return(PrimitiveCreator.CreateInstanceOfDateTimeOffset(rndGen));

            case 14:
            case 15:
                // TODO: 199532 fix uri comparer
                return(PrimitiveCreator.CreateInstanceOfString(rndGen));

            default:
                return(PrimitiveCreator.CreateInstanceOfBoolean(rndGen));
            }
        }
Exemplo n.º 7
0
        public static string GetUniqueNonNullInstanceOfString(int seed, JsonObject sourceJson)
        {
            string retValue = string.Empty;
            Random rndGen   = new Random(seed);

            do
            {
                retValue = PrimitiveCreator.CreateInstanceOfString(rndGen);
            }while (retValue == null || sourceJson.Keys.Contains(retValue));

            return(retValue);
        }
        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());

            Assert.True(JsonValueVerifier.Compare(sourceJson, newJson));
        }
Exemplo n.º 9
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.");
            }
        }
Exemplo n.º 10
0
        public static JsonValue CreateDeepLevelJsonValue()
        {
            int seed = Environment.TickCount;

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

            bool           myBool           = PrimitiveCreator.CreateInstanceOfBoolean(rndGen);
            byte           myByte           = PrimitiveCreator.CreateInstanceOfByte(rndGen);
            DateTime       myDatetime       = PrimitiveCreator.CreateInstanceOfDateTime(rndGen);
            DateTimeOffset myDateTimeOffset = PrimitiveCreator.CreateInstanceOfDateTimeOffset(rndGen);
            decimal        myDecimal        = PrimitiveCreator.CreateInstanceOfDecimal(rndGen);
            double         myDouble         = PrimitiveCreator.CreateInstanceOfDouble(rndGen);
            short          myInt16          = PrimitiveCreator.CreateInstanceOfInt16(rndGen);
            int            myInt32          = PrimitiveCreator.CreateInstanceOfInt32(rndGen);
            long           myInt64          = PrimitiveCreator.CreateInstanceOfInt64(rndGen);
            sbyte          mySByte          = PrimitiveCreator.CreateInstanceOfSByte(rndGen);
            float          mySingle         = PrimitiveCreator.CreateInstanceOfSingle(rndGen);
            string         myString         = PrimitiveCreator.CreateInstanceOfString(rndGen, 20, null);
            ushort         myUInt16         = PrimitiveCreator.CreateInstanceOfUInt16(rndGen);
            uint           myUInt32         = PrimitiveCreator.CreateInstanceOfUInt32(rndGen);
            ulong          myUInt64         = PrimitiveCreator.CreateInstanceOfUInt64(rndGen);
            JsonArray      myArray          = new JsonArray {
                myBool, myByte, myDatetime, myDateTimeOffset, myDecimal, myDouble, myInt16, myInt32, myInt64, mySByte, mySingle, myString, myUInt16, myUInt32, myUInt64
            };
            JsonArray myArrayLevel2 = new JsonArray {
                myArray, myArray, myArray
            };
            JsonArray myArrayLevel3 = new JsonArray {
                myArrayLevel2, myArrayLevel2, myArrayLevel2
            };
            JsonArray myArrayLevel4 = new JsonArray {
                myArrayLevel3, myArrayLevel3, myArrayLevel3
            };
            JsonArray myArrayLevel5 = new JsonArray {
                myArrayLevel4, myArrayLevel4, myArrayLevel4
            };
            JsonArray myArrayLevel6 = new JsonArray {
                myArrayLevel5, myArrayLevel5, myArrayLevel5
            };
            JsonArray myArrayLevel7 = new JsonArray {
                myArrayLevel6, myArrayLevel6, myArrayLevel6
            };

            JsonArray sourceJson = BuildJsonArrayinSequence1(myBool, myByte, myDatetime, myDateTimeOffset, myDecimal, myDouble, myInt16, myInt32, myInt64, mySByte, mySingle, myString, myUInt16, myUInt32, myUInt64, myArray, myArrayLevel2, myArrayLevel3, myArrayLevel4, myArrayLevel5, myArrayLevel6, myArrayLevel7);

            return(sourceJson);
        }
        public void WebFaultExceptionOfT()
        {
            int              seed     = MethodBase.GetCurrentMethod().Name.GetHashCode();
            Random           rndGen   = new Random(seed);
            WebHttpBinding   binding  = new WebHttpBinding();
            WebHttpBehavior3 behavior = new WebHttpBehavior3();

            behavior.FaultExceptionEnabled = false;

            HttpStatusCode[] statusCodes = new HttpStatusCode[] { HttpStatusCode.Unauthorized, HttpStatusCode.ServiceUnavailable, HttpStatusCode.RequestTimeout };
            foreach (HttpStatusCode statusCode in statusCodes)
            {
                string detail      = PrimitiveCreator.CreateRandomString(rndGen, 30, "abcdefghijklmnopqrstuvwxyz");
                string requestBody = "{\"statusCode\":" + (int)statusCode + ",\"detail\":\"" + detail + "\"}";
                Test(binding, behavior, "POST", WebHttpBehavior3Tests.Endpoint + "/ThrowWebFaultExceptionOfT", "text/json", requestBody, statusCode, WebHttpBehavior3Tests.ApplicationJsonContentTypeWithCharset, "\"" + detail + "\"");
            }
        }
Exemplo n.º 12
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;
            }
        }
Exemplo n.º 13
0
        public static JsonObject CreateRandomPopulatedJsonObject(int seed, int length)
        {
            JsonObject myObject;

            myObject = new JsonObject(
                new Dictionary <string, JsonValue>()
            {
                { "Name", "myArray" },
                { "Index", 1 }
            });

            for (int i = myObject.Count; i < length / 2; i++)
            {
                myObject.Add(PrimitiveCreator.CreateInstanceOfString(new Random(seed + i)), GetRandomJsonPrimitives(seed + (i * 2)));
            }

            for (int i = myObject.Count; i < length; i++)
            {
                myObject.Add(new KeyValuePair <string, JsonValue>(PrimitiveCreator.CreateInstanceOfString(new Random(seed + (i * 10))), GetRandomJsonPrimitives(seed + (i * 20))));
            }

            return(myObject);
        }
Exemplo n.º 14
0
        private JsonArray CreateArrayOfPeople()
        {
            int    seed   = 1;
            Random rndGen = new Random(seed);

            return(new JsonArray(new List <JsonValue>()
            {
                new JsonObject(new Dictionary <string, JsonValue>()
                {
                    { "Name", "Alex" },
                    { "Age", 18 },
                    { "Birthday", PrimitiveCreator.CreateInstanceOfDateTime(rndGen) }
                }),
                new JsonObject(new Dictionary <string, JsonValue>()
                {
                    { "Name", "Joe" },
                    { "Age", 19 },
                    { "Birthday", DateTime.MinValue }
                }),
                new JsonObject(new Dictionary <string, JsonValue>()
                {
                    { "Name", "Chris" },
                    { "Age", 20 },
                    { "Birthday", DateTime.Now }
                }),
                new JsonObject(new Dictionary <string, JsonValue>()
                {
                    { "Name", "Jeff" },
                    { "Age", 21 },
                    { "Birthday", DateTime.MaxValue }
                }),
                new JsonObject(new Dictionary <string, JsonValue>()
                {
                    { "Name", "Carlos" },
                    { "Age", 22 },
                    { "Birthday", PrimitiveCreator.CreateInstanceOfDateTime(rndGen) }
                }),
                new JsonObject(new Dictionary <string, JsonValue>()
                {
                    { "Name", "Mohammad" },
                    { "Age", 23 },
                    { "Birthday", PrimitiveCreator.CreateInstanceOfDateTime(rndGen) }
                }),
                new JsonObject(new Dictionary <string, JsonValue>()
                {
                    { "Name", "Sara" },
                    { "Age", 24 },
                    { "Birthday", new DateTime(1998, 3, 20) }
                }),
                new JsonObject(new Dictionary <string, JsonValue>()
                {
                    { "Name", "Tomasz" },
                    { "Age", 25 },
                    { "Birthday", PrimitiveCreator.CreateInstanceOfDateTime(rndGen) }
                }),
                new JsonObject(new Dictionary <string, JsonValue>()
                {
                    { "Name", "Suwat" },
                    { "Age", 26 },
                    { "Birthday", new DateTime(1500, 12, 20) }
                }),
                new JsonObject(new Dictionary <string, JsonValue>()
                {
                    { "Name", "Eugene" },
                    { "Age", 27 },
                    { "Birthday", PrimitiveCreator.CreateInstanceOfDateTime(rndGen) }
                })
            }));
        }
        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());
                    Assert.True(JsonValueVerifier.Compare(sourceJson, newJson));
                }
            }
            finally
            {
                CreatorSettings.CreateDateTimeWithSubMilliseconds = oldValue;
            }
        }
        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;
                }

                Assert.True(retValue);
            }
        }
Exemplo n.º 17
0
        public static JsonPrimitive GetRandomJsonPrimitives(int seed)
        {
            JsonPrimitive myObject;
            Random        rndGen = new Random(seed);

            int mod = seed % 13;

            switch (mod)
            {
            case 1:
                myObject = new JsonPrimitive(PrimitiveCreator.CreateInstanceOfBoolean(rndGen));
                break;

            case 2:
                myObject = new JsonPrimitive(PrimitiveCreator.CreateInstanceOfByte(rndGen));
                break;

            case 3:
                myObject = new JsonPrimitive(PrimitiveCreator.CreateInstanceOfDateTime(rndGen));
                break;

            case 4:
                myObject = new JsonPrimitive(PrimitiveCreator.CreateInstanceOfDecimal(rndGen));
                break;

            case 5:
                myObject = new JsonPrimitive(PrimitiveCreator.CreateInstanceOfInt16(rndGen));
                break;

            case 6:
                myObject = new JsonPrimitive(PrimitiveCreator.CreateInstanceOfInt32(rndGen));
                break;

            case 7:
                myObject = new JsonPrimitive(PrimitiveCreator.CreateInstanceOfInt64(rndGen));
                break;

            case 8:
                myObject = new JsonPrimitive(PrimitiveCreator.CreateInstanceOfSByte(rndGen));
                break;

            case 9:
                myObject = new JsonPrimitive(PrimitiveCreator.CreateInstanceOfSingle(rndGen));
                break;

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

                myObject = new JsonPrimitive(temp);
                break;

            case 11:
                myObject = new JsonPrimitive(PrimitiveCreator.CreateInstanceOfUInt16(rndGen));
                break;

            case 12:
                myObject = new JsonPrimitive(PrimitiveCreator.CreateInstanceOfUInt32(rndGen));
                break;

            default:
                myObject = new JsonPrimitive(PrimitiveCreator.CreateInstanceOfUInt64(rndGen));
                break;
            }

            return(myObject);
        }
        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);
        }