@SuppressWarnings("unchecked") public static Object parseInput(String s) { boolean requiresLeftBoundary = false; boolean requiresRightBoundary = false; s = s.trim(); if (s.length() > 0 && s.charAt(0) == BOUNDARY) { requiresLeftBoundary = true; s = s.substring(1).trim(); } if (s.length() > 0 && s.charAt(s.length() - 1) == BOUNDARY) { requiresRightBoundary = true; s = s.substring(0, s.length() - 1).trim(); } int pos = s.indexOf('*'); if (pos > -1) { if (pos < (s.length() -1)) { return new ValidationError(WILDCARD + " is only allowed at the end of the input: " + s); } else if (requiresRightBoundary) { return new ValidationError(WILDCARD + " cannot be combined with right boundary"); } } Object expr = parseTermExpression(s); return (expr instanceof ValidationError) ? expr : new Input((List<Term>) expr, requiresLeftBoundary, requiresRightBoundary); }
@Test public void testParseTermExpressionDoesNotAllowAWildCardOnly() { assertTrue("parseTermExpression must not allow single wild card", LineParser.parseTermExpression("*") instanceof ValidationError); }
@SuppressWarnings("unchecked") @Test public void testParseTermExpressionSingleTerm() { assertThat((List<Term>) LineParser.parseTermExpression("abc"), contains(term("abc"))); }
@SuppressWarnings("unchecked") @Test public void testParseTermExpressionSingleLetter() { assertThat((List<Term>) LineParser.parseTermExpression("a"), contains(term("a"))); }
Object expr = parseTermExpression(instructionTerms); if (expr instanceof ValidationError) { return new ValidationError("Cannot parse line: " + line +" : " + ((ValidationError)expr).getMessage());
@SuppressWarnings("unchecked") @Test public void testParseTermExpressionMultiplePrefixes() { assertThat((List<Term>) LineParser.parseTermExpression("abc* def*"), contains(prefix("abc"), prefix("def"))); }
@SuppressWarnings("unchecked") @Test public void testParseTermExpressionMultipleTerms() { assertThat((List<Term>) LineParser.parseTermExpression("abc def"), contains(term("abc"), term("def"))); }
@SuppressWarnings("unchecked") @Test public void testParseTermExpressionMixed() { assertThat((List<Term>) LineParser.parseTermExpression("abc* def ghij* klmn"), contains(prefix("abc"), term("def"), prefix("ghij"), term("klmn"))); }