public void DynamicMapNullAssignmentFiresAddAndDelete() { var three = new MyIntItem(3); var four = new MyIntItem(4); DynamicMap <MyIntItem> dm = new DynamicMap <MyIntItem> { { "fromnull", null }, { "tonull", three } }; DynamicMap <MyIntItem> dm2 = new DynamicMap <MyIntItem> { { "fromnull", four }, { "tonull", null } }; bool added = false; dm.ItemAdded += (sender, args) => { Assert.False(added); added = true; Assert.Equal("fromnull", args.Key); Assert.Same(four, args.NewItem); }; dm.ItemUpdated += (sender, args) => Assert.False(true); bool deleted = false; dm.ItemDeleted += (sender, args) => { Assert.False(deleted); deleted = true; Assert.Equal("tonull", args.Key); Assert.Same(three, args.OldItem); }; dm.Assign(dm2); Assert.True(added); Assert.True(deleted); }
public void DynamicMapAssignmentChangesValue() { MyComposite sm1 = new MyComposite { si = 42, ss1 = "foo", ss2 = null, nested = new MyComposite.MyNested { x = 10, y = 20, s = "bar" }, ci = null }; MyComposite sm2 = new MyComposite { si = 43, ss1 = "foo", ss2 = null, nested = new MyComposite.MyNested { x = 10, y = 20, s = "bar" }, ci = null }; DynamicMap <ConfigItem> dm = new DynamicMap <ConfigItem> { { "name", (SingleItem <string>) "foo" }, { "number", (SingleValue <int>) 42 }, { "smap", sm1 } }; DynamicMap <ConfigItem> dm2 = new DynamicMap <ConfigItem> { { "name", (SingleItem <string>) "bar" }, { "number", (SingleValue <int>) 40 }, { "smap", sm2 } }; dm.Assign(dm2); Assert.Equal("bar", (SingleItem <string>)dm["name"]); Assert.Equal <int>(40, (SingleValue <int>)dm["number"]); Assert.Equal <int>(43, ((MyComposite)dm["smap"]).si); Assert.Same(sm1, dm["smap"]); }
public void DynamicMapAssignKeepsReferences() { var three = new MyIntItem(3); var four = new MyIntItem(4); var newFive = new MyIntItem(5); DynamicMap <MyIntItem> dm = new DynamicMap <MyIntItem> { { "a", three }, { "b", four } }; DynamicMap <MyIntItem> dm2 = new DynamicMap <MyIntItem> { { "a", new MyIntItem(3) }, { "b", four }, { "c", newFive } }; dm.Assign(dm2); Assert.Same(three, dm["a"]); Assert.Same(four, dm["b"]); Assert.Same(newFive, dm["c"]); }
public void DynamicMapInvalidAssignmentThrows() { { DynamicMap <ConfigItem> dm = new DynamicMap <ConfigItem> { { "name", (SingleItem <string>) "foo" }, { "number", (SingleValue <int>) 42 } }; ConfigItem ci = new MyFalseEquatableItem(); Assert.Throws <ConfigItem.InvalidTypeAssignmentException>(() => dm.Assign(ci)); } { DynamicMap <ConfigItem> dm = new DynamicMap <ConfigItem> { { "name", (SingleItem <string>) "foo" }, { "comp", new MyComposite { ss1 = "foo" } } }; DynamicMap <ConfigItem> dm2 = new DynamicMap <ConfigItem> { { "name", (SingleItem <string>) "foo" }, { "comp", new MyComposite { ss1 = null } } }; bool thrown = false; try { dm.Assign(dm2); } catch (ConfigItem.InvalidChildAssignmentException e) { thrown = true; Assert.Single(e.ChildExceptions); var ce = e.ChildExceptions.First(); Assert.IsType <ConfigItem.InvalidChildAssignmentException>(ce); var ceces = ((ConfigItem.InvalidChildAssignmentException)ce).ChildExceptions; Assert.Single(ceces); var cecesFirst = ceces.First(); Assert.IsType <ConfigItem.InvalidTypeAssignmentException>(cecesFirst); var cece = (ConfigItem.InvalidTypeAssignmentException)cecesFirst; Assert.Equal(new SingleItem <string>("foo"), cece.OldItem); Assert.Null(cece.NewItem); } Assert.True(thrown); } }
public void DynamicMapEventsAreCalled() { SingleValue <int> three = 3; SingleValue <int> four = 4; SingleValue <int> five = 5; SingleValue <int> n2 = 6; DynamicMap <SingleValue <int> > dm = new DynamicMap <SingleValue <int> > { { "a", three }, { "b", four }, { "n1", null }, { "n2", null } }; DynamicMap <SingleValue <int> > dm2 = new DynamicMap <SingleValue <int> > { { "b", 42 }, { "c", five }, { "n1", null }, { "n2", n2 } }; bool threeDeleted = false; bool othersDeleted = false; void deleteHandler(object sender, DynamicMap <SingleValue <int> > .ItemDeletedArgs args) { Assert.Same(dm, sender); if (args.Key == "a" && object.ReferenceEquals(args.OldItem, three)) { Assert.False(threeDeleted); threeDeleted = true; } else { othersDeleted = true; } } dm.ItemDeleted += deleteHandler; bool fiveAdded = false; bool n2Added = false; bool othersAdded = false; void addHandler(object sender, DynamicMap <SingleValue <int> > .ItemAddedArgs args) { Assert.Same(dm, sender); if (args.Key == "c" && object.ReferenceEquals(args.NewItem, five)) { Assert.False(fiveAdded); fiveAdded = true; } else if (args.Key == "n2" && object.ReferenceEquals(args.NewItem, n2)) { Assert.False(n2Added); n2Added = true; } else { othersAdded = true; } } dm.ItemAdded += addHandler; bool fourUpdated = false; bool othersUpdated = false; void updateHandler(object sender, DynamicMap <SingleValue <int> > .ItemUpdatedArgs args) { Assert.Same(dm, sender); if (args.Key == "b" && object.ReferenceEquals(args.Item, four)) { Assert.False(fourUpdated); fourUpdated = true; } else { othersUpdated = true; } } dm.ItemUpdated += updateHandler; bool fourObjectUpdated = false; void fourUpdateHandler(object sender, SingleValue <int> .UpdatedArgs args) { Assert.False(fourObjectUpdated); fourObjectUpdated = true; } dm["b"].Updated += fourUpdateHandler; bool mapUpdateCalled = false; dm.Updated += (sender, args) => { Assert.False(mapUpdateCalled); mapUpdateCalled = true; var deleted = new List <KeyValuePair <string, SingleValue <int> > >(args.DeletedItems); Assert.Single(deleted); Assert.Equal("a", deleted[0].Key); Assert.Same(three, deleted[0].Value); var added = new List <KeyValuePair <string, SingleValue <int> > >(args.AddedItems); Assert.Equal(2, added.Count); Assert.Equal("c", added[0].Key); Assert.Same(five, added[0].Value); Assert.Equal("n2", added[1].Key); Assert.Same(n2, added[1].Value); var updated = new List <KeyValuePair <string, SingleValue <int> > >(args.UpdatedItems); Assert.Single(updated); Assert.Equal("b", updated[0].Key); Assert.Same(four, updated[0].Value); }; dm.Assign(dm2); Assert.True(threeDeleted); Assert.False(othersDeleted); Assert.True(fiveAdded); Assert.True(n2Added); Assert.False(othersAdded); Assert.True(fourUpdated); Assert.False(othersUpdated); Assert.True(fourObjectUpdated); Assert.True(mapUpdateCalled); Assert.Equal(four, dm["b"]); Assert.Equal <SingleValue <int> >(42, four); Assert.Equal(five, dm["c"]); }