Advertisement
apieceoffruit

MonitoredSettings_Should

Jul 6th, 2022
1,150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.55 KB | None | 0 0
  1. using System.Linq;
  2. using System.Xml;
  3. using AnomieUtilities;
  4. using FluentAssertions;
  5. using NUnit.Framework;
  6.  
  7. namespace AnomieUtilitiesTests
  8. {
  9.     [TestFixture]
  10.     public class MonitoredSettings_Should
  11.     {
  12.         MonitoredSettings _settings;
  13.  
  14.         [SetUp]
  15.         public void Setup()
  16.         {
  17.             _settings = new MonitoredSettings();
  18.         }
  19.        
  20.         [Test]
  21.         public void Start_Empty()
  22.         {
  23.             _settings.Count.Should().Be(0);
  24.             _settings.IsDirty.Should().BeFalse();
  25.             _settings.DirtiedProperties.Should().BeEmpty();
  26.         }
  27.  
  28.         [Test]
  29.         public void Set_A_String()
  30.         {
  31.             _settings.Set("Name", "Jason");
  32.             _settings.Count.Should().Be(1);
  33.             _settings.Get<string>("Name").Should().Be("Jason");
  34.         }
  35.  
  36.         [Test]
  37.         public void Set_A_Int()
  38.         {
  39.             _settings.Set("Age",33);
  40.             _settings.Count.Should().Be(1);
  41.             _settings.IsDirty.Should().BeTrue();
  42.             _settings.Get<int>("Age").Should().Be(33);
  43.         }
  44.  
  45.         [Test]
  46.         public void Set_Multiple_Property_Types()
  47.         {
  48.             _settings.Set("Age",33);
  49.             _settings.Set("Name","Jason");
  50.             _settings.Set("IsAwesome",true);
  51.  
  52.             _settings.Count.Should().Be(3);
  53.             _settings.DirtiedProperties.Should().HaveCount(3);
  54.             _settings.IsDirty.Should().BeTrue();
  55.         }
  56.        
  57.         [Test]
  58.         public void Resolve_Multiple_Property_Types()
  59.         {
  60.             _settings.Set("Age",33);
  61.             _settings.Set("Name","Jason");
  62.             _settings.Set("IsAwesome",true);
  63.             _settings.Resolved();
  64.            
  65.             _settings.Count.Should().Be(3);
  66.             _settings.DirtiedProperties.Should().HaveCount(0);
  67.             _settings.IsDirty.Should().BeFalse();
  68.         }
  69.  
  70.         [Test]
  71.         public void Fire_Dirty_Event_For_Multiple_Properties()
  72.         {
  73.             int callCount = 0;
  74.             _settings.Dirtied += () => callCount++;
  75.            
  76.             _settings.Set("Age",33);
  77.             _settings.Set("Name","Jason");
  78.             _settings.Set("IsAwesome",true);
  79.  
  80.             callCount.Should().Be(3);
  81.         }
  82.        
  83.         [Test]
  84.         public void Not_Fire_Dirty_Event_For_Assigning_Multiple_Properties_To_Same_Values()
  85.         {
  86.             int callCount = 0;
  87.            
  88.             _settings.Set("Age",33);
  89.             _settings.Set("Name","Jason");
  90.             _settings.Set("IsAwesome",true);
  91.             _settings.Resolved();
  92.             _settings.Dirtied += () => callCount++;
  93.             _settings.Set("Age",33);
  94.             _settings.Set("Name","Jason");
  95.             _settings.Set("IsAwesome",true);
  96.             callCount.Should().Be(0);
  97.         }
  98.  
  99.         [Test]
  100.         public void Get_Values_Of_Dirties_Properties()
  101.         {
  102.             _settings.Set("Age",33);
  103.             _settings.Set("Name","Jason");
  104.             _settings.Set("IsAwesome",true);
  105.  
  106.             var p = _settings.DirtiedProperties.ToList();
  107.  
  108.             PropertyValueShouldMatch(p[0],33);
  109.             PropertyValueShouldMatch(p[1],"Jason");
  110.             PropertyValueShouldMatch(p[2],true);
  111.         }
  112.  
  113.  
  114.         [Test]
  115.         public void Get_Value_As_Type()
  116.         {
  117.             _settings.Set("Age",33);
  118.             int val = _settings.Get<int>("Age");
  119.             val.Should().Be(33);
  120.         }
  121.  
  122.         [Test]
  123.         public void Get_Dirtied_Property_As_Type()
  124.         {
  125.             _settings.Set("Age", 33);
  126.             var p = _settings.DirtiedProperties.First();
  127.             int val = _settings.Get<int>(p);
  128.             val.Should().Be(33);
  129.         }
  130.        
  131.        
  132.        
  133.  
  134.         void PropertyValueShouldMatch(MonitoredSettings.PropertyInfo info,object expected)
  135.         {
  136.             var found = _settings.TryGet(info,out var val);
  137.             found.Should().BeTrue();
  138.             val.Should().BeEquivalentTo(expected);
  139.         }
  140.        
  141.        
  142.        
  143.  
  144.         [Test]
  145.         public void Be_Dirty_On_Change()
  146.         {
  147.             _settings.Set("Name", "Jason");
  148.             _settings.IsDirty.Should().BeTrue();
  149.         }
  150.        
  151.        
  152.         [Test]
  153.         public void Clear_Dirty_State_On_Resolving()
  154.         {
  155.             _settings.Set("Name", "Jason");
  156.             _settings.Resolved();
  157.             _settings.IsDirty.Should().BeFalse();
  158.         }
  159.  
  160.         [Test]
  161.         public void Not_Be_Dirty_When_Same_Value()
  162.         {
  163.             _settings.Set("Name","Jason");
  164.             _settings.Resolved();
  165.             _settings.Set("Name","Jason");
  166.             _settings.IsDirty.Should().BeFalse();
  167.         }
  168.        
  169.         [Test]
  170.         public void Send_Dirtied_Event_On_Change()
  171.         {
  172.             int eventCount = 0;
  173.             _settings.Dirtied += () => eventCount++;
  174.             _settings.Set("Name","Jason");
  175.             _settings.Set("Location","Ireland");
  176.             _settings.Set("Name","Tom");
  177.             _settings.Set("Location","Ireland");
  178.             eventCount.Should().Be(3);
  179.         }
  180.  
  181.         [Test]
  182.         public void When_One_Changed_Return_One_Property_In_DirtyList()
  183.         {
  184.             _settings.Set("Test","Val");
  185.             var s = _settings.DirtiedProperties.ToList();
  186.             s.Should().ContainSingle();
  187.             s[0].Key.Should().Be("Test");
  188.             s[0].Type.Should().Be(typeof(string));
  189.         }
  190.  
  191.  
  192.         [Test]
  193.         public void When_TryGetting_Existing_Value_Returns_Expected()
  194.         {
  195.             _settings.Set("Damage",0.5f);
  196.             var changed = _settings.DirtiedProperties.First();
  197.             var found = _settings.TryGet<float>(changed, out var r);
  198.             found.Should().BeTrue();
  199.             r.Should().Be(0.5f);
  200.         }
  201.        
  202.         [Test]
  203.         public void When_Resolving_Empty_DirtyList()
  204.         {
  205.             _settings.Set("Test","Test");
  206.             _settings.Resolved();
  207.  
  208.             _settings.DirtiedProperties.Should().BeEmpty();
  209.         }
  210.  
  211.         [Test]
  212.         public void Getting_Value_That_Does_Not_Exist_Does_Not_Throw()
  213.         {
  214.             var found = _settings.TryGet(typeof(double), "My String", out var v);
  215.             found.Should().BeFalse();
  216.             v.Should().BeNull();
  217.         }
  218.  
  219.  
  220.         [Test]
  221.         public void Get_Known_Value()
  222.         {
  223.             _settings.Set("FavDecimal",0.95m);
  224.             decimal val = _settings.Get<decimal>("FavDecimal");
  225.             val.Should().Be(0.95m);
  226.         }
  227.  
  228.         [Test]
  229.         public void Try_Get_Known_Value()
  230.         {
  231.             _settings.Set("Dec",0.84m);
  232.  
  233.             if (_settings.TryGet<decimal>("Dec", out var d))
  234.                 d.Should().Be(0.84m);
  235.         }
  236.  
  237.         [Test]
  238.         public void Set_PropertyInfo()
  239.         {
  240.             var pi = new MonitoredSettings.PropertyInfo
  241.             {
  242.                 Key = "Works",
  243.                 Type = typeof(bool)
  244.             };
  245.  
  246.             _settings.Set(pi, true);
  247.  
  248.             _settings.Get<bool>("Works").Should().BeTrue();
  249.  
  250.  
  251.         }
  252.  
  253.         [Test]
  254.         public void Create_PropertyInfo()
  255.         {
  256.             var prop = new MonitoredSettings.PropertyInfo
  257.             {
  258.                 Key = "Thing",
  259.                 Type = typeof(XmlDocument)
  260.             };
  261.            
  262.             _settings.Create(prop);
  263.  
  264.             var existing = _settings.Get<XmlDocument>("Thing");
  265.             existing.Should().BeNull();
  266.         }
  267.  
  268.        
  269.         [Test]
  270.         public void Set_Created_PropertyInfo()
  271.         {
  272.             var prop = new MonitoredSettings.PropertyInfo
  273.             {
  274.                 Key = "Thing",
  275.                 Type = typeof(XmlDocument)
  276.             };
  277.            
  278.             _settings.Create(prop);
  279.  
  280.             var doc = new XmlDocument();
  281.             doc.AppendChild( doc.CreateElement( string.Empty, "body", string.Empty ) );
  282.             _settings.Set("Thing",doc);
  283.  
  284.             XmlDocument returnedDoc = _settings.Get<XmlDocument>("Thing");
  285.             returnedDoc.Should().BeEquivalentTo(doc);
  286.  
  287.         }
  288.        
  289.        
  290.  
  291.         [Test]
  292.         public void Set_PropertyInfo_Float()
  293.         {
  294.             var pi = new MonitoredSettings.PropertyInfo
  295.             {
  296.                 Key = "Angle",
  297.                 Type = typeof(float)
  298.             };
  299.  
  300.             _settings.Set(pi, 90f);
  301.  
  302.             _settings.Get<float>("Angle").Should().Be(90);
  303.         }
  304.        
  305.         [Test]
  306.         public void Create_PropertyInfo_Float()
  307.         {
  308.             var pi = new MonitoredSettings.PropertyInfo
  309.             {
  310.                 Key = "Angle",
  311.                 Type = typeof(float)
  312.             };
  313.  
  314.             _settings.Create(pi);
  315.  
  316.             _settings.Get<float>("Angle").Should().Be(0);
  317.         }
  318.        
  319.  
  320.         [Test]
  321.         public void Create_OwnClass_From_PropertyInfo()
  322.         {
  323.             var pi = new MonitoredSettings.PropertyInfo
  324.             {
  325.                 Key = "123",
  326.                 Type = typeof(MyTestClass)
  327.             };
  328.  
  329.             var tc = new MyTestClass { Val = 71 };
  330.             _settings.Create(pi);
  331.  
  332.             _settings.Set(pi,tc);
  333.             _settings.Get<MyTestClass>("123").Should().Be(tc);
  334.         }
  335.  
  336.         [Test]
  337.         public void Getting_Non_Existent_Property_Should_Not_Throw()
  338.         {
  339.             _settings.Get<float>("Angle").Should().Be(0);
  340.             _settings.Get<XmlDocument>("MyDoc").Should().BeNull();
  341.         }
  342.        
  343.         class MyTestClass
  344.         {
  345.             public int Val;
  346.         }
  347.     }
  348. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement