Improved float handling

This commit is contained in:
moandji.ezana 2014-08-12 15:42:18 +02:00
parent 922408e979
commit 155781b5e2
5 changed files with 36 additions and 2 deletions

View file

@ -15,7 +15,7 @@ class ValueAnalysis {
private static final List<Object> INVALID_ARRAY = new ArrayList<Object>();
private static final Pattern BOOLEAN_REGEX = Pattern.compile("(true|false)(.*)");
private static final Pattern FLOAT_REGEX = Pattern.compile("(-?[0-9\\.]*)(.*)");
private static final Pattern FLOAT_REGEX = Pattern.compile("(-?\\d+\\.\\d+)(.*)");
private static final Pattern INTEGER_REGEX = Pattern.compile("(-?[0-9]*)(.*)");
private static final Pattern DATE_REGEX = Pattern.compile("(\\d{4}-[0-1][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9]Z)(.*)");
private static final Pattern UNICODE_REGEX = Pattern.compile("\\\\u(.*)");

View file

@ -149,6 +149,16 @@ public class BurntSushiTest {
public void datetime_malformed_no_leads() throws Exception {
runInvalidTest("datetime-malformed-no-leads");
}
@Test
public void float_no_leading_zero() throws Exception {
runInvalidTest("float-no-leading-zero");
}
@Test
public void float_no_trailing_digits() throws Exception {
runInvalidTest("float-no-trailing-digits");
}
@After
public void after() throws IOException {

View file

@ -347,12 +347,32 @@ public class TomlTest {
public void should_fail_when_illegal_characters_after_table() throws Exception {
new Toml().parse("[error] if you didn't catch this, your parser is broken");
}
@Test(expected = IllegalStateException.class)
public void should_fail_when_illegal_characters_after_key() throws Exception {
new Toml().parse("number = 3.14 pi");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_float_without_leading_0() {
new Toml().parse("answer = .12345");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_negative_float_without_leading_0() {
new Toml().parse("answer = -.12345");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_float_without_digits_after_dot() {
new Toml().parse("answer = 1.");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_negative_float_without_digits_after_dot() {
new Toml().parse("answer = -1.");
}
private File file(String file) {
return new File(getClass().getResource(file + ".toml").getFile());
}

View file

@ -0,0 +1,2 @@
answer = .12345
neganswer = -.12345

View file

@ -0,0 +1,2 @@
answer = 1.
neganswer = -1.