Search in sources :

Example 16 with ParserFailure

use of com.nike.riposte.util.text.parsercombinator.Parser.ParserFailure in project riposte by Nike-Inc.

the class ParserTest method test_test2_works.

@Test
public void test_test2_works() throws ParserFailure {
    final Pattern numberPattern = Pattern.compile("([0-9])");
    final Parser<Integer> number = regex(numberPattern).map(m -> new Integer(m.group(1)));
    Parser<Number> parser;
    Optional<Number> oResult;
    parser = number.thenParse(number).filter(test((one, two) -> false)).map(match((one, two) -> 0));
    oResult = parser.tryParse("12");
    assertThat(oResult.isPresent()).isFalse();
    parser = number.thenParse(number).filter(test((one, two) -> true)).map(match((one, two) -> one + two));
    oResult = parser.tryParse("12");
    assertThat(oResult.isPresent()).isTrue();
    assertThat(oResult.get()).isNotNull();
    assertThat(oResult.get()).isEqualTo(3);
    oResult = parser.tryParse("1");
    assertThat(oResult.isPresent()).isFalse();
}
Also used : Parsers.string(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.string) Parsers.then(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.then) Parsers.skip(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.skip) ParserFailure(com.nike.riposte.util.text.parsercombinator.Parser.ParserFailure) Parsers.begin(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.begin) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MatchResult(java.util.regex.MatchResult) Parsers.regex(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.regex) Test(org.junit.Test) Supplier(java.util.function.Supplier) Parsers.zeroOrMore(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.zeroOrMore) List(java.util.List) ParserInput(com.nike.riposte.util.text.parsercombinator.Parser.ParserInput) Apply.test(com.nike.riposte.util.text.parsercombinator.Parser.Apply.test) Optional(java.util.Optional) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) Pattern(java.util.regex.Pattern) Pair(com.nike.internal.util.Pair) Apply.match(com.nike.riposte.util.text.parsercombinator.Parser.Apply.match) Parsers.oneOrMore(com.nike.riposte.util.text.parsercombinator.Parser.Parsers.oneOrMore) Pattern(java.util.regex.Pattern) Test(org.junit.Test)

Aggregations

Pair (com.nike.internal.util.Pair)16 Apply.match (com.nike.riposte.util.text.parsercombinator.Parser.Apply.match)16 Apply.test (com.nike.riposte.util.text.parsercombinator.Parser.Apply.test)16 ParserFailure (com.nike.riposte.util.text.parsercombinator.Parser.ParserFailure)16 ParserInput (com.nike.riposte.util.text.parsercombinator.Parser.ParserInput)16 Parsers.begin (com.nike.riposte.util.text.parsercombinator.Parser.Parsers.begin)16 Parsers.oneOrMore (com.nike.riposte.util.text.parsercombinator.Parser.Parsers.oneOrMore)16 Parsers.regex (com.nike.riposte.util.text.parsercombinator.Parser.Parsers.regex)16 Parsers.skip (com.nike.riposte.util.text.parsercombinator.Parser.Parsers.skip)16 Parsers.string (com.nike.riposte.util.text.parsercombinator.Parser.Parsers.string)16 Parsers.then (com.nike.riposte.util.text.parsercombinator.Parser.Parsers.then)16 Parsers.zeroOrMore (com.nike.riposte.util.text.parsercombinator.Parser.Parsers.zeroOrMore)16 List (java.util.List)16 Optional (java.util.Optional)16 Supplier (java.util.function.Supplier)16 MatchResult (java.util.regex.MatchResult)16 Pattern (java.util.regex.Pattern)16 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)16 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)16 Test (org.junit.Test)16