Skip to content

Instantly share code, notes, and snippets.

@josketres
Last active April 11, 2016 12:05
Show Gist options
  • Save josketres/6816802 to your computer and use it in GitHub Desktop.
Save josketres/6816802 to your computer and use it in GitHub Desktop.
unit-testing-exercise-java

#Ejercicio didáctico de pruebas unitarias de software# El programa acepta como entrada una cadena de caracteres, la salida es una lista de los caracteres que esten rodeados por guiones "-".

Por ejemplo: Entrada: aaa-bb-ccc Salida: [bb]

##Restricción arbitraria## Se restringe el uso de expresiones regulares. El programa Main.java debe de ser modificado de tal manera que no se haga uso de la clase java.util.regex.Pattern.

##Archivos##

  • Main.java (implementación inicial propuesta por el docente).
  • MainTest.java (pruebas unitarias) (requiere JUnit 4)

Para correr las pruebas desde la línea de comandos: java -cp /usr/share/java/junit.jar org.junit.runner.JUnitCore MainTest

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String input = args[0];
List<String> output = captureItemsBetweenDashes(input);
System.out.println(output);
}
static List<String> captureItemsBetweenDashes(String input) {
List<String> captured = new ArrayList<String>();
Matcher m = Pattern.compile("-([^-]+)-").matcher(input);
while (m.find()) {
captured.add(m.group(1));
}
return captured;
}
}
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import org.junit.Test;
public class MainTest {
@Test
public void should_return_empty_list_for_no_matches() {
assertTrue(Main.captureItemsBetweenDashes("").isEmpty());
}
@Test
public void should_capture_nothing_if_just_one_dash_is_present() {
assertTrue(Main.captureItemsBetweenDashes("-").isEmpty());
}
@Test
public void should_not_capture_empty_item() {
assertTrue(Main.captureItemsBetweenDashes("--").isEmpty());
}
@Test
public void should_capture_single_item() {
String input = "-1-";
assertFalse(Main.captureItemsBetweenDashes(input).isEmpty());
assertEquals(1, Main.captureItemsBetweenDashes(input).size());
assertEquals("1", Main.captureItemsBetweenDashes(input).get(0));
}
@Test
public void should_capture_single_item_with_trailing_dash() {
String input = "-1--";
assertFalse(Main.captureItemsBetweenDashes(input).isEmpty());
assertEquals(1, Main.captureItemsBetweenDashes(input).size());
assertEquals("1", Main.captureItemsBetweenDashes(input).get(0));
}
@Test
public void should_capture_single_item_with_leading_dash() {
String input = "--1-";
assertFalse(Main.captureItemsBetweenDashes(input).isEmpty());
assertEquals(1, Main.captureItemsBetweenDashes(input).size());
assertEquals("1", Main.captureItemsBetweenDashes(input).get(0));
}
@Test
public void should_capture_multiple_items() {
String input = "-1-22222-3-";
assertFalse(Main.captureItemsBetweenDashes(input).isEmpty());
assertEquals(2, Main.captureItemsBetweenDashes(input).size());
assertEquals("1", Main.captureItemsBetweenDashes(input).get(0));
assertEquals("2", Main.captureItemsBetweenDashes(input).get(1));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment