Added test for example-v0.3.0.toml

This commit is contained in:
moandji.ezana 2015-01-22 15:18:03 +02:00
parent 6ad2743276
commit 3ca947785d
2 changed files with 252 additions and 2 deletions

View file

@ -1,13 +1,17 @@
package com.moandjiezana.toml;
import static java.util.Arrays.asList;
import static org.hamcrest.Matchers.contains;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;
import org.junit.Test;
@ -63,10 +67,74 @@ public class RealWorldTest {
assertEquals(asList("Test #11 ]proved that", "Experiment #9 was a success"), toml.getList("the.hard.test_array2", String.class));
assertEquals(" Same thing, but with a string #", toml.getString("the.hard.another_test_string"));
assertEquals(" And when \"'s are in the string, along with # \"", toml.getString("the.hard.harder_test_string"));
Toml theHardBit = toml.getTable("the.hard.bit#");
assertEquals("You don't think some user won't do that?", theHardBit.getString("what?"));
Toml theHardBit = toml.getTable("the.hard.\"bit#\"");
assertEquals("You don't think some user won't do that?", theHardBit.getString("\"what?\""));
assertEquals(asList("]"), theHardBit.getList("multi_line_array", String.class));
}
@SuppressWarnings("unchecked")
@Test
public void should_parse_current_version_example() throws Exception {
Toml toml = new Toml().parse(new File(getClass().getResource("example-v0.3.0.toml").getFile()));
assertEquals("value", toml.getString("Table.key"));
assertEquals("pug", toml.getString("dog.tater.type"));
assertNotNull(toml.getTable("x.y.z").getTable("w"));
assertEquals("I'm a string. \"You can quote me\". Name\tJosé\nLocation\tSF.", toml.getString("String.basic"));
assertEquals("One\nTwo", toml.getString("String.Multiline.key3"));
assertEquals(toml.getString("String.Multiline.key3"), toml.getString("String.Multiline.key1"));
assertEquals(toml.getString("String.Multiline.key3"), toml.getString("String.Multiline.key2"));
assertEquals("The quick brown fox jumps over the lazy dog.", toml.getString("String.Multilined.Singleline.key3"));
assertEquals(toml.getString("String.Multilined.Singleline.key3"), toml.getString("String.Multilined.Singleline.key1"));
assertEquals(toml.getString("String.Multilined.Singleline.key3"), toml.getString("String.Multilined.Singleline.key2"));
assertEquals("C:\\Users\\nodejs\\templates", toml.getString("String.Literal.winpath"));
assertEquals("\\\\ServerX\\admin$\\system32\\", toml.getString("String.Literal.winpath2"));
assertEquals("Tom \"Dubs\" Preston-Werner", toml.getString("String.Literal.quoted"));
assertEquals("<\\i\\c*\\s*>", toml.getString("String.Literal.regex"));
assertEquals("I [dw]on't need \\d{2} apples", toml.getString("String.Literal.Multiline.regex2"));
assertEquals("The first newline is\ntrimmed in raw strings.\n All other whitespace\n is preserved.\n", toml.getString("String.Literal.Multiline.lines"));
assertEquals(99, toml.getLong("Integer.key1").intValue());
assertEquals(42, toml.getLong("Integer.key2").intValue());
assertEquals(0, toml.getLong("Integer.key3").intValue());
assertEquals(-17, toml.getLong("Integer.key4").intValue());
assertEquals(1.0, toml.getDouble("Float.fractional.key1").doubleValue(), 0);
assertEquals(3.1415, toml.getDouble("Float.fractional.key2").doubleValue(), 0);
assertEquals(-0.01, toml.getDouble("Float.fractional.key3").doubleValue(), 0);
assertEquals(5e+22, toml.getDouble("Float.exponent.key1").doubleValue(), 0);
assertEquals(1e6, toml.getDouble("Float.exponent.key2").longValue(), 0);
assertEquals(-2E-2, toml.getDouble("Float.exponent.key3").doubleValue(), 0);
assertEquals(6.626e-34, toml.getDouble("Float.both.key").doubleValue(), 0);
assertTrue(toml.getBoolean("Booleans.True"));
assertFalse(toml.getBoolean("Booleans.False"));
assertThat(toml.getList("Array.key1", Long.class), contains(1L, 2L, 3L));
assertThat(toml.getList("Array.key2", String.class), contains("red", "yellow", "green"));
assertEquals(asList(asList(1L, 2L), asList(3L, 4L, 5L)), toml.getList("Array.key3", List.class));
assertEquals(asList(asList(1L, 2L), asList("a", "b", "c")), toml.getList("Array.key4", List.class));
assertThat(toml.getList("Array.key5", Long.class), contains(1L, 2L, 3L));
assertThat(toml.getList("Array.key6", Long.class), contains(1L, 2L));
assertEquals("Hammer", toml.getString("products[0].name"));
assertEquals(738594937, toml.getLong("products[0].sku").intValue());
assertNotNull(toml.getTable("products[1]"));
assertEquals("Nail", toml.getString("products[2].name"));
assertEquals(284758393, toml.getLong("products[2].sku").intValue());
assertEquals("gray", toml.getString("products[2].color"));
assertEquals("apple", toml.getString("fruit[0].name"));
assertEquals("red", toml.getString("fruit[0].physical.color"));
assertEquals("round", toml.getString("fruit[0].physical.shape"));
assertEquals("red delicious", toml.getString("fruit[0].variety[0].name"));
assertEquals("granny smith", toml.getString("fruit[0].variety[1].name"));
assertEquals("banana", toml.getString("fruit[1].name"));
assertEquals("plantain", toml.getString("fruit[1].variety[0].name"));
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(), toml.getDate("Datetime.key1"));
assertEquals(dob.getTime(), toml.getDate("Datetime.key2"));
dob.set(Calendar.MILLISECOND, 999);
assertEquals(dob.getTime(), toml.getDate("Datetime.key3"));
}
@Test
public void should_allow_keys_with_same_name_in_different_tables() throws Exception {

View file

@ -0,0 +1,182 @@
# Comment
# I am a comment. Hear me roar. Roar.
# Table
# Tables (also known as hash tables or dictionaries) are collections of key/value pairs.
# They appear in square brackets on a line by themselves.
[Table]
key = "value" # Yeah, you can do this.
# Nested tables are denoted by table names with dots in them. Name your tables whatever crap you please, just don't use #, ., [ or ].
[dog.tater]
type = "pug"
# You don't need to specify all the super-tables if you don't want to. TOML knows how to do it for you.
# [x] you
# [x.y] don't
# [x.y.z] need these
[x.y.z.w] # for this to work
# String
# There are four ways to express strings: basic, multi-line basic, literal, and multi-line literal.
# All strings must contain only valid UTF-8 characters.
[String]
basic = "I'm a string. \"You can quote me\". Name\tJos\u00E9\nLocation\tSF."
[String.Multiline]
# The following strings are byte-for-byte equivalent:
key1 = "One\nTwo"
key2 = """One\nTwo"""
key3 = """
One
Two"""
[String.Multilined.Singleline]
# The following strings are byte-for-byte equivalent:
key1 = "The quick brown fox jumps over the lazy dog."
key2 = """
The quick brown \
fox jumps over \
the lazy dog."""
key3 = """\
The quick brown \
fox jumps over \
the lazy dog.\
"""
[String.Literal]
# What you see is what you get.
winpath = 'C:\Users\nodejs\templates'
winpath2 = '\\ServerX\admin$\system32\'
quoted = 'Tom "Dubs" Preston-Werner'
regex = '<\i\c*\s*>'
[String.Literal.Multiline]
regex2 = '''I [dw]on't need \d{2} apples'''
lines = '''
The first newline is
trimmed in raw strings.
All other whitespace
is preserved.
'''
# Integer
# Integers are whole numbers. Positive numbers may be prefixed with a plus sign.
# Negative numbers are prefixed with a minus sign.
[Integer]
key1 = +99
key2 = 42
key3 = 0
key4 = -17
# Float
# A float consists of an integer part (which may be prefixed with a plus or minus sign)
# followed by a fractional part and/or an exponent part.
[Float.fractional]
# fractional
key1 = +1.0
key2 = 3.1415
key3 = -0.01
[Float.exponent]
# exponent
key1 = 5e+22
key2 = 1e6
key3 = -2E-2
[Float.both]
# both
key = 6.626e-34
# Boolean
# Booleans are just the tokens you're used to. Always lowercase.
[Booleans]
True = true
False = false
# Datetime
# Datetimes are RFC 3339 dates.
[Datetime]
key1 = 1979-05-27T07:32:00Z
key2 = 1979-05-27T00:32:00-07:00
key3 = 1979-05-27T00:32:00.999-07:00
# Array
# Arrays are square brackets with other primitives inside. Whitespace is ignored. Elements are separated by commas. Data types may not be mixed.
[Array]
key1 = [ 1, 2, 3 ]
key2 = [ "red", "yellow", "green" ]
key3 = [ [ 1, 2 ], [3, 4, 5] ]
key4 = [ [ 1, 2 ], ["a", "b", "c"] ] # this is ok
#Arrays can also be multiline. So in addition to ignoring whitespace, arrays also ignore newlines between the brackets.
# Terminating commas are ok before the closing bracket.
key5 = [
1, 2, 3
]
key6 = [
1,
2, # this is ok
]
# Array of Tables
# These can be expressed by using a table name in double brackets.
# Each table with the same double bracketed name will be an element in the array.
# The tables are inserted in the order encountered.
[[products]]
name = "Hammer"
sku = 738594937
[[products]]
[[products]]
name = "Nail"
sku = 284758393
color = "gray"
# You can create nested arrays of tables as well.
[[fruit]]
name = "apple"
[fruit.physical]
color = "red"
shape = "round"
[[fruit.variety]]
name = "red delicious"
[[fruit.variety]]
name = "granny smith"
[[fruit]]
name = "banana"
[[fruit.variety]]
name = "plantain"