Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Commit

Permalink
Add Code unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
powersagitar committed Jan 9, 2025
1 parent bd8eeb1 commit 1ad6379
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
4 changes: 3 additions & 1 deletion culminating-mastermind/.editorconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
root = true

[*.java]
max_line_length = 80
max_line_length = 80
indent_style = space
indent_size = 4
62 changes: 62 additions & 0 deletions culminating-mastermind/app/src/test/java/mastermind/CodeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package mastermind;

import mastermind.core.Code;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.HashMap;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

class CodeTest {
@Test
void testGenerateRandomCode() {
final Code randomCode = Code.generateRandomCode(List.of());
final int expectedCodeLength = Mastermind.CODE_LENGTH;
final var colorArray = randomCode.getColors();

assertEquals(expectedCodeLength, colorArray.size());
}

@Test
void testCodeFromIndices() {
final Code code = new Code(List.of(0, 1, 2, 3));
final List<Code.Color> expectedColors = List.of(Code.Color.Green, Code.Color.Red, Code.Color.Blue,
Code.Color.Yellow);

assertEquals(expectedColors, code.getColors());
}

@Test
void testGetColor() {
final Code code = new Code(List.of(0, 1, 2, 3));
final List<Code.Color> expectedColors = List.of(Code.Color.Green, Code.Color.Red, Code.Color.Blue,
Code.Color.Yellow);

for (int i = 0; i < Mastermind.CODE_LENGTH; ++i) {
assertEquals(expectedColors.get(i), code.getColor(i));
}
}

@Test
void testGetColors() {
final Code code = new Code(List.of(0, 1, 2, 3));
final List<Code.Color> expectedColors = List.of(Code.Color.Green, Code.Color.Red, Code.Color.Blue,
Code.Color.Yellow);

assertEquals(expectedColors, code.getColors());
}

@Test
void testGetOccurrences() {
final Code code = new Code(List.of(0, 1, 2, 3));
HashMap<Code.Color, Integer> expectedOccurrences = new HashMap<>(4);
expectedOccurrences.put(Code.Color.Green, 1);
expectedOccurrences.put(Code.Color.Red, 1);
expectedOccurrences.put(Code.Color.Blue, 1);
expectedOccurrences.put(Code.Color.Yellow, 1);

assumeTrue(code.getOccurrences().equals(expectedOccurrences));
}
}

0 comments on commit 1ad6379

Please sign in to comment.