mirror of
https://github.com/plexusorg/toml4j.git
synced 2024-12-29 11:42:15 +00:00
Improved test coverage
This commit is contained in:
parent
6193e68a8f
commit
1e97e7602b
4 changed files with 59 additions and 0 deletions
|
@ -1,7 +1,14 @@
|
|||
package com.moandjiezana.toml;
|
||||
|
||||
import static org.hamcrest.Matchers.instanceOf;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
|
||||
import org.junit.Test;
|
||||
|
@ -21,4 +28,35 @@ public class TomlParseTest {
|
|||
|
||||
assertEquals(1, toml.getLong("key").intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_fail_on_missing_file() throws Exception {
|
||||
try {
|
||||
new Toml().parse(new File("missing"));
|
||||
fail("Exception should have been thrown");
|
||||
} catch (RuntimeException e) {
|
||||
assertThat(e.getCause(), instanceOf(FileNotFoundException.class));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_fail_on_io_error() throws Exception {
|
||||
Reader readerThatThrows = new Reader() {
|
||||
|
||||
@Override
|
||||
public int read(char[] cbuf, int off, int len) throws IOException {
|
||||
throw new IOException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {}
|
||||
};
|
||||
|
||||
try {
|
||||
new Toml().parse(readerThatThrows);
|
||||
fail("Exception should have been thrown");
|
||||
} catch (RuntimeException e) {
|
||||
assertThat(e.getCause(), instanceOf(IOException.class));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -273,4 +273,10 @@ public class TomlTest {
|
|||
public void should_fail_when_illegal_characters_after_table() throws Exception {
|
||||
new Toml().parse("[error] if you didn't catch this, your parser is broken");
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void should_fail_when_illegal_characters_after_key() throws Exception {
|
||||
new Toml().parse("number = 3.14 pi");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package com.moandjiezana.toml;
|
|||
import static org.hamcrest.Matchers.arrayContaining;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
|
@ -72,6 +73,19 @@ public class TomlToClassTest {
|
|||
assertEquals("value2", tomlTables.group2.string);
|
||||
}
|
||||
|
||||
@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"));
|
||||
|
||||
TomlTables tomlTables = toml.to(TomlTables.class);
|
||||
|
||||
assertEquals("value1", tomlTables.group1.string);
|
||||
assertEquals("value2", tomlTables.group2.string);
|
||||
assertNull(tomlTables.group2.number);
|
||||
assertEquals("defaultValue3", tomlTables.group3.string);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_use_defaults() throws Exception {
|
||||
Toml defaults = new Toml().parse(file("should_convert_tables.toml"));
|
||||
|
|
|
@ -5,4 +5,5 @@ public class TomlTables {
|
|||
|
||||
public TomlPrimitives group1;
|
||||
public TomlPrimitives group2;
|
||||
public TomlPrimitives group3;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue