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)),
            });
        }
示例#2
0
        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])));
        }