Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
47a4af1cc0 | |||
61b60cfbaf |
BIN
Homework 09.pdf
BIN
Homework 09.pdf
Binary file not shown.
5
dicegame/.gitignore
vendored
5
dicegame/.gitignore
vendored
@ -1 +1,4 @@
|
||||
/target
|
||||
.gradle/
|
||||
.idea/
|
||||
src/build/
|
||||
target/
|
||||
|
@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="AdditionalModuleElements">
|
||||
<content url="file://$MODULE_DIR$" dumb="true">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
</component>
|
||||
</module>
|
@ -1,29 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.sq2024.homework08</groupId>
|
||||
<artifactId>dicegame</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>5.11.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>5.7.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -1,118 +0,0 @@
|
||||
package dicegame.logic;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class LogicTest {
|
||||
Logic logic;
|
||||
|
||||
/**
|
||||
* K1 Passed index:
|
||||
* - index: 0-5
|
||||
* - error: out of bounds
|
||||
*
|
||||
* K2 The number of the dice passed:
|
||||
* - value: 1 | 5
|
||||
* - error: !(1 | 5)
|
||||
*
|
||||
* K3 Existing fixation of the passed dice:
|
||||
* - fixable: true | false
|
||||
* - error: dice was fixed previously
|
||||
*
|
||||
* K4 Number of other dice of the same number fixed in the current roll:
|
||||
* - bonus triggered: (2 | 5) of the same number fixed
|
||||
* - no bonus triggered: (1 | 3 | 4) of the same number fixed
|
||||
* - first fix of the game: 0 of the same number fixed
|
||||
*
|
||||
* K5 Number of fixed dice of the same number in previous rolls:
|
||||
* - bonus triggered: (2 | 5) of the same number fixed
|
||||
* - no bonus triggered: (1 | 3 | 4) of the same number fixed
|
||||
* - first fix of the game: 0 of the same number fixed
|
||||
*/
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
logic = null;
|
||||
}
|
||||
|
||||
// K2 (c)
|
||||
// Valid index: 0-5
|
||||
// The value for this index is not 1 or 5
|
||||
@Test
|
||||
void changeDiceStateWithValidDiceNot1Or5() {
|
||||
startGameAndRollDiceWithValue(2);
|
||||
|
||||
Exception e = assertThrows(SelectionException.class, () -> logic.changeDiceState(0));
|
||||
String expectedMessage = "Es können nur 1'en und 5'en fixiert werden!";
|
||||
String actualMessage = e.getMessage();
|
||||
assertEquals(expectedMessage, actualMessage);
|
||||
}
|
||||
|
||||
// K3 (a)
|
||||
@ParameterizedTest
|
||||
@CsvSource({"1","5"})
|
||||
void changeDiceStateWithValidDice1Or5(int diceValue) {
|
||||
startGameAndRollDiceWithValue(diceValue);
|
||||
|
||||
// Fixing dice in round 1
|
||||
assertDoesNotThrow(() -> logic.changeDiceState(0));
|
||||
|
||||
// Starting next round
|
||||
logic.rollDice();
|
||||
|
||||
// Trying to fix dice again in round 2
|
||||
Exception e = assertThrows(SelectionException.class, () -> logic.changeDiceState(0));
|
||||
String expectedMessage = "Der Würfel wurde bereits in einer vorherigen Runde fixiert!";
|
||||
String actualMessage = e.getMessage();
|
||||
assertEquals(expectedMessage, actualMessage);
|
||||
}
|
||||
|
||||
// K3 (b)
|
||||
// Valid index: 0-5
|
||||
// Value: 0 | 5
|
||||
// Index refers to dice which has been fixed in current round
|
||||
@ParameterizedTest
|
||||
@CsvSource({"1","5"})
|
||||
void changeDiceStateWhenFixedInThisRoll(int diceValue) {
|
||||
startGameAndRollDiceWithValue(diceValue);
|
||||
assertFalse(logic.isDieFixed(0));
|
||||
|
||||
// Fixing dice
|
||||
assertDoesNotThrow(() -> logic.changeDiceState(0));
|
||||
assertTrue(logic.isDieFixed(0));
|
||||
|
||||
//Unfixing dice
|
||||
assertDoesNotThrow(() -> logic.changeDiceState(0));
|
||||
assertFalse(logic.isDieFixed(0));
|
||||
}
|
||||
|
||||
// K3 (c)
|
||||
// Valid index: 0-5
|
||||
// Value: 0 | 5
|
||||
// Index refers to dice which is not fixed
|
||||
@ParameterizedTest
|
||||
@CsvSource({"1","5"})
|
||||
void changeDiceStateWhenNotFixed(int diceValue) {
|
||||
startGameAndRollDiceWithValue(diceValue);
|
||||
assertFalse(logic.isDieFixed(0));
|
||||
|
||||
// Fixing dice
|
||||
assertDoesNotThrow(() -> logic.changeDiceState(0));
|
||||
assertTrue(logic.isDieFixed(0));
|
||||
}
|
||||
|
||||
private void startGameAndRollDiceWithValue(int value) {
|
||||
Random mockedRandom = Mockito.mock(Random.class);
|
||||
Mockito.when(mockedRandom.nextInt(6)).thenReturn(value - 1);
|
||||
logic = new Logic(mockedRandom);
|
||||
logic.startGame();
|
||||
logic.rollDice();
|
||||
}
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
package org.jabref.model.search.matchers;
|
||||
|
||||
import org.jabref.model.search.SearchMatcher;
|
||||
import org.jabref.model.search.rules.MockSearchMatcher;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class AndMatcherTest extends SearchMatcherTest {
|
||||
@Override
|
||||
protected List<SearchMatcher> makeTrueMatchers() {
|
||||
List<SearchMatcher> matchers = new ArrayList<>();
|
||||
matchers.add(buildMatcher(true, true));
|
||||
return matchers;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<SearchMatcher> makeFalseMatchers() {
|
||||
List<SearchMatcher> matchers = new ArrayList<>();
|
||||
matchers.add(buildMatcher(true, false));
|
||||
matchers.add(buildMatcher(false, true));
|
||||
matchers.add(buildMatcher(false, false));
|
||||
return matchers;
|
||||
}
|
||||
|
||||
private SearchMatcher buildMatcher(boolean first, boolean second) {
|
||||
MatcherSet matcherSet = MatcherSets.build(MatcherSets.MatcherType.AND);
|
||||
matcherSet.addRule(new MockSearchMatcher(first));
|
||||
matcherSet.addRule(new MockSearchMatcher(second));
|
||||
return matcherSet;
|
||||
}
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
package org.jabref.model.search.matchers;
|
||||
|
||||
import org.jabref.model.search.SearchMatcher;
|
||||
import org.jabref.model.search.rules.MockSearchMatcher;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class NotAndMatcherTest extends SearchMatcherTest {
|
||||
@Override
|
||||
protected List<SearchMatcher> makeTrueMatchers() {
|
||||
List<SearchMatcher> matchers = new ArrayList<>();
|
||||
matchers.add(buildNotMatcher(true, false));
|
||||
matchers.add(buildNotMatcher(false, true));
|
||||
matchers.add(buildNotMatcher(false, false));
|
||||
return matchers;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<SearchMatcher> makeFalseMatchers() {
|
||||
List<SearchMatcher> matchers = new ArrayList<>();
|
||||
matchers.add(buildNotMatcher(true, true));
|
||||
return matchers;
|
||||
}
|
||||
|
||||
private SearchMatcher buildNotMatcher(boolean first, boolean second) {
|
||||
MatcherSet matcherSet = MatcherSets.build(MatcherSets.MatcherType.AND);
|
||||
matcherSet.addRule(new MockSearchMatcher(first));
|
||||
matcherSet.addRule(new MockSearchMatcher(second));
|
||||
return new NotMatcher(matcherSet);
|
||||
}
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
package org.jabref.model.search.matchers;
|
||||
|
||||
import org.jabref.model.search.SearchMatcher;
|
||||
import org.jabref.model.search.rules.MockSearchMatcher;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class NotMatcherTest extends SearchMatcherTest {
|
||||
@Override
|
||||
protected List<SearchMatcher> makeTrueMatchers() {
|
||||
List<SearchMatcher> matchers = new ArrayList<>();
|
||||
matchers.add(new NotMatcher(new MockSearchMatcher(false)));
|
||||
matchers.add(new NotMatcher(new NotMatcher(new MockSearchMatcher(true))));
|
||||
return matchers;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<SearchMatcher> makeFalseMatchers() {
|
||||
List<SearchMatcher> matchers = new ArrayList<>();
|
||||
matchers.add(new NotMatcher(new MockSearchMatcher(true)));
|
||||
matchers.add(new NotMatcher(new NotMatcher(new MockSearchMatcher(false))));
|
||||
return matchers;
|
||||
}
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
package org.jabref.model.search.matchers;
|
||||
|
||||
import org.jabref.model.search.SearchMatcher;
|
||||
import org.jabref.model.search.rules.MockSearchMatcher;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class NotOrMatcherTest extends SearchMatcherTest {
|
||||
@Override
|
||||
protected List<SearchMatcher> makeTrueMatchers() {
|
||||
List<SearchMatcher> matchers = new ArrayList<>();
|
||||
matchers.add(buildNotMatcher(false, false));
|
||||
return matchers;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<SearchMatcher> makeFalseMatchers() {
|
||||
List<SearchMatcher> matchers = new ArrayList<>();
|
||||
matchers.add(buildNotMatcher(true, true));
|
||||
matchers.add(buildNotMatcher(true, false));
|
||||
matchers.add(buildNotMatcher(false, true));
|
||||
return matchers;
|
||||
}
|
||||
|
||||
private SearchMatcher buildNotMatcher(boolean first, boolean second) {
|
||||
MatcherSet matcherSet = MatcherSets.build(MatcherSets.MatcherType.OR);
|
||||
matcherSet.addRule(new MockSearchMatcher(first));
|
||||
matcherSet.addRule(new MockSearchMatcher(second));
|
||||
return new NotMatcher(matcherSet);
|
||||
}
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
package org.jabref.model.search.matchers;
|
||||
|
||||
import org.jabref.model.search.SearchMatcher;
|
||||
import org.jabref.model.search.rules.MockSearchMatcher;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class OrMatcherTest extends SearchMatcherTest {
|
||||
@Override
|
||||
protected List<SearchMatcher> makeTrueMatchers() {
|
||||
List<SearchMatcher> matchers = new ArrayList<>();
|
||||
matchers.add(buildMatcher(true, true));
|
||||
matchers.add(buildMatcher(true, false));
|
||||
matchers.add(buildMatcher(false, true));
|
||||
return matchers;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<SearchMatcher> makeFalseMatchers() {
|
||||
List<SearchMatcher> matchers = new ArrayList<>();
|
||||
matchers.add(buildMatcher(false, false));
|
||||
return matchers;
|
||||
}
|
||||
|
||||
private SearchMatcher buildMatcher(boolean first, boolean second) {
|
||||
MatcherSet matcherSet = MatcherSets.build(MatcherSets.MatcherType.OR);
|
||||
matcherSet.addRule(new MockSearchMatcher(first));
|
||||
matcherSet.addRule(new MockSearchMatcher(second));
|
||||
return matcherSet;
|
||||
}
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
package org.jabref.model.search.matchers;
|
||||
|
||||
import org.jabref.model.entry.BibEntry;
|
||||
import org.jabref.model.search.SearchMatcher;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ArgumentsSource;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public abstract class SearchMatcherTest {
|
||||
|
||||
private List<SearchMatcher> trueMatchers;
|
||||
private List<SearchMatcher> falseMatchers;
|
||||
protected abstract List<SearchMatcher> makeTrueMatchers();
|
||||
protected abstract List<SearchMatcher> makeFalseMatchers();
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
trueMatchers = makeTrueMatchers();
|
||||
falseMatchers = makeFalseMatchers();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsMatch() {
|
||||
for (SearchMatcher matcher : trueMatchers) {
|
||||
assertTrue(matcher.isMatch(new BibEntry()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsNotMatch() {
|
||||
for (SearchMatcher matcher : falseMatchers) {
|
||||
assertFalse(matcher.isMatch(new BibEntry()));
|
||||
}
|
||||
}
|
||||
}
|
7
mockito-tutorial/.gitignore
vendored
Normal file
7
mockito-tutorial/.gitignore
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
/.idea/
|
||||
|
||||
# Ignore Gradle project-specific cache directory
|
||||
/.gradle
|
||||
|
||||
# Ignore Gradle build output directory
|
||||
/app/build/
|
Loading…
x
Reference in New Issue
Block a user