|
| 1 | +package ru.lanwen.verbalregex; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 6 | +import static org.hamcrest.CoreMatchers.is; |
| 7 | +import static org.hamcrest.CoreMatchers.not; |
| 8 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 9 | +import static ru.lanwen.verbalregex.VerbalExpression.regex; |
| 10 | + |
| 11 | +/** |
| 12 | + * User: lanwen |
| 13 | + * Date: 13.05.14 |
| 14 | + * Time: 16:26 |
| 15 | + */ |
| 16 | +public class PredefinedCharClassesTest { |
| 17 | + |
| 18 | + public static final String LETTERS_NO_DIGITS = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM_"; |
| 19 | + public static final String DIGITS = "0123456789"; |
| 20 | + public static final String NON_LETTERS = ";'[]{}|?/"; |
| 21 | + public static final String SPACE = " \t\n\f\r"; |
| 22 | + |
| 23 | + @Test |
| 24 | + public void testWordChar() throws Exception { |
| 25 | + VerbalExpression regex = regex().wordChar().build(); |
| 26 | + |
| 27 | + assertThat("Not matches on letters", regex.test(LETTERS_NO_DIGITS + DIGITS), is(true)); |
| 28 | + assertThat("matches on non letters", regex.test(NON_LETTERS + SPACE), is(false)); |
| 29 | + assertThat("Extracts wrong word chars", |
| 30 | + regex.getText(LETTERS_NO_DIGITS + DIGITS + NON_LETTERS + SPACE), equalTo(LETTERS_NO_DIGITS + DIGITS)); |
| 31 | + |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void testNonWordChar() throws Exception { |
| 36 | + VerbalExpression regex = regex().nonWordChar().build(); |
| 37 | + |
| 38 | + assertThat("matches on letters", regex.test(LETTERS_NO_DIGITS + DIGITS), is(false)); |
| 39 | + assertThat("Not matches on non letters", regex.test(NON_LETTERS + SPACE), is(true)); |
| 40 | + assertThat("Extracts wrong chars", |
| 41 | + regex.getText(LETTERS_NO_DIGITS + DIGITS + NON_LETTERS + SPACE), equalTo(NON_LETTERS + SPACE)); |
| 42 | + |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testSpace() throws Exception { |
| 47 | + VerbalExpression regex = regex().space().build(); |
| 48 | + |
| 49 | + assertThat("matches on letters", regex.test(LETTERS_NO_DIGITS + DIGITS + NON_LETTERS), is(false)); |
| 50 | + assertThat("Not matches on space", regex.test(SPACE), is(true)); |
| 51 | + assertThat("Extracts wrong chars", |
| 52 | + regex.getText(LETTERS_NO_DIGITS + DIGITS + NON_LETTERS + SPACE), equalTo(SPACE)); |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testNonSpace() throws Exception { |
| 58 | + VerbalExpression regex = regex().nonSpace().build(); |
| 59 | + |
| 60 | + assertThat("Not matches on non space", regex.test(LETTERS_NO_DIGITS + DIGITS + NON_LETTERS), is(true)); |
| 61 | + assertThat("matches on space", regex.test(SPACE), is(false)); |
| 62 | + assertThat("Extracts wrong chars", |
| 63 | + regex.getText(LETTERS_NO_DIGITS + DIGITS + NON_LETTERS + SPACE), not(SPACE)); |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testDigit() throws Exception { |
| 69 | + VerbalExpression regex = regex().digit().build(); |
| 70 | + |
| 71 | + assertThat("matches on letters", regex.test(LETTERS_NO_DIGITS + SPACE + NON_LETTERS), is(false)); |
| 72 | + assertThat("Not matches on digits", regex.test(DIGITS), is(true)); |
| 73 | + assertThat("Extracts wrong chars", |
| 74 | + regex.getText(LETTERS_NO_DIGITS + DIGITS + NON_LETTERS + SPACE), is(DIGITS)); |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testNonDigit() throws Exception { |
| 80 | + VerbalExpression regex = regex().nonDigit().build(); |
| 81 | + |
| 82 | + assertThat("Not matches on letters", regex.test(LETTERS_NO_DIGITS + SPACE + NON_LETTERS), is(true)); |
| 83 | + assertThat("matches on digits", regex.test(DIGITS), is(false)); |
| 84 | + assertThat("Extracts wrong chars", |
| 85 | + regex.getText(LETTERS_NO_DIGITS + DIGITS + NON_LETTERS + SPACE), not(DIGITS)); |
| 86 | + |
| 87 | + } |
| 88 | +} |
0 commit comments