Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests para las clases Counter.java y App.java añadidos #21

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

141 changes: 141 additions & 0 deletions src/test/java/es/upm/grise/profundizacion/wc/AppTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package es.upm.grise.profundizacion.wc;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintStream;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class AppTest {

private final File testFile = new File("testFile.txt");
private final File emptyFile = new File("emptyFile.txt");

@AfterEach
public void cleanUpFiles() {
if (testFile.exists()) {
testFile.delete();
}
if (emptyFile.exists()) {
emptyFile.delete();
}
}

@Test
public void testUsageWithoutParameters() {
ByteArrayOutputStream output = new ByteArrayOutputStream();
System.setOut(new PrintStream(output));

App.main(new String[]{});

String actualOutput = output.toString().trim();
assertEquals("Usage: wc [-clw file]", actualOutput);
}

@Test
public void testFileNotFound() {
ByteArrayOutputStream output = new ByteArrayOutputStream();
System.setOut(new PrintStream(output));

App.main(new String[]{"-c", "nonexistent.txt"});

assertEquals("Cannot find file: nonexistent.txt", output.toString().trim());
}

@Test
public void testInvalidArguments() {
ByteArrayOutputStream output = new ByteArrayOutputStream();
System.setOut(new PrintStream(output));

App.main(new String[]{"-c", "file.txt", "extraParam"});

assertEquals("Wrong arguments!", output.toString().trim());
}

@Test
public void testCorrectOutput() throws Exception {
try (FileWriter writer = new FileWriter(testFile)) {
writer.write("Hello World\nThis is a test.\n");
}

ByteArrayOutputStream output = new ByteArrayOutputStream();
System.setOut(new PrintStream(output));

App.main(new String[]{"-lcw", testFile.getName()});

assertEquals("2\t28\t6\ttestFile.txt", output.toString().trim());
}

@Test
public void testCorrectOutputWithOnlyCharactersFlag() throws Exception {
try (FileWriter writer = new FileWriter(testFile)) {
writer.write("Hello World\nThis is a test.\n");
}

ByteArrayOutputStream output = new ByteArrayOutputStream();
System.setOut(new PrintStream(output));

App.main(new String[]{"-c", testFile.getName()});
assertEquals("28\ttestFile.txt", output.toString().trim());
}

@Test
public void testCorrectOutputWithOnlyWordsFlag() throws Exception {
try (FileWriter writer = new FileWriter(testFile)) {
writer.write("Hello World\nThis is a test.\n");
}

ByteArrayOutputStream output = new ByteArrayOutputStream();
System.setOut(new PrintStream(output));

App.main(new String[]{"-w", testFile.getName()});
assertEquals("6\ttestFile.txt", output.toString().trim());
}

@Test
public void testCorrectOutputWithOnlyLinesFlag() throws Exception {
try (FileWriter writer = new FileWriter(testFile)) {
writer.write("Hello World\nThis is a test.\n");
}

ByteArrayOutputStream output = new ByteArrayOutputStream();
System.setOut(new PrintStream(output));

App.main(new String[]{"-l", testFile.getName()});
assertEquals("2\ttestFile.txt", output.toString().trim());
}

@Test
public void testCorrectOutputForEmptyFile() throws Exception {
try (FileWriter writer = new FileWriter(emptyFile)) {
writer.write("");
}

ByteArrayOutputStream output = new ByteArrayOutputStream();
System.setOut(new PrintStream(output));

App.main(new String[]{"-lcw", emptyFile.getName()});
assertEquals("0\t0\t0\temptyFile.txt", output.toString().trim());
}

@Test
public void testInvalidFlag() throws Exception {
String filePath = "testFile.txt";
try (FileWriter writer = new FileWriter(filePath)) {
writer.write("This is a test.");
}

ByteArrayOutputStream output = new ByteArrayOutputStream();
System.setOut(new PrintStream(output));

App.main(new String[]{"-z", filePath});

assertEquals("Unrecognized command: z", output.toString().trim());

new File(filePath).delete();
}

}
82 changes: 82 additions & 0 deletions src/test/java/es/upm/grise/profundizacion/wc/CounterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package es.upm.grise.profundizacion.wc;

import java.io.BufferedReader;
import java.io.StringReader;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;


public class CounterTest {

@Test
public void testCountCharacters() throws Exception {
String content = "Hello World\nThis is a test.\n";
BufferedReader br = new BufferedReader(new StringReader(content));
Counter counter = new Counter(br);

assertEquals(28, counter.getNumberCharacters());
}

@Test
public void testCountWords() throws Exception {
String content = "Hello World\nThis is a test.\n";
BufferedReader br = new BufferedReader(new StringReader(content));
Counter counter = new Counter(br);

assertEquals(6, counter.getNumberWords());
}

@Test
public void testCountLines() throws Exception {
String content = "Hello World\nThis is a test.\n";
BufferedReader br = new BufferedReader(new StringReader(content));
Counter counter = new Counter(br);

assertEquals(2, counter.getNumberLines());
}

@Test
public void testEmptyFile() throws Exception {
String content = "";
BufferedReader br = new BufferedReader(new StringReader(content));
Counter counter = new Counter(br);
assertEquals(0, counter.getNumberCharacters());
assertEquals(0, counter.getNumberWords());
assertEquals(0, counter.getNumberLines());
}

@Test
public void testFileWithMultipleEmptyLines() throws Exception {
String content = "\n\n\n";
BufferedReader br = new BufferedReader(new StringReader(content));
Counter counter = new Counter(br);

assertEquals(3, counter.getNumberLines());
assertEquals(3, counter.getNumberWords()); // Cada salto de línea cuenta como una palabra
assertEquals(3, counter.getNumberCharacters());
}

@Test
public void testFileWithOnlySpaces() throws Exception {
String content = " ";
BufferedReader br = new BufferedReader(new StringReader(content));
Counter counter = new Counter(br);

assertEquals(5, counter.getNumberCharacters());
assertEquals(5, counter.getNumberWords()); // Cada espacio cuenta como una palabra
assertEquals(0, counter.getNumberLines());
}

@Test
public void testFileWithOneWord() throws Exception {
String content = "Word";
BufferedReader br = new BufferedReader(new StringReader(content));
Counter counter = new Counter(br);

assertEquals(4, counter.getNumberCharacters());
assertEquals(0, counter.getNumberWords()); // Counter no detecta palabras sin separadores
assertEquals(0, counter.getNumberLines());
}

}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.