From e38876fe76065884723cb8e4c775ee114d3355bc Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 26 Jan 2025 16:56:29 -0600 Subject: [PATCH] Add some initial domain objects and some tests --- WireMeUp.sln | 28 +++++++++++++ WireMeUp/Domain/Amps.cs | 32 +++++++++++++++ WireMeUp/Domain/Electricity.cs | 32 +++++++++++++++ WireMeUp/Domain/Voltage.cs | 26 ++++++++++++ WireMeUp/Domain/Watts.cs | 28 +++++++++++++ WireMeUp/Program.cs | 27 +++++++++++++ WireMeUp/WireMeUp.csproj | 15 +++++++ WireMeUpTests/Domain/ElectricityTests.cs | 51 ++++++++++++++++++++++++ WireMeUpTests/WireMeUpTests.csproj | 33 +++++++++++++++ 9 files changed, 272 insertions(+) create mode 100644 WireMeUp.sln create mode 100644 WireMeUp/Domain/Amps.cs create mode 100644 WireMeUp/Domain/Electricity.cs create mode 100644 WireMeUp/Domain/Voltage.cs create mode 100644 WireMeUp/Domain/Watts.cs create mode 100644 WireMeUp/Program.cs create mode 100644 WireMeUp/WireMeUp.csproj create mode 100644 WireMeUpTests/Domain/ElectricityTests.cs create mode 100644 WireMeUpTests/WireMeUpTests.csproj diff --git a/WireMeUp.sln b/WireMeUp.sln new file mode 100644 index 0000000..bda723f --- /dev/null +++ b/WireMeUp.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.12.35707.178 d17.12 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WireMeUp", "WireMeUp\WireMeUp.csproj", "{FCDBD7E9-6A33-4FDA-AE47-5E9B7315D539}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WireMeUpTests", "WireMeUpTests\WireMeUpTests.csproj", "{D879C371-104E-4D06-A38E-B6286522E9F4}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {FCDBD7E9-6A33-4FDA-AE47-5E9B7315D539}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FCDBD7E9-6A33-4FDA-AE47-5E9B7315D539}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FCDBD7E9-6A33-4FDA-AE47-5E9B7315D539}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FCDBD7E9-6A33-4FDA-AE47-5E9B7315D539}.Release|Any CPU.Build.0 = Release|Any CPU + {D879C371-104E-4D06-A38E-B6286522E9F4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D879C371-104E-4D06-A38E-B6286522E9F4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D879C371-104E-4D06-A38E-B6286522E9F4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D879C371-104E-4D06-A38E-B6286522E9F4}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/WireMeUp/Domain/Amps.cs b/WireMeUp/Domain/Amps.cs new file mode 100644 index 0000000..bee7f7d --- /dev/null +++ b/WireMeUp/Domain/Amps.cs @@ -0,0 +1,32 @@ +namespace WireMeUp.Domain +{ + public class Amps(double value) + { + public double Value { get; } = value; + + public override bool Equals(object? obj) + { + return obj is Amps amp && + Value == amp.Value; + } + + public override int GetHashCode() + { + return HashCode.Combine(Value); + } + + public static bool operator ==(Amps? left, Amps? right) + { + return EqualityComparer.Default.Equals(left, right); + } + + public static bool operator !=(Amps? left, Amps? right) + { + return !(left == right); + } + + public static Watts operator *(Amps left, Voltage right) => new(left.Value * right.Value); + + public static implicit operator Amps(double v) => new(v); + } +} diff --git a/WireMeUp/Domain/Electricity.cs b/WireMeUp/Domain/Electricity.cs new file mode 100644 index 0000000..13f7c02 --- /dev/null +++ b/WireMeUp/Domain/Electricity.cs @@ -0,0 +1,32 @@ +using System.Text.Json; + +namespace WireMeUp.Domain +{ + public class Electricity(Voltage voltage, Amps current) + { + public Amps Current { get; } = current; + public Voltage Voltage { get; } = voltage; + + public Watts CalculatePower() + { + return Current * Voltage; + } + + public override bool Equals(object? obj) + { + return obj is Electricity electricity && + Current == electricity.Current && + Voltage == electricity.Voltage; + } + + public override int GetHashCode() + { + return HashCode.Combine(Current, Voltage); + } + + public override string? ToString() + { + return JsonSerializer.Serialize(this); + } + } +} diff --git a/WireMeUp/Domain/Voltage.cs b/WireMeUp/Domain/Voltage.cs new file mode 100644 index 0000000..a921491 --- /dev/null +++ b/WireMeUp/Domain/Voltage.cs @@ -0,0 +1,26 @@ +namespace WireMeUp.Domain +{ + public class Voltage(double value) + { + public double Value { get; } = value; + + public override bool Equals(object? obj) + { + return obj is Voltage voltage && + Value == voltage.Value; + } + + public override int GetHashCode() + { + return HashCode.Combine(Value); + } + + public static bool operator ==(Voltage? left, Voltage? right) => EqualityComparer.Default.Equals(left, right); + + public static bool operator !=(Voltage? left, Voltage? right) => !(left == right); + + public static Watts operator *(Voltage left, Amps right) => new(left.Value * right.Value); + + public static implicit operator Voltage(double v) => new(v); + } +} diff --git a/WireMeUp/Domain/Watts.cs b/WireMeUp/Domain/Watts.cs new file mode 100644 index 0000000..9434323 --- /dev/null +++ b/WireMeUp/Domain/Watts.cs @@ -0,0 +1,28 @@ +namespace WireMeUp.Domain +{ + public class Watts(double value) + { + public double Value { get; } = value; + + public override bool Equals(object? obj) + { + return obj is Watts watt && + Value == watt.Value; + } + + public override int GetHashCode() + { + return HashCode.Combine(Value); + } + + public static bool operator ==(Watts? left, Watts? right) + { + return EqualityComparer.Default.Equals(left, right); + } + + public static bool operator !=(Watts? left, Watts? right) + { + return !(left == right); + } + } +} diff --git a/WireMeUp/Program.cs b/WireMeUp/Program.cs new file mode 100644 index 0000000..0c7c657 --- /dev/null +++ b/WireMeUp/Program.cs @@ -0,0 +1,27 @@ +using WireMeUp.Domain; + +internal class Program +{ + private static async Task Main(string[] args) + { + var x = 0; + CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); + var emmitter = StaticEmitter(new Electricity(new Voltage(12), new Amps(1)), cancellationTokenSource.Token); + await foreach (var item in emmitter) + { + x++; + if (x == 10) cancellationTokenSource.Cancel(); + Console.WriteLine(item); + } + } + + private static async IAsyncEnumerable StaticEmitter(Electricity electricity, CancellationToken token) + { + while (true) + { + if (token.IsCancellationRequested) { break; } + yield return electricity; + Thread.Sleep(1000); + } + } +} \ No newline at end of file diff --git a/WireMeUp/WireMeUp.csproj b/WireMeUp/WireMeUp.csproj new file mode 100644 index 0000000..91fd900 --- /dev/null +++ b/WireMeUp/WireMeUp.csproj @@ -0,0 +1,15 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + + diff --git a/WireMeUpTests/Domain/ElectricityTests.cs b/WireMeUpTests/Domain/ElectricityTests.cs new file mode 100644 index 0000000..c02eb32 --- /dev/null +++ b/WireMeUpTests/Domain/ElectricityTests.cs @@ -0,0 +1,51 @@ +using System.Linq; +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; + +namespace WireMeUp.Domain.Tests +{ + public class ElectricityTests + { + [Fact()] + public void CalculatePowerTest() + { + //Setup + var elect = new Electricity(new Voltage(120), new Amps(5)); + + //Execute + var power = elect.CalculatePower(); + + //Assert + Assert.Equal(new Watts(600), power); + } + + [Fact()] + public void EqualsTest() + { + //Setup + var elect1 = new Electricity(new Voltage(120), new Amps(5)); + var elect2 = new Electricity(new Voltage(120), new Amps(5)); + + //Execute + var result = elect1.Equals(elect2); + + //Assert + Assert.True(result); + } + + [Fact()] + public void GetHashCodeTest() + { + //Setup + var elect1 = new Electricity(new Voltage(120), new Amps(5)); + var elect2 = new Electricity(new Voltage(120), new Amps(5)); + + //Execute + var hashcode = elect1.GetHashCode(); + + //Assert + Assert.Equal(elect2.GetHashCode(), hashcode); + } + } +} \ No newline at end of file diff --git a/WireMeUpTests/WireMeUpTests.csproj b/WireMeUpTests/WireMeUpTests.csproj new file mode 100644 index 0000000..e920ff6 --- /dev/null +++ b/WireMeUpTests/WireMeUpTests.csproj @@ -0,0 +1,33 @@ + + + + net8.0 + enable + enable + + false + true + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + +