public void JsonArrayCopytoFunctionalTest() { 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[] destJson = new JsonValue[arrayLength]; sourceJson.CopyTo(destJson, 0); for (int k = 0; k < destJson.Length; k++) { if (destJson[k] != sourceJson[k]) { retValue = false; } } Assert.True(retValue, "[JsonArrayCopytoFunctionalTest] JsonArray.CopyTo() failed to function properly. destJson.GetLength(0) = " + destJson.GetLength(0)); } }
public void JsonArrayItemsFunctionalTest() { 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.Contains(JsonValue) // JsonArray.IndexOf(JsonValue) JsonArray sourceJson = SpecialJsonValueHelper.CreatePrePopulatedJsonArray(seed, arrayLength); for (int j = 0; j < sourceJson.Count; j++) { if (!sourceJson.Contains(sourceJson[j])) { Log.Info("[JsonArrayItemsFunctionalTest] JsonArray.Contains(JsonValue) failed to function properly."); retValue = false; } else { Log.Info("[JsonArrayItemsFunctionalTest] JsonArray.Contains(JsonValue) passed test."); } if (sourceJson.IndexOf(sourceJson[j]) != j) { Log.Info("[JsonArrayItemsFunctionalTest] JsonArray.IndexOf(JsonValue) failed to function properly."); retValue = false; } else { Log.Info("[JsonArrayItemsFunctionalTest] JsonArray.IndexOf(JsonValue) passed test."); } } // JsonArray.Insert(int, JsonValue) JsonValue newItem = SpecialJsonValueHelper.GetRandomJsonPrimitives(seed); sourceJson.Insert(3, newItem); if (sourceJson[3] != newItem || sourceJson.Count != arrayLength + 1) { Log.Info("[JsonArrayItemsFunctionalTest] JsonArray.Insert(int, JsonValue) failed to function properly."); retValue = false; } else { Log.Info("[JsonArrayItemsFunctionalTest] JsonArray.Insert(int, JsonValue) passed test."); } Assert.True(retValue); } }
public static JsonPrimitive GetUniqueValue(int seed, JsonObject sourceJson) { JsonPrimitive newValue; int i = 0; do { newValue = SpecialJsonValueHelper.GetRandomJsonPrimitives(seed + i); i++; }while (sourceJson.ToString().IndexOf(newValue.ToString()) > 0); return(newValue); }
public void JsonObjectCopytoFunctionalTest() { 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); KeyValuePair <string, JsonValue>[] destJson = new KeyValuePair <string, JsonValue> [arrayLength]; if (sourceJson != null && destJson != null) { sourceJson.CopyTo(destJson, 0); } else { Log.Info("[JsonObjectCopytoFunctionalTest] sourceJson.ToString() = " + sourceJson.ToString()); Log.Info("[JsonObjectCopytoFunctionalTest] destJson.ToString() = " + destJson.ToString()); Assert.False(true, "[JsonObjectCopytoFunctionalTest] failed to create the source JsonObject object."); return; } if (destJson.Length == arrayLength) { for (int k = 0; k < destJson.Length; k++) { JsonValue temp; sourceJson.TryGetValue(k.ToString(), out temp); if (!(temp != null && destJson[k].Value == temp)) { retValue = false; } } } else { retValue = false; } Assert.True(retValue, "[JsonObjectCopytoFunctionalTest] JsonObject.CopyTo() failed to function properly. destJson.GetLength(0) = " + destJson.GetLength(0)); } }
public void DynamicEventsTest() { int seed = 1; int maxObj = 10; JsonArray ja = new JsonArray(); dynamic d = ja.AsDynamic(); TestEventsDynamic( d, (Action <dynamic>)(arr => arr.Add(1)), new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> > { new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs(1, JsonValueChange.Add, 0)), new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs(1, JsonValueChange.Add, 0)), }); const string key1 = "first"; const string key2 = "second"; JsonObject jo = new JsonObject { { key1, SpecialJsonValueHelper.GetRandomJsonPrimitives(seed) }, }; JsonObject objectToAdd = SpecialJsonValueHelper.CreateRandomPopulatedJsonObject(seed, maxObj); dynamic d2 = jo.AsDynamic(); TestEventsDynamic( d2, (Action <dynamic>)(obj => obj[key2] = objectToAdd), new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> > { new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(objectToAdd, JsonValueChange.Add, key2)), new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(objectToAdd, JsonValueChange.Add, key2)), }); TestEventsDynamic( d2, (Action <dynamic>)(obj => obj[key2] = objectToAdd), new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> > { new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(objectToAdd, JsonValueChange.Replace, key2)), new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(objectToAdd, JsonValueChange.Replace, key2)), }); }
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.ToString())); 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."); } } Assert.True(retValue); } }
public void JsonObjectAddRemoveFunctionalTest() { int seed = 1; for (int i = 0; i < iterationCount / 10; i++) { seed++; Log.Info("Seed: {0}", seed); Random rndGen = new Random(seed); bool retValue = true; JsonObject sourceJson = SpecialJsonValueHelper.CreateIndexPopulatedJsonObject(seed, arrayLength); // JsonObject.JsonType if (sourceJson.JsonType != JsonType.Object) { Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonArray.JsonType failed to function properly."); retValue = false; } // JsonObject.Add(KeyValuePair<string, JsonValue> item) // JsonObject.Add(string key, JsonValue value) // + various numers below so .AddRange() won't try to add an already existing value sourceJson.Add(SpecialJsonValueHelper.GetUniqueNonNullInstanceOfString(seed + 3, sourceJson), SpecialJsonValueHelper.GetUniqueValue(seed, sourceJson)); KeyValuePair <string, JsonValue> kvp; int startingSeed = seed + 1; do { kvp = SpecialJsonValueHelper.CreatePrePopulatedKeyValuePair(startingSeed); startingSeed++; }while (sourceJson.ContainsKey(kvp.Key)); sourceJson.Add(kvp); do { kvp = SpecialJsonValueHelper.CreatePrePopulatedKeyValuePair(startingSeed); startingSeed++; }while (sourceJson.ContainsKey(kvp.Key)); sourceJson.Add(kvp); if (sourceJson.Count != arrayLength + 3) { Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.Add() failed to function properly."); retValue = false; } else { Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.Add() passed test."); } // JsonObject.Clear() sourceJson.Clear(); if (sourceJson.Count > 0) { Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.Clear() failed to function properly."); retValue = false; } else { Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.Clear() passed test."); } // JsonObject.AddRange(IEnumerable<KeyValuePair<string, JsonValue>> items) sourceJson = SpecialJsonValueHelper.CreateIndexPopulatedJsonObject(seed, arrayLength); // + various numers below so .AddRange() won't try to add an already existing value sourceJson.AddRange(SpecialJsonValueHelper.CreatePrePopulatedListofKeyValuePair(seed + 13 + (arrayLength * 2), 5)); if (sourceJson.Count != arrayLength + 5) { Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.AddRange(IEnumerable<KeyValuePair<string, JsonValue>> items) failed to function properly."); retValue = false; } else { Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.AddRange(IEnumerable<KeyValuePair<string, JsonValue>> items) passed test."); } // JsonObject.AddRange(params KeyValuePair<string, JsonValue>[] items) sourceJson = SpecialJsonValueHelper.CreateIndexPopulatedJsonObject(seed, arrayLength); // + various numers below so .AddRange() won't try to add an already existing value KeyValuePair <string, JsonValue> item1 = SpecialJsonValueHelper.CreatePrePopulatedKeyValuePair(seed + arrayLength + 41); KeyValuePair <string, JsonValue> item2 = SpecialJsonValueHelper.CreatePrePopulatedKeyValuePair(seed + arrayLength + 47); KeyValuePair <string, JsonValue> item3 = SpecialJsonValueHelper.CreatePrePopulatedKeyValuePair(seed + arrayLength + 53); sourceJson.AddRange(new KeyValuePair <string, JsonValue>[] { item1, item2, item3 }); if (sourceJson.Count != arrayLength + 3) { Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.AddRange(params KeyValuePair<string, JsonValue>[] items) failed to function properly."); retValue = false; } else { Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.AddRange(params KeyValuePair<string, JsonValue>[] items) passed test."); } sourceJson.Clear(); // JsonObject.Remove(Key) sourceJson = SpecialJsonValueHelper.CreateIndexPopulatedJsonObject(seed, arrayLength); int count = sourceJson.Count; List <string> keys = new List <string>(sourceJson.Keys); foreach (string key in keys) { sourceJson.Remove(key); } if (sourceJson.Count > 0) { Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.Remove(Key) failed to function properly."); retValue = false; } else { Log.Info("[JsonObjectAddRemoveFunctionalTest] JsonObject.Remove(Key) passed test."); } Assert.True(retValue); } }
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); } }
public void JLinqDeepQueryTest() { int seed = 1; JsonArray mixedOrderJsonObj; JsonArray myJsonObj = SpecialJsonValueHelper.CreateDeepLevelJsonValuePair(seed, out mixedOrderJsonObj); if (myJsonObj != null && mixedOrderJsonObj != null) { bool retValue = true; var dict = new Dictionary <string, int> { { "myArray", 1 }, { "myArrayLevel2", 2 }, { "myArrayLevel3", 3 }, { "myArrayLevel4", 4 }, { "myArrayLevel5", 5 }, { "myArrayLevel6", 6 }, { "myArrayLevel7", 7 }, { "myBool", 8 }, { "myByte", 9 }, { "myDatetime", 10 }, { "myDateTimeOffset", 11 }, { "myDecimal", 12 }, { "myDouble", 13 }, { "myInt16", 14 }, { "myInt32", 15 }, { "myInt64", 16 }, { "mySByte", 17 }, { "mySingle", 18 }, { "myString", 19 }, { "myUInt16", 20 }, { "myUInt32", 21 }, { "myUInt64", 22 } }; foreach (string name in dict.Keys) { if (!this.InternalVerificationViaLinqQuery(myJsonObj, name, dict[name])) { retValue = false; } if (!this.InternalVerificationViaLinqQuery(mixedOrderJsonObj, name, dict[name])) { retValue = false; } if (!this.CrossJsonValueVerificationOnNameViaLinqQuery(myJsonObj, mixedOrderJsonObj, name)) { retValue = false; } if (!this.CrossJsonValueVerificationOnIndexViaLinqQuery(myJsonObj, mixedOrderJsonObj, dict[name])) { retValue = false; } } Assert.True(retValue, "The JsonValue did not verify as expected!"); } else { Assert.True(false, "Failed to create the pair of JsonValues!"); } }
public void JsonObjectEventsTest() { int seed = 1; const int maxObj = 10; const string key1 = "first"; const string key2 = "second"; const string key3 = "third"; const string key4 = "fourth"; const string key5 = "fifth"; JsonObject jo = new JsonObject { { key1, SpecialJsonValueHelper.GetRandomJsonPrimitives(seed) }, { key2, SpecialJsonValueHelper.GetRandomJsonPrimitives(seed) }, { key3, null }, }; JsonObject objToAdd = SpecialJsonValueHelper.CreateRandomPopulatedJsonObject(seed, maxObj); TestEvents( jo, obj => obj.Add(key4, objToAdd), new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> > { new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(objToAdd, JsonValueChange.Add, key4)), new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(objToAdd, JsonValueChange.Add, key4)), }, obj => obj.Add("key44", objToAdd)); JsonArray jaToAdd = SpecialJsonValueHelper.CreatePrePopulatedJsonArray(seed, maxObj); JsonValue replaced = jo[key2]; TestEvents( jo, obj => obj[key2] = jaToAdd, new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> > { new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(jaToAdd, JsonValueChange.Replace, key2)), new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(replaced, JsonValueChange.Replace, key2)), }); JsonValue jpToAdd = SpecialJsonValueHelper.GetRandomJsonPrimitives(seed); TestEvents( jo, obj => obj[key5] = jpToAdd, new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> > { new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(jpToAdd, JsonValueChange.Add, key5)), new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(jpToAdd, JsonValueChange.Add, key5)), }); jo.Remove(key4); jo.Remove(key5); JsonValue jp1 = SpecialJsonValueHelper.GetRandomJsonPrimitives(seed); JsonValue jp2 = SpecialJsonValueHelper.GetRandomJsonPrimitives(seed); TestEvents( jo, obj => obj.AddRange(new JsonObject { { key4, jp1 }, { key5, jp1 } }), new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> > { new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(jp1, JsonValueChange.Add, key4)), new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(jp2, JsonValueChange.Add, key5)), new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(jp1, JsonValueChange.Add, key4)), new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(jp2, JsonValueChange.Add, key5)), }, obj => obj.AddRange(new JsonObject { { "new key", jp1 }, { "newnewKey", jp2 } })); TestEvents( jo, obj => obj.Remove(key5), new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> > { new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(jp2, JsonValueChange.Remove, key5)), new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(jp2, JsonValueChange.Remove, key5)), }); TestEvents( jo, obj => obj.Remove("not there"), new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >()); jo = new JsonObject { { key1, 1 }, { key2, 2 }, { key3, 3 } }; TestEvents( jo, obj => obj.Clear(), new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> > { new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(null, JsonValueChange.Clear, null)), new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(null, JsonValueChange.Clear, null)), }); jo = new JsonObject { { key1, 1 }, { key2, 2 }, { key3, 3 } }; TestEvents( jo, obj => ((IDictionary <string, JsonValue>)obj).Remove(new KeyValuePair <string, JsonValue>(key2, jo[key2])), new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> > { new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, jo, new JsonValueChangeEventArgs(2, JsonValueChange.Remove, key2)), new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, jo, new JsonValueChangeEventArgs(2, JsonValueChange.Remove, key2)), }, obj => ((IDictionary <string, JsonValue>)obj).Remove(new KeyValuePair <string, JsonValue>(key1, jo[key1]))); }
public void JsonArrayEventsTest() { int seed = 1; const int maxArrayLength = 1024; Random rand = new Random(seed); JsonArray ja = SpecialJsonValueHelper.CreatePrePopulatedJsonArray(seed, rand.Next(maxArrayLength)); int addPosition = ja.Count; JsonValue insertValue = SpecialJsonValueHelper.GetRandomJsonPrimitives(seed); TestEvents( ja, arr => arr.Add(insertValue), new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> > { new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs(insertValue, JsonValueChange.Add, addPosition)), new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs(insertValue, JsonValueChange.Add, addPosition)), }); addPosition = ja.Count; JsonValue jv1 = SpecialJsonValueHelper.GetRandomJsonPrimitives(seed); JsonValue jv2 = SpecialJsonValueHelper.GetRandomJsonPrimitives(seed); TestEvents( ja, arr => arr.AddRange(jv1, jv2), new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> > { new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs( jv1, JsonValueChange.Add, addPosition)), new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs( jv2, JsonValueChange.Add, addPosition + 1)), new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs( jv1, JsonValueChange.Add, addPosition)), new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs( jv2, JsonValueChange.Add, addPosition + 1)), }); int replacePosition = rand.Next(ja.Count - 1); JsonValue oldValue = ja[replacePosition]; JsonValue newValue = SpecialJsonValueHelper.GetRandomJsonPrimitives(seed); TestEvents( ja, arr => arr[replacePosition] = newValue, new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> > { new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs(newValue, JsonValueChange.Replace, replacePosition)), new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs(oldValue, JsonValueChange.Replace, replacePosition)), }); int insertPosition = rand.Next(ja.Count - 1); insertValue = SpecialJsonValueHelper.GetRandomJsonPrimitives(seed); TestEvents( ja, arr => arr.Insert(insertPosition, insertValue), new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> > { new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs(insertValue, JsonValueChange.Add, insertPosition)), new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs(insertValue, JsonValueChange.Add, insertPosition)), }); TestEvents( ja, arr => arr.RemoveAt(insertPosition), new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> > { new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs(insertValue, JsonValueChange.Remove, insertPosition)), new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs(insertValue, JsonValueChange.Remove, insertPosition)), }); ja.Insert(0, insertValue); TestEvents( ja, arr => arr.Remove(insertValue), new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> > { new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs(insertValue, JsonValueChange.Remove, 0)), new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs(insertValue, JsonValueChange.Remove, 0)), }); TestEvents( ja, arr => arr.Clear(), new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> > { new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs(null, JsonValueChange.Clear, 0)), new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs(null, JsonValueChange.Clear, 0)), }); ja = new JsonArray(1, 2, 3); TestEvents( ja, arr => arr.Remove(new JsonPrimitive("Not there")), new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> >()); JsonValue elementInArray = ja[1]; TestEvents( ja, arr => arr.Remove(elementInArray), new List <Tuple <bool, JsonValue, JsonValueChangeEventArgs> > { new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(true, ja, new JsonValueChangeEventArgs(elementInArray, JsonValueChange.Remove, 1)), new Tuple <bool, JsonValue, JsonValueChangeEventArgs>(false, ja, new JsonValueChangeEventArgs(elementInArray, JsonValueChange.Remove, 1)), }); }