public void TestDisposePatchListening()
        {
            var model = ModelType.Create();

            model.Unprotected();

            var patches = new List <IJsonPatch>();

            var disposable = model.OnPatch((patch, _) => patches.Add(patch));

            model.To = "universe";

            disposable.Dispose();

            model.To = "mars";

            Assert.Single(patches);

            Assert.Equal(JsonPatchOperation.Replace, patches[0].Operation);
            Assert.Equal("/To", patches[0].Path);
            Assert.Equal("universe", patches[0].Value);

            var snapshot = model.GetSnapshot <IModelSnapshot>();

            Assert.NotNull(snapshot);
            Assert.Equal("mars", snapshot.To);
        }
        public void TestApplyActionCalls()
        {
            var model = ModelType.Create();

            var calls = new ISerializedActionCall[]
            {
                new SerializedActionCall
                {
                    Name = "SetTo",

                    Path = "",

                    Arguments = new object[] { "mars" }
                },

                new SerializedActionCall
                {
                    Name = "SetTo",

                    Path = "",

                    Arguments = new object[] { "universe" }
                }
            };

            model.ApplyAction(calls);

            var snapshot = model.GetSnapshot <IModelSnapshot>();

            Assert.NotNull(snapshot);
            Assert.Equal("universe", snapshot.To);
        }
        public void TestEmitUpdatePatch()
        {
            var modelMap = ModelMapType.Create();

            modelMap.Unprotected();

            modelMap["hello"] = ModelType.Create();

            var patches = new List <IJsonPatch>();

            modelMap.OnPatch((patch, _) => patches.Add(patch));

            modelMap["hello"] = ModelType.Create(new ModelSnapshot {
                To = "universe"
            });

            Assert.Single(patches);

            Assert.NotNull(patches[0]);

            Assert.Equal(JsonPatchOperation.Replace, patches[0].Operation);
            Assert.Equal("/hello", patches[0].Path);
            Assert.True(patches[0].Value is IModelSnapshot);
            Assert.Equal("universe", (patches[0].Value as IModelSnapshot).To);
        }
        public void TestDefaultSnapshot()
        {
            var model = ModelType.Create();

            var snapshot = model.GetSnapshot <IModelSnapshot>();

            Assert.NotNull(snapshot);
            Assert.Equal("world", snapshot.To);
        }
        public void TestActionIsCalled()
        {
            var model = ModelType.Create();

            model.SetTo("universe");

            var snapshot = model.GetSnapshot <IModelSnapshot>();

            Assert.NotNull(snapshot);
            Assert.Equal("universe", snapshot.To);
        }
        public void TestFactoryCreation()
        {
            var instance = ModelType.Create();

            Assert.Equal("world", instance.To);

            var snapshot = instance.GetSnapshot <IModelSnapshot>();

            Assert.NotNull(snapshot);

            Assert.Equal("world", snapshot.To);
        }
        /// <summary>
        /// Gets a <see cref="ModelInstance"/> with the specified type and id, which may be either an existing
        /// instance or a new instance created during the scope of the current transaction.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public ModelInstance GetInstance(ModelType type, string id)
        {
            // First check to see if this is a new instance that has been cached by the transaction
            ModelInstance instance;

            if (newInstances != null && newInstances.TryGetValue(GetNewInstanceKey(type, id), out instance))
            {
                return(instance);
            }

            // Otherwise, assume it is an existing instance
            return(type.Create(id));
        }
        public void TestApplySnapshots()
        {
            var model = ModelType.Create();

            // model.Unprotected();

            model.ApplySnapshot <IModelSnapshot>(new ModelSnapshot {
                To = "universe"
            });

            var snapshot = model.GetSnapshot <IModelSnapshot>();

            Assert.NotNull(snapshot);
            Assert.Equal("universe", snapshot.To);
        }
        public void TestEmitActionCall()
        {
            var model = ModelType.Create();

            var calls = new List <ISerializedActionCall>();

            model.OnAction(call => calls.Add(call));

            model.SetTo("universe");

            Assert.Single(calls);
            Assert.Equal("SetTo", calls[0].Name);
            Assert.Equal("", calls[0].Path);
            Assert.Equal("universe", calls[0].Arguments[0]);
        }
        public void TestApplyPatche()
        {
            var model = ModelType.Create();

            model.Unprotected();

            model.ApplyPatch(new JsonPatch {
                Operation = JsonPatchOperation.Replace, Path = "/To", Value = "universe"
            });

            var snapshot = model.GetSnapshot <IModelSnapshot>();

            Assert.NotNull(snapshot);
            Assert.Equal("universe", snapshot.To);
        }
        public void TestEmitSnapshots()
        {
            var model = ModelType.Create();

            model.Unprotected();

            var snapshots = new List <IModelSnapshot>();

            model.OnSnapshot <IModelSnapshot>(snapshot => snapshots.Add(snapshot));

            model.To = "universe";

            Assert.Single(snapshots);
            Assert.Equal("universe", snapshots[0].To);
        }
        public void TestRestoreSnapshotState()
        {
            var instance = ModelType.Create(new ModelSnapshot {
                To = "universe"
            });

            Assert.Equal("universe", instance.To);

            var snapshot = instance.GetSnapshot <IModelSnapshot>();

            Assert.NotNull(snapshot);

            Assert.Equal(typeof(ModelSnapshot), snapshot.GetType());

            Assert.Equal("universe", snapshot.To);
        }
        public void TestEmitPatches()
        {
            var model = ModelType.Create();

            model.Unprotected();

            var patches = new List <IJsonPatch>();

            model.OnPatch((patch, _) => patches.Add(patch));

            model.To = "universe";

            Assert.Single(patches);

            Assert.Equal(JsonPatchOperation.Replace, patches[0].Operation);
            Assert.Equal("/To", patches[0].Path);
            Assert.Equal("universe", patches[0].Value);
        }
        public void TestEmitSnapshot()
        {
            var modelMap = ModelMapType.Create();

            modelMap.Unprotected();

            var snapshots = new List <IMap <string, IModelSnapshot> >();

            modelMap.OnSnapshot <IMap <string, IModelSnapshot> >(snapshot => snapshots.Add(snapshot));

            modelMap["hello"] = ModelType.Create();

            var snapshot = snapshots[0];

            Assert.NotNull(snapshot);
            Assert.True(snapshot.ContainsKey("hello"));
            Assert.Equal(1, snapshot.Keys.Count);

            Assert.True(snapshot["hello"] is IModelSnapshot);
            Assert.Equal("world", snapshot["hello"].To);
        }
        public void TestEmitRemovePatch()
        {
            var modelMap = ModelMapType.Create();

            modelMap.Unprotected();

            modelMap["hello"] = ModelType.Create();

            var patches = new List <IJsonPatch>();

            modelMap.OnPatch((patch, _) => patches.Add(patch));

            modelMap.Remove("hello");

            Assert.Single(patches);

            Assert.NotNull(patches[0]);

            Assert.Equal(JsonPatchOperation.Remove, patches[0].Operation);
            Assert.Equal("/hello", patches[0].Path);
            Assert.Null(patches[0].Value);
        }
        public void TestUpdatedSnapshot()
        {
            var modelMap = ModelMapType.Create();

            modelMap.Unprotected();

            modelMap["hello"] = ModelType.Create();

            Assert.True(modelMap.ContainsKey("hello"));
            Assert.Equal(1, modelMap.Keys.Count);

            Assert.True(modelMap["hello"] is IModelSnapshot);
            Assert.Equal("world", modelMap["hello"].To);

            var snapshot = modelMap.GetSnapshot <IMap <string, IModelSnapshot> >();

            Assert.NotNull(snapshot);
            Assert.True(snapshot.ContainsKey("hello"));
            Assert.Equal(1, snapshot.Keys.Count);

            Assert.True(snapshot["hello"] is IModelSnapshot);
            Assert.Equal("world", snapshot["hello"].To);
        }
        public void TestAppyRemovePatch()
        {
            var modelMap = ModelMapType.Create();

            modelMap.Unprotected();

            modelMap["hello"] = ModelType.Create();

            modelMap.ApplyPatch(new JsonPatch
            {
                Operation = JsonPatchOperation.Remove,

                Path = "hello"
            });

            Assert.False(modelMap.ContainsKey("hello"));
            Assert.Equal(0, modelMap.Keys.Count);

            var snapshot = modelMap.GetSnapshot <IMap <string, IModelSnapshot> >();

            Assert.NotNull(snapshot);
            Assert.False(snapshot.ContainsKey("hello"));
            Assert.Equal(0, snapshot.Keys.Count);
        }