コード例 #1
0
        public void SettingsContainerBasicStuffTest()
        {
            var tag      = Guid.NewGuid().ToString();
            var testData = new Dictionary <string, object> {
                ["minusInt"]       = -1,
                ["zeroInt"]        = 0,
                ["plusInto"]       = 1,
                ["minusDouble"]    = -1.5,
                ["zeroDouble"]     = 0.0,
                ["plusDouble"]     = 1.5,
                ["emptystring"]    = "",
                ["string"]         = "unko",
                ["DateTime"]       = DateTime.Now,
                ["DateTiemOffest"] = DateTimeOffset.Now,
                ["Guid"]           = Guid.NewGuid(),
                ["Class"]          = new SettingsTestClass {
                    Test = new SettingsTestClass {
                        Int    = 10,
                        String = Guid.NewGuid().ToString(),
                        Float  = 10.5f
                    },
                    Int    = 1,
                    String = Guid.NewGuid().ToString(),
                    Float  = 5.5f,
                },
            };

            var container = new SettingsContainer(tag);

            Assert.AreEqual(tag, container.Tag);

            int changingCalledCount = 0, changedCalledCount = 0;

            container.SettingChanging += (_, __) => changingCalledCount++;
            container.SettingChanged  += (_, __) => changedCalledCount++;

            foreach (var data in testData)
            {
                container.Set(data.Key, data.Value);
                var actual = container.Get <object>(data.Key);

                Assert.AreEqual(data.Value, actual, data.Key);
            }

            Assert.AreEqual(testData.Count, container.Settings.Count());
            Assert.AreEqual(testData.Count, changingCalledCount);
            Assert.AreEqual(testData.Count, changedCalledCount);


            var expectedCount = container.Settings.Count();

            foreach (var data in testData.TakeWhile(x => x.Value != null))
            {
                container.Remove(data.Key);
                expectedCount--;
                Assert.AreEqual(expectedCount, container.Settings.Count());
            }

            Assert.AreEqual(0, container.Settings.Count());

            foreach (var data in testData)
            {
                container.Set(data.Key, data.Value);
                var actual = container.Get <object>(data.Key);
            }

            Assert.AreEqual(testData.Count, container.Settings.Count());
            container.Clear();
            Assert.AreEqual(0, container.Settings.Count());
        }