Пример #1
0
        public void TestMementoRestorationAndEquality()
        {
            SmartPropertyBag spb1 = new SmartPropertyBag();

            spb1.AddValue("Fred", 12);
            //spb1.AddExpression("Bill","Math.Max(Fred,17)",new string[]{"Fred"});
            spb1.AddDelegate("Steve", new SmartPropertyBag.SPBDoubleDelegate(ComputeSteve));
            spb1.AddString("Donkey", "Kong");
            spb1.AddBoolean("HabaneroHot", true);

            Highpoint.Sage.Utility.Mementos.IMemento mem1 = spb1.Memento;

            SmartPropertyBag spb2 = new SmartPropertyBag();

            spb2.Memento = mem1;

            Assert.IsTrue(spb1 != spb2, "Object spb1 and spb2 cannot be the same instance");
            Assert.IsTrue(spb1.Equals(spb2), "SPB1 and SPB2 should be equal");
            Assert.IsTrue(spb1.Memento == spb2.Memento, "SPB1.Memento and SPB2.Memento should be equal");

            Debug.WriteLine(DiagnosticAids.DumpDictionary("Before", spb1.Memento.GetDictionary()));
            Debug.WriteLine(DiagnosticAids.DumpDictionary("After", spb2.Memento.GetDictionary()));
        }
Пример #2
0
        public void TestMementoCaching()
        {
            m_steve = 12;
            SmartPropertyBag spb1 = new SmartPropertyBag();

            spb1.AddValue("Fred", 12);
            spb1.AddDelegate("Steve", new SmartPropertyBag.SPBDoubleDelegate(ComputeSteve));
            spb1.AddString("Name", "SPB1");

            Highpoint.Sage.Utility.Mementos.IMemento mem1 = spb1.Memento;
            Debug.WriteLine(DiagnosticAids.DumpDictionary("mem1", mem1.GetDictionary()));
            Highpoint.Sage.Utility.Mementos.IMemento mem2 = spb1.Memento;
            Debug.WriteLine(DiagnosticAids.DumpDictionary("mem2", mem2.GetDictionary()));
            Debug.WriteLine("The new snapshot is " + (mem1 == mem2?"":"not ") + "equal to the one preceding it.\r\n");
            Assert.IsTrue(mem2 == mem1, "Memento 1 is not equal Memento 2");

            Assert.IsTrue((double)spb1["Steve"] == 12, "Steve is not 12");
            Assert.IsTrue("SPB1".Equals(spb1["Name"]), "Name is not SPB1");

            Debug.WriteLine("Changing \"Fred\" to 14 from 12.");
            spb1["Fred"] = 14;
            Highpoint.Sage.Utility.Mementos.IMemento mem3 = spb1.Memento;
            Debug.WriteLine(DiagnosticAids.DumpDictionary("mem3", mem3.GetDictionary()));
            Debug.WriteLine("The new snapshot is " + (mem2 == mem3?"":"not ") + "equal to the one preceding it.\r\n");
            Assert.IsTrue(mem2 != mem3, "Memento 2 is not not equal to Memento 3");
            Assert.IsTrue((double)spb1["Fred"] == 14, "Fred is not 14");

            Debug.WriteLine("Changing \"Name\" to \"spb1\" from \"SPB1\".");
            spb1["Name"] = "spb1";
            Highpoint.Sage.Utility.Mementos.IMemento mem3a = spb1.Memento;
            Debug.WriteLine(DiagnosticAids.DumpDictionary("mem3a", mem3a.GetDictionary()));
            Debug.WriteLine("The new snapshot is " + (mem2 == mem3a?"":"not ") + "equal to the one preceding it.\r\n");
            Assert.IsTrue(mem2 != mem3a, "Memento 2 is equal to memento 3a");

            spb1.AddValue("_Connie", 99);

            SmartPropertyBag spb2 = new SmartPropertyBag();

            spb2.AddValue("_Pete", 14);
            spb1.AddAlias("Pete", spb2, "_Pete");

            Highpoint.Sage.Utility.Mementos.IMemento mem4 = spb1.Memento;
            Highpoint.Sage.Utility.Mementos.IMemento mem5 = spb1.Memento;
            Debug.WriteLine("The new snapshot is " + (mem4 == mem5?"":"not ") + "equal to the one preceding it.\r\n");
            Assert.IsTrue(mem5 == mem4, "Memento 5 is not equal to memento 4");

            Debug.WriteLine("\r\nChanging a value in a foreign alias.");
            spb2["_Pete"] = 16;

            Highpoint.Sage.Utility.Mementos.IMemento mem6 = spb1.Memento;
            Debug.WriteLine("The new snapshot is " + (mem5 == mem6?"":"not ") + "equal to the one preceding it.\r\n");
            Assert.IsTrue(mem5 != mem6, "Memento is equal to memento 6");

            Debug.WriteLine("Capturing another snapshot without change.");
            Highpoint.Sage.Utility.Mementos.IMemento mem7 = spb1.Memento;
            Debug.WriteLine("The new snapshot is " + (mem6 == mem7?"":"not ") + "equal to the one preceding it.\r\n");
            Assert.IsTrue(mem6 == mem7, "Memento 6 is not equal to memento 7");

            Debug.WriteLine("Changing the data driving a delegate-computed value.");
            m_steve = 14;

            Highpoint.Sage.Utility.Mementos.IMemento mem8 = spb1.Memento;
            Debug.WriteLine("The new snapshot is " + (mem8 == mem7?"":"not ") + "equal to the one preceding it.\r\n");
            Assert.IsTrue(mem7 != mem8, "Memento 7 is equal to memento 8");

            Debug.WriteLine("\r\nWe will now test snapshotting's functionality as it pertains to subsidiaries.");

            SmartPropertyBag spb1_1 = new SmartPropertyBag();

            spb1.AddChildSPB("Sub", spb1_1);
            spb1_1.AddValue("marine", 12);

            Highpoint.Sage.Utility.Mementos.IMemento mem9 = spb1.Memento;
            Debug.WriteLine(DiagnosticAids.DumpDictionary("Main", mem9.GetDictionary()));
            Debug.WriteLine("Taking another immediately-following snapshot.");
            Highpoint.Sage.Utility.Mementos.IMemento mem10 = spb1.Memento;
            Debug.WriteLine("The new snapshot is " + (mem9 == mem10?"":"not ") + "equal to the one preceding it.\r\n");
            Assert.IsTrue(mem10 == mem9, "Memento 10 is not equal to memento 9");


            Debug.WriteLine("Changing a data member in a subsidiary.");
            spb1_1["marine"] = 21;
            Highpoint.Sage.Utility.Mementos.IMemento mem11 = spb1.Memento;
            Debug.WriteLine("The new snapshot is " + (mem10 == mem11?"":"not ") + "equal to the one preceding it.\r\n");
            Assert.IsTrue(mem11 != mem10, "Memento 11 is equal to memento 10");
        }