Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
HeEb authored and HeEb committed Aug 2, 2024
1 parent 397d1ed commit a8e262b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 68 deletions.
44 changes: 0 additions & 44 deletions src/main/java/uta/cse3310/WordList.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,48 +89,4 @@ public ArrayList<String> getArrList()



public static HashSet<Character> findletters(ArrayList<String> wordsforgame) {

HashSet<Character> 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<String> 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());
}
}
}

83 changes: 59 additions & 24 deletions src/test/Java/uta/cse3310/WordListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> 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<String> 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<String> words = wordList.getWords();
void testGetArrList() {
ArrayList<String> words = wordList.getArrList();
assertNotNull(words);
assertEquals(5, words.size());
assertTrue(words.isEmpty());
}
}


0 comments on commit a8e262b

Please sign in to comment.