-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Calculate winners ignores ignored choices #247
- Loading branch information
Showing
6 changed files
with
133 additions
and
1 deletion.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
src/main/java/org/rulez/demokracia/pdengine/CanCalculateWinners.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.rulez.demokracia.pdengine; | ||
|
||
import java.util.List; | ||
|
||
public interface CanCalculateWinners { | ||
|
||
List<String> calculateWinners(List<String> ignoredChoices); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/org/rulez/demokracia/pdengine/WinnerCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.rulez.demokracia.pdengine; | ||
|
||
import java.util.List; | ||
|
||
public interface WinnerCalculator { | ||
|
||
List<String> calculateWinners(final BeatTable beatTable); | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/org/rulez/demokracia/pdengine/WinnerCalculatorImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.rulez.demokracia.pdengine; | ||
|
||
import java.util.List; | ||
|
||
public class WinnerCalculatorImpl implements WinnerCalculator { | ||
|
||
@Override | ||
public List<String> calculateWinners(final BeatTable beatTable) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
} |
66 changes: 66 additions & 0 deletions
66
src/test/java/org/rulez/demokracia/pdengine/CalculateWinnersTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.rulez.demokracia.pdengine; | ||
|
||
import static org.junit.Assert.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.stubbing.Answer; | ||
import org.rulez.demokracia.pdengine.annotations.TestedBehaviour; | ||
import org.rulez.demokracia.pdengine.annotations.TestedFeature; | ||
import org.rulez.demokracia.pdengine.annotations.TestedOperation; | ||
import org.rulez.demokracia.pdengine.testhelpers.CreatedDefaultCastVoteWithRankedChoices; | ||
|
||
@TestedFeature("Vote") | ||
@TestedOperation("calculate winners") | ||
public class CalculateWinnersTest extends CreatedDefaultCastVoteWithRankedChoices { | ||
|
||
private final Answer<?> answerKeyCollection = (invocation) -> { | ||
BeatTable beatTable = (BeatTable) invocation.getArguments()[0]; | ||
return beatTable.getKeyCollection().stream().collect(Collectors.toList()); | ||
}; | ||
|
||
private ComputedVote computedVote; | ||
private final ArgumentCaptor<BeatTable> captor = ArgumentCaptor.forClass(BeatTable.class); | ||
|
||
@Override | ||
@Before | ||
public void setUp() { | ||
super.setUp(); | ||
getTheVote().votesCast = castVote; | ||
WinnerCalculatorImpl winnerCalculator = mock(WinnerCalculatorImpl.class); | ||
when(winnerCalculator.calculateWinners(captor.capture())).thenAnswer(answerKeyCollection); | ||
|
||
computedVote = new ComputedVote(getTheVote()); | ||
computedVote.computeVote(); | ||
computedVote.setWinnerCalculator(winnerCalculator); | ||
} | ||
|
||
@TestedBehaviour("only choices not in ignoredChoices are considered") | ||
@Test | ||
public void calculate_winners_returns_none_of_the_ignored_choices() { | ||
List<String> winners = computedVote.calculateWinners(Arrays.asList("A")); | ||
assertFalse(winners.contains("A")); | ||
|
||
} | ||
|
||
@TestedBehaviour("only choices not in ignoredChoices are considered") | ||
@Test | ||
public void calculate_winners_returns_not_ignored_winner() { | ||
List<String> winners = computedVote.calculateWinners(Arrays.asList("C")); | ||
assertBeatTableContainChoice(winners, "A"); | ||
} | ||
|
||
private void assertBeatTableContainChoice(final List<String> winners, final String choice) { | ||
assertTrue(winners.contains(choice)); | ||
BeatTable capturedBeatTable = captor.getValue(); | ||
assertNotNull(capturedBeatTable.getElement(choice, "B")); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters