Add some initial domain objects and some tests

This commit is contained in:
Ryan 2025-01-26 16:56:29 -06:00
parent 167e015f24
commit e38876fe76
9 changed files with 272 additions and 0 deletions

28
WireMeUp.sln Normal file
View File

@ -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

32
WireMeUp/Domain/Amps.cs Normal file
View File

@ -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<Amps>.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);
}
}

View File

@ -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);
}
}
}

View File

@ -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<Voltage>.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);
}
}

28
WireMeUp/Domain/Watts.cs Normal file
View File

@ -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<Watts>.Default.Equals(left, right);
}
public static bool operator !=(Watts? left, Watts? right)
{
return !(left == right);
}
}
}

27
WireMeUp/Program.cs Normal file
View File

@ -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<Electricity> StaticEmitter(Electricity electricity, CancellationToken token)
{
while (true)
{
if (token.IsCancellationRequested) { break; }
yield return electricity;
Thread.Sleep(1000);
}
}
}

15
WireMeUp/WireMeUp.csproj Normal file
View File

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Folder Include="Adapter\" />
<Folder Include="Application\" />
</ItemGroup>
</Project>

View File

@ -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);
}
}
}

View File

@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\WireMeUp\WireMeUp.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
</Project>