Skip to content

Instantly share code, notes, and snippets.

@es-repo
Last active May 22, 2022 20:58
Show Gist options
  • Save es-repo/fe17787db63f51cf426a1b0495cf03f9 to your computer and use it in GitHub Desktop.
Save es-repo/fe17787db63f51cf426a1b0495cf03f9 to your computer and use it in GitHub Desktop.
better-unit-test-in-c#-article-Box-v1.cs
public sealed record Box
{
private readonly Dictionary<string, Thing> thingsInside;
public int Size { get; init; }
public bool IsOpen { get; private set; }
public Box()
{
thingsInside = new();
}
public void Open()
{
IsOpen = true;
}
public void Close()
{
IsOpen = false;
}
public int GetAvailableSpace()
{
return Size - thingsInside.Values.Sum(thing => thing.Size);
}
public bool CanPutInside(Thing thing, string label)
{
return IsOpen && GetAvailableSpace() - thing.Size >= 0 && !thingsInside.ContainsKey(label);
}
public bool PutInside(
Thing thing,
string label)
{
if (!CanPutInside(thing, label))
{
return false;
}
thingsInside.Add(label, thing);
return true;
}
}
public sealed record Thing
{
public int Size { get; init; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment