Skip to content

Commit

Permalink
[DigiTally] Output max points and program version
Browse files Browse the repository at this point in the history
  • Loading branch information
ermshiperete committed Jun 12, 2024
1 parent 9084797 commit 4159912
Show file tree
Hide file tree
Showing 18 changed files with 253 additions and 44 deletions.
9 changes: 7 additions & 2 deletions DigiTally/ReadBallots.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
// This software is licensed under the GNU General Public License version 3
// (https://opensource.org/licenses/GPL-3.0)

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using DigitaleBriefwahl.Model;
using SIL.Providers;

namespace DigitaleBriefwahl.Tally
{
public class ReadBallots
{
private Configuration _configuration;
private Dictionary<ElectionModel, Dictionary<string, CandidateResult>> _results;
private readonly Configuration _configuration;
private readonly Dictionary<ElectionModel, Dictionary<string, CandidateResult>> _results;

public ReadBallots(string configFileName)
{
Expand Down Expand Up @@ -78,6 +80,9 @@ public string GetResultString()
strBuilder.AppendLine(new string('-', election.Key.Name.Length));
strBuilder.AppendLine(election.Key.GetResultString(election.Value));
}

strBuilder.AppendLine($"(DigiTally version {GitVersionInformation.SemVer}; " +
$"report executed {DateTimeProvider.Current.Now:yyyy-MM-dd HH:mm})");
return strBuilder.ToString();
}
}
Expand Down
14 changes: 12 additions & 2 deletions DigiTallyTests/AcceptanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
// This software is licensed under the GNU General Public License version 3
// (https://opensource.org/licenses/GPL-3.0)

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using NUnit.Framework;
using SIL.Providers;
using SIL.TestUtilities.Providers;

namespace DigitaleBriefwahl.Tally.Tests
{
Expand Down Expand Up @@ -58,6 +61,7 @@ public void Teardown()
[Test]
public void EndToEnd()
{
// Setup
var ballotFileName1 = Path.Combine(_ballotDirectoryName, Path.GetRandomFileName());
File.WriteAllText(ballotFileName1, "The election\r\n" +
"============\r\n" +
Expand Down Expand Up @@ -127,9 +131,14 @@ public void EndToEnd()
"\r\n" +
"12345\r\n");

DateTimeProvider.SetProvider(new ReproducibleDateTimeProvider(new DateTime(2024, 6,
12, 15, 32, 0)));

// Execute
var output = Program.TallyBallots(_configFileName, _ballotDirectoryName);

Assert.That(output, Is.EqualTo(@"The election
// Verify
Assert.That(output, Is.EqualTo(@$"The election
============
Total of 3 ballots, thereof 1 at least partially invalid.
Expand All @@ -140,7 +149,7 @@ 1. Dagobert Duck (4 points)
2. Mickey Mouse (3 points)
Donald Duck (2 points)
Daisy Duck (0 points)
(3 ballots, thereof 0 invalid)
(3 ballots, thereof 0 invalid; max 6 points)
Election2
---------
Expand All @@ -150,6 +159,7 @@ Daisy Duck (0 points)
Four: 0 J, 0 N, 2 E
(3 ballots, thereof 1 invalid)
(DigiTally version {GitVersionInformation.SemVer}; report executed 2024-06-12 15:32)
"));
}

Expand Down
4 changes: 3 additions & 1 deletion DigiTallyTests/DigiTallyTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
<OutputPath>bin/$(Configuration)</OutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="GitVersion.MsBuild" Version="5.12.0" PrivateAssets="all"/>
<PackageReference Include="ini-parser" Version="2.5.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DigitaleBriefwahlTests\DigitaleBriefwahlTests.csproj" />
<ProjectReference Include="..\DigiTally\DigiTally.csproj" />
</ItemGroup>
</Project>
10 changes: 8 additions & 2 deletions DigiTallyTests/ReadBallotsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
// This software is licensed under the GNU General Public License version 3
// (https://opensource.org/licenses/GPL-3.0)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NUnit.Framework;
using SIL.Providers;
using SIL.TestUtilities.Providers;

namespace DigitaleBriefwahl.Tally.Tests
{
Expand Down Expand Up @@ -194,6 +197,8 @@ public void AddBallot_BothInvalid()
[Test]
public void GetResultString_Valid_OnlyOneNominee()
{
DateTimeProvider.SetProvider(new ReproducibleDateTimeProvider(new DateTime(2024, 6,
12, 15, 32, 0)));
File.WriteAllText(_configFileName, @"[Wahlen]
Titel=The election
Wahl1=Election
Expand Down Expand Up @@ -226,11 +231,12 @@ public void GetResultString_Valid_OnlyOneNominee()
var election1 = sut.Results.First();
Assert.That(election1.Key.BallotsProcessed, Is.EqualTo(1));
Assert.That(election1.Key.Invalid, Is.EqualTo(0));
Assert.That(sut.GetResultString(), Is.EqualTo(@"Election
Assert.That(sut.GetResultString(), Is.EqualTo($@"Election
--------
1. Mickey Mouse (1 points)
(1 ballots, thereof 0 invalid)
(1 ballots, thereof 0 invalid; max 2 points)
(DigiTally version {GitVersionInformation.SemVer}; report executed 2024-06-12 15:32)
"));
}

Expand Down
21 changes: 15 additions & 6 deletions DigiTallyTests/ReadBallotsTests_Weighted.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// Copyright (c) 2024 Eberhard Beilharz
// This software is licensed under the GNU General Public License version 3
// (https://opensource.org/licenses/GPL-3.0)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using DigitaleBriefwahl.Model;
using NUnit.Framework;
using SIL.Providers;
using SIL.TestUtilities.Providers;

namespace DigitaleBriefwahl.Tally.Tests
{
Expand Down Expand Up @@ -43,6 +47,8 @@ public void Setup()
Kandidat3=Dagobert Duck
Kandidat4=Daisy Duck
");
DateTimeProvider.SetProvider(new ReproducibleDateTimeProvider(new DateTime(2024, 6,
12, 15, 32, 0)));
}

[TearDown]
Expand Down Expand Up @@ -283,14 +289,15 @@ public void GetResultString()
_ballotFileNames.Add(ballotFileName);
var sut = new ReadBallots(_configFileName);
Assert.That(sut.AddBallot(ballotFileName), Is.True);
Assert.That(sut.GetResultString(), Is.EqualTo(@"Election
Assert.That(sut.GetResultString(), Is.EqualTo($@"Election
--------
1. Dagobert Duck (2 points)
2. Mickey Mouse (1 points)
Daisy Duck (0 points)
Donald Duck (0 points)
(1 ballots, thereof 0 invalid)
(1 ballots, thereof 0 invalid; max 2 points)
(DigiTally version {GitVersionInformation.SemVer}; report executed 2024-06-12 15:32)
"));
}

Expand Down Expand Up @@ -330,14 +337,15 @@ public void GetResultString_SameVotes()
var sut = new ReadBallots(_configFileName);
Assert.That(sut.AddBallot(ballotFileName1), Is.True);
Assert.That(sut.AddBallot(ballotFileName2), Is.True);
Assert.That(sut.GetResultString(), Is.EqualTo(@"Election
Assert.That(sut.GetResultString(), Is.EqualTo($@"Election
--------
1. Dagobert Duck (3 points)
1. Mickey Mouse (3 points)
Daisy Duck (0 points)
Donald Duck (0 points)
(2 ballots, thereof 0 invalid)
(2 ballots, thereof 0 invalid; max 4 points)
(DigiTally version {GitVersionInformation.SemVer}; report executed 2024-06-12 15:32)
"));
}

Expand All @@ -361,14 +369,15 @@ public void GetResultString_Abstains()
_ballotFileNames.Add(ballotFileName);
var sut = new ReadBallots(_configFileName);
Assert.That(sut.AddBallot(ballotFileName), Is.True);
Assert.That(sut.GetResultString(), Is.EqualTo(@"Election
Assert.That(sut.GetResultString(), Is.EqualTo($@"Election
--------
Dagobert Duck (0 points)
Daisy Duck (0 points)
Donald Duck (0 points)
Mickey Mouse (0 points)
(1 ballots, thereof 0 invalid)
(1 ballots, thereof 0 invalid; max 2 points)
(DigiTally version {GitVersionInformation.SemVer}; report executed 2024-06-12 15:32)
"));
}

Expand Down
13 changes: 11 additions & 2 deletions DigiTallyTests/ReadBallotsTests_YesNo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
// This software is licensed under the GNU General Public License version 3
// (https://opensource.org/licenses/GPL-3.0)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using DigitaleBriefwahl.Model;
using NUnit.Framework;
using SIL.Providers;
using SIL.TestUtilities.Providers;

namespace DigitaleBriefwahl.Tally.Tests
{
Expand Down Expand Up @@ -279,6 +282,8 @@ public void YesNoElection_Valid_Incomplete_NoSpace()
[Test]
public void GetResultString()
{
DateTimeProvider.SetProvider(new ReproducibleDateTimeProvider(new DateTime(2024, 6,
12, 15, 32, 0)));
var ballotFileName = Path.GetTempFileName();
File.WriteAllText(ballotFileName, "The election\r\n" +
"============\r\n" +
Expand All @@ -296,20 +301,23 @@ public void GetResultString()
_ballotFileNames.Add(ballotFileName);
var sut = new ReadBallots(_configFileName);
Assert.That(sut.AddBallot(ballotFileName), Is.True);
Assert.That(sut.GetResultString(), Is.EqualTo(@"Election
Assert.That(sut.GetResultString(), Is.EqualTo($@"Election
--------
Mickey Mouse: 1 J, 0 N, 0 E
Donald Duck: 0 J, 1 N, 0 E
Dagobert Duck: 1 J, 0 N, 0 E
Daisy Duck: 0 J, 0 N, 1 E
(1 ballots, thereof 0 invalid)
(DigiTally version {GitVersionInformation.SemVer}; report executed 2024-06-12 15:32)
"));
}

[Test]
public void GetResultString_DifferentOrder()
{
DateTimeProvider.SetProvider(new ReproducibleDateTimeProvider(new DateTime(2024, 6,
12, 15, 32, 0)));
var ballotFileName = Path.GetTempFileName();
File.WriteAllText(ballotFileName, "The election\r\n" +
"============\r\n" +
Expand All @@ -327,14 +335,15 @@ public void GetResultString_DifferentOrder()
_ballotFileNames.Add(ballotFileName);
var sut = new ReadBallots(_configFileName);
Assert.That(sut.AddBallot(ballotFileName), Is.True);
Assert.That(sut.GetResultString(), Is.EqualTo(@"Election
Assert.That(sut.GetResultString(), Is.EqualTo($@"Election
--------
Mickey Mouse: 1 J, 0 N, 0 E
Donald Duck: 0 J, 1 N, 0 E
Dagobert Duck: 1 J, 0 N, 0 E
Daisy Duck: 0 J, 0 N, 1 E
(1 ballots, thereof 0 invalid)
(DigiTally version {GitVersionInformation.SemVer}; report executed 2024-06-12 15:32)
"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.1.0"/>
Expand Down
10 changes: 10 additions & 0 deletions DigitaleBriefwahl/Model/CandidateResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,15 @@ public abstract class CandidateResult
{
public int Invalid { get; protected set; }
public abstract void CopyFrom(CandidateResult other);

public override bool Equals(object obj)
{
return obj is CandidateResult other && Invalid == other.Invalid;
}

public override int GetHashCode()
{
return 37 * Invalid;
}
}
}
7 changes: 6 additions & 1 deletion DigitaleBriefwahl/Model/ElectionModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,12 @@ public Dictionary<string, CandidateResult> ReadVotesFromBallot(StreamReader stre

public virtual string GetResultString(Dictionary<string, CandidateResult> results)
{
return $"({BallotsProcessed} ballots, thereof {Invalid} invalid)";
return $"({GetResultStringSummary(results)})";
}

protected virtual string GetResultStringSummary(Dictionary<string, CandidateResult> results)
{
return $"{BallotsProcessed} ballots, thereof {Invalid} invalid";
}

public abstract bool SkipNominee(string name, int iVote);
Expand Down
31 changes: 31 additions & 0 deletions DigitaleBriefwahl/Model/WeightedCandidateResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,43 @@ namespace DigitaleBriefwahl.Model
{
public class WeightedCandidateResult: CandidateResult
{
/// <summary>
/// Default c'tor
/// </summary>
public WeightedCandidateResult()
{
}

/// <summary>
/// C'tor for unit tests
/// </summary>
/// <param name="invalid"></param>
/// <param name="points"></param>
public WeightedCandidateResult(int invalid, int points)
{
Invalid = invalid;
Points = points;
}

public int Points { get; internal set; }

public override void CopyFrom(CandidateResult other)
{
if (other is WeightedCandidateResult result)
Points += result.Points;
}

public override bool Equals(object obj)
{
return base.Equals(obj) && obj is WeightedCandidateResult other && other.Points == Points;
}

public override int GetHashCode()
{
var result = 17;
result = 19 * result + base.GetHashCode();
result = 19 * result + Points;
return result;
}
}
}
7 changes: 6 additions & 1 deletion DigitaleBriefwahl/Model/WeightedElectionModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,15 @@ public override string GetResultString(Dictionary<string, CandidateResult> resul
bldr.AppendLine($"{placing} {candidate} ({weightedResult.Points} points)");
}

bldr.AppendLine(base.GetResultString(results));
bldr.AppendLine(GetResultStringSummary(results));
return bldr.ToString();
}

protected override string GetResultStringSummary(Dictionary<string, CandidateResult> results)
{
return $"({base.GetResultStringSummary(results)}; max {(BallotsProcessed - Invalid) * Votes} points)";
}

public override bool SkipNominee(string name, int iVote)
{
var vote = iVote + 1;
Expand Down
Loading

0 comments on commit 4159912

Please sign in to comment.