Skip to content

Instantly share code, notes, and snippets.

@rhyskeepence
Created March 28, 2012 14:04
Show Gist options
  • Save rhyskeepence/2226445 to your computer and use it in GitHub Desktop.
Save rhyskeepence/2226445 to your computer and use it in GitHub Desktop.
import com.googlecode.totallylazy.Option;
import com.googlecode.totallylazy.Sequences;
import org.junit.Test;
import static com.googlecode.totallylazy.Sequences.sequence;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
public class RegexTest {
@Test
public void findAllGroups() throws Exception {
Regex regex = new Regex("(\\w)(\\w)(\\w)");
Option<Iterable<String>> abc = regex.findGroupsIn("ABC");
Iterable<String> expected = sequence("A", "B", "C");
assertThat(abc.get(), equalTo(expected));
}
@Test
public void findAllGroupsWithNoGroup() throws Exception {
Regex regex = new Regex(".*");
Option<Iterable<String>> abc = regex.findGroupsIn("ABC");
Iterable<String> expected = Sequences.empty(String.class);
assertThat(abc.get(), equalTo(expected));
}
@Test
public void findAllGroupsWithNoMatch() throws Exception {
Regex regex = new Regex("XYZ");
Option<Iterable<String>> abc = regex.findGroupsIn("ABC");
assertThat(abc.isEmpty(), is(true));
}
@Test
public void findIn() throws Exception {
Regex regex = new Regex("[A-Z]+");
Option<String> foundValue = regex.findIn("asdfABCasdf");
assertThat(foundValue.get(), equalTo("ABC"));
}
@Test
public void doesNotFindIn() throws Exception {
Regex regex = new Regex("[A-Z]+");
Option<String> foundValue = regex.findIn("asdf");
assertThat(foundValue.isEmpty(), is(true));
}
@Test
public void findFirstGroupIn() throws Exception {
Regex regex = new Regex("goo(.*)");
Option<String> firstGroup = regex.findFirstGroupIn("gooFOO");
assertThat(firstGroup.get(), equalTo("FOO"));
}
@Test
public void findFirstGroupWithNoGroup() throws Exception {
Regex regex = new Regex("blah");
Option<String> firstGroup = regex.findFirstGroupIn("blahblah");
assertThat(firstGroup.isEmpty(), is(true));
}
@Test
public void matches() throws Exception {
Regex regex = new Regex(".*FOO");
assertThat(regex.matches("gooFOO"), is(true));
}
@Test
public void doesNotMatch() throws Exception {
Regex regex = new Regex("FOO");
assertThat(regex.matches("BAR"), is(false));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment