Esempio n. 1
0
        public void Item_Get_InvalidIndex_ThrowsArgumentOutOfRangeException()
        {
            JsonArray array = new JsonArray(new JsonPrimitive(true));

            Assert.Throws<ArgumentOutOfRangeException>("index", () => array[-1]);
            Assert.Throws<ArgumentOutOfRangeException>("index", () => array[1]);
        }
Esempio n. 2
0
 public void Item_Set_Get()
 {
     JsonArray array = new JsonArray(new JsonPrimitive(true));
     JsonPrimitive value = new JsonPrimitive(false);
     array[0] = value;
     Assert.Same(value, array[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"]);
            }

            Assert.Equal(count, 6);
        }
Esempio n. 4
0
 private static void VerifyJsonArray(JsonArray array, JsonValue[] values)
 {
     Assert.Equal(values.Length, array.Count);
     for (int i = 0; i < values.Length; i++)
     {
         Assert.Equal(values[i], array[i]);
     }
 }
Esempio n. 5
0
 public void Ctor_IEnumerable_Works()
 {
 	// Workaround xunit/xunit#987: InvalidOperationException thrown if this is in MemberData
     JsonValue[] items = new JsonValue[] { new JsonPrimitive(true) };
     JsonArray array = new JsonArray((IEnumerable<JsonValue>)items);
     Assert.Equal(1, array.Count);
     Assert.Same(items[0], array[0]);
 }
Esempio n. 6
0
        public void general_case()
        {
            var jobject = new JsonObject();
            jobject["name"] = new JsonValue("test");
            jobject["value"] = new JsonValue(1);
            jobject["list"] = new JsonArray { new JsonValue(5), new JsonValue("test") };

            var json = jobject.ToString();

            json.ShouldEqual("{\"name\":\"test\",\"value\":1,\"list\":[5,\"test\"]}");
        }
        private bool CrossJsonValueVerificationOnIndexViaLinqQuery(JsonArray sourceJson, JsonArray newJson, int index)
        {
            var itemsByIndex = from JsonValue itemByIndex in sourceJson
                               where (itemByIndex != null && (int)itemByIndex["Index"] == index)
                               select itemByIndex;
            int countByIndex = 0;
            foreach (JsonValue a in itemsByIndex)
            {
                countByIndex++;
            }

            Log.Info("Original Collection contains: " + countByIndex + " item By Index " + index);

            var newItemsByIndex = from JsonValue newItemByIndex in newJson
                                  where (newItemByIndex != null && (int)newItemByIndex["Index"] == index)
                                  select newItemByIndex;
            int newcountByIndex = 0;
            foreach (JsonValue a in newItemsByIndex)
            {
                newcountByIndex++;
            }

            Log.Info("New Collection contains: " + newcountByIndex + " item By Index " + index);

            if (countByIndex != newcountByIndex)
            {
                Log.Info("Count by Original JsonValue = " + countByIndex + "; Count by New JsonValue = " + newcountByIndex);
                Log.Info("The number of items matching the provided Index does NOT equal between these two JsonValues!");
                return false;
            }
            else
            {
                return true;
            }
        }
        private bool InternalVerificationViaLinqQuery(JsonArray sourceJson, string name, int index)
        {
            var itemsByName = from JsonValue itemByName in sourceJson
                              where (itemByName != null && (string)itemByName["Name"] == name)
                              select itemByName;
            int countByName = 0;
            foreach (JsonValue a in itemsByName)
            {
                countByName++;
            }

            Log.Info("Collection contains: " + countByName + " item By Name " + name);

            var itemsByIndex = from JsonValue itemByIndex in sourceJson
                               where (itemByIndex != null && (int)itemByIndex["Index"] == index)
                               select itemByIndex;
            int countByIndex = 0;
            foreach (JsonValue a in itemsByIndex)
            {
                countByIndex++;
            }

            Log.Info("Collection contains: " + countByIndex + " item By Index " + index);

            if (countByIndex != countByName)
            {
                Log.Info("Count by Name = " + countByName + "; Count by Index = " + countByIndex);
                Log.Info("The number of items matching the provided Name does NOT equal to that matching the provided Index, The two JsonValues are not equal!");
                return false;
            }
            else
            {
                return true;
            }
        }
        private bool CrossJsonValueVerificationOnNameViaLinqQuery(JsonArray sourceJson, JsonArray newJson, string name)
        {
            var itemsByName = from JsonValue itemByName in sourceJson
                              where (itemByName != null && (string)itemByName["Name"] == name)
                              select itemByName;
            int countByName = 0;
            foreach (JsonValue a in itemsByName)
            {
                countByName++;
            }

            Log.Info("Original Collection contains: " + countByName + " item By Name " + name);

            var newItemsByName = from JsonValue newItemByName in newJson
                                 where (newItemByName != null && (string)newItemByName["Name"] == name)
                                 select newItemByName;
            int newcountByName = 0;
            foreach (JsonValue a in newItemsByName)
            {
                newcountByName++;
            }

            Log.Info("New Collection contains: " + newcountByName + " item By Name " + name);

            if (countByName != newcountByName)
            {
                Log.Info("Count by Original JsonValue = " + countByName + "; Count by New JsonValue = " + newcountByName);
                Log.Info("The number of items matching the provided Name does NOT equal between these two JsonValues!");
                return false;
            }
            else
            {
                return true;
            }
        }
Esempio n. 10
0
        public void RemoveAt_InvalidIndex_ThrowsArgumentOutOfRangeException()
        {
            JsonArray array = new JsonArray(new JsonPrimitive(true));

            Assert.Throws<ArgumentOutOfRangeException>("index", () => array.RemoveAt(-1));
            Assert.Throws<ArgumentOutOfRangeException>("index", () => array.RemoveAt(1));
        }
Esempio n. 11
0
        public void Insert_InvalidIndex_ThrowsArgumentOutOfRangeException()
        {
            JsonArray array = new JsonArray(new JsonPrimitive(true));

            Assert.Throws<ArgumentOutOfRangeException>("index", () => array.Insert(-1, new JsonPrimitive(false)));
            Assert.Throws<ArgumentOutOfRangeException>("index", () => array.Insert(2, new JsonPrimitive(false)));
        }
Esempio n. 12
0
 public void create_json_object_with_array_of_mix_types_property()
 {
     var jobject = new JsonObject();
     jobject["test"] = new JsonArray(new object[] { 1, "my", true });
     jobject.ToString().ShouldEqual("{\"test\":[1,\"my\",true]}");
 }
Esempio n. 13
0
 public void create_json_object_with_array_with_only_one_element()
 {
     var jobject = new JsonObject();
     jobject["test"] = new JsonArray(new object[] { 5 });
     jobject.ToString().ShouldEqual("{\"test\":[5]}");
 }
Esempio n. 14
0
        public void GetEnumerator_NonGenericIEnumerable()
        {
            JsonArray array = new JsonArray(new JsonPrimitive(true));

            IEnumerator enumerator = ((IEnumerable)array).GetEnumerator();
            for (int i = 0; i < 2; i++)
            {
                int counter = 0;
                while (enumerator.MoveNext())
                {
                    Assert.Same(array[counter], enumerator.Current);
                    counter++;
                }
                Assert.Equal(array.Count, counter);

                enumerator.Reset();
            }
        }
Esempio n. 15
0
        public void NestedChangingEventTest()
        {
            const string key1 = "first";

            JsonObject target = new JsonObject { { key1, new JsonArray { 1, 2 } } };
            JsonArray child = target[key1] as JsonArray;
            TestEvents(
                target,
                obj => ((JsonArray)obj[key1]).Add(5),
                new List<Tuple<bool, JsonValue, JsonValueChangeEventArgs>>());

            target = new JsonObject();
            child = new JsonArray(1, 2);
            TestEvents(
                target,
                obj =>
                {
                    obj.Add(key1, child);
                    ((JsonArray)obj[key1]).Add(5);
                },
                new List<Tuple<bool, JsonValue, JsonValueChangeEventArgs>>
                {
                    new Tuple<bool, JsonValue, JsonValueChangeEventArgs>(true, target, new JsonValueChangeEventArgs(child, JsonValueChange.Add, key1)),
                    new Tuple<bool, JsonValue, JsonValueChangeEventArgs>(false, target, new JsonValueChangeEventArgs(child, JsonValueChange.Add, key1)),
                });
        }
Esempio n. 16
0
 public void Save_TextWriter()
 {
     JsonArray array = new JsonArray(new JsonPrimitive(true), null);
     using (StringWriter writer = new StringWriter())
     {
         array.Save(writer);
         Assert.Equal("[true, null]", writer.ToString());
     }
 }
Esempio n. 17
0
 public void Save_NullStream_ThrowsArgumentNullException()
 {
     JsonArray array = new JsonArray();
     Assert.Throws<ArgumentNullException>("stream", () => array.Save((Stream)null));
     Assert.Throws<ArgumentNullException>("textWriter", () => array.Save((TextWriter)null));
 }
Esempio n. 18
0
        public void CopyTo(int arrayIndex)
        {
            JsonArray array = new JsonArray(new JsonPrimitive(true));
            JsonValue[] copy = new JsonValue[array.Count + arrayIndex];
            array.CopyTo(copy, arrayIndex);

            for (int i = 0; i < arrayIndex; i++)
            {
                Assert.Null(copy[i]);
            }
            for (int i = arrayIndex; i < copy.Length; i++)
            {
                Assert.Same(array[i - arrayIndex], copy[i]);
            }
        }
Esempio n. 19
0
 public void Save_Stream()
 {
     JsonArray array = new JsonArray(new JsonPrimitive(true), null);
     using (MemoryStream stream = new MemoryStream())
     {
         array.Save(stream);
         string result = Encoding.UTF8.GetString(stream.ToArray());
         Assert.Equal("[true, null]", result);
     }
 }
Esempio n. 20
0
        public void Clear()
        {
            JsonArray array = new JsonArray(new JsonValue[3]);
            array.Clear();
            Assert.Equal(0, array.Count);

            array.Clear();
            Assert.Equal(0, array.Count);
        }
Esempio n. 21
0
 public void ImplicitConversion_NotJsonPrimitive_ThrowsInvalidCastException()
 {
     Assert.Throws<InvalidCastException>(() => { bool i = new JsonArray(); });
     Assert.Throws<InvalidCastException>(() => { byte i = new JsonArray(); });
     Assert.Throws<InvalidCastException>(() => { char i = new JsonArray(); });
     Assert.Throws<InvalidCastException>(() => { decimal i = new JsonArray(); });
     Assert.Throws<InvalidCastException>(() => { double i = new JsonArray(); });
     Assert.Throws<InvalidCastException>(() => { float i = new JsonArray(); });
     Assert.Throws<InvalidCastException>(() => { int i = new JsonArray(); });
     Assert.Throws<InvalidCastException>(() => { long i = new JsonArray(); });
     Assert.Throws<InvalidCastException>(() => { sbyte i = new JsonArray(); });
     Assert.Throws<InvalidCastException>(() => { short i = new JsonArray(); });
     Assert.Throws<InvalidCastException>(() => { string i = new JsonArray(); });
     Assert.Throws<InvalidCastException>(() => { uint i = new JsonArray(); });
     Assert.Throws<InvalidCastException>(() => { ulong i = new JsonArray(); });
     Assert.Throws<InvalidCastException>(() => { ushort i = new JsonArray(); });
     Assert.Throws<InvalidCastException>(() => { DateTime i = new JsonArray(); });
     Assert.Throws<InvalidCastException>(() => { DateTimeOffset i = new JsonArray(); });
     Assert.Throws<InvalidCastException>(() => { TimeSpan i = new JsonArray(); });
     Assert.Throws<InvalidCastException>(() => { Guid i = new JsonArray(); });
     Assert.Throws<InvalidCastException>(() => { Uri i = new JsonArray(); });
 }
Esempio n. 22
0
        public void Insert()
        {
            JsonArray array = new JsonArray(new JsonPrimitive(true));
            JsonPrimitive value = new JsonPrimitive(false);
            array.Insert(1, value);

            Assert.Equal(2, array.Count);
            Assert.Same(value, array[1]);
        }
 public void ToJsonObjectFromArray()
 {
     JsonArray ja = new JsonArray("first", "second");
     JsonObject jo = ja.ToJsonObject();
     Assert.Equal("{\"0\":\"first\",\"1\":\"second\"}", jo.ToString());
 }
Esempio n. 24
0
        public void Contains()
        {
            JsonValue[] items = new JsonValue[] { new JsonPrimitive(true) };
            JsonArray array = new JsonArray((IEnumerable<JsonValue>)items);

            Assert.True(array.Contains(items[0]));
            Assert.False(array.Contains(new JsonPrimitive(false)));
        }
Esempio n. 25
0
 public void create_json_object_with_array_property()
 {
     var jobject = new JsonObject();
     jobject["test"] = new JsonArray(new[] { 1, 2, 3 });
     jobject.ToString().ShouldEqual("{\"test\":[1,2,3]}");
 }
Esempio n. 26
0
        public void IndexOf()
        {
            JsonValue[] items = new JsonValue[] { new JsonPrimitive(true) };
            JsonArray array = new JsonArray((IEnumerable<JsonValue>)items);

            Assert.Equal(0, array.IndexOf(items[0]));
            Assert.Equal(-1, array.IndexOf(new JsonPrimitive(false)));
        }
Esempio n. 27
0
 public void create_json_object_with_empty_array()
 {
     var jobject = new JsonObject();
     jobject["test"] = new JsonArray(new object[0]);
     jobject.ToString().ShouldEqual("{\"test\":[]}");
 }
Esempio n. 28
0
        public void RemoveAt()
        {
            JsonValue[] items = new JsonValue[] { new JsonPrimitive(true) };
            JsonArray array = new JsonArray((IEnumerable<JsonValue>)items);

            array.RemoveAt(0);
            Assert.Equal(0, array.Count);
        }
Esempio n. 29
0
 public void Ctor_NullArray_Works()
 {
     JsonArray array = new JsonArray(null);
     Assert.Equal(0, array.Count);
 }
Esempio n. 30
0
 public void AddRange_NullIEnumerable_ThrowsArgumentNullException()
 {
     JsonArray array = new JsonArray();
     Assert.Throws<ArgumentNullException>("items", () => array.AddRange((IEnumerable<JsonValue>)null));
 }