ExtendedParser extends BaseParser { protected final float readNumber() throws ParserException { float number = 0; boolean charWasRead = false; while (available() && Character.isDigit(peek())) { charWasRead = true; number *= 10; number += pop() - 48; } if (!charWasRead) { die("Expected number"); } if (available() && peek() == '.') { check('.'); float factor = 0.1f; charWasRead = false; while (available() && Character.isDigit(peek())) { charWasRead = true; number += (pop() - 48) * factor; factor /= 10; } if (!charWasRead) { die("Expected number"); } } return number; } ExtendedParser(String toParse);   }
@Test public void testNumber() throws ParserException { ExtendedParser parser = new ExtendedParser("0"); assertEquals(0f, parser.readNumber(), 0.0001); parser = new ExtendedParser("424242"); assertEquals(424242f, parser.readNumber(), 0.0001); parser = new ExtendedParser("0.05"); assertEquals(0.05f, parser.readNumber(), 0.0001); parser = new ExtendedParser("45.67"); assertEquals(45.67f, parser.readNumber(), 0.0001); }
@Test public void testInvalidNumbers() { try { new ExtendedParser("a").readNumber(); fail("Expected invalid number error"); } catch (ParserException e) { } try { new ExtendedParser("-").readNumber(); fail("Expected invalid number error"); } catch (ParserException e) { } try { new ExtendedParser("0.").readNumber(); fail("Expected invalid number error"); } catch (ParserException e) { } try { new ExtendedParser(".0").readNumber(); fail("Expected invalid number error"); } catch (ParserException e) { } }