package com.google.common.base; import static org.junit.Assert.*; import org.junit.Test; public class TestCharMatcher { @Test public void anyOfTest() { CharMatcher matcher = CharMatcher.anyOf("abc"); assertTrue(matcher.matches('a')); assertTrue(matcher.matches('b')); assertTrue(matcher.matches('c')); assertFalse(matcher.matches('x')); assertTrue(matcher.matchesAnyOf("abxcd")); } @Test public void forPredicateTest() { Predicate predicate = new Predicate() { @Override public boolean apply(Character input) { return Character.isLetter(input); } }; CharMatcher matcher = CharMatcher.forPredicate(predicate); assertTrue(matcher.matches('a')); assertTrue(matcher.matches('b')); assertTrue(matcher.matches('c')); assertFalse(matcher.matches('1')); } @Test public void inRangeTest() { CharMatcher matcher = CharMatcher.inRange('A', 'E'); assertTrue(matcher.matches('A')); assertTrue(matcher.matches('B')); assertTrue(matcher.matches('E')); assertFalse(matcher.matches('F')); assertFalse(matcher.matches('a')); } @Test public void noneOfTest() { CharMatcher matcher = CharMatcher.noneOf("abg"); assertTrue(matcher.matches('x')); assertTrue(matcher.matches('y')); assertFalse(matcher.matches('g')); assertFalse(matcher.matches('a')); assertFalse(matcher.matches('b')); } @Test public void andTest() { CharMatcher matcher = CharMatcher.inRange('A', 'E'); matcher = matcher.and(CharMatcher.anyOf("ABD")); assertTrue(matcher.matches('A')); assertTrue(matcher.matches('B')); assertFalse(matcher.matches('E')); assertFalse(matcher.matches('a')); } @Test public void collapseFromTest() { CharMatcher matcher = CharMatcher.inRange('A', 'E'); String result = matcher.collapseFrom("helloABEworld", '-'); assertEquals("hello-world", result); } @Test public void countInTest() { CharMatcher matcher = CharMatcher.inRange('A', 'E'); int result = matcher.countIn("ABer Hallo"); assertEquals(2, result); } @Test public void removeFromTest() { CharMatcher matcher = CharMatcher.inRange('A', 'E'); String result = matcher.removeFrom("helloABCDEworld"); assertEquals("helloworld", result); } @Test public void retainFromTest() { CharMatcher matcher = CharMatcher.inRange('A', 'E'); String result = matcher.retainFrom("ABER HALLO"); assertEquals("ABEA", result); } @Test public void replaceFromTest() { CharMatcher matcher = CharMatcher.inRange('A', 'E'); String result = matcher.replaceFrom("ABER HALLO", 'O'); assertEquals("OOOR HOLLO", result); } @Test public void trimFromTest() { CharMatcher matcher = CharMatcher.inRange('A', 'E'); String result = matcher.trimFrom("ABER HALLOE"); assertEquals("R HALLO", result); } @Test public void trimLeadingFromTest() { CharMatcher matcher = CharMatcher.inRange('A', 'E'); String result = matcher.trimLeadingFrom("ABER HALLOE"); assertEquals("R HALLOE", result); } @Test public void trimTrailingFromTest() { CharMatcher matcher = CharMatcher.inRange('A', 'E'); String result = matcher.trimTrailingFrom("ABER HALLOE"); assertEquals("ABER HALLO", result); } @Test public void trimAndCollapseFromTest() { CharMatcher matcher = CharMatcher.inRange('A', 'E'); String result = matcher.trimAndCollapseFrom("ABERHALLOABERWAS", '-'); System.out.println(result); assertEquals("RH-LLO-RW-S", result); } }