Limited the range of characters bare keys can accept

This commit is contained in:
moandji.ezana 2015-01-22 14:39:08 +02:00
parent 89d2c70b15
commit 1380c72d48
4 changed files with 40 additions and 19 deletions

View file

@ -7,6 +7,8 @@ import java.util.List;
class Keys {
private static final String ALLOWED_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_-.";
static class Key {
final String name;
final int index;
@ -65,7 +67,7 @@ class Keys {
}
/**
* @param line raw TOML iine to parse
* @param line trimmed TOML line to parse
* @return null if line is not a valid table identifier
*/
static String getTableName(String line) {
@ -81,7 +83,7 @@ class Keys {
} else if (!quoted && c == ']') {
terminated = true;
break;
} else if (!quoted && c == '[') {
} else if (!quoted && (ALLOWED_CHARS.indexOf(c) == -1)) {
break;
}

View file

@ -0,0 +1,33 @@
package com.moandjiezana.toml;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
public class BareKeysTest {
@Rule
public final ExpectedException exception = ExpectedException.none();
@Test
public void should_fail_when_characters_outside_accept_range_are_used() throws Exception {
exception.expect(IllegalStateException.class);
exception.expectMessage("Invalid table definition: [~]");
new Toml().parse("[~]");
}
@Test
public void should_fail_on_sharp_sign_in_table_names() throws Exception {
exception.expect(IllegalStateException.class);
new Toml().parse("[group#]\nkey=1");
}
@Test
public void should_fail_on_spaces_in_table_names() throws Exception {
exception.expect(IllegalStateException.class);
new Toml().parse("[valid key]");
}
}

View file

@ -14,6 +14,7 @@ import java.util.TimeZone;
import org.junit.After;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import com.google.gson.Gson;
@ -198,12 +199,12 @@ public class BurntSushiValidTest {
run("table-sub-empty");
}
@Test
@Test @Ignore
public void table_whitespace() throws Exception {
run("table-whitespace");
}
@Test
@Test @Ignore
public void table_with_pound() throws Exception {
run("table-with-pound");
}

View file

@ -1,7 +1,6 @@
package com.moandjiezana.toml;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@ -147,20 +146,6 @@ public class TomlTest {
assertEquals(1, toml.getLong("group_a.a").intValue());
}
@Test
public void should_support_sharp_sign_in_table_names() throws Exception {
Toml toml = new Toml().parse("[group#]\nkey=1");
assertEquals(1, toml.getLong("group#.key").intValue());
}
@Test
public void should_support_spaces_in_table_names() throws Exception {
Toml toml = new Toml().parse("[valid key]");
assertNotNull(toml.getTable("valid key"));
}
@Test
public void should_support_blank_lines() throws Exception {
Toml toml = new Toml().parse(new File(getClass().getResource("should_support_blank_line.toml").getFile()));