Rename parse to read and ValueConverter to ValueReader

This commit is contained in:
moandji.ezana 2015-08-14 11:12:08 +02:00
parent 039b487115
commit d60736e104
38 changed files with 369 additions and 356 deletions

View file

@ -1,22 +1,22 @@
package com.moandjiezana.toml;
import static com.moandjiezana.toml.ValueConverters.CONVERTERS;
import static com.moandjiezana.toml.ValueReaders.VALUE_READERS;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
class ArrayConverter implements ValueConverter {
class ArrayValueReader implements ValueReader {
static final ArrayConverter ARRAY_PARSER = new ArrayConverter();
static final ArrayValueReader ARRAY_VALUE_READER = new ArrayValueReader();
@Override
public boolean canConvert(String s) {
public boolean canRead(String s) {
return s.startsWith("[");
}
@Override
public Object convert(String s, AtomicInteger index, Context context) {
public Object read(String s, AtomicInteger index, Context context) {
AtomicInteger line = context.line;
int startLine = line.get();
int startIndex = index.get();
@ -37,7 +37,7 @@ class ArrayConverter implements ValueConverter {
} else if (inComment || Character.isWhitespace(c) || c == ',') {
continue;
} else if (c == '[') {
Object converted = convert(s, index, context);
Object converted = read(s, index, context);
if (converted instanceof Results.Errors) {
errors.add((Results.Errors) converted);
} else if (!isHomogenousArray(converted, arrayItems)) {
@ -50,7 +50,7 @@ class ArrayConverter implements ValueConverter {
terminated = true;
break;
} else {
Object converted = CONVERTERS.convert(s, index, context);
Object converted = VALUE_READERS.convert(s, index, context);
if (converted instanceof Results.Errors) {
errors.add((Results.Errors) converted);
} else if (!isHomogenousArray(converted, arrayItems)) {
@ -76,5 +76,5 @@ class ArrayConverter implements ValueConverter {
return values.isEmpty() || values.get(0).getClass().isAssignableFrom(o.getClass()) || o.getClass().isAssignableFrom(values.get(0).getClass());
}
private ArrayConverter() {}
private ArrayValueReader() {}
}

View file

@ -3,17 +3,17 @@ package com.moandjiezana.toml;
import java.util.concurrent.atomic.AtomicInteger;
class BooleanConverter implements ValueConverter, ValueWriter {
class BooleanValueReaderWriter implements ValueReader, ValueWriter {
static final BooleanConverter BOOLEAN_PARSER = new BooleanConverter();
static final BooleanValueReaderWriter BOOLEAN_VALUE_READER_WRITER = new BooleanValueReaderWriter();
@Override
public boolean canConvert(String s) {
public boolean canRead(String s) {
return s.startsWith("true") || s.startsWith("false");
}
@Override
public Object convert(String s, AtomicInteger index, Context context) {
public Object read(String s, AtomicInteger index, Context context) {
s = s.substring(index.get());
Boolean b = s.startsWith("true") ? Boolean.TRUE : Boolean.FALSE;
@ -39,7 +39,7 @@ class BooleanConverter implements ValueConverter, ValueWriter {
return true;
}
private BooleanConverter() {}
private BooleanValueReaderWriter() {}
@Override
public String toString() {

View file

@ -7,14 +7,14 @@ import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class DateConverter implements ValueConverter, ValueWriter {
class DateValueReaderWriter implements ValueReader, ValueWriter {
static final DateConverter DATE_PARSER = new DateConverter();
static final DateConverter DATE_PARSER_JDK_6 = new DateConverterJdk6();
static final DateValueReaderWriter DATE_VALUE_READER_WRITER = new DateValueReaderWriter();
static final DateValueReaderWriter DATE_PARSER_JDK_6 = new DateConverterJdk6();
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])(\\.\\d*)?(Z|(?:[+\\-]\\d{2}:\\d{2}))(.*)");
@Override
public boolean canConvert(String s) {
public boolean canRead(String s) {
if (s.length() < 5) {
return false;
}
@ -35,7 +35,7 @@ class DateConverter implements ValueConverter, ValueWriter {
}
@Override
public Object convert(String original, AtomicInteger index, Context context) {
public Object read(String original, AtomicInteger index, Context context) {
StringBuilder sb = new StringBuilder();
for (int i = index.get(); i < original.length(); i = index.incrementAndGet()) {
@ -126,9 +126,9 @@ class DateConverter implements ValueConverter, ValueWriter {
return "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
}
private DateConverter() {}
private DateValueReaderWriter() {}
private static class DateConverterJdk6 extends DateConverter {
private static class DateConverterJdk6 extends DateValueReaderWriter {
@Override
public void write(Object value, WriterContext context) {
DateFormat formatter = super.getFormatter(context.getDatePolicy());

View file

@ -82,7 +82,7 @@ class Identifier {
}
}
return StringConverter.STRING_PARSER.replaceUnicodeCharacters(sb.toString());
return StringValueReaderWriter.STRING_VALUE_READER_WRITER.replaceUnicodeCharacters(sb.toString());
}
private static boolean isValidKey(String name, Context context) {

View file

@ -1,21 +1,21 @@
package com.moandjiezana.toml;
import static com.moandjiezana.toml.ValueConverters.CONVERTERS;
import static com.moandjiezana.toml.ValueReaders.VALUE_READERS;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;
class InlineTableConverter implements ValueConverter {
class InlineTableValueReader implements ValueReader {
static final InlineTableConverter INLINE_TABLE_PARSER = new InlineTableConverter();
static final InlineTableValueReader INLINE_TABLE_VALUE_READER = new InlineTableValueReader();
@Override
public boolean canConvert(String s) {
public boolean canRead(String s) {
return s.startsWith("{");
}
@Override
public Object convert(String s, AtomicInteger sharedIndex, Context context) {
public Object read(String s, AtomicInteger sharedIndex, Context context) {
AtomicInteger line = context.line;
int startLine = line.get();
int startIndex = sharedIndex.get();
@ -30,7 +30,7 @@ class InlineTableConverter implements ValueConverter {
char c = s.charAt(i);
if (inValue && !Character.isWhitespace(c)) {
Object converted = CONVERTERS.convert(s, sharedIndex, context.with(Identifier.from(currentKey.toString(), context)));
Object converted = VALUE_READERS.convert(s, sharedIndex, context.with(Identifier.from(currentKey.toString(), context)));
if (converted instanceof Results.Errors) {
errors.add((Results.Errors) converted);
@ -73,5 +73,5 @@ class InlineTableConverter implements ValueConverter {
return results;
}
private InlineTableConverter() {}
private InlineTableValueReader() {}
}

View file

@ -3,17 +3,17 @@ package com.moandjiezana.toml;
import java.util.concurrent.atomic.AtomicInteger;
class LiteralStringConverter implements ValueConverter {
class LiteralStringValueReader implements ValueReader {
static final LiteralStringConverter LITERAL_STRING_PARSER = new LiteralStringConverter();
static final LiteralStringValueReader LITERAL_STRING_VALUE_READER = new LiteralStringValueReader();
@Override
public boolean canConvert(String s) {
public boolean canRead(String s) {
return s.startsWith("'");
}
@Override
public Object convert(String s, AtomicInteger index, Context context) {
public Object read(String s, AtomicInteger index, Context context) {
int startLine = context.line.get();
boolean terminated = false;
int startIndex = index.incrementAndGet();
@ -38,5 +38,5 @@ class LiteralStringConverter implements ValueConverter {
return substring;
}
private LiteralStringConverter() {}
private LiteralStringValueReader() {}
}

View file

@ -2,17 +2,17 @@ package com.moandjiezana.toml;
import java.util.concurrent.atomic.AtomicInteger;
class MultilineLiteralStringConverter implements ValueConverter {
class MultilineLiteralStringValueReader implements ValueReader {
static final MultilineLiteralStringConverter MULTILINE_LITERAL_STRING_CONVERTER = new MultilineLiteralStringConverter();
static final MultilineLiteralStringValueReader MULTILINE_LITERAL_STRING_VALUE_READER = new MultilineLiteralStringValueReader();
@Override
public boolean canConvert(String s) {
public boolean canRead(String s) {
return s.startsWith("'''");
}
@Override
public Object convert(String s, AtomicInteger index, Context context) {
public Object read(String s, AtomicInteger index, Context context) {
AtomicInteger line = context.line;
int startLine = line.get();
int originalStartIndex = index.get();
@ -47,5 +47,5 @@ class MultilineLiteralStringConverter implements ValueConverter {
return s.substring(startIndex, endIndex);
}
private MultilineLiteralStringConverter() {}
private MultilineLiteralStringValueReader() {}
}

View file

@ -2,17 +2,17 @@ package com.moandjiezana.toml;
import java.util.concurrent.atomic.AtomicInteger;
class MultilineStringConverter implements ValueConverter {
class MultilineStringValueReader implements ValueReader {
static final MultilineStringConverter MULTILINE_STRING_PARSER = new MultilineStringConverter();
static final MultilineStringValueReader MULTILINE_STRING_VALUE_READER = new MultilineStringValueReader();
@Override
public boolean canConvert(String s) {
public boolean canRead(String s) {
return s.startsWith("\"\"\"");
}
@Override
public Object convert(String s, AtomicInteger index, Context context) {
public Object read(String s, AtomicInteger index, Context context) {
AtomicInteger line = context.line;
int startLine = line.get();
int originalStartIndex = index.get();
@ -44,13 +44,13 @@ class MultilineStringConverter implements ValueConverter {
s = s.substring(startIndex, endIndex);
s = s.replaceAll("\\\\\\s+", "");
s = StringConverter.STRING_PARSER.replaceUnicodeCharacters(s);
s = StringConverter.STRING_PARSER.replaceSpecialCharacters(s);
s = StringValueReaderWriter.STRING_VALUE_READER_WRITER.replaceUnicodeCharacters(s);
s = StringValueReaderWriter.STRING_VALUE_READER_WRITER.replaceSpecialCharacters(s);
return s;
}
private MultilineStringConverter() {
private MultilineStringValueReader() {
}
}

View file

@ -2,18 +2,18 @@ package com.moandjiezana.toml;
import java.util.concurrent.atomic.AtomicInteger;
class NumberConverter implements ValueConverter, ValueWriter {
static final NumberConverter NUMBER_PARSER = new NumberConverter();
class NumberValueReaderWriter implements ValueReader, ValueWriter {
static final NumberValueReaderWriter NUMBER_VALUE_READER_WRITER = new NumberValueReaderWriter();
@Override
public boolean canConvert(String s) {
public boolean canRead(String s) {
char firstChar = s.charAt(0);
return firstChar == '+' || firstChar == '-' || Character.isDigit(firstChar);
}
@Override
public Object convert(String s, AtomicInteger index, Context context) {
public Object read(String s, AtomicInteger index, Context context) {
boolean signable = true;
boolean dottable = false;
boolean exponentable = false;

View file

@ -6,9 +6,9 @@ import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class StringConverter implements ValueConverter, ValueWriter {
class StringValueReaderWriter implements ValueReader, ValueWriter {
static final StringConverter STRING_PARSER = new StringConverter();
static final StringValueReaderWriter STRING_VALUE_READER_WRITER = new StringValueReaderWriter();
private static final Pattern UNICODE_REGEX = Pattern.compile("\\\\[uU](.{4})");
static private final String[] specialCharacterEscapes = new String[93];
@ -24,12 +24,12 @@ class StringConverter implements ValueConverter, ValueWriter {
}
@Override
public boolean canConvert(String s) {
public boolean canRead(String s) {
return s.startsWith("\"");
}
@Override
public Object convert(String s, AtomicInteger index, Context context) {
public Object read(String s, AtomicInteger index, Context context) {
int startIndex = index.incrementAndGet();
int endIndex = -1;
@ -119,7 +119,7 @@ class StringConverter implements ValueConverter, ValueWriter {
}
}
private StringConverter() {}
private StringValueReaderWriter() {}
@Override
public String toString() {

View file

@ -1,11 +1,24 @@
package com.moandjiezana.toml;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import java.io.*;
import java.util.*;
/**
* <p>Provides access to the keys and tables in a TOML data source.</p>
*
@ -13,11 +26,11 @@ import java.util.*;
* Getters for simple values (String, Date, etc.) will return null if no matching key exists.
* {@link #getList(String)}, {@link #getTable(String)} and {@link #getTables(String)} return empty values if there is no matching key.</p>
*
* <p>All parse methods throw an {@link IllegalStateException} if the TOML is incorrect.</p>
* <p>All read methods throw an {@link IllegalStateException} if the TOML is incorrect.</p>
*
* <p>Example usage:</p>
* <pre><code>
* Toml toml = new Toml().parse(getTomlFile());
* Toml toml = new Toml().read(getTomlFile());
* String name = toml.getString("name");
* Long port = toml.getLong("server.ip"); // compound key. Is equivalent to:
* Long port2 = toml.getTable("server").getLong("ip");
@ -53,9 +66,9 @@ public class Toml {
* @return this instance
* @throws IllegalStateException If file contains invalid TOML
*/
public Toml parse(File file) {
public Toml read(File file) {
try {
return parse(new FileReader(file));
return read(new FileReader(file));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
@ -68,8 +81,8 @@ public class Toml {
* @return this instance
* @throws IllegalStateException If file contains invalid TOML
*/
public Toml parse(InputStream inputStream) {
return parse(new InputStreamReader(inputStream));
public Toml read(InputStream inputStream) {
return read(new InputStreamReader(inputStream));
}
/**
@ -79,7 +92,7 @@ public class Toml {
* @return this instance
* @throws IllegalStateException If file contains invalid TOML
*/
public Toml parse(Reader reader) {
public Toml read(Reader reader) {
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(reader);
@ -90,7 +103,7 @@ public class Toml {
w.append(line).append('\n');
line = bufferedReader.readLine();
}
parse(w.toString());
read(w.toString());
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
@ -108,7 +121,7 @@ public class Toml {
* @return this instance
* @throws IllegalStateException If tomlString is not valid TOML
*/
public Toml parse(String tomlString) throws IllegalStateException {
public Toml read(String tomlString) throws IllegalStateException {
Results results = TomlParser.run(tomlString);
if (results.errors.hasErrors()) {
throw new IllegalStateException(results.errors.toString());

View file

@ -46,7 +46,7 @@ class TomlParser {
value = null;
line.incrementAndGet();
} else if (!inComment && identifier != null && identifier.isKey() && value == null && !Character.isWhitespace(c)) {
value = ValueConverters.CONVERTERS.convert(tomlString, index, new Context(identifier, line, results.errors));
value = ValueReaders.VALUE_READERS.convert(tomlString, index, new Context(identifier, line, results.errors));
if (value instanceof Results.Errors) {
results.errors.add((Results.Errors) value);

View file

@ -1,37 +0,0 @@
package com.moandjiezana.toml;
import static com.moandjiezana.toml.ArrayConverter.ARRAY_PARSER;
import static com.moandjiezana.toml.BooleanConverter.BOOLEAN_PARSER;
import static com.moandjiezana.toml.DateConverter.DATE_PARSER;
import static com.moandjiezana.toml.InlineTableConverter.INLINE_TABLE_PARSER;
import static com.moandjiezana.toml.LiteralStringConverter.LITERAL_STRING_PARSER;
import static com.moandjiezana.toml.MultilineLiteralStringConverter.MULTILINE_LITERAL_STRING_CONVERTER;
import static com.moandjiezana.toml.MultilineStringConverter.MULTILINE_STRING_PARSER;
import static com.moandjiezana.toml.NumberConverter.NUMBER_PARSER;
import static com.moandjiezana.toml.StringConverter.STRING_PARSER;
import java.util.concurrent.atomic.AtomicInteger;
class ValueConverters {
static final ValueConverters CONVERTERS = new ValueConverters();
Object convert(String value, AtomicInteger index, Context context) {
String substring = value.substring(index.get());
for (ValueConverter valueParser : PARSERS) {
if (valueParser.canConvert(substring)) {
return valueParser.convert(value, index, context);
}
}
Results.Errors errors = new Results.Errors();
errors.invalidValue(context.identifier.getName(), substring, context.line.get());
return errors;
}
private ValueConverters() {}
private static final ValueConverter[] PARSERS = {
MULTILINE_STRING_PARSER, MULTILINE_LITERAL_STRING_CONVERTER, LITERAL_STRING_PARSER, STRING_PARSER, DATE_PARSER, NUMBER_PARSER, BOOLEAN_PARSER, ARRAY_PARSER, INLINE_TABLE_PARSER
};
}

View file

@ -2,20 +2,20 @@ package com.moandjiezana.toml;
import java.util.concurrent.atomic.AtomicInteger;
interface ValueConverter {
interface ValueReader {
/**
* @param s must already have been trimmed
*/
boolean canConvert(String s);
boolean canRead(String s);
/**
* Partial validation. Stops after type terminator, rather than at EOI.
*
* @param s must already have been validated by {@link #canConvert(String)}
* @param s must already have been validated by {@link #canRead(String)}
* @param index where to start in s
* @param line current line number, used for error reporting
* @return a value or a {@link Results.Errors}
*/
Object convert(String s, AtomicInteger index, Context context);
Object read(String s, AtomicInteger index, Context context);
}

View file

@ -0,0 +1,37 @@
package com.moandjiezana.toml;
import static com.moandjiezana.toml.ArrayValueReader.ARRAY_VALUE_READER;
import static com.moandjiezana.toml.BooleanValueReaderWriter.BOOLEAN_VALUE_READER_WRITER;
import static com.moandjiezana.toml.DateValueReaderWriter.DATE_VALUE_READER_WRITER;
import static com.moandjiezana.toml.InlineTableValueReader.INLINE_TABLE_VALUE_READER;
import static com.moandjiezana.toml.LiteralStringValueReader.LITERAL_STRING_VALUE_READER;
import static com.moandjiezana.toml.MultilineLiteralStringValueReader.MULTILINE_LITERAL_STRING_VALUE_READER;
import static com.moandjiezana.toml.MultilineStringValueReader.MULTILINE_STRING_VALUE_READER;
import static com.moandjiezana.toml.NumberValueReaderWriter.NUMBER_VALUE_READER_WRITER;
import static com.moandjiezana.toml.StringValueReaderWriter.STRING_VALUE_READER_WRITER;
import java.util.concurrent.atomic.AtomicInteger;
class ValueReaders {
static final ValueReaders VALUE_READERS = new ValueReaders();
Object convert(String value, AtomicInteger index, Context context) {
String substring = value.substring(index.get());
for (ValueReader valueParser : READERS) {
if (valueParser.canRead(substring)) {
return valueParser.read(value, index, context);
}
}
Results.Errors errors = new Results.Errors();
errors.invalidValue(context.identifier.getName(), substring, context.line.get());
return errors;
}
private ValueReaders() {}
private static final ValueReader[] READERS = {
MULTILINE_STRING_VALUE_READER, MULTILINE_LITERAL_STRING_VALUE_READER, LITERAL_STRING_VALUE_READER, STRING_VALUE_READER_WRITER, DATE_VALUE_READER_WRITER, NUMBER_VALUE_READER_WRITER, BOOLEAN_VALUE_READER_WRITER, ARRAY_VALUE_READER, INLINE_TABLE_VALUE_READER
};
}

View file

@ -1,13 +1,13 @@
package com.moandjiezana.toml;
import static com.moandjiezana.toml.BooleanConverter.BOOLEAN_PARSER;
import static com.moandjiezana.toml.DateConverter.DATE_PARSER;
import static com.moandjiezana.toml.DateConverter.DATE_PARSER_JDK_6;
import static com.moandjiezana.toml.BooleanValueReaderWriter.BOOLEAN_VALUE_READER_WRITER;
import static com.moandjiezana.toml.DateValueReaderWriter.DATE_VALUE_READER_WRITER;
import static com.moandjiezana.toml.DateValueReaderWriter.DATE_PARSER_JDK_6;
import static com.moandjiezana.toml.MapValueWriter.MAP_VALUE_WRITER;
import static com.moandjiezana.toml.NumberConverter.NUMBER_PARSER;
import static com.moandjiezana.toml.NumberValueReaderWriter.NUMBER_VALUE_READER_WRITER;
import static com.moandjiezana.toml.ObjectValueWriter.OBJECT_VALUE_WRITER;
import static com.moandjiezana.toml.PrimitiveArrayValueWriter.PRIMITIVE_ARRAY_VALUE_WRITER;
import static com.moandjiezana.toml.StringConverter.STRING_PARSER;
import static com.moandjiezana.toml.StringValueReaderWriter.STRING_VALUE_READER_WRITER;
import static com.moandjiezana.toml.TableArrayValueWriter.TABLE_ARRAY_VALUE_WRITER;
class ValueWriters {
@ -15,7 +15,7 @@ class ValueWriters {
static final ValueWriters WRITERS = new ValueWriters();
ValueWriter findWriterFor(Object value) {
for (ValueWriter valueWriter : VALUE_WRITERs) {
for (ValueWriter valueWriter : VALUE_WRITERS) {
if (valueWriter.canWrite(value)) {
return valueWriter;
}
@ -26,12 +26,12 @@ class ValueWriters {
private ValueWriters() {}
private static DateConverter getPlatformSpecificDateConverter() {
return Runtime.class.getPackage().getSpecificationVersion().startsWith("1.6") ? DATE_PARSER_JDK_6 : DATE_PARSER;
private static DateValueReaderWriter getPlatformSpecificDateConverter() {
return Runtime.class.getPackage().getSpecificationVersion().startsWith("1.6") ? DATE_PARSER_JDK_6 : DATE_VALUE_READER_WRITER;
}
private static final ValueWriter[] VALUE_WRITERs = {
STRING_PARSER, NUMBER_PARSER, BOOLEAN_PARSER, getPlatformSpecificDateConverter(),
private static final ValueWriter[] VALUE_WRITERS = {
STRING_VALUE_READER_WRITER, NUMBER_VALUE_READER_WRITER, BOOLEAN_VALUE_READER_WRITER, getPlatformSpecificDateConverter(),
MAP_VALUE_WRITER, PRIMITIVE_ARRAY_VALUE_WRITER, TABLE_ARRAY_VALUE_WRITER
};
}

View file

@ -19,21 +19,21 @@ public class ArrayTest {
@Test
public void should_get_array() throws Exception {
Toml toml = new Toml().parse("list = [\"a\", \"b\", \"c\"]");
Toml toml = new Toml().read("list = [\"a\", \"b\", \"c\"]");
assertEquals(asList("a", "b", "c"), toml.<String>getList("list"));
}
@Test
public void should_return_null_if_no_value_for_key() throws Exception {
Toml toml = new Toml().parse("");
Toml toml = new Toml().read("");
assertNull(toml.getList("a"));
}
@Test
public void should_allow_multiline_array() throws Exception {
Toml toml = new Toml().parse(file("should_allow_multiline_array"));
Toml toml = new Toml().read(file("should_allow_multiline_array"));
assertEquals(asList("a", "b", "c"), toml.<String>getList("a"));
}
@ -41,14 +41,14 @@ public class ArrayTest {
@Test
@SuppressWarnings("unchecked")
public void should_get_nested_arrays() throws Exception {
Toml clients = new Toml().parse("data = [ [\"gamma\", \"delta\"], [1, 2]] # just an update to make sure parsers support it");
Toml clients = new Toml().read("data = [ [\"gamma\", \"delta\"], [1, 2]] # just an update to make sure parsers support it");
assertEquals(asList(asList("gamma", "delta"), asList(1L, 2L)), clients.<String>getList("data"));
}
@Test
public void should_get_deeply_nested_arrays() throws Exception {
List<List<?>> data = new Toml().parse("data = [[[1], [2]], [3, 4]]").getList("data");
List<List<?>> data = new Toml().read("data = [[[1], [2]], [3, 4]]").getList("data");
assertThat(data, hasSize(2));
assertEquals(Arrays.asList(1L), data.get(0).get(0));
@ -59,35 +59,35 @@ public class ArrayTest {
@Test
@SuppressWarnings("unchecked")
public void should_get_nested_arrays_with_no_space_between_outer_and_inner_array() throws Exception {
Toml clients = new Toml().parse("data = [[\"gamma\", \"delta\"], [1, 2]] # just an update to make sure parsers support it");
Toml clients = new Toml().read("data = [[\"gamma\", \"delta\"], [1, 2]] # just an update to make sure parsers support it");
assertEquals(asList(asList("gamma", "delta"), asList(1L, 2L)), clients.<String>getList("data"));
}
@Test
public void should_ignore_comma_at_end_of_array() throws Exception {
Toml toml = new Toml().parse("key=[1,2,3,]");
Toml toml = new Toml().read("key=[1,2,3,]");
assertEquals(asList(1L, 2L, 3L), toml.<Long>getList("key"));
}
@Test
public void should_support_mixed_string_types() throws Exception {
Toml toml = new Toml().parse("key = [\"a\", 'b', \"\"\"c\"\"\", '''d''']");
Toml toml = new Toml().read("key = [\"a\", 'b', \"\"\"c\"\"\", '''d''']");
assertThat(toml.<String>getList("key"), contains("a", "b", "c", "d"));
}
@Test
public void should_support_array_terminator_in_strings() throws Exception {
Toml toml = new Toml().parse("key = [\"a]\", 'b]', \"\"\"c]\"\"\", '''d]''']");
Toml toml = new Toml().read("key = [\"a]\", 'b]', \"\"\"c]\"\"\", '''d]''']");
assertThat(toml.<String>getList("key"), contains("a]", "b]", "c]", "d]"));
}
@Test
public void should_support_array_of_inline_tables() throws Exception {
Toml toml = new Toml().parse(getClass().getResourceAsStream("should_support_array_of_inline_tables.toml"));
Toml toml = new Toml().read(getClass().getResourceAsStream("should_support_array_of_inline_tables.toml"));
assertThat(toml.getList("points"), hasSize(4));
assertEquals(1, toml.getLong("points[0].x").longValue());

View file

@ -8,116 +8,116 @@ public class BareKeysTest {
@Test
public void should_ignore_spaces_around_key_segments() throws Exception {
Toml toml = new Toml().parse("[ a . b . c ] \n key = \"a\"");
Toml toml = new Toml().read("[ a . b . c ] \n key = \"a\"");
assertEquals("a", toml.getString("a.b.c.key"));
}
@Test
public void should_support_underscores_in_key_names() throws Exception {
Toml toml = new Toml().parse("a_a = 1");
Toml toml = new Toml().read("a_a = 1");
assertEquals(1, toml.getLong("a_a").intValue());
}
@Test
public void should_support_underscores_in_table_names() throws Exception {
Toml toml = new Toml().parse("[group_a]\na = 1");
Toml toml = new Toml().read("[group_a]\na = 1");
assertEquals(1, toml.getLong("group_a.a").intValue());
}
@Test
public void should_support_numbers_in_key_names() throws Exception {
Toml toml = new Toml().parse("a1 = 1");
Toml toml = new Toml().read("a1 = 1");
assertEquals(1, toml.getLong("a1").intValue());
}
@Test
public void should_support_numbers_in_table_names() throws Exception {
Toml toml = new Toml().parse("[group1]\na = 1");
Toml toml = new Toml().read("[group1]\na = 1");
assertEquals(1, toml.getLong("group1.a").intValue());
}
@Test(expected = IllegalStateException.class)
public void should_fail_when_characters_outside_accept_range_are_used_in_table_name() throws Exception {
new Toml().parse("[~]");
new Toml().read("[~]");
}
@Test(expected = IllegalStateException.class)
public void should_fail_when_characters_outside_accept_range_are_used_in_table_array_name() throws Exception {
new Toml().parse("[[~]]");
new Toml().read("[[~]]");
}
@Test(expected = IllegalStateException.class)
public void should_fail_when_dots_in_key_name() throws Exception {
new Toml().parse("a.b = 1");
new Toml().read("a.b = 1");
}
@Test(expected = IllegalStateException.class)
public void should_fail_when_characters_outside_accept_range_are_used_in_key_name() throws Exception {
new Toml().parse("~ = 1");
new Toml().read("~ = 1");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_sharp_sign_in_table_name() throws Exception {
new Toml().parse("[group#]\nkey=1");
new Toml().read("[group#]\nkey=1");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_spaces_in_table_name() throws Exception {
new Toml().parse("[valid key]");
new Toml().read("[valid key]");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_sharp_sign_in_table_array_name() throws Exception {
new Toml().parse("[[group#]]\nkey=1");
new Toml().read("[[group#]]\nkey=1");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_spaces_in_table_array_name() throws Exception {
new Toml().parse("[[valid key]]");
new Toml().read("[[valid key]]");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_question_marks_in_key_name() throws Exception {
new Toml().parse("key?=true");
new Toml().read("key?=true");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_empty_table_name() {
new Toml().parse("[]");
new Toml().read("[]");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_nested_table_name_ending_with_empty_table_name() {
new Toml().parse("[a.]");
new Toml().read("[a.]");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_nested_table_name_containing_empty_table_name() {
new Toml().parse("[a..b]");
new Toml().read("[a..b]");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_nested_table_name_starting_with_empty_table_name() {
new Toml().parse("[.b]");
new Toml().read("[.b]");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_nested_table_array_name_ending_with_empty_table_name() {
new Toml().parse("[[a.]]");
new Toml().read("[[a.]]");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_nested_table_array_name_containing_empty_table_name() {
new Toml().parse("[[a..b]]");
new Toml().read("[[a..b]]");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_nested_table_array_name_starting_with_empty_table_name() {
new Toml().parse("[[.b]]");
new Toml().read("[[.b]]");
}
}

View file

@ -9,7 +9,7 @@ public class BooleanTest {
@Test
public void should_get_boolean() throws Exception {
Toml toml = new Toml().parse("bool_false = false\nbool_true = true");
Toml toml = new Toml().read("bool_false = false\nbool_true = true");
assertFalse(toml.getBoolean("bool_false"));
assertTrue(toml.getBoolean("bool_true"));
@ -17,11 +17,11 @@ public class BooleanTest {
@Test(expected = IllegalStateException.class)
public void should_fail_on_invalid_boolean_true() {
new Toml().parse("answer = true abc");
new Toml().read("answer = true abc");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_invalid_boolean_false() {
new Toml().parse("answer = false abc");
new Toml().read("answer = false abc");
}
}

View file

@ -209,7 +209,7 @@ public class BurntSushiInvalidTest {
inputToml = getClass().getResourceAsStream("burntsushi/invalid/" + testName + ".toml");
try {
new Toml().parse(inputToml);
new Toml().read(inputToml);
Assert.fail("Should have rejected invalid input!");
} catch (IllegalStateException e) {
// success

View file

@ -252,7 +252,7 @@ public class BurntSushiValidTest {
Reader expectedJsonReader = new InputStreamReader(getClass().getResourceAsStream("burntsushi/valid/" + testName + ".json"));
JsonElement expectedJson = GSON.fromJson(expectedJsonReader, JsonElement.class);
Toml toml = new Toml().parse(inputToml);
Toml toml = new Toml().read(inputToml);
JsonElement actual = TEST_GSON.toJsonTree(toml).getAsJsonObject().get("values");
assertEquals(expectedJson, actual);

View file

@ -9,7 +9,7 @@ public class ContainsTest {
@Test
public void should_contain_top_level_of_any_type() throws Exception {
Toml toml = new Toml().parse("a = 1 \n [b] \n b1 = 1 \n [[c]] \n c1 = 1");
Toml toml = new Toml().read("a = 1 \n [b] \n b1 = 1 \n [[c]] \n c1 = 1");
assertTrue(toml.contains("a"));
assertTrue(toml.contains("b"));
@ -19,7 +19,7 @@ public class ContainsTest {
@Test
public void should_contain_nested_of_any_type() throws Exception {
Toml toml = new Toml().parse("[a] \n a1 = 1 \n [[b]] \n b1 = 1 \n [[b]] \n b1 = 2 \n b2 = 3");
Toml toml = new Toml().read("[a] \n a1 = 1 \n [[b]] \n b1 = 1 \n [[b]] \n b1 = 2 \n b2 = 3");
assertTrue(toml.contains("a.a1"));
assertTrue(toml.contains("b[0].b1"));
@ -30,7 +30,7 @@ public class ContainsTest {
@Test
public void should_contain_key() throws Exception {
Toml toml = new Toml().parse("a = 1 \n [b] \n b1 = 1 \n [[c]] \n c1 = 1");
Toml toml = new Toml().read("a = 1 \n [b] \n b1 = 1 \n [[c]] \n c1 = 1");
assertTrue(toml.containsKey("a"));
assertTrue(toml.containsKey("b.b1"));
@ -42,7 +42,7 @@ public class ContainsTest {
@Test
public void should_contain_table() throws Exception {
Toml toml = new Toml().parse("a = 1 \n [b] \n b1 = 1 \n [b.b2] \n [[c]] \n c1 = 1 \n [c.c2]");
Toml toml = new Toml().read("a = 1 \n [b] \n b1 = 1 \n [b.b2] \n [[c]] \n c1 = 1 \n [c.c2]");
assertTrue(toml.containsTable("b"));
assertTrue(toml.containsTable("b.b2"));
@ -56,7 +56,7 @@ public class ContainsTest {
@Test
public void should_contain_table_array() throws Exception {
Toml toml = new Toml().parse("a = 1 \n [b] \n b1 = 1 \n [[c]] \n c1 = 1 \n [c.c2] \n [[c]] \n [[c.c3]] \n c4 = 4");
Toml toml = new Toml().read("a = 1 \n [b] \n b1 = 1 \n [[c]] \n c1 = 1 \n [c.c2] \n [[c]] \n [[c.c3]] \n c4 = 4");
assertTrue(toml.containsTableArray("c"));
assertTrue(toml.containsTableArray("c[1].c3"));
@ -69,7 +69,7 @@ public class ContainsTest {
@Test
public void should_not_contain_when_parent_table_is_missing() throws Exception {
Toml toml = new Toml().parse("a = \"1\"");
Toml toml = new Toml().read("a = \"1\"");
assertFalse(toml.contains("b.b1"));
assertFalse(toml.containsKey("b.b1"));

View file

@ -13,7 +13,7 @@ public class DateTest {
@Test
public void should_get_date() throws Exception {
Toml toml = new Toml().parse("a_date = 2011-11-10T13:12:00Z");
Toml toml = new Toml().read("a_date = 2011-11-10T13:12:00Z");
Calendar calendar = Calendar.getInstance(UTC);
calendar.set(2011, Calendar.NOVEMBER, 10, 13, 12, 00);
@ -24,7 +24,7 @@ public class DateTest {
@Test
public void should_get_date_with_offset() throws Exception {
Toml toml = new Toml().parse("a_date = 1979-05-27T00:32:00-07:00");
Toml toml = new Toml().read("a_date = 1979-05-27T00:32:00-07:00");
Calendar calendar = Calendar.getInstance(UTC);
calendar.set(1979, Calendar.MAY, 27, 7, 32, 00);
@ -35,7 +35,7 @@ public class DateTest {
@Test
public void should_get_date_with_fractional_seconds() throws Exception {
Toml toml = new Toml().parse("a_date = 1979-05-27T00:32:00.999Z");
Toml toml = new Toml().read("a_date = 1979-05-27T00:32:00.999Z");
Calendar calendar = Calendar.getInstance(UTC);
calendar.set(1979, Calendar.MAY, 27, 0, 32, 00);
@ -46,7 +46,7 @@ public class DateTest {
@Test
public void should_get_date_with_fractional_seconds_and_offset() throws Exception {
Toml toml = new Toml().parse("a_date = 1979-05-27T00:32:00.999-07:00");
Toml toml = new Toml().read("a_date = 1979-05-27T00:32:00.999-07:00");
Calendar calendar = Calendar.getInstance(UTC);
calendar.set(1979, Calendar.MAY, 27, 7, 32, 00);
@ -57,6 +57,6 @@ public class DateTest {
@Test(expected = IllegalStateException.class)
public void should_fail_on_non_existant_date() throws Exception {
new Toml().parse("d = 2012-13-01T15:00:00Z");
new Toml().read("d = 2012-13-01T15:00:00Z");
}
}

View file

@ -28,69 +28,69 @@ public class DefaultValueTest {
@Test
public void should_get_string() throws Exception {
Toml toml = new Toml().parse("s = \"string value\"");
Toml toml = new Toml().read("s = \"string value\"");
assertEquals("string value", toml.getString("s", "default string value"));
}
@Test
public void should_get_string_default_value() throws Exception {
Toml toml = new Toml().parse("");
Toml toml = new Toml().read("");
assertEquals("default string value", toml.getString("s", "default string value"));
}
@Test
public void should_get_long() throws Exception {
Toml toml = new Toml().parse("n = 1001");
Toml toml = new Toml().read("n = 1001");
assertEquals(Long.valueOf(1001), toml.getLong("n", 1002L));
}
@Test
public void should_get_long_default_value() throws Exception {
Toml toml = new Toml().parse("");
Toml toml = new Toml().read("");
assertEquals(Long.valueOf(1002), toml.getLong("n", 1002L));
}
@Test
public void should_get_double() throws Exception {
Toml toml = new Toml().parse("n = 0.5");
Toml toml = new Toml().read("n = 0.5");
assertEquals(Double.valueOf(0.5), toml.getDouble("n", Double.valueOf(0.6)));
}
@Test
public void should_get_double_default_value() throws Exception {
Toml toml = new Toml().parse("");
Toml toml = new Toml().read("");
assertEquals(Double.valueOf(0.6), toml.getDouble("n", Double.valueOf(0.6)));
}
@Test
public void should_get_boolean() throws Exception {
Toml toml = new Toml().parse("b = true");
Toml toml = new Toml().read("b = true");
assertEquals(Boolean.TRUE, toml.getBoolean("b", Boolean.FALSE));
}
@Test
public void should_get_boolean_default_value() throws Exception {
Toml toml = new Toml().parse("");
Toml toml = new Toml().read("");
assertEquals(Boolean.FALSE, toml.getBoolean("b", Boolean.FALSE));
}
@Test
public void should_get_date() throws Exception {
Toml toml = new Toml().parse("d = 2011-11-10T13:12:00Z");
Toml toml = new Toml().read("d = 2011-11-10T13:12:00Z");
assertEquals(_2011_11_10, toml.getDate("d", _2012_11_10));
}
@Test
public void should_get_date_default_value() throws Exception {
Toml toml = new Toml().parse("");
Toml toml = new Toml().read("");
assertEquals(_2012_11_10, toml.getDate("d", _2012_11_10));
}
@Test
public void should_get_array() throws Exception {
Toml toml = new Toml().parse("a = [1, 2, 3]\n b = []");
Toml toml = new Toml().read("a = [1, 2, 3]\n b = []");
assertEquals(asList(1L, 2L, 3L), toml.getList("a", asList(3L, 2L, 1L)));
assertEquals(Collections.emptyList(), toml.getList("b", asList(3L, 2L, 1L)));
@ -98,7 +98,7 @@ public class DefaultValueTest {
@Test
public void should_get_empty_array() throws Exception {
Toml toml = new Toml().parse("a = []");
Toml toml = new Toml().read("a = []");
assertEquals(Collections.emptyList(), toml.getList("a", asList(3L, 2L, 1L)));
}
@ -112,8 +112,8 @@ public class DefaultValueTest {
@Test
public void should_prefer_default_from_constructor() throws Exception {
Toml defaults = new Toml().parse("n = 1\n d = 1.1\n b = true\n date = 2011-11-10T13:12:00Z\n s = 'a'\n a = [1, 2, 3]");
Toml toml = new Toml(defaults).parse("");
Toml defaults = new Toml().read("n = 1\n d = 1.1\n b = true\n date = 2011-11-10T13:12:00Z\n s = 'a'\n a = [1, 2, 3]");
Toml toml = new Toml(defaults).read("");
assertEquals(1, toml.getLong("n", 2L).intValue());
assertEquals(1.1, toml.getDouble("d", 2.2), 0);

View file

@ -13,125 +13,125 @@ public class ErrorMessagesTest {
public void invalid_table() throws Exception {
e.expectMessage("Invalid table definition on line 1: [in valid]");
new Toml().parse("[in valid]");
new Toml().read("[in valid]");
}
@Test
public void duplicate_table() throws Exception {
e.expectMessage("Duplicate table definition on line 2: [again]");
new Toml().parse("[again]\n[again]");
new Toml().read("[again]\n[again]");
}
@Test
public void empty_implicit_table_name() throws Exception {
e.expectMessage("Invalid table definition due to empty implicit table name: [a..b]");
new Toml().parse("[a..b]");
new Toml().read("[a..b]");
}
@Test
public void duplicate_key() throws Exception {
e.expectMessage("Duplicate key on line 2: k");
new Toml().parse("k = 1\n k = 2");
new Toml().read("k = 1\n k = 2");
}
@Test
public void invalid_key() throws Exception {
e.expectMessage("Key is not followed by an equals sign on line 1: k\" = 1");
new Toml().parse("k\" = 1");
new Toml().read("k\" = 1");
}
@Test
public void invalid_table_array() throws Exception {
e.expectMessage("Invalid table array definition on line 1: [[in valid]]");
new Toml().parse("[[in valid]]");
new Toml().read("[[in valid]]");
}
@Test
public void invalid_value() throws Exception {
e.expectMessage("Invalid text after key k on line 1");
new Toml().parse("k = 1 t");
new Toml().read("k = 1 t");
}
@Test
public void unterminated_multiline_literal_string() throws Exception {
e.expectMessage("Unterminated value on line 1: k = '''abc");
new Toml().parse("k = '''abc");
new Toml().read("k = '''abc");
}
@Test
public void unterminated_multiline_string() throws Exception {
e.expectMessage("Unterminated value on line 1: k = \"\"\"abc\"\"");
new Toml().parse("k = \"\"\"abc\"\"");
new Toml().read("k = \"\"\"abc\"\"");
}
@Test
public void unterminated_array() throws Exception {
e.expectMessage("Unterminated value on line 1: k = [\"abc\"");
new Toml().parse("k = [\"abc\"");
new Toml().read("k = [\"abc\"");
}
@Test
public void unterminated_inline_table() throws Exception {
e.expectMessage("Unterminated value on line 1: k = { a = \"abc\"");
new Toml().parse("k = { a = \"abc\"");
new Toml().read("k = { a = \"abc\"");
}
@Test
public void key_without_equals() throws Exception {
e.expectMessage("Key is not followed by an equals sign on line 2: k");
new Toml().parse("\nk\n=3");
new Toml().read("\nk\n=3");
}
@Test
public void heterogeneous_array() throws Exception {
e.expectMessage("k becomes a heterogeneous array on line 2");
new Toml().parse("k = [ 1,\n 1.1 ]");
new Toml().read("k = [ 1,\n 1.1 ]");
}
@Test
public void key_in_root_is_overwritten_by_table() throws Exception {
e.expectMessage("Key already exists for table defined on line 2: [a]");
new Toml().parse("a=1\n [a]");
new Toml().read("a=1\n [a]");
}
@Test
public void table_is_overwritten_by_key() throws Exception {
e.expectMessage("Table already exists for key defined on line 3: b");
new Toml().parse("[a.b]\n [a]\n b=1");
new Toml().read("[a.b]\n [a]\n b=1");
}
@Test
public void should_display_correct_line_number_with_literal_multiline_string() throws Exception {
e.expectMessage("on line 8");
new Toml().parse("[table]\n\n k = '''abc\n\ndef\n'''\n # comment \n j = 4.\n l = 5");
new Toml().read("[table]\n\n k = '''abc\n\ndef\n'''\n # comment \n j = 4.\n l = 5");
}
@Test
public void should_display_correct_line_number_with_multiline_string() throws Exception {
e.expectMessage("on line 9");
new Toml().parse("[table]\n\n k = \"\"\"\nabc\n\ndef\n\"\"\"\n # comment \n j = 4.\n l = 5");
new Toml().read("[table]\n\n k = \"\"\"\nabc\n\ndef\n\"\"\"\n # comment \n j = 4.\n l = 5");
}
@Test
public void should_display_correct_line_number_with_array() throws Exception {
e.expectMessage("on line 10");
new Toml().parse("[table]\n\n k = [\"\"\"\nabc\n\ndef\n\"\"\"\n, \n # comment \n j = 4.,\n l = 5\n]");
new Toml().read("[table]\n\n k = [\"\"\"\nabc\n\ndef\n\"\"\"\n, \n # comment \n j = 4.,\n l = 5\n]");
}
}

View file

@ -24,14 +24,14 @@ public class InlineTableTest {
@Test
public void should_read_empty_inline_table() throws Exception {
Toml toml = new Toml().parse("key = {}");
Toml toml = new Toml().read("key = {}");
assertTrue(toml.getTable("key").isEmpty());
}
@Test
public void should_read_inline_table_with_strings() throws Exception {
Toml toml = new Toml().parse("name = { first = \"Tom\", last = \"Preston-Werner\"}");
Toml toml = new Toml().read("name = { first = \"Tom\", last = \"Preston-Werner\"}");
assertEquals("Tom", toml.getTable("name").getString("first"));
assertEquals("Preston-Werner", toml.getString("name.last"));
@ -39,7 +39,7 @@ public class InlineTableTest {
@Test
public void should_read_inline_table_with_integers() throws Exception {
Toml toml = new Toml().parse("point = { x = 1, y = 2 }");
Toml toml = new Toml().read("point = { x = 1, y = 2 }");
assertEquals(1, toml.getTable("point").getLong("x").longValue());
assertEquals(2, toml.getLong("point.y").longValue());
@ -47,7 +47,7 @@ public class InlineTableTest {
@Test
public void should_read_inline_table_with_floats() throws Exception {
Toml toml = new Toml().parse("point = { x = 1.5, y = 2.3 }");
Toml toml = new Toml().read("point = { x = 1.5, y = 2.3 }");
assertEquals(1.5, toml.getTable("point").getDouble("x").doubleValue(), 0);
assertEquals(2.3, toml.getDouble("point.y").doubleValue(), 0);
@ -55,7 +55,7 @@ public class InlineTableTest {
@Test
public void should_read_inline_table_with_booleans() throws Exception {
Toml toml = new Toml().parse("point = { x = false, y = true }");
Toml toml = new Toml().read("point = { x = false, y = true }");
assertTrue(toml.getTable("point").getBoolean("y"));
assertFalse(toml.getBoolean("point.x"));
@ -63,7 +63,7 @@ public class InlineTableTest {
@Test
public void should_read_inline_table_with_dates() throws Exception {
Toml toml = new Toml().parse("point = { x = 2015-02-09T22:05:00Z, y = 2015-02-09T21:05:00Z }");
Toml toml = new Toml().read("point = { x = 2015-02-09T22:05:00Z, y = 2015-02-09T21:05:00Z }");
Calendar x = Calendar.getInstance(UTC);
x.set(2015, Calendar.FEBRUARY, 9, 22, 5, 00);
@ -79,7 +79,7 @@ public class InlineTableTest {
@Test
public void should_read_arrays() throws Exception {
Toml toml = new Toml().parse("arrays = { integers = [1, 2, 3], strings = [\"a\", \"b\", \"c\"] }");
Toml toml = new Toml().read("arrays = { integers = [1, 2, 3], strings = [\"a\", \"b\", \"c\"] }");
assertThat(toml.<Long>getList("arrays.integers"), contains(1L, 2L, 3L));
assertThat(toml.<String>getList("arrays.strings"), contains("a", "b", "c"));
@ -87,7 +87,7 @@ public class InlineTableTest {
@Test
public void should_read_nested_arrays() throws Exception {
Toml toml = new Toml().parse("arrays = { nested = [[1, 2, 3], [4, 5, 6]] }").getTable("arrays");
Toml toml = new Toml().read("arrays = { nested = [[1, 2, 3], [4, 5, 6]] }").getTable("arrays");
List<List<Long>> nested = toml.<List<Long>>getList("nested");
assertThat(nested, hasSize(2));
@ -97,7 +97,7 @@ public class InlineTableTest {
@Test
public void should_read_mixed_inline_table() throws Exception {
Toml toml = new Toml().parse("point = { date = 2015-02-09T22:05:00Z, bool = true, integer = 123, float = 123.456, string = \"abc\", list = [5, 6, 7, 8] }").getTable("point");
Toml toml = new Toml().read("point = { date = 2015-02-09T22:05:00Z, bool = true, integer = 123, float = 123.456, string = \"abc\", list = [5, 6, 7, 8] }").getTable("point");
Calendar date = Calendar.getInstance(UTC);
@ -114,7 +114,7 @@ public class InlineTableTest {
@Test
public void should_read_nested_inline_tables() throws Exception {
Toml tables = new Toml().parse("tables = { t1 = { t1_1 = 1, t1_2 = 2}, t2 = { t2_1 = { t2_1_1 = \"a\" }} }").getTable("tables");
Toml tables = new Toml().read("tables = { t1 = { t1_1 = 1, t1_2 = 2}, t2 = { t2_1 = { t2_1_1 = \"a\" }} }").getTable("tables");
assertEquals(1L, tables.getLong("t1.t1_1").longValue());
assertEquals(2L, tables.getLong("t1.t1_2").longValue());
@ -123,7 +123,7 @@ public class InlineTableTest {
@Test
public void should_read_all_string_types() throws Exception {
Toml strings = new Toml().parse("strings = { literal = 'ab]\"c', multiline = \"\"\"de]\"f\"\"\", multiline_literal = '''gh]\"i''' }").getTable("strings");
Toml strings = new Toml().read("strings = { literal = 'ab]\"c', multiline = \"\"\"de]\"f\"\"\", multiline_literal = '''gh]\"i''' }").getTable("strings");
assertEquals("ab]\"c", strings.getString("literal"));
assertEquals("de]\"f", strings.getString("multiline"));
@ -132,14 +132,14 @@ public class InlineTableTest {
@Test
public void should_read_inline_table_in_regular_table() throws Exception {
Toml toml = new Toml().parse("[tbl]\n tbl = { tbl = 1 }");
Toml toml = new Toml().read("[tbl]\n tbl = { tbl = 1 }");
assertEquals(1, toml.getLong("tbl.tbl.tbl").intValue());
}
@Test
public void should_mix_with_tables() throws Exception {
Toml toml = new Toml().parse("t = { k = 1 }\n [b]\n k = 2\n t = { k = 3}");
Toml toml = new Toml().read("t = { k = 1 }\n [b]\n k = 2\n t = { k = 3}");
assertEquals(1, toml.getLong("t.k").intValue());
assertEquals(2, toml.getLong("b.k").intValue());
@ -148,7 +148,7 @@ public class InlineTableTest {
@Test
public void should_add_properties_to_existing_inline_table() throws Exception {
Toml toml = new Toml().parse("[a]\n b = {k = 1}\n [a.b.c]\n k = 2");
Toml toml = new Toml().read("[a]\n b = {k = 1}\n [a.b.c]\n k = 2");
assertEquals(1, toml.getLong("a.b.k").intValue());
assertEquals(2, toml.getLong("a.b.c.k").intValue());
@ -156,7 +156,7 @@ public class InlineTableTest {
@Test
public void should_mix_with_table_arrays() throws Exception {
Toml toml = new Toml().parse("t = { k = 1 }\n [[b]]\n t = { k = 2 }\n [[b]]\n t = { k = 3 }");
Toml toml = new Toml().read("t = { k = 1 }\n [[b]]\n t = { k = 2 }\n [[b]]\n t = { k = 3 }");
assertEquals(1, toml.getLong("t.k").intValue());
assertEquals(2, toml.getLong("b[0].t.k").intValue());
@ -165,17 +165,17 @@ public class InlineTableTest {
@Test(expected = IllegalStateException.class)
public void should_fail_on_invalid_key() throws Exception {
new Toml().parse("tbl = { a. = 1 }");
new Toml().read("tbl = { a. = 1 }");
}
@Test(expected = IllegalStateException.class)
public void should_fail_when_unterminated() throws Exception {
new Toml().parse("tbl = { a = 1 ");
new Toml().read("tbl = { a = 1 ");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_invalid_value() throws Exception {
new Toml().parse("tbl = { a = abc }");
new Toml().read("tbl = { a = abc }");
}
@Test
@ -183,7 +183,7 @@ public class InlineTableTest {
e.expect(IllegalStateException.class);
e.expectMessage("Duplicate key on line 1: a");
new Toml().parse("tbl = { a = 1, a = 2 }");
new Toml().read("tbl = { a = 1, a = 2 }");
}
@Test
@ -191,7 +191,7 @@ public class InlineTableTest {
e.expect(IllegalStateException.class);
e.expectMessage("Table already exists for key defined on line 2: tbl");
new Toml().parse("tbl = { a = 1 }\n tbl = 1");
new Toml().read("tbl = { a = 1 }\n tbl = 1");
}
@Test
@ -199,7 +199,7 @@ public class InlineTableTest {
e.expect(IllegalStateException.class);
e.expectMessage("Duplicate table definition on line 2: [tbl]");
new Toml().parse("tbl = { a = 1 }\n tbl = {}");
new Toml().read("tbl = { a = 1 }\n tbl = {}");
}
@Test
@ -207,7 +207,7 @@ public class InlineTableTest {
e.expect(IllegalStateException.class);
e.expectMessage("Duplicate table definition on line 2: [tbl]");
new Toml().parse("tbl = {}\n [tbl]");
new Toml().read("tbl = {}\n [tbl]");
}
@Test
@ -215,7 +215,7 @@ public class InlineTableTest {
e.expect(IllegalStateException.class);
e.expectMessage("Duplicate table definition on line 3: [a.b]");
new Toml().parse("[a.b]\n [a]\n b = {}");
new Toml().read("[a.b]\n [a]\n b = {}");
}
@Test
@ -223,7 +223,7 @@ public class InlineTableTest {
e.expect(IllegalStateException.class);
e.expectMessage("Duplicate table definition on line 3: [a.b]");
new Toml().parse("[a.b]\n [a]\n b = {}");
new Toml().read("[a.b]\n [a]\n b = {}");
}
@Test
@ -231,6 +231,6 @@ public class InlineTableTest {
e.expect(IllegalStateException.class);
e.expectMessage("Duplicate table definition on line 3: [a.b]");
new Toml().parse("[a]\n b = {}\n [a.b]");
new Toml().read("[a]\n b = {}\n [a.b]");
}
}

View file

@ -29,7 +29,7 @@ public class IterationTest {
@Test
public void should_iterate_over_primitive() throws Exception {
Toml toml = new Toml().parse(file("long"));
Toml toml = new Toml().read(file("long"));
Map.Entry<String, Object> entry = toml.entrySet().iterator().next();
assertEquals("long", entry.getKey());
@ -39,7 +39,7 @@ public class IterationTest {
@Test
@SuppressWarnings("unchecked")
public void should_iterate_over_list() throws Exception {
Toml toml = new Toml().parse(file("list"));
Toml toml = new Toml().read(file("list"));
Map.Entry<String, Object> entry = toml.entrySet().iterator().next();
assertEquals("list", entry.getKey());
@ -49,7 +49,7 @@ public class IterationTest {
@Test
@SuppressWarnings("unchecked")
public void should_iterate_over_empty_list() throws Exception {
Toml toml = new Toml().parse("list = []");
Toml toml = new Toml().read("list = []");
Map.Entry<String, Object> entry = toml.entrySet().iterator().next();
assertEquals("list", entry.getKey());
@ -58,7 +58,7 @@ public class IterationTest {
@Test
public void should_iterate_over_table() throws Exception {
Toml toml = new Toml().parse(file("table"));
Toml toml = new Toml().read(file("table"));
Map.Entry<String, Object> entry = toml.entrySet().iterator().next();
assertEquals("table", entry.getKey());
@ -68,7 +68,7 @@ public class IterationTest {
@Test
@SuppressWarnings("unchecked")
public void should_iterate_over_table_array() throws Exception {
Toml toml = new Toml().parse(file("table_array"));
Toml toml = new Toml().read(file("table_array"));
Map.Entry<String, Object> entry = toml.entrySet().iterator().next();
List<Toml> tableArray = (List<Toml>) entry.getValue();
@ -80,7 +80,7 @@ public class IterationTest {
@Test
@SuppressWarnings("unchecked")
public void should_iterate_over_multiple_entries() throws Exception {
Toml toml = new Toml().parse(file("multiple"));
Toml toml = new Toml().read(file("multiple"));
Map<String, Object> entries = new HashMap<String, Object>();
for (Map.Entry<String, Object> entry : toml.entrySet()) {
@ -96,7 +96,7 @@ public class IterationTest {
@Test
public void should_not_support_setValue_method() throws Exception {
Map.Entry<String, Object> entry = new Toml().parse("a = 1").entrySet().iterator().next();
Map.Entry<String, Object> entry = new Toml().read("a = 1").entrySet().iterator().next();
expectedException.expect(UnsupportedOperationException.class);
entry.setValue(2L);
@ -104,7 +104,7 @@ public class IterationTest {
@Test
public void should_not_iterate_over_empty_toml() throws Exception {
Toml toml = new Toml().parse("");
Toml toml = new Toml().read("");
assertThat(toml.entrySet(), empty());
}

View file

@ -7,49 +7,49 @@ import org.junit.Test;
public class NumberTest {
@Test
public void should_get_number() throws Exception {
Toml toml = new Toml().parse("b = 1001");
Toml toml = new Toml().read("b = 1001");
assertEquals(1001, toml.getLong("b").intValue());
}
@Test
public void should_get_negative_number() throws Exception {
Toml toml = new Toml().parse("b = -1001");
Toml toml = new Toml().read("b = -1001");
assertEquals(-1001, toml.getLong("b").intValue());
}
@Test
public void should_get_number_with_plus_sign() throws Exception {
Toml toml = new Toml().parse("a = +1001\nb = 1001");
Toml toml = new Toml().read("a = +1001\nb = 1001");
assertEquals(toml.getLong("b"), toml.getLong("a"));
}
@Test
public void should_get_double() throws Exception {
Toml toml = new Toml().parse("double = 5.25");
Toml toml = new Toml().read("double = 5.25");
assertEquals(5.25D, toml.getDouble("double").doubleValue(), 0.0);
}
@Test
public void should_get_negative_double() throws Exception {
Toml toml = new Toml().parse("double = -5.25");
Toml toml = new Toml().read("double = -5.25");
assertEquals(-5.25D, toml.getDouble("double").doubleValue(), 0.0);
}
@Test
public void should_get_double_with_a_plus_sign() throws Exception {
Toml toml = new Toml().parse("double = +5.25");
Toml toml = new Toml().read("double = +5.25");
assertEquals(5.25D, toml.getDouble("double").doubleValue(), 0.0);
}
@Test
public void should_get_exponent() throws Exception {
Toml toml = new Toml().parse("lower_case = 1e6\nupper_case = 2E6\nwith_plus = 5e+22\nboth_plus = +5E+22\nnegative = -2E-2\nfractional = 6.626e-34");
Toml toml = new Toml().read("lower_case = 1e6\nupper_case = 2E6\nwith_plus = 5e+22\nboth_plus = +5E+22\nnegative = -2E-2\nfractional = 6.626e-34");
assertEquals(1e6, toml.getDouble("lower_case"), 0.0);
assertEquals(2E6, toml.getDouble("upper_case"), 0.0);
@ -61,150 +61,150 @@ public class NumberTest {
@Test
public void should_get_integer_with_underscores() throws Exception {
Toml toml = new Toml().parse("val = 100_000_000");
Toml toml = new Toml().read("val = 100_000_000");
assertEquals(100000000L, toml.getLong("val").intValue());
}
@Test
public void should_get_float_with_underscores() throws Exception {
Toml toml = new Toml().parse("val = 100_000.123_456");
Toml toml = new Toml().read("val = 100_000.123_456");
assertEquals(100000.123456, toml.getDouble("val").doubleValue(), 0);
}
@Test
public void should_get_exponent_with_underscores() throws Exception {
Toml toml = new Toml().parse("val = 1_5e1_00");
Toml toml = new Toml().read("val = 1_5e1_00");
assertEquals(15e100, toml.getDouble("val").doubleValue(), 0.0);
}
@Test
public void should_accept_irregular_underscores() throws Exception {
Toml toml = new Toml().parse("val = 1_2_3_4_5");
Toml toml = new Toml().read("val = 1_2_3_4_5");
assertEquals(12345L, toml.getLong("val").longValue());
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_invalid_number() throws Exception {
new Toml().parse("a = 200-");
new Toml().read("a = 200-");
}
@Test(expected = IllegalStateException.class)
public void should_fail_when_illegal_characters_after_float() throws Exception {
new Toml().parse("number = 3.14 pi");
new Toml().read("number = 3.14 pi");
}
@Test(expected = IllegalStateException.class)
public void should_fail_when_illegal_characters_after_integer() throws Exception {
new Toml().parse("number = 314 pi");
new Toml().read("number = 314 pi");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_float_without_leading_0() {
new Toml().parse("answer = .12345");
new Toml().read("answer = .12345");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_negative_float_without_leading_0() {
new Toml().parse("answer = -.12345");
new Toml().read("answer = -.12345");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_float_with_sign_after_dot() {
new Toml().parse("answer = 1.-1");
new Toml().parse("answer = 1.+1");
new Toml().read("answer = 1.-1");
new Toml().read("answer = 1.+1");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_float_without_digits_after_dot() {
new Toml().parse("answer = 1.");
new Toml().read("answer = 1.");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_negative_float_without_digits_after_dot() {
new Toml().parse("answer = -1.");
new Toml().read("answer = -1.");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_exponent_without_digits_after_dot() {
new Toml().parse("answer = 1.E1");
new Toml().read("answer = 1.E1");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_negative_exponent_without_digits_after_dot() {
new Toml().parse("answer = -1.E1");
new Toml().read("answer = -1.E1");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_exponent_with_dot_in_exponent_part() {
new Toml().parse("answer = -1E1.0");
new Toml().read("answer = -1E1.0");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_exponent_without_numbers_after_E() {
new Toml().parse("answer = -1E");
new Toml().read("answer = -1E");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_exponent_with_two_E() {
new Toml().parse("answer = -1E1E1");
new Toml().read("answer = -1E1E1");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_float_with_two_dots() {
new Toml().parse("answer = 1.1.1");
new Toml().read("answer = 1.1.1");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_underscore_at_beginning() {
new Toml().parse("answer = _1");
new Toml().read("answer = _1");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_underscore_at_end() {
new Toml().parse("answer = 1_");
new Toml().read("answer = 1_");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_two_underscores_in_a_row() {
new Toml().parse("answer = 1__1");
new Toml().read("answer = 1__1");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_underscore_after_minus_sign() {
new Toml().parse("answer = -_1");
new Toml().read("answer = -_1");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_underscore_after_plus_sign() {
new Toml().parse("answer = +_1");
new Toml().read("answer = +_1");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_underscore_before_dot() {
new Toml().parse("answer = 1_.1");
new Toml().read("answer = 1_.1");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_underscore_after_dot() {
new Toml().parse("answer = 1._1");
new Toml().read("answer = 1._1");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_underscore_before_E() {
new Toml().parse("answer = 1_E1");
new Toml().read("answer = 1_E1");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_underscore_after_E() {
new Toml().parse("answer = 1E_1");
new Toml().read("answer = 1E_1");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_underscore_followed_by_whitespace() {
new Toml().parse("answer = _ 1");
new Toml().read("answer = _ 1");
}
}

View file

@ -13,7 +13,7 @@ public class QuotedKeysTest {
@Test
public void should_accept_quoted_key_for_value() throws Exception {
Toml toml = new Toml().parse("\"127.0.0.1\" = \"localhost\" \n \"character encoding\" = \"UTF-8\" \n \"ʎǝʞ\" = \"value\"");
Toml toml = new Toml().read("\"127.0.0.1\" = \"localhost\" \n \"character encoding\" = \"UTF-8\" \n \"ʎǝʞ\" = \"value\"");
assertEquals("localhost", toml.getString("\"127.0.0.1\""));
assertEquals("UTF-8", toml.getString("\"character encoding\""));
@ -22,14 +22,14 @@ public class QuotedKeysTest {
@Test
public void should_accept_quoted_key_for_table_name() throws Exception {
Toml toml = new Toml().parse("[\"abc def\"]\n val = 1");
Toml toml = new Toml().read("[\"abc def\"]\n val = 1");
assertEquals(1L, toml.getTable("\"abc def\"").getLong("val").longValue());
}
@Test
public void should_accept_partially_quoted_table_name() throws Exception {
Toml toml = new Toml().parse("[dog.\"tater.man\"] \n type = \"pug0\" \n[dog.tater] \n type = \"pug1\"\n[dog.tater.man] \n type = \"pug2\"");
Toml toml = new Toml().read("[dog.\"tater.man\"] \n type = \"pug0\" \n[dog.tater] \n type = \"pug1\"\n[dog.tater.man] \n type = \"pug2\"");
Toml dogs = toml.getTable("dog");
assertEquals("pug0", dogs.getTable("\"tater.man\"").getString("type"));
@ -42,7 +42,7 @@ public class QuotedKeysTest {
@Test
@SuppressWarnings("unchecked")
public void should_conserve_quoted_key_in_map() throws Exception {
Toml toml = new Toml().parse("[dog.\"tater.man\"] \n type = \"pug0\" \n[dog.tater] \n type = \"pug1\"\n[dog.tater.man] \n type = \"pug2\"");
Toml toml = new Toml().read("[dog.\"tater.man\"] \n type = \"pug0\" \n[dog.tater] \n type = \"pug1\"\n[dog.tater.man] \n type = \"pug2\"");
Toml dogs = toml.getTable("dog");
Map<String, Map<String, Object>> map = dogs.to(Map.class);
@ -54,7 +54,7 @@ public class QuotedKeysTest {
@Test
public void should_convert_quoted_keys_to_map_but_not_to_object_fields() throws Exception {
Quoted quoted = new Toml().parse("\"ʎǝʞ\" = \"value\" \n[map] \n \"ʎǝʞ\" = \"value\"").to(Quoted.class);
Quoted quoted = new Toml().read("\"ʎǝʞ\" = \"value\" \n[map] \n \"ʎǝʞ\" = \"value\"").to(Quoted.class);
assertNull(quoted.ʎǝʞ);
assertEquals("value", quoted.map.get("\"ʎǝʞ\""));
@ -62,7 +62,7 @@ public class QuotedKeysTest {
@Test
public void should_support_table_array_index_with_quoted_key() throws Exception {
Toml toml = new Toml().parse("[[ dog. \" type\" ]] \n name = \"type0\" \n [[dog.\" type\"]] \n name = \"type1\"");
Toml toml = new Toml().read("[[ dog. \" type\" ]] \n name = \"type0\" \n [[dog.\" type\"]] \n name = \"type1\"");
assertEquals("type0", toml.getString("dog.\" type\"[0].name"));
assertEquals("type1", toml.getString("dog.\" type\"[1].name"));
@ -70,14 +70,14 @@ public class QuotedKeysTest {
@Test
public void should_support_table_array_index_with_dot_in_quoted_key() throws Exception {
Toml toml = new Toml().parse("[[ dog. \"a.type\" ]] \n name = \"type0\"");
Toml toml = new Toml().read("[[ dog. \"a.type\" ]] \n name = \"type0\"");
assertEquals("type0", toml.getString("dog.\"a.type\"[0].name"));
}
@Test
public void should_support_quoted_key_containing_square_brackets() throws Exception {
Toml toml = new Toml().parse("[dog.\" type[abc]\"] \n name = \"type0\" \n [dog.\" type[1]\"] \n \"name[]\" = \"type1\"");
Toml toml = new Toml().read("[dog.\" type[abc]\"] \n name = \"type0\" \n [dog.\" type[1]\"] \n \"name[]\" = \"type1\"");
assertEquals("type0", toml.getString("dog.\" type[abc]\".name"));
assertEquals("type1", toml.getString("dog.\" type[1]\".\"name[]\""));
@ -85,28 +85,28 @@ public class QuotedKeysTest {
@Test
public void should_support_quoted_key_containing_escaped_quote() throws Exception {
Toml toml = new Toml().parse("[dog.\"ty\\\"pe\"] \n \"na\\\"me\" = \"type0\"");
Toml toml = new Toml().read("[dog.\"ty\\\"pe\"] \n \"na\\\"me\" = \"type0\"");
assertEquals("type0", toml.getString("dog.\"ty\\\"pe\".\"na\\\"me\""));
}
@Test
public void should_support_fully_quoted_table_name() throws Exception {
Toml toml = new Toml().parse("[\"abc.def\"] \n key = 1");
Toml toml = new Toml().read("[\"abc.def\"] \n key = 1");
assertEquals(1, toml.getLong("\"abc.def\".key").intValue());
}
@Test
public void should_support_whitespace_around_key_segments() throws Exception {
Toml toml = new Toml().parse("[ dog. \"type\". breed ] \n name = \"type0\"");
Toml toml = new Toml().read("[ dog. \"type\". breed ] \n name = \"type0\"");
assertEquals("type0", toml.getString("dog.\"type\".breed.name"));
}
@Test
public void should_support_unicode() throws Exception {
Toml toml = new Toml().parse("[[\"\\u00B1\"]]\n \"\\u00B1\" = \"a\"\n [\"\\u00B11\"]\n \"±\" = 1");
Toml toml = new Toml().read("[[\"\\u00B1\"]]\n \"\\u00B1\" = \"a\"\n [\"\\u00B11\"]\n \"±\" = 1");
assertThat(toml.getTables("\"±\""), hasSize(1));
assertEquals("a", toml.getTables("\"±\"").get(0).getString("\"±\""));
@ -115,17 +115,17 @@ public class QuotedKeysTest {
@Test(expected = IllegalStateException.class)
public void should_fail_on_malformed_quoted_key() throws Exception {
new Toml().parse("k\"ey\" = 1");
new Toml().read("k\"ey\" = 1");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_malformed_quoted_table() throws Exception {
new Toml().parse("[a\"bc\"]");
new Toml().read("[a\"bc\"]");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_malformed_quoted_nested_table() throws Exception {
new Toml().parse("[a.a\"bc\"]");
new Toml().read("[a.a\"bc\"]");
}
private static class Quoted {

View file

@ -21,7 +21,7 @@ public class RealWorldTest {
@SuppressWarnings("unchecked")
@Test
public void should_parse_example() throws Exception {
Toml toml = new Toml().parse(new File(getClass().getResource("example.toml").getFile()));
Toml toml = new Toml().read(new File(getClass().getResource("example.toml").getFile()));
// printMap(root);
@ -60,7 +60,7 @@ public class RealWorldTest {
@Test
public void should_parse_hard_example() throws Exception {
Toml toml = new Toml().parse(new File(getClass().getResource("hard_example.toml").getFile()));
Toml toml = new Toml().read(new File(getClass().getResource("hard_example.toml").getFile()));
assertEquals("You'll hate me after this - #", toml.getString("the.test_string"));
assertEquals(asList("] ", " # "), toml.<String>getList("the.hard.test_array"));
@ -75,7 +75,7 @@ public class RealWorldTest {
@SuppressWarnings("unchecked")
@Test
public void should_parse_current_version_example() throws Exception {
Toml toml = new Toml().parse(new File(getClass().getResource("example-v0.4.0.toml").getFile()));
Toml toml = new Toml().read(new File(getClass().getResource("example-v0.4.0.toml").getFile()));
assertEquals("value", toml.getString("table.key"));
assertEquals("another value", toml.getString("table.subtable.key"));
@ -147,7 +147,7 @@ public class RealWorldTest {
@Test
public void should_allow_keys_with_same_name_in_different_tables() throws Exception {
Toml toml = new Toml().parse(new File(getClass().getResource("should_allow_keys_with_same_name_in_different_tables.toml").getFile()));
Toml toml = new Toml().read(new File(getClass().getResource("should_allow_keys_with_same_name_in_different_tables.toml").getFile()));
assertTrue(toml.getTable("siteInfo.local.sh").getBoolean("enable"));
assertFalse(toml.getTable("siteInfo.localMobile.sh").getBoolean("enable"));

View file

@ -13,26 +13,26 @@ public class StringTest {
@Test
public void should_get_string() throws Exception {
Toml toml = new Toml().parse("a = \"a\"");
Toml toml = new Toml().read("a = \"a\"");
assertEquals("a", toml.getString("a"));
}
@Test
public void should_get_empty_string() {
Toml toml = new Toml().parse("a = \"\"");
Toml toml = new Toml().read("a = \"\"");
assertEquals("", toml.getString("a"));
}
@Test
public void should_get_empty_string_with_trailing_new_line() {
Toml toml = new Toml().parse("a = \"\"\n");
Toml toml = new Toml().read("a = \"\"\n");
assertEquals("", toml.getString("a"));
}
@Test
public void should_get_basic_multiline_string() throws Exception {
Toml toml = new Toml().parse(file("should_get_basic_multiline_string"));
Toml toml = new Toml().read(file("should_get_basic_multiline_string"));
assertEquals(toml.getString("ref"), toml.getString("one_line"));
assertEquals(toml.getString("ref"), toml.getString("many_lines"));
@ -40,7 +40,7 @@ public class StringTest {
@Test
public void should_get_multiline_string_without_new_lines() throws Exception {
Toml toml = new Toml().parse(file("should_get_multiline_string_without_new_lines"));
Toml toml = new Toml().read(file("should_get_multiline_string_without_new_lines"));
assertEquals(toml.getString("ref"), toml.getString("multi1"));
assertEquals(toml.getString("ref"), toml.getString("multi2"));
@ -48,7 +48,7 @@ public class StringTest {
@Test
public void should_get_literal_string() throws Exception {
Toml toml = new Toml().parse(file("should_get_literal_string"));
Toml toml = new Toml().read(file("should_get_literal_string"));
assertEquals("C:\\Users\\nodejs\\templates", toml.getString("winpath"));
assertEquals("\\\\ServerX\\admin$\\system32\\", toml.getString("winpath2"));
@ -58,7 +58,7 @@ public class StringTest {
@Test
public void should_get_multiline_literal_string() throws Exception {
Toml toml = new Toml().parse(file("should_get_multiline_literal_string"));
Toml toml = new Toml().read(file("should_get_multiline_literal_string"));
assertTrue(toml.getString("empty_line").isEmpty());
assertEquals(toml.getString("regex2_ref"), toml.getString("regex2"));
@ -67,14 +67,14 @@ public class StringTest {
@Test
public void should_support_special_characters_in_strings() {
Toml toml = new Toml().parse(new File(getClass().getResource("should_support_special_characters_in_strings.toml").getFile()));
Toml toml = new Toml().read(new File(getClass().getResource("should_support_special_characters_in_strings.toml").getFile()));
assertEquals("\" \t \n \r \\ \b \f", toml.getString("key"));
}
@Test
public void should_support_unicode_characters_in_strings() throws Exception {
Toml toml = new Toml().parse(new File(getClass().getResource("should_support_special_characters_in_strings.toml").getFile()));
Toml toml = new Toml().read(new File(getClass().getResource("should_support_special_characters_in_strings.toml").getFile()));
assertEquals("more or less ±", toml.getString("unicode_key"));
assertEquals("more or less ±", toml.getString("unicode_key_uppercase"));
@ -82,47 +82,47 @@ public class StringTest {
@Test(expected = IllegalStateException.class)
public void should_fail_on_reserved_special_character_in_strings() throws Exception {
new Toml().parse("key=\"\\m\"");
new Toml().read("key=\"\\m\"");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_escaped_slash() throws Exception {
new Toml().parse("key=\"\\/\"");
new Toml().read("key=\"\\/\"");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_text_after_literal_string() {
new Toml().parse("a = ' ' jdkf");
new Toml().read("a = ' ' jdkf");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_unterminated_literal_string() throws Exception {
new Toml().parse("a = 'some text");
new Toml().read("a = 'some text");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_multiline_literal_string_with_malformed_comment() throws Exception {
new Toml().parse("a = '''some\n text\n''\nb = '''1'''");
new Toml().read("a = '''some\n text\n''\nb = '''1'''");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_unterminated_multiline_literal_string() throws Exception {
new Toml().parse("a = '''some\n text\n''");
new Toml().read("a = '''some\n text\n''");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_unterminated_multiline_literal_string_on_single_line() throws Exception {
new Toml().parse("a = '''some text''");
new Toml().read("a = '''some text''");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_text_outside_multiline_string() {
new Toml().parse("a = \"\"\" \"\"\" jdkf");
new Toml().read("a = \"\"\" \"\"\" jdkf");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_unterminated_multiline_string() throws Exception {
new Toml().parse("a = \"\"\"some text\"\"");
new Toml().read("a = \"\"\"some text\"\"");
}
private File file(String file) {

View file

@ -16,7 +16,7 @@ public class TableArrayTest {
@Test
public void should_parse_table_array() throws Exception {
Toml toml = new Toml().parse(file("products_table_array"));
Toml toml = new Toml().read(file("products_table_array"));
List<Toml> products = toml.getTables("products");
@ -35,7 +35,7 @@ public class TableArrayTest {
@Test
public void should_parse_table_array_out_of_order() throws Exception {
Toml toml = new Toml().parse(file("should_parse_table_array_out_of_order"));
Toml toml = new Toml().read(file("should_parse_table_array_out_of_order"));
List<Toml> tables = toml.getTables("product");
List<Toml> employees = toml.getTables("employee");
@ -49,7 +49,7 @@ public class TableArrayTest {
@Test
public void should_parse_nested_table_arrays() throws Exception {
Toml toml = new Toml().parse(file("fruit_table_array"));
Toml toml = new Toml().read(file("fruit_table_array"));
List<Toml> fruits = toml.getTables("fruit");
@ -69,14 +69,14 @@ public class TableArrayTest {
@Test
public void should_create_array_ancestors_as_tables() throws Exception {
Toml toml = new Toml().parse("[[a.b.c]]\n id=3");
Toml toml = new Toml().read("[[a.b.c]]\n id=3");
assertEquals(3, toml.getTable("a").getTable("b").getTables("c").get(0).getLong("id").intValue());
}
@Test
public void should_navigate_array_with_compound_key() throws Exception {
Toml toml = new Toml().parse(file("fruit_table_array"));
Toml toml = new Toml().read(file("fruit_table_array"));
List<Toml> appleVarieties = toml.getTables("fruit[0].variety");
Toml appleVariety = toml.getTable("fruit[0].variety[1]");
@ -90,7 +90,7 @@ public class TableArrayTest {
@Test
public void should_return_null_for_missing_table_array() throws Exception {
Toml toml = new Toml().parse("[a]");
Toml toml = new Toml().read("[a]");
assertNull(toml.getTables("b"));
}
@ -105,14 +105,14 @@ public class TableArrayTest {
@Test
public void should_return_null_for_index_out_of_bounds() throws Exception {
Toml toml = new Toml().parse("[[a]]\n c = 1");
Toml toml = new Toml().read("[[a]]\n c = 1");
assertNull(toml.getTable("a[1]"));
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_empty_table_array_name() {
new Toml().parse("[[]]");
new Toml().read("[[]]");
}
private File file(String fileName) {

View file

@ -10,7 +10,7 @@ public class TableTest {
@Test
public void should_get_table() throws Exception {
Toml toml = new Toml().parse("[group]\nkey = \"value\"");
Toml toml = new Toml().read("[group]\nkey = \"value\"");
Toml group = toml.getTable("group");
@ -19,35 +19,35 @@ public class TableTest {
@Test
public void should_get_value_for_multi_key() throws Exception {
Toml toml = new Toml().parse("[group]\nkey = \"value\"");
Toml toml = new Toml().read("[group]\nkey = \"value\"");
assertEquals("value", toml.getString("group.key"));
}
@Test
public void should_get_value_for_multi_key_with_no_parent_table() throws Exception {
Toml toml = new Toml().parse("[group.sub]\nkey = \"value\"");
Toml toml = new Toml().read("[group.sub]\nkey = \"value\"");
assertEquals("value", toml.getString("group.sub.key"));
}
@Test
public void should_get_table_for_multi_key() throws Exception {
Toml toml = new Toml().parse("[group]\nother=1\n[group.sub]\nkey = \"value\"");
Toml toml = new Toml().read("[group]\nother=1\n[group.sub]\nkey = \"value\"");
assertEquals("value", toml.getTable("group.sub").getString("key"));
}
@Test
public void should_get_table_for_multi_key_with_no_parent_table() throws Exception {
Toml toml = new Toml().parse("[group.sub]\nkey = \"value\"");
Toml toml = new Toml().read("[group.sub]\nkey = \"value\"");
assertEquals("value", toml.getTable("group.sub").getString("key"));
}
@Test
public void should_get_value_from_table_with_sub_table() throws Exception {
Toml toml = new Toml().parse("[a.b]\nc=1\n[a]\nd=2");
Toml toml = new Toml().read("[a.b]\nc=1\n[a]\nd=2");
assertEquals(2, toml.getLong("a.d").intValue());
assertEquals(1, toml.getTable("a.b").getLong("c").intValue());
@ -55,7 +55,7 @@ public class TableTest {
@Test
public void should_get_empty_table() throws Exception {
Toml toml = new Toml().parse("[a]");
Toml toml = new Toml().read("[a]");
assertTrue(toml.getTable("a").isEmpty());
}
@ -75,18 +75,18 @@ public class TableTest {
@Test
public void should_return_null_when_no_value_for_multi_key() throws Exception {
Toml toml = new Toml().parse("");
Toml toml = new Toml().read("");
assertNull(toml.getString("group.key"));
}
@Test(expected = IllegalStateException.class)
public void should_fail_when_table_defined_twice() throws Exception {
new Toml().parse("[a]\nb=1\n[a]\nc=2");
new Toml().read("[a]\nb=1\n[a]\nc=2");
}
@Test(expected = IllegalStateException.class)
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");
new Toml().read("[error] if you didn't catch this, your parser is broken");
}
}

View file

@ -16,7 +16,7 @@ public class TomlDefaultsTest {
@Before
public void before() {
defaultToml = new Toml().parse("a = \"a\"\n [group]\n a=\"a\"\n [[array]]\n b=1\n [[array]]\n b=2");
defaultToml = new Toml().read("a = \"a\"\n [group]\n a=\"a\"\n [[array]]\n b=1\n [[array]]\n b=2");
}
@Test
@ -28,35 +28,35 @@ public class TomlDefaultsTest {
@Test
public void should_use_value_when_present_in_values_and_defaults() {
Toml toml = new Toml(defaultToml).parse("a = \"b\"");
Toml toml = new Toml(defaultToml).read("a = \"b\"");
assertEquals("b", toml.getString("a"));
}
@Test
public void should_return_null_when_no_defaults_for_key() throws Exception {
Toml toml = new Toml(defaultToml).parse("");
Toml toml = new Toml(defaultToml).read("");
assertNull(toml.getString("b"));
}
@Test
public void should_fall_back_to_default_with_multi_key() throws Exception {
Toml toml = new Toml(defaultToml).parse("");
Toml toml = new Toml(defaultToml).read("");
assertEquals("a", toml.getString("group.a"));
}
@Test
public void should_fall_back_to_table() throws Exception {
Toml toml = new Toml(defaultToml).parse("");
Toml toml = new Toml(defaultToml).read("");
assertEquals("a", toml.getTable("group").getString("a"));
}
@Test
public void should_fall_back_to_table_array() throws Exception {
Toml toml = new Toml(defaultToml).parse("");
Toml toml = new Toml(defaultToml).read("");
assertThat(toml.getTables("array"), hasSize(2));
assertThat(toml.getLong("array[1].b"), Matchers.equalTo(2L));
@ -64,8 +64,8 @@ public class TomlDefaultsTest {
@Test
public void should_perform_shallow_merge() throws Exception {
Toml toml = new Toml(defaultToml).parse("[group]\nb=1\n [[array]]\n b=0");
Toml toml2 = new Toml(defaultToml).parse("[[array]]\n b=1\n [[array]]\n b=2\n [[array]]\n b=3");
Toml toml = new Toml(defaultToml).read("[group]\nb=1\n [[array]]\n b=0");
Toml toml2 = new Toml(defaultToml).read("[[array]]\n b=1\n [[array]]\n b=2\n [[array]]\n b=3");
assertEquals(1, toml.getTable("group").getLong("b").intValue());
assertNull(toml.getTable("group").getString("a"));

View file

@ -17,14 +17,14 @@ public class TomlParseTest {
@Test
public void should_parse_input_stream() throws Exception {
Toml toml = new Toml().parse(getClass().getResourceAsStream("should_load_from_file.toml"));
Toml toml = new Toml().read(getClass().getResourceAsStream("should_load_from_file.toml"));
assertEquals("value", toml.getString("key"));
}
@Test
public void should_parse_reader() throws Exception {
Toml toml = new Toml().parse(new StringReader("key=1"));
Toml toml = new Toml().read(new StringReader("key=1"));
assertEquals(1, toml.getLong("key").intValue());
}
@ -32,7 +32,7 @@ public class TomlParseTest {
@Test
public void should_fail_on_missing_file() throws Exception {
try {
new Toml().parse(new File("missing"));
new Toml().read(new File("missing"));
fail("Exception should have been thrown");
} catch (RuntimeException e) {
assertThat(e.getCause(), instanceOf(FileNotFoundException.class));
@ -53,7 +53,7 @@ public class TomlParseTest {
};
try {
new Toml().parse(readerThatThrows);
new Toml().read(readerThatThrows);
fail("Exception should have been thrown");
} catch (RuntimeException e) {
assertThat(e.getCause(), instanceOf(IOException.class));

View file

@ -22,28 +22,28 @@ public class TomlTest {
@Test
public void should_return_null_if_no_value_for_key() throws Exception {
Toml toml = new Toml().parse("");
Toml toml = new Toml().read("");
assertNull(toml.getString("a"));
}
@Test
public void should_load_from_file() throws Exception {
Toml toml = new Toml().parse(new File(getClass().getResource("should_load_from_file.toml").getFile()));
Toml toml = new Toml().read(new File(getClass().getResource("should_load_from_file.toml").getFile()));
assertEquals("value", toml.getString("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()));
Toml toml = new Toml().read(new File(getClass().getResource("should_support_blank_line.toml").getFile()));
assertEquals(1, toml.getLong("group.key").intValue());
}
@Test
public void should_allow_comment_after_values() throws Exception {
Toml toml = new Toml().parse(new File(getClass().getResource("should_allow_comment_after_values.toml").getFile()));
Toml toml = new Toml().read(new File(getClass().getResource("should_allow_comment_after_values.toml").getFile()));
assertEquals(1, toml.getLong("a").intValue());
assertEquals(1.1, toml.getDouble("b").doubleValue(), 0);
@ -64,37 +64,37 @@ public class TomlTest {
@Test
public void should_be_empty_if_no_values() throws Exception {
assertTrue(new Toml().isEmpty());
assertFalse(new Toml().parse("a = 1").isEmpty());
assertFalse(new Toml().read("a = 1").isEmpty());
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_empty_key_name() throws Exception {
new Toml().parse(" = 1");
new Toml().read(" = 1");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_key_name_with_hash() throws Exception {
new Toml().parse("a# = 1");
new Toml().read("a# = 1");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_key_name_starting_with_square_bracket() throws Exception {
new Toml().parse("[a = 1");
new Toml().read("[a = 1");
}
@Test(expected = IllegalStateException.class)
public void should_fail_when_key_is_overwritten_by_table() {
new Toml().parse("[a]\nb=1\n[a.b]\nc=2");
new Toml().read("[a]\nb=1\n[a.b]\nc=2");
}
@Test
public void should_fail_when_key_in_root_is_overwritten_by_table() throws Exception {
expectedException.expect(IllegalStateException.class);
new Toml().parse("a=1\n [a]");
new Toml().read("a=1\n [a]");
}
@Test(expected = IllegalStateException.class)
public void should_fail_when_key_is_overwritten_by_another_key() {
new Toml().parse("[fruit]\ntype=\"apple\"\ntype=\"orange\"");
new Toml().read("[fruit]\ntype=\"apple\"\ntype=\"orange\"");
}
}

View file

@ -29,7 +29,7 @@ public class TomlToClassTest {
@Test
public void should_convert_toml_primitives() throws Exception {
Toml toml = new Toml().parse(file("should_convert_primitive_values.toml"));
Toml toml = new Toml().read(file("should_convert_primitive_values.toml"));
TomlPrimitives values = toml.to(TomlPrimitives.class);
@ -47,7 +47,7 @@ public class TomlToClassTest {
@Test
public void should_convert_to_non_toml_primitives() throws Exception {
ExtraPrimitives extraPrimitives = new Toml().parse(file("should_convert_extra_primitives.toml")).to(ExtraPrimitives.class);
ExtraPrimitives extraPrimitives = new Toml().read(file("should_convert_extra_primitives.toml")).to(ExtraPrimitives.class);
assertEquals("Did not convert table to map", "value", extraPrimitives.group.get("key"));
assertEquals("Did not convert double to BigDecimal", BigDecimal.valueOf(1.2), extraPrimitives.bigDecimal);
@ -65,7 +65,7 @@ public class TomlToClassTest {
@Test
public void should_convert_tables() throws Exception {
String fileName = "should_convert_tables.toml";
Toml toml = new Toml().parse(file(fileName));
Toml toml = new Toml().read(file(fileName));
TomlTables tomlTables = toml.to(TomlTables.class);
@ -75,8 +75,8 @@ public class TomlToClassTest {
@Test
public void should_convert_tables_with_defaults() throws Exception {
Toml defaultToml = new Toml().parse("[group2]\n string=\"defaultValue2\"\n number=2\n [group3]\n string=\"defaultValue3\"");
Toml toml = new Toml(defaultToml).parse(file("should_convert_tables.toml"));
Toml defaultToml = new Toml().read("[group2]\n string=\"defaultValue2\"\n number=2\n [group3]\n string=\"defaultValue3\"");
Toml toml = new Toml(defaultToml).read(file("should_convert_tables.toml"));
TomlTables tomlTables = toml.to(TomlTables.class);
@ -88,8 +88,8 @@ public class TomlToClassTest {
@Test
public void should_use_defaults() throws Exception {
Toml defaults = new Toml().parse(file("should_convert_tables.toml"));
Toml toml = new Toml(defaults).parse("");
Toml defaults = new Toml().read(file("should_convert_tables.toml"));
Toml toml = new Toml(defaults).read("");
TomlTables tomlTables = toml.to(TomlTables.class);
@ -99,14 +99,14 @@ public class TomlToClassTest {
@Test
public void should_ignore_keys_not_in_class() throws Exception {
TomlPrimitives tomlPrimitives = new Toml().parse("a=1\nstring=\"s\"").to(TomlPrimitives.class);
TomlPrimitives tomlPrimitives = new Toml().read("a=1\nstring=\"s\"").to(TomlPrimitives.class);
assertEquals("s", tomlPrimitives.string);
}
@Test
public void should_convert_table_array() throws Exception {
TomlTableArrays toml = new Toml().parse(file("should_convert_table_array_to_class.toml")).to(TomlTableArrays.class);
TomlTableArrays toml = new Toml().read(file("should_convert_table_array_to_class.toml")).to(TomlTableArrays.class);
assertEquals(2, toml.groupers.size());
assertEquals("grouper 1", toml.groupers.get(0).string);
@ -118,7 +118,7 @@ public class TomlToClassTest {
@Test
public void should_convert_fruit_table_array() throws Exception {
FruitArray fruitArray = new Toml().parse(file("fruit_table_array.toml")).to(FruitArray.class);
FruitArray fruitArray = new Toml().read(file("fruit_table_array.toml")).to(FruitArray.class);
assertEquals(2, fruitArray.fruit.size());
Fruit apple = fruitArray.fruit.get(0);

View file

@ -8,14 +8,14 @@ public class UnicodeTest {
@Test
public void should_support_short_escape_form() throws Exception {
Toml toml = new Toml().parse("key = \"Jos\u00E9\\nLocation\tSF\"");
Toml toml = new Toml().read("key = \"Jos\u00E9\\nLocation\tSF\"");
assertEquals("José\nLocation\tSF", toml.getString("key"));
}
@Test
public void should_support_unicode_literal() throws Exception {
Toml toml = new Toml().parse("key = \"José LöcÄtion SF\"");
Toml toml = new Toml().read("key = \"José LöcÄtion SF\"");
assertEquals("José LöcÄtion SF", toml.getString("key"));
}