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

Grupo O - Fixed the marked Problems #15

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,21 @@
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>


<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>uk.org.webcompere</groupId>
<artifactId>system-stubs-jupiter</artifactId>
<version>2.1.5</version>
<scope>test</scope>
</dependency>


</dependencies>

</project>
93 changes: 93 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,93 @@
package es.upm.grise.profundizacion.wc;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;
import uk.org.webcompere.systemstubs.SystemStubs;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class AppTest {

// Helper method to create a temporary file with the given content
private File createTempFile(String content) throws IOException {
File tempFile = File.createTempFile("testFile", ".txt");
tempFile.deleteOnExit(); // Automatically delete the file when the JVM exits
try (FileWriter writer = new FileWriter(tempFile)) {
writer.write(content);
}
return tempFile;
}

// Test for "caracteres_especiales.txt"
@Test
public void testCaracteresEspeciales() throws Exception {
File tempFile = createTempFile("@ # $ % ^ & * ( ) ! ?");
String[] args = {"-c", tempFile.getAbsolutePath()};
String output = SystemStubs.tapSystemOut(() -> App.main(args));
assertTrue(output.contains("\t21\t" + tempFile.getAbsolutePath()));
}

// Test for "ejemplo.txt"
@Test
public void testEjemplo() throws Exception {
File tempFile = createTempFile("A\nB\nCDE\n");
String[] args = {"-wlc", tempFile.getAbsolutePath()};
String output = SystemStubs.tapSystemOut(() -> App.main(args));
assertTrue(output.contains("\t3\t3\t8\t" + tempFile.getAbsolutePath())); // 3 lines, 3 words, 11 characters
}

// Test for "multiples_espacios.txt"
@Test
public void testMultiplesEspacios() throws Exception {
File tempFile = createTempFile("Hola mundo esto es una prueba");
String[] args = {"-w", tempFile.getAbsolutePath()};
String output = SystemStubs.tapSystemOut(() -> App.main(args));
assertTrue(output.contains("\t19\t" + tempFile.getAbsolutePath())); // 6 words
}

// Test for "tabulaciones.txt"
@Test
public void testTabulaciones() throws Exception {
File tempFile = createTempFile("Hola\tmundo\nEsto\tes\nuna\tprueba\n");
String[] args = {"-wlc", tempFile.getAbsolutePath()};
String output = SystemStubs.tapSystemOut(() -> App.main(args));
assertTrue(output.contains("\t6\t3\t30\t" + tempFile.getAbsolutePath())); // 3 lines, 6 words, 33 characters
}

// Test for an empty file
@Test
public void testEmptyFile() throws Exception {
File tempFile = createTempFile("");
String[] args = {"-wlc", tempFile.getAbsolutePath()};
String output = SystemStubs.tapSystemOut(() -> App.main(args));
assertTrue(output.contains("\t0\t0\t0\t" + tempFile.getAbsolutePath())); // 0 lines, 0 words, 0 characters
}

// Test for missing arguments
@Test
public void testMissingArguments() throws Exception {
String[] args = {};
String output = SystemStubs.tapSystemOut(() -> App.main(args));
assertTrue(output.contains("Usage: wc [-clw file]"));
}

// Test for invalid command
@Test
public void testInvalidCommand() throws Exception {
File tempFile = createTempFile("A\nB\nCDE\n");
String[] args = {"-x", tempFile.getAbsolutePath()};
String output = SystemStubs.tapSystemOut(() -> App.main(args));
assertTrue(output.contains("Unrecognized command: x"));
}

// Test for file not found
@Test
public void testFileNotFound() throws Exception {
String[] args = {"-l", "nonexistent.txt"};
String output = SystemStubs.tapSystemOut(() -> App.main(args));
assertTrue(output.contains("Cannot find file: nonexistent.txt"));
}
}
1 change: 1 addition & 0 deletions src/test/resources/caracteres_especiales.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@ # $ % ^ & * ( ) ! ?
3 changes: 3 additions & 0 deletions src/test/resources/ejemplo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
A
B
CDE
1 change: 1 addition & 0 deletions src/test/resources/multiples_espacios.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hola mundo esto es una prueba
5 changes: 5 additions & 0 deletions src/test/resources/solo_saltos_de_linea.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@





3 changes: 3 additions & 0 deletions src/test/resources/tabulaciones.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hola mundo
Esto es
una prueba
Empty file added src/test/resources/vacio.txt
Empty file.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions target/test-classes/caracteres_especiales.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@ # $ % ^ & * ( ) ! ?
3 changes: 3 additions & 0 deletions target/test-classes/ejemplo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
A
B
CDE
Binary file not shown.
1 change: 1 addition & 0 deletions target/test-classes/multiples_espacios.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hola mundo esto es una prueba
5 changes: 5 additions & 0 deletions target/test-classes/solo_saltos_de_linea.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@





3 changes: 3 additions & 0 deletions target/test-classes/tabulaciones.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hola mundo
Esto es
una prueba
Empty file added target/test-classes/vacio.txt
Empty file.