PageParser { public static PageRange parse(String pages) { CharStream cs = CharStreams.fromString(pages); InternalPageLexer lexer = new InternalPageLexer(cs); lexer.removeErrorListeners(); CommonTokenStream tokens = new CommonTokenStream(lexer); InternalPageParser parser = new InternalPageParser(tokens); parser.removeErrorListeners(); PagesContext ctx = parser.pages(); if (ctx.literal == null || ctx.literal.isEmpty() || ctx.exception != null || parser.getNumberOfSyntaxErrors() > 0) { return new PageRange(pages, null, null, false); } return new PageRange(ctx.literal, ctx.pageFrom, ctx.numberOfPages, ctx.multiplePages); }  static PageRange parse(String pages);  }
@Test public void singlePage() { PageRange pr = PageParser.parse("10"); assertEquals(Integer.valueOf(1), pr.getNumberOfPages()); assertFalse(pr.isMultiplePages()); }
@Test public void simplePageRange() { PageRange pr = PageParser.parse("10-20"); assertEquals("10-20", pr.getLiteral()); assertEquals("10", pr.getPageFirst()); assertEquals(Integer.valueOf(11), pr.getNumberOfPages()); assertTrue(pr.isMultiplePages()); pr = PageParser.parse("10 - 20"); assertEquals("10-20", pr.getLiteral()); assertEquals("10", pr.getPageFirst()); assertEquals(Integer.valueOf(11), pr.getNumberOfPages()); assertTrue(pr.isMultiplePages()); pr = PageParser.parse("10--20"); assertEquals("10-20", pr.getLiteral()); assertEquals("10", pr.getPageFirst()); assertEquals(Integer.valueOf(11), pr.getNumberOfPages()); assertTrue(pr.isMultiplePages()); pr = PageParser.parse("10---20"); assertEquals("10-20", pr.getLiteral()); assertEquals("10", pr.getPageFirst()); assertEquals(Integer.valueOf(11), pr.getNumberOfPages()); assertTrue(pr.isMultiplePages()); pr = PageParser.parse("10\u201320"); assertEquals("10-20", pr.getLiteral()); assertEquals("10", pr.getPageFirst()); assertEquals(Integer.valueOf(11), pr.getNumberOfPages()); assertTrue(pr.isMultiplePages()); }
@Test public void twoPageRanges() { PageRange pr = PageParser.parse("10-20,30--40"); assertEquals("10-20,30-40", pr.getLiteral()); assertEquals("10", pr.getPageFirst()); assertEquals(Integer.valueOf(22), pr.getNumberOfPages()); assertTrue(pr.isMultiplePages()); pr = PageParser.parse("10 - 20 ,30 --40"); assertEquals("10-20,30-40", pr.getLiteral()); assertEquals("10", pr.getPageFirst()); assertEquals(Integer.valueOf(22), pr.getNumberOfPages()); assertTrue(pr.isMultiplePages()); }
@Test public void complex() { PageRange pr = PageParser.parse("10-20,30--40,45,50\u201355,5"); assertEquals("10-20,30-40,45,50-55,5", pr.getLiteral()); assertEquals("5", pr.getPageFirst()); assertEquals(Integer.valueOf(30), pr.getNumberOfPages()); assertTrue(pr.isMultiplePages()); }
@Test public void fromToUnknown() { PageRange pr = PageParser.parse("10-??"); assertEquals("10-??", pr.getLiteral()); assertEquals("10", pr.getPageFirst()); assertNull(pr.getNumberOfPages()); assertTrue(pr.isMultiplePages()); pr = PageParser.parse("10 - ??"); assertEquals("10 - ??", pr.getLiteral()); assertEquals("10", pr.getPageFirst()); assertNull(pr.getNumberOfPages()); assertTrue(pr.isMultiplePages()); }
@Test public void unknownRange() { PageRange pr = PageParser.parse("??-??"); assertEquals("??-??", pr.getLiteral()); assertEquals("??", pr.getPageFirst()); assertNull(pr.getNumberOfPages()); assertTrue(pr.isMultiplePages()); pr = PageParser.parse("?? - ??"); assertEquals("?? - ??", pr.getLiteral()); assertEquals("??", pr.getPageFirst()); assertNull(pr.getNumberOfPages()); assertTrue(pr.isMultiplePages()); }
@Test public void unknownPage() { PageRange pr = PageParser.parse("??"); assertEquals("??", pr.getLiteral()); assertEquals("??", pr.getPageFirst()); assertNull(pr.getNumberOfPages()); assertFalse(pr.isMultiplePages()); }
@Test public void singlePageRange() { PageRange pr = PageParser.parse("10-10"); assertEquals("10", pr.getLiteral()); assertEquals("10", pr.getPageFirst()); assertEquals(Integer.valueOf(1), pr.getNumberOfPages()); assertTrue(pr.isMultiplePages()); }
@Test public void twoPages() { PageRange pr = PageParser.parse("10,11"); assertEquals("10,11", pr.getLiteral()); assertEquals("10", pr.getPageFirst()); assertEquals(Integer.valueOf(2), pr.getNumberOfPages()); assertTrue(pr.isMultiplePages()); PageParser.parse("10, 11"); assertEquals("10,11", pr.getLiteral()); assertEquals("10", pr.getPageFirst()); assertEquals(Integer.valueOf(2), pr.getNumberOfPages()); assertTrue(pr.isMultiplePages()); }
@Test public void literal() { PageRange pr = PageParser.parse("A page"); assertEquals("A page", pr.getLiteral()); assertNull(pr.getPageFirst()); assertNull(pr.getNumberOfPages()); assertFalse(pr.isMultiplePages()); pr = PageParser.parse("A,,page"); assertEquals("A,,page", pr.getLiteral()); assertNull(pr.getPageFirst()); assertNull(pr.getNumberOfPages()); assertFalse(pr.isMultiplePages()); }