예제 #1
0
        public void BaObjectChanges()
        {
            var obj = new BaObject(true, new { Name = "", Greeting = "" });

            var changes = obj.GetChanges();

            Assert.AreEqual(0, changes.Count);

            obj["Name"] = "John";
            changes     = obj.GetChanges();
            Assert.AreEqual(1, changes.Count);
            Assert.AreEqual("John", changes["Name"]);

            obj["Greeting"] = "Hello";
            changes         = obj.GetChanges();
            Assert.AreEqual(2, changes.Count);
            Assert.AreEqual("John", changes["Name"]);
            Assert.AreEqual("Hello", changes["Greeting"]);

            obj["Greeting"] = "";
            changes         = obj.GetChanges();
            Assert.AreEqual(1, changes.Count);
            Assert.IsFalse(changes.ContainsKey("Greeting"));

            obj.UpdateDefaults();
            changes = obj.GetChanges();
            Assert.AreEqual(0, changes.Count);
        }
예제 #2
0
        public void SetValueToDifferentType()
        {
            var obj = new BaObject(true, new { Str = "", Base = new MyBaseObject(), Derived = new MyDerivedObject() });

            Assert.That.Throws <InvalidOperationException>(() => { obj["Str"] = 123; });
            obj["Base"] = new MyDerivedObject();
            Assert.That.Throws <InvalidOperationException>(() => { obj["Derived"] = new MyBaseObject(); });
        }
예제 #3
0
        public void CreateRelaxedBaObject()
        {
            var obj = new BaObject(false);

            Assert.IsNull(obj["INVALID"]);

            obj["VALID"] = 123;
            Assert.AreEqual(123, obj["VALID"]);
        }
예제 #4
0
        public void CreateMixedBaObject()
        {
            var obj = new BaObject(new BaObjectOptions()
            {
                StrictSet = false, StrictGet = true
            });
            object test;

            Assert.That.Throws <ArgumentOutOfRangeException>(() => { test = obj["Test"]; });
            obj["Test"] = 123;
            Assert.AreEqual(123, obj["Test"]);
        }
예제 #5
0
        public void BaObjectIgnoreChanges()
        {
            var obj = new BaObject(true, new { Name = "", Greeting = "" });

            obj["Name"]     = "John";
            obj["Greeting"] = "Hello";

            var changes = obj.GetChanges("Greeting");

            Assert.AreEqual(1, changes.Count);
            Assert.IsFalse(changes.ContainsKey("Greeting"));
        }
예제 #6
0
        public void PropertyChangedEvent()
        {
            var obj         = new BaObject(true, new { Name = (string)null, Greeting = (string)null });
            var lastChanged = (string)null;

            obj.PropertyChanged += (sender, e) =>
            {
                lastChanged = e.PropertyName;
                Debug.WriteLine($"Field '{e.PropertyName}' changed.");
            };

            obj["Name"] = "John";
            Assert.AreEqual("Name", lastChanged);
        }
예제 #7
0
        public void CreateDynamicBaObject()
        {
            dynamic obj = new BaObject(false);

            Assert.IsNull(obj.INVALID);
            obj.VALID = 123;
            Assert.AreEqual(123, obj.VALID);

            obj = new BaObject(true, new { VALID = 0 });
            string invalid;             // Just needed to assign to.

            Assert.That.Throws <RuntimeBinderException>(() => { invalid = obj.INVALID as string; });
            obj.VALID = 123;
            Assert.AreEqual(123, obj.VALID);
        }
예제 #8
0
        public void CreateBaObjectWithSchema()
        {
            var obj = new BaObject(true, new { Test = 123 });
            var fld = obj.Fields["Test"];

            Assert.IsNotNull(fld);
            obj["Test"] = 123;
            Assert.AreEqual("Test", fld.Name);
            Assert.AreEqual(123, fld.Value);
            Assert.AreEqual(123, fld.DefaultValue);
            Assert.AreEqual(typeof(int), fld.FieldType);
            Assert.IsTrue(fld.IsSet);
            Assert.IsFalse(fld.IsChanged);

            Assert.That.Throws <ArgumentOutOfRangeException>(() => { var x = obj["INVALID"]; });
        }
예제 #9
0
        public void CreateStrictBaObject()
        {
            var obj = new BaObject(true);
            var fld = obj.Add("Test", 123);

            Assert.AreEqual("Test", fld.Name);
            Assert.AreEqual(123, fld.Value);
            Assert.AreEqual(123, fld.DefaultValue);
            Assert.AreEqual(typeof(int), fld.FieldType);
            Assert.IsFalse(fld.IsChanged);
            Assert.IsFalse(fld.IsSet);
            fld.Value = fld.Value;
            Assert.IsTrue(fld.IsSet);             // Even though we are setting it to itself, it should still be set now (just not changed).
            Assert.IsFalse(fld.IsChanged);
            fld.Value = 321;
            Assert.IsTrue(fld.IsChanged);

            Assert.That.Throws <ArgumentOutOfRangeException>(() => { var x = obj["INVALID"]; });
        }