Test uses Toml object

This commit is contained in:
moandji.ezana 2014-04-06 21:11:52 +02:00
parent 122d7dd8f8
commit 40e5adfd2e

View file

@ -8,58 +8,49 @@ import java.io.File;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Map;
import java.util.Scanner;
import java.util.TimeZone;
import org.junit.Test;
import org.parboiled.Parboiled;
import org.parboiled.parserunners.RecoveringParseRunner;
import org.parboiled.support.ParsingResult;
public class RealWorldTest {
@SuppressWarnings("unchecked")
@Test
public void should_parse_example() throws Exception {
TomlParser parser = Parboiled.createParser(TomlParser.class);
String toml = new Scanner(new File(getClass().getResource("example.toml").getFile())).useDelimiter("\\Z").next();
ParsingResult<Object> result = new RecoveringParseRunner<Object>(parser.Toml()).run(toml);
Map<String, Object> root = (Map<String, Object>) result.valueStack.peek(result.valueStack.size() - 2);
Toml toml = new Toml().parse(new File(getClass().getResource("example.toml").getFile()));
// printMap(root);
assertEquals("TOML Example", root.get("title"));
assertEquals("TOML Example", toml.getString("title"));
Map<String, Object> owner = get(root, "owner");
assertEquals("Tom Preston-Werner", owner.get("name"));
assertEquals("GitHub", owner.get("organization"));
assertEquals("GitHub Cofounder & CEO\nLikes tater tots and beer.", owner.get("bio"));
Toml owner = toml.getKeyGroup("owner");
assertEquals("Tom Preston-Werner", owner.getString("name"));
assertEquals("GitHub", owner.getString("organization"));
assertEquals("GitHub Cofounder & CEO\nLikes tater tots and beer.", owner.getString("bio"));
Calendar dob = Calendar.getInstance();
dob.set(1979, Calendar.MAY, 27, 7, 32, 0);
dob.set(Calendar.MILLISECOND, 0);
dob.setTimeZone(TimeZone.getTimeZone("UTC"));
assertEquals(dob.getTime(), owner.get("dob"));
assertEquals(dob.getTime(), owner.getDate("dob"));
Map<String, Object> database = get(root, "database");
assertEquals("192.168.1.1", database.get("server"));
assertEquals(5000L, database.get("connection_max"));
assertTrue((Boolean) database.get("enabled"));
assertEquals(Arrays.asList(8001L, 8001L, 8002L), database.get("ports"));
Toml database = toml.getKeyGroup("database");
assertEquals("192.168.1.1", database.getString("server"));
assertEquals(5000L, database.getLong("connection_max").longValue());
assertTrue(database.getBoolean("enabled"));
assertEquals(Arrays.asList(8001L, 8001L, 8002L), database.getList("ports", Long.class));
Map<String, Object> servers = get(root, "servers");
Map<String, Object> alphaServers = get(servers, "alpha");
assertEquals("10.0.0.1", alphaServers.get("ip"));
assertEquals("eqdc10", alphaServers.get("dc"));
Map<String, Object> betaServers = get(servers, "beta");
assertEquals("10.0.0.2", betaServers.get("ip"));
assertEquals("eqdc10", betaServers.get("dc"));
Toml servers = toml.getKeyGroup("servers");
Toml alphaServers = servers.getKeyGroup("alpha");
assertEquals("10.0.0.1", alphaServers.getString("ip"));
assertEquals("eqdc10", alphaServers.getString("dc"));
Toml betaServers = servers.getKeyGroup("beta");
assertEquals("10.0.0.2", betaServers.getString("ip"));
assertEquals("eqdc10", betaServers.getString("dc"));
Map<String, Object> clients = get(root, "clients");
assertEquals(asList(asList("gamma", "delta"), asList(1L, 2L)), clients.get("data"));
assertEquals(asList("alpha", "omega"), clients.get("hosts"));
Toml clients = toml.getKeyGroup("clients");
assertEquals(asList(asList("gamma", "delta"), asList(1L, 2L)), clients.getList("data", String.class));
assertEquals(asList("alpha", "omega"), clients.getList("hosts", String.class));
}
@Test
@ -88,9 +79,4 @@ public class RealWorldTest {
}
}
}
@SuppressWarnings("unchecked")
private Map<String, Object> get(Map<String, Object> map, String key) {
return (Map<String, Object>) map.get(key);
}
}