Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Linq;
- using System.Xml;
- using AnomieUtilities;
- using FluentAssertions;
- using NUnit.Framework;
- namespace AnomieUtilitiesTests
- {
- [TestFixture]
- public class MonitoredSettings_Should
- {
- MonitoredSettings _settings;
- [SetUp]
- public void Setup()
- {
- _settings = new MonitoredSettings();
- }
- [Test]
- public void Start_Empty()
- {
- _settings.Count.Should().Be(0);
- _settings.IsDirty.Should().BeFalse();
- _settings.DirtiedProperties.Should().BeEmpty();
- }
- [Test]
- public void Set_A_String()
- {
- _settings.Set("Name", "Jason");
- _settings.Count.Should().Be(1);
- _settings.Get<string>("Name").Should().Be("Jason");
- }
- [Test]
- public void Set_A_Int()
- {
- _settings.Set("Age",33);
- _settings.Count.Should().Be(1);
- _settings.IsDirty.Should().BeTrue();
- _settings.Get<int>("Age").Should().Be(33);
- }
- [Test]
- public void Set_Multiple_Property_Types()
- {
- _settings.Set("Age",33);
- _settings.Set("Name","Jason");
- _settings.Set("IsAwesome",true);
- _settings.Count.Should().Be(3);
- _settings.DirtiedProperties.Should().HaveCount(3);
- _settings.IsDirty.Should().BeTrue();
- }
- [Test]
- public void Resolve_Multiple_Property_Types()
- {
- _settings.Set("Age",33);
- _settings.Set("Name","Jason");
- _settings.Set("IsAwesome",true);
- _settings.Resolved();
- _settings.Count.Should().Be(3);
- _settings.DirtiedProperties.Should().HaveCount(0);
- _settings.IsDirty.Should().BeFalse();
- }
- [Test]
- public void Fire_Dirty_Event_For_Multiple_Properties()
- {
- int callCount = 0;
- _settings.Dirtied += () => callCount++;
- _settings.Set("Age",33);
- _settings.Set("Name","Jason");
- _settings.Set("IsAwesome",true);
- callCount.Should().Be(3);
- }
- [Test]
- public void Not_Fire_Dirty_Event_For_Assigning_Multiple_Properties_To_Same_Values()
- {
- int callCount = 0;
- _settings.Set("Age",33);
- _settings.Set("Name","Jason");
- _settings.Set("IsAwesome",true);
- _settings.Resolved();
- _settings.Dirtied += () => callCount++;
- _settings.Set("Age",33);
- _settings.Set("Name","Jason");
- _settings.Set("IsAwesome",true);
- callCount.Should().Be(0);
- }
- [Test]
- public void Get_Values_Of_Dirties_Properties()
- {
- _settings.Set("Age",33);
- _settings.Set("Name","Jason");
- _settings.Set("IsAwesome",true);
- var p = _settings.DirtiedProperties.ToList();
- PropertyValueShouldMatch(p[0],33);
- PropertyValueShouldMatch(p[1],"Jason");
- PropertyValueShouldMatch(p[2],true);
- }
- [Test]
- public void Get_Value_As_Type()
- {
- _settings.Set("Age",33);
- int val = _settings.Get<int>("Age");
- val.Should().Be(33);
- }
- [Test]
- public void Get_Dirtied_Property_As_Type()
- {
- _settings.Set("Age", 33);
- var p = _settings.DirtiedProperties.First();
- int val = _settings.Get<int>(p);
- val.Should().Be(33);
- }
- void PropertyValueShouldMatch(MonitoredSettings.PropertyInfo info,object expected)
- {
- var found = _settings.TryGet(info,out var val);
- found.Should().BeTrue();
- val.Should().BeEquivalentTo(expected);
- }
- [Test]
- public void Be_Dirty_On_Change()
- {
- _settings.Set("Name", "Jason");
- _settings.IsDirty.Should().BeTrue();
- }
- [Test]
- public void Clear_Dirty_State_On_Resolving()
- {
- _settings.Set("Name", "Jason");
- _settings.Resolved();
- _settings.IsDirty.Should().BeFalse();
- }
- [Test]
- public void Not_Be_Dirty_When_Same_Value()
- {
- _settings.Set("Name","Jason");
- _settings.Resolved();
- _settings.Set("Name","Jason");
- _settings.IsDirty.Should().BeFalse();
- }
- [Test]
- public void Send_Dirtied_Event_On_Change()
- {
- int eventCount = 0;
- _settings.Dirtied += () => eventCount++;
- _settings.Set("Name","Jason");
- _settings.Set("Location","Ireland");
- _settings.Set("Name","Tom");
- _settings.Set("Location","Ireland");
- eventCount.Should().Be(3);
- }
- [Test]
- public void When_One_Changed_Return_One_Property_In_DirtyList()
- {
- _settings.Set("Test","Val");
- var s = _settings.DirtiedProperties.ToList();
- s.Should().ContainSingle();
- s[0].Key.Should().Be("Test");
- s[0].Type.Should().Be(typeof(string));
- }
- [Test]
- public void When_TryGetting_Existing_Value_Returns_Expected()
- {
- _settings.Set("Damage",0.5f);
- var changed = _settings.DirtiedProperties.First();
- var found = _settings.TryGet<float>(changed, out var r);
- found.Should().BeTrue();
- r.Should().Be(0.5f);
- }
- [Test]
- public void When_Resolving_Empty_DirtyList()
- {
- _settings.Set("Test","Test");
- _settings.Resolved();
- _settings.DirtiedProperties.Should().BeEmpty();
- }
- [Test]
- public void Getting_Value_That_Does_Not_Exist_Does_Not_Throw()
- {
- var found = _settings.TryGet(typeof(double), "My String", out var v);
- found.Should().BeFalse();
- v.Should().BeNull();
- }
- [Test]
- public void Get_Known_Value()
- {
- _settings.Set("FavDecimal",0.95m);
- decimal val = _settings.Get<decimal>("FavDecimal");
- val.Should().Be(0.95m);
- }
- [Test]
- public void Try_Get_Known_Value()
- {
- _settings.Set("Dec",0.84m);
- if (_settings.TryGet<decimal>("Dec", out var d))
- d.Should().Be(0.84m);
- }
- [Test]
- public void Set_PropertyInfo()
- {
- var pi = new MonitoredSettings.PropertyInfo
- {
- Key = "Works",
- Type = typeof(bool)
- };
- _settings.Set(pi, true);
- _settings.Get<bool>("Works").Should().BeTrue();
- }
- [Test]
- public void Create_PropertyInfo()
- {
- var prop = new MonitoredSettings.PropertyInfo
- {
- Key = "Thing",
- Type = typeof(XmlDocument)
- };
- _settings.Create(prop);
- var existing = _settings.Get<XmlDocument>("Thing");
- existing.Should().BeNull();
- }
- [Test]
- public void Set_Created_PropertyInfo()
- {
- var prop = new MonitoredSettings.PropertyInfo
- {
- Key = "Thing",
- Type = typeof(XmlDocument)
- };
- _settings.Create(prop);
- var doc = new XmlDocument();
- doc.AppendChild( doc.CreateElement( string.Empty, "body", string.Empty ) );
- _settings.Set("Thing",doc);
- XmlDocument returnedDoc = _settings.Get<XmlDocument>("Thing");
- returnedDoc.Should().BeEquivalentTo(doc);
- }
- [Test]
- public void Set_PropertyInfo_Float()
- {
- var pi = new MonitoredSettings.PropertyInfo
- {
- Key = "Angle",
- Type = typeof(float)
- };
- _settings.Set(pi, 90f);
- _settings.Get<float>("Angle").Should().Be(90);
- }
- [Test]
- public void Create_PropertyInfo_Float()
- {
- var pi = new MonitoredSettings.PropertyInfo
- {
- Key = "Angle",
- Type = typeof(float)
- };
- _settings.Create(pi);
- _settings.Get<float>("Angle").Should().Be(0);
- }
- [Test]
- public void Create_OwnClass_From_PropertyInfo()
- {
- var pi = new MonitoredSettings.PropertyInfo
- {
- Key = "123",
- Type = typeof(MyTestClass)
- };
- var tc = new MyTestClass { Val = 71 };
- _settings.Create(pi);
- _settings.Set(pi,tc);
- _settings.Get<MyTestClass>("123").Should().Be(tc);
- }
- [Test]
- public void Getting_Non_Existent_Property_Should_Not_Throw()
- {
- _settings.Get<float>("Angle").Should().Be(0);
- _settings.Get<XmlDocument>("MyDoc").Should().BeNull();
- }
- class MyTestClass
- {
- public int Val;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement