added events
This commit is contained in:
@ -4,8 +4,29 @@
|
||||
{
|
||||
private int content;
|
||||
|
||||
public delegate void FullEventHandler(Container container, EventArgs eventArgs);
|
||||
public delegate void OverflowEventHandler(Container container, OverflowEventArgs eventArgs);
|
||||
public event FullEventHandler? Full;
|
||||
public event OverflowEventHandler? Overflowed;
|
||||
|
||||
public int Capacity { get; }
|
||||
public int Content {get => content; set { ArgumentOutOfRangeException.ThrowIfNegative(value, nameof(Content)); ArgumentOutOfRangeException.ThrowIfGreaterThan(value, Capacity, nameof(Content)); content = value; } }
|
||||
public int Content
|
||||
{
|
||||
get => content;
|
||||
set
|
||||
{
|
||||
ArgumentOutOfRangeException.ThrowIfNegative(value, nameof(Content));
|
||||
if (value == Capacity)
|
||||
{
|
||||
Full?.Invoke(this, new());
|
||||
}
|
||||
else if (value > Capacity)
|
||||
{
|
||||
Overflowed?.Invoke(this, new(value - Capacity));
|
||||
}
|
||||
content = Math.Min(value, Capacity);
|
||||
}
|
||||
}
|
||||
|
||||
public Container(int capacity, int content)
|
||||
{
|
||||
|
7
Buckets.Logic/OverflowEventArgs.cs
Normal file
7
Buckets.Logic/OverflowEventArgs.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace Buckets.Logic
|
||||
{
|
||||
public class OverflowEventArgs(int overflowAmount) : EventArgs
|
||||
{
|
||||
public int OverflowAmount { get; } = overflowAmount;
|
||||
}
|
||||
}
|
@ -30,17 +30,6 @@ namespace Buckets.Test
|
||||
});
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Fill_BucketOverCapacity_ShouldThrow()
|
||||
{
|
||||
Bucket bucket = new(10, 0);
|
||||
Bucket bucket2 = new(20, 20);
|
||||
Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
|
||||
{
|
||||
bucket.Fill(bucket2);
|
||||
});
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Fill_SecondBucket_ShouldEqual0()
|
||||
{
|
||||
|
@ -23,15 +23,6 @@ namespace Buckets.Test
|
||||
});
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Container_ContentLargerThanCapacity_ShouldThrow()
|
||||
{
|
||||
Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
|
||||
{
|
||||
Bucket container = new(0, 1);
|
||||
});
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Container_ValidParameters_ShouldEqual10()
|
||||
{
|
||||
@ -39,16 +30,6 @@ namespace Buckets.Test
|
||||
Assert.AreEqual(10, container.Content);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Fill_ContentLargerThanCapacity_ShouldThrow()
|
||||
{
|
||||
Bucket container = new(20, 10);
|
||||
Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
|
||||
{
|
||||
container.Fill(20);
|
||||
});
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Fill_ValidParameters_ShouldEqual15()
|
||||
{
|
||||
|
28
Buckets.Test/EventTest.cs
Normal file
28
Buckets.Test/EventTest.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using Buckets.Logic;
|
||||
|
||||
namespace Buckets.Test
|
||||
{
|
||||
[TestClass]
|
||||
public class EventTest
|
||||
{
|
||||
[TestMethod]
|
||||
public void FullEvent()
|
||||
{
|
||||
Bucket bucket = new(200, 100);
|
||||
bool didRun = false;
|
||||
bucket.Full += (Container container, EventArgs eventArgs) => didRun = true;
|
||||
bucket.Fill(100);
|
||||
Assert.IsTrue(didRun);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void OverflowEvent()
|
||||
{
|
||||
Bucket bucket = new(200, 100);
|
||||
int overflow = 0;
|
||||
bucket.Overflowed += (Container container, OverflowEventArgs eventArgs) => overflow = eventArgs.OverflowAmount;
|
||||
bucket.Fill(200);
|
||||
Assert.AreEqual(100, overflow);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user