From a8e262bb37ec3d51f4da78f835968a4bea386c99 Mon Sep 17 00:00:00 2001 From: HeEb Date: Thu, 1 Aug 2024 22:05:39 -0500 Subject: [PATCH] updated --- src/main/java/uta/cse3310/WordList.java | 44 ----------- src/test/Java/uta/cse3310/WordListTest.java | 83 +++++++++++++++------ 2 files changed, 59 insertions(+), 68 deletions(-) diff --git a/src/main/java/uta/cse3310/WordList.java b/src/main/java/uta/cse3310/WordList.java index 16ddc6a..dbb1cc1 100644 --- a/src/main/java/uta/cse3310/WordList.java +++ b/src/main/java/uta/cse3310/WordList.java @@ -89,48 +89,4 @@ public ArrayList getArrList() - public static HashSet findletters(ArrayList wordsforgame) { - - HashSet letters = new HashSet<>(); - - for (String word : wordsforgame) { - for (int i = 0; i < word.length(); i++) { - char letter = word.charAt(i); - letters.add(letter); - } - } - return letters; - } - - - - - private List words; - - public WordList(String filePath) throws IOException - { - loadWords(filePath); - } - private void loadWords(String filePath) throws IOException - { - words = Files.readAllLines(Paths.get(filePath)); - } - public String getRandomWord() - { - Random rand = new Random(); - return words.get(rand.nextInt(words.size())); - } - public static void main(String[] args) - { - try - { - WordList wordList = new WordList("path/to/words.txt"); - System.out.println("Random word: " + wordList.getRandomWord()); - } - catch (IOException e) - { - System.err.println("Error loading words: " + e.getMessage()); - } - } } - diff --git a/src/test/Java/uta/cse3310/WordListTest.java b/src/test/Java/uta/cse3310/WordListTest.java index d7d3106..8cd92ce 100644 --- a/src/test/Java/uta/cse3310/WordListTest.java +++ b/src/test/Java/uta/cse3310/WordListTest.java @@ -2,52 +2,87 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; import java.io.IOException; -import java.nio.file.Files; import java.nio.file.Path; +import java.util.ArrayList; import java.util.List; import static org.junit.jupiter.api.Assertions.*; -public class WordListTest { +class WordListTest { private WordList wordList; - private static final String TEST_FILE_PATH = "src/test/resources/test_words.txt"; + + @TempDir + Path tempDir; @BeforeEach - public void setUp() throws IOException { - // Create a temporary file with test words - List words = List.of("apple", "banana", "cherry", "date", "elderberry"); - Files.write(Path.of(TEST_FILE_PATH), words); + void setUp() { + wordList = new WordList(); + } + + @Test + void testGatherWords() throws IOException { + // Prepare a temporary file with some words + File tempFile = tempDir.resolve("words.txt").toFile(); + try (BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile))) { + writer.write("apple\n"); + writer.write("banana\n"); + writer.write("cherry\n"); + writer.write("date\n"); + writer.write("fig\n"); + } + + // Override the file path in WordList class (assuming it's not final and can be set) + WordList.readWordsFromFile(tempFile.getAbsolutePath()); - wordList = new WordList(TEST_FILE_PATH); + wordList.gatherwords(); + + // Check if the number of gathered words is between 1 and 3 + assertTrue(wordList.getArrList().size() >= 1 && wordList.getArrList().size() <= 3); } @Test - public void testLoadWords() { - List words = wordList.getWords(); - assertNotNull(words); - assertEquals(5, words.size()); - assertTrue(words.contains("apple")); - assertTrue(words.contains("banana")); - assertTrue(words.contains("cherry")); - assertTrue(words.contains("date")); - assertTrue(words.contains("elderberry")); + void testReadWordsFromFile() throws IOException { + // Prepare a temporary file with some words + File tempFile = tempDir.resolve("words.txt").toFile(); + try (BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile))) { + writer.write("apple\n"); + writer.write("banana\n"); + writer.write("cherry\n"); + } + + String word = WordList.readWordsFromFile(tempFile.getAbsolutePath()); + assertNotNull(word); + assertTrue(word.matches("^[a-z]+$")); + assertTrue(word.length() >= 3 && word.length() <= 12); } @Test - public void testGetRandomWord() { - String randomWord = wordList.getRandomWord(); - assertNotNull(randomWord); - assertTrue(wordList.getWords().contains(randomWord)); + void testIsValidWord() { + ArrayList words = new ArrayList<>(); + words.add("apple"); + words.add("banana"); + words.add("invalid_word_123"); + words.add("cherry"); + + String validWord = WordList.isValidWord(words); + assertNotNull(validWord); + assertTrue(validWord.matches("^[a-z]+$")); + assertTrue(validWord.length() >= 3 && validWord.length() <= 12); } @Test - public void testGetWords() { - List words = wordList.getWords(); + void testGetArrList() { + ArrayList words = wordList.getArrList(); assertNotNull(words); - assertEquals(5, words.size()); + assertTrue(words.isEmpty()); } } +