From f2ba45296cac1e12b446747c8611c27c6fea663f Mon Sep 17 00:00:00 2001 From: ramyamusuku95 Date: Sun, 7 Jul 2024 11:04:20 -0400 Subject: [PATCH] -m "TDD tests" --- src1/pom.xml | 52 +++++++++++++ src1/src/main/java/Elf.java | 16 ++++ src1/src/main/java/JavaKeyWordChecker.java | 25 +++++++ src1/src/main/java/Main.java | 14 ++++ src1/src/main/java/OddNumbers.java | 11 +++ src1/src/main/java/Player.java | 63 ++++++++++++++++ src1/src/main/java/Warrior.java | 20 +++++ src1/src/main/java/Wizard.java | 20 +++++ src1/src/test/java/ElfTest.java | 31 ++++++++ .../src/test/java/JavaKeywordCheckerTest.java | 15 ++++ src1/src/test/java/OddIntegersTest.java | 35 +++++++++ src1/src/test/java/PlayerTest.java | 74 +++++++++++++++++++ src1/src/test/java/WizardTest.java | 41 ++++++++++ 13 files changed, 417 insertions(+) create mode 100644 src1/pom.xml create mode 100644 src1/src/main/java/Elf.java create mode 100644 src1/src/main/java/JavaKeyWordChecker.java create mode 100644 src1/src/main/java/Main.java create mode 100644 src1/src/main/java/OddNumbers.java create mode 100644 src1/src/main/java/Player.java create mode 100644 src1/src/main/java/Warrior.java create mode 100644 src1/src/main/java/Wizard.java create mode 100644 src1/src/test/java/ElfTest.java create mode 100644 src1/src/test/java/JavaKeywordCheckerTest.java create mode 100644 src1/src/test/java/OddIntegersTest.java create mode 100644 src1/src/test/java/PlayerTest.java create mode 100644 src1/src/test/java/WizardTest.java diff --git a/src1/pom.xml b/src1/pom.xml new file mode 100644 index 0000000..59f1969 --- /dev/null +++ b/src1/pom.xml @@ -0,0 +1,52 @@ + + + 4.0.0 + + org.example + src1 + 1.0-SNAPSHOT + + + org.junit.jupiter + junit-jupiter-api + ${junit.jupiter.version} + test + + + org.junit.jupiter + junit-jupiter-params + ${junit.jupiter.version} + test + + + org.junit.jupiter + junit-jupiter-engine + ${junit.jupiter.version} + test + + + + org.mockito + mockito-core + 2.25.0 + test + + + org.hamcrest + hamcrest-all + 1.3 + test + + + + 17 + 17 + UTF-8 + false + + 5.4.0-RC1 + + + \ No newline at end of file diff --git a/src1/src/main/java/Elf.java b/src1/src/main/java/Elf.java new file mode 100644 index 0000000..46d4eeb --- /dev/null +++ b/src1/src/main/java/Elf.java @@ -0,0 +1,16 @@ +public class Elf extends Player { + private int speed; + + public Elf(int health, int strength, int lives, int speed) { + super(health, strength, lives); + this.speed = speed; + } + + public int getSpeed() { + return speed; + } + + public void setSpeed(int speed) { + this.speed = speed; + } +} diff --git a/src1/src/main/java/JavaKeyWordChecker.java b/src1/src/main/java/JavaKeyWordChecker.java new file mode 100644 index 0000000..5bd72a1 --- /dev/null +++ b/src1/src/main/java/JavaKeyWordChecker.java @@ -0,0 +1,25 @@ +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class JavaKeyWordChecker { + + private static final Set JAVA_KEYWORDS = new HashSet<>(Arrays.asList( + "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", + "const", "continue", "default", "do", "double", "else", "enum", "exports", "extends", + "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", + "int", "interface", "long", "module", "native", "new", "package", "private", "protected", + "public", "requires", "return", "short", "static", "strictfp", "super", "switch", + "synchronized", "this", "throw", "throws", "transient", "try", "var", "void", "volatile", "while" + )); + + public static boolean containsJavaKeyword(String input) { + String[] words = input.split("\\W+"); + for (String word : words) { + if (JAVA_KEYWORDS.contains(word)) { + return true; + } + } + return false; + } +} diff --git a/src1/src/main/java/Main.java b/src1/src/main/java/Main.java new file mode 100644 index 0000000..a26a894 --- /dev/null +++ b/src1/src/main/java/Main.java @@ -0,0 +1,14 @@ +public class Main { + public static void main(String[] args) { + Warrior warrior1 = new Warrior(100, 20, 3,40); + Warrior warrior2 = new Warrior(80, 15, 2,60); + + warrior1.attack(warrior2); + System.out.println("Warrior2 Health: " + warrior2.getHealth()); + System.out.println("Warrior2 Lives: " + warrior2.getLives()); + + warrior2.attack(warrior1); + System.out.println("Warrior1 Health: " + warrior1.getHealth()); + System.out.println("Warrior1 Lives: " + warrior1.getLives()); + } +} diff --git a/src1/src/main/java/OddNumbers.java b/src1/src/main/java/OddNumbers.java new file mode 100644 index 0000000..3c8fb9e --- /dev/null +++ b/src1/src/main/java/OddNumbers.java @@ -0,0 +1,11 @@ +public class OddNumbers { + public static int[] getOddIntegers(int n) { + if (n < 1) { + return new int[0]; + } + + return java.util.stream.IntStream.rangeClosed(1, n) + .filter(i -> i % 2 != 0) + .toArray(); + } +} diff --git a/src1/src/main/java/Player.java b/src1/src/main/java/Player.java new file mode 100644 index 0000000..642cf20 --- /dev/null +++ b/src1/src/main/java/Player.java @@ -0,0 +1,63 @@ +public abstract class Player { + private int health; + private int originalHealth; + private int strength; + private int lives; + + public Player(int health, int strength, int lives) { + this.health = health; + this.originalHealth = health; // Store the original health + this.strength = strength; + this.lives = lives; + } + + // Getters + public int getHealth() { + return health; + } + + public int getStrength() { + return strength; + } + + public int getLives() { + return lives; + } + + // Setters + public void setHealth(int health) { + this.health = health; + } + + public void setStrength(int strength) { + this.strength = strength; + } + + public void setLives(int lives) { + this.lives = lives; + } + + // Method to decrement lives and restore health + public void decrementLive() { + if (lives > 0) { + lives--; + health = originalHealth; + if (lives <= 0) { + System.out.println("This character is dead"); + } + } + } + + // Method to attack another player + public void attack(Player playerToAttack) { + playerToAttack.setHealth(playerToAttack.getHealth() - this.strength); + playerToAttack.checkHealth(); + } + + // Method to check health and call decrementLive() if health is 0 or less + public void checkHealth() { + if (this.health <= 0) { + decrementLive(); + } + } +} diff --git a/src1/src/main/java/Warrior.java b/src1/src/main/java/Warrior.java new file mode 100644 index 0000000..db64f5f --- /dev/null +++ b/src1/src/main/java/Warrior.java @@ -0,0 +1,20 @@ +public class Warrior extends Player { + + private int force; + public Warrior(int health, int strength, int lives, int force) { + super(health, strength, lives); + this.force=force; + } + + // Additional methods specific to Warrior can be added here + public int getForce() { + return force; + } + + public void setForce(int force) { + this.force = force; + } + public Elf convertToElf() { + return new Elf(this.getHealth(), this.getStrength(), this.getLives(), this.force); + } +} diff --git a/src1/src/main/java/Wizard.java b/src1/src/main/java/Wizard.java new file mode 100644 index 0000000..0bb6245 --- /dev/null +++ b/src1/src/main/java/Wizard.java @@ -0,0 +1,20 @@ +public class Wizard extends Player { + private String spell; + + public Wizard(int health, int strength, int lives, String spell) { + super(health, strength, lives); + this.spell = spell; + } + + public String getSpell() { + return spell; + } + + public void setSpell(String spell) { + this.spell = spell; + } + + public Elf convertToElf() { + return new Elf(this.getHealth(), this.getStrength(), this.getLives(), 0); // Assuming spell power doesn't convert to speed, set speed to 0 + } +} diff --git a/src1/src/test/java/ElfTest.java b/src1/src/test/java/ElfTest.java new file mode 100644 index 0000000..bc700f8 --- /dev/null +++ b/src1/src/test/java/ElfTest.java @@ -0,0 +1,31 @@ +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class ElfTest { + + private Elf elf; + + @BeforeEach + public void setUp() { + elf = new Elf(100, 20, 3, 15); + } + + @Test + public void testGettersAndSetters() { + assertEquals(100, elf.getHealth()); + assertEquals(20, elf.getStrength()); + assertEquals(3, elf.getLives()); + assertEquals(15, elf.getSpeed()); + + elf.setHealth(90); + elf.setStrength(25); + elf.setLives(2); + elf.setSpeed(20); + + assertEquals(90, elf.getHealth()); + assertEquals(25, elf.getStrength()); + assertEquals(2, elf.getLives()); + assertEquals(20, elf.getSpeed()); + } +} diff --git a/src1/src/test/java/JavaKeywordCheckerTest.java b/src1/src/test/java/JavaKeywordCheckerTest.java new file mode 100644 index 0000000..c28913e --- /dev/null +++ b/src1/src/test/java/JavaKeywordCheckerTest.java @@ -0,0 +1,15 @@ +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class JavaKeywordCheckerTest { + + @Test + public void testContainsJavaKeyword() { + assertTrue(JavaKeyWordChecker.containsJavaKeyword("Don't break my heart")); + assertFalse(JavaKeyWordChecker.containsJavaKeyword("I love to breakdance")); + assertTrue(JavaKeyWordChecker.containsJavaKeyword("This is a class")); + assertFalse(JavaKeyWordChecker.containsJavaKeyword("This is classic music")); + assertTrue(JavaKeyWordChecker.containsJavaKeyword("Switch to the new version")); + assertTrue(JavaKeyWordChecker.containsJavaKeyword("Switching to a new version")); + } +} diff --git a/src1/src/test/java/OddIntegersTest.java b/src1/src/test/java/OddIntegersTest.java new file mode 100644 index 0000000..d49b5f2 --- /dev/null +++ b/src1/src/test/java/OddIntegersTest.java @@ -0,0 +1,35 @@ +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import org.junit.jupiter.api.Test; + +public class OddIntegersTest { + + @Test + public void testOddIntegersUpTo1() { + int[] expected = {1}; + assertArrayEquals(expected, OddNumbers.getOddIntegers(1)); + } + + @Test + public void testOddIntegersUpTo5() { + int[] expected = {1, 3, 5}; + assertArrayEquals(expected, OddNumbers.getOddIntegers(5)); + } + + @Test + public void testOddIntegersUpTo10() { + int[] expected = {1, 3, 5, 7, 9}; + assertArrayEquals(expected, OddNumbers.getOddIntegers(10)); + } + + @Test + public void testOddIntegersUpTo0() { + int[] expected = {}; + assertArrayEquals(expected, OddNumbers.getOddIntegers(0)); + } + + @Test + public void testOddIntegersUpToNegative() { + int[] expected = {}; + assertArrayEquals(expected, OddNumbers.getOddIntegers(-5)); + } +} diff --git a/src1/src/test/java/PlayerTest.java b/src1/src/test/java/PlayerTest.java new file mode 100644 index 0000000..a3eb018 --- /dev/null +++ b/src1/src/test/java/PlayerTest.java @@ -0,0 +1,74 @@ +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class PlayerTest { + + private Warrior warrior1; + private Warrior warrior2; + + @BeforeEach + public void setUp() { + warrior1 = new Warrior(100, 20, 3,60); + warrior2 = new Warrior(80, 15, 2,70); + } + + @Test + public void testInitialValues() { + assertEquals(100, warrior1.getHealth()); + assertEquals(20, warrior1.getStrength()); + assertEquals(3, warrior1.getLives()); + + assertEquals(80, warrior2.getHealth()); + assertEquals(15, warrior2.getStrength()); + assertEquals(2, warrior2.getLives()); + } + + @Test + public void testAttack() { + warrior1.attack(warrior2); + assertEquals(60, warrior2.getHealth()); + assertEquals(2, warrior2.getLives()); + + warrior2.attack(warrior1); + assertEquals(85, warrior1.getHealth()); + assertEquals(3, warrior1.getLives()); + } + + @Test + public void testCheckHealth() { + warrior2.setHealth(0); + warrior2.checkHealth(); + assertEquals(80, warrior2.getHealth()); // Restored health + assertEquals(1, warrior2.getLives()); + } + + @Test + public void testDecrementLive() { + warrior2.decrementLive(); + assertEquals(80, warrior2.getHealth()); // Restored health + assertEquals(1, warrior2.getLives()); + + warrior2.decrementLive(); + assertEquals(80, warrior2.getHealth()); + assertEquals(0, warrior2.getLives()); + + warrior2.decrementLive(); // Should print "This character is dead" + assertEquals(80, warrior2.getHealth()); + assertEquals(0, warrior2.getLives()); + } + + @Test + public void testDeadCharacter() { + warrior2.setLives(1); + warrior2.setHealth(10); + + warrior1.attack(warrior2); + assertEquals(0, warrior2.getLives()); + assertEquals(80, warrior2.getHealth()); + + warrior1.attack(warrior2); // Further attack should not decrement lives + assertEquals(0, warrior2.getLives()); + assertEquals(60, warrior2.getHealth()); // Health still decreases + } +} diff --git a/src1/src/test/java/WizardTest.java b/src1/src/test/java/WizardTest.java new file mode 100644 index 0000000..a03f73e --- /dev/null +++ b/src1/src/test/java/WizardTest.java @@ -0,0 +1,41 @@ +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class WizardTest { + + private Wizard wizard; + private Elf elf; + + @BeforeEach + public void setUp() { + wizard = new Wizard(80, 15, 2, "Fireball"); + } + + @Test + public void testGettersAndSetters() { + assertEquals(80, wizard.getHealth()); + assertEquals(15, wizard.getStrength()); + assertEquals(2, wizard.getLives()); + assertEquals("Fireball", wizard.getSpell()); + + wizard.setHealth(70); + wizard.setStrength(20); + wizard.setLives(1); + wizard.setSpell("Lightning"); + + assertEquals(70, wizard.getHealth()); + assertEquals(20, wizard.getStrength()); + assertEquals(1, wizard.getLives()); + assertEquals("Lightning", wizard.getSpell()); + } + + @Test + public void testConvertToElf() { + elf = wizard.convertToElf(); + assertEquals(wizard.getHealth(), elf.getHealth()); + assertEquals(wizard.getStrength(), elf.getStrength()); + assertEquals(wizard.getLives(), elf.getLives()); + assertEquals(0, elf.getSpeed()); + } +}