SimpleRegularExpression { public boolean evaluate(String source, String pattern) { if (source == null || pattern == null) { throw new IllegalArgumentException("You can't pass a null strings as input"); } if (pattern.length() == 0) return source.length() == 0; if (pattern.length() == 1 || pattern.charAt(1) != '*') { if (source.length() < 1 || (pattern.charAt(0) != '.' && source.charAt(0) != pattern.charAt( 0))) { return false; } return evaluate(source.substring(1), pattern.substring(1)); } else { int len = source.length(); int i = -1; while (i < len && (i < 0 || pattern.charAt(0) == '.' || pattern.charAt(0) == source.charAt( i))) { if (evaluate(source.substring(i + 1), pattern.substring(2))) return true; i++; } return false; } }  boolean evaluate(String source, String pattern);  }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptNullStringsAsInput() { simpleRegularExpression.evaluate(null, null); }
@Test public void shouldReturnFalseIfInputDoesNotMatchesAndDoesNotContainKeyElements() { String input = "aaa"; String regularExpression = "a"; boolean result = simpleRegularExpression.evaluate(input, regularExpression); assertFalse(result); }
@Test public void shouldReturnTrueIfInputMatchesAndDoesNotContainKeyElements() { String input = "aaa"; String regularExpression = "aaa"; boolean result = simpleRegularExpression.evaluate(input, regularExpression); assertTrue(result); }
@Test public void shouldReturnTrueIfMatchesUsingAsterisk() { String input = "aaa"; String regularExpression = "a*"; boolean result = simpleRegularExpression.evaluate(input, regularExpression); assertTrue(result); }
@Test public void shouldReturnFalseIfDoesNotMatchUsingAsterisk() { String input = "aaa"; String regularExpression = "b*"; boolean result = simpleRegularExpression.evaluate(input, regularExpression); assertFalse(result); }
@Test public void shouldReturnTrueIfMatchesUsingDot() { String input = "aa"; String regularExpression = "a."; boolean result = simpleRegularExpression.evaluate(input, regularExpression); assertTrue(result); }
@Test public void shouldReturnFalseIfDoesNotMatchUsingDot() { String input = "aaa"; String regularExpression = "a."; boolean result = simpleRegularExpression.evaluate(input, regularExpression); assertFalse(result); }
@Test public void shouldReturnTrueIfMatchesUsingAsteriskAndDot() { String input = "aaa"; String regularExpression = ".a*"; boolean result = simpleRegularExpression.evaluate(input, regularExpression); assertTrue(result); }
@Test public void shouldReturnFalseIfDoesNotMatchUsingAsteriskAndDot() { String input = "aaa"; String regularExpression = ".b*"; boolean result = simpleRegularExpression.evaluate(input, regularExpression); assertFalse(result); }