2014-12-15 11:01:48 +00:00
|
|
|
package com.moandjiezana.toml;
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import com.moandjiezana.toml.testutils.Utils;
|
|
|
|
|
|
|
|
public class StringTest {
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_get_string() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
Toml toml = new Toml().read("a = \"a\"");
|
2014-12-15 11:01:48 +00:00
|
|
|
|
|
|
|
assertEquals("a", toml.getString("a"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_get_empty_string() {
|
2015-08-14 09:12:08 +00:00
|
|
|
Toml toml = new Toml().read("a = \"\"");
|
2014-12-15 11:01:48 +00:00
|
|
|
assertEquals("", toml.getString("a"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_get_empty_string_with_trailing_new_line() {
|
2015-08-14 09:12:08 +00:00
|
|
|
Toml toml = new Toml().read("a = \"\"\n");
|
2014-12-15 11:01:48 +00:00
|
|
|
assertEquals("", toml.getString("a"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_get_basic_multiline_string() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
Toml toml = new Toml().read(file("should_get_basic_multiline_string"));
|
2014-12-15 11:01:48 +00:00
|
|
|
|
|
|
|
assertEquals(toml.getString("ref"), toml.getString("one_line"));
|
|
|
|
assertEquals(toml.getString("ref"), toml.getString("many_lines"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_get_multiline_string_without_new_lines() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
Toml toml = new Toml().read(file("should_get_multiline_string_without_new_lines"));
|
2014-12-15 11:01:48 +00:00
|
|
|
|
|
|
|
assertEquals(toml.getString("ref"), toml.getString("multi1"));
|
|
|
|
assertEquals(toml.getString("ref"), toml.getString("multi2"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_get_literal_string() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
Toml toml = new Toml().read(file("should_get_literal_string"));
|
2014-12-15 11:01:48 +00:00
|
|
|
|
|
|
|
assertEquals("C:\\Users\\nodejs\\templates", toml.getString("winpath"));
|
|
|
|
assertEquals("\\\\ServerX\\admin$\\system32\\", toml.getString("winpath2"));
|
|
|
|
assertEquals("Tom \"Dubs\" Preston-Werner", toml.getString("quoted"));
|
|
|
|
assertEquals("<\\i\\c*\\s*>", toml.getString("regex"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_get_multiline_literal_string() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
Toml toml = new Toml().read(file("should_get_multiline_literal_string"));
|
2014-12-15 11:01:48 +00:00
|
|
|
|
|
|
|
assertTrue(toml.getString("empty_line").isEmpty());
|
|
|
|
assertEquals(toml.getString("regex2_ref"), toml.getString("regex2"));
|
|
|
|
assertEquals(toml.getString("lines_ref"), toml.getString("lines"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_support_special_characters_in_strings() {
|
2015-08-14 09:12:08 +00:00
|
|
|
Toml toml = new Toml().read(new File(getClass().getResource("should_support_special_characters_in_strings.toml").getFile()));
|
2014-12-15 11:01:48 +00:00
|
|
|
|
2015-01-22 14:02:32 +00:00
|
|
|
assertEquals("\" \t \n \r \\ \b \f", toml.getString("key"));
|
2014-12-15 11:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void should_support_unicode_characters_in_strings() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
Toml toml = new Toml().read(new File(getClass().getResource("should_support_special_characters_in_strings.toml").getFile()));
|
2014-12-15 11:01:48 +00:00
|
|
|
|
|
|
|
assertEquals("more or less ±", toml.getString("unicode_key"));
|
2014-12-15 14:02:49 +00:00
|
|
|
assertEquals("more or less ±", toml.getString("unicode_key_uppercase"));
|
2014-12-15 11:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = IllegalStateException.class)
|
|
|
|
public void should_fail_on_reserved_special_character_in_strings() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
new Toml().read("key=\"\\m\"");
|
2014-12-15 11:01:48 +00:00
|
|
|
}
|
|
|
|
|
2015-01-22 14:02:32 +00:00
|
|
|
@Test(expected = IllegalStateException.class)
|
|
|
|
public void should_fail_on_escaped_slash() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
new Toml().read("key=\"\\/\"");
|
2015-01-22 14:02:32 +00:00
|
|
|
}
|
|
|
|
|
2014-12-15 11:01:48 +00:00
|
|
|
@Test(expected = IllegalStateException.class)
|
|
|
|
public void should_fail_on_text_after_literal_string() {
|
2015-08-14 09:12:08 +00:00
|
|
|
new Toml().read("a = ' ' jdkf");
|
2014-12-15 11:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = IllegalStateException.class)
|
|
|
|
public void should_fail_on_unterminated_literal_string() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
new Toml().read("a = 'some text");
|
2014-12-15 11:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = IllegalStateException.class)
|
2015-01-23 20:03:55 +00:00
|
|
|
public void should_fail_on_multiline_literal_string_with_malformed_comment() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
new Toml().read("a = '''some\n text\n''\nb = '''1'''");
|
2014-12-15 11:01:48 +00:00
|
|
|
}
|
|
|
|
|
2015-01-23 20:03:55 +00:00
|
|
|
@Test(expected = IllegalStateException.class)
|
|
|
|
public void should_fail_on_unterminated_multiline_literal_string() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
new Toml().read("a = '''some\n text\n''");
|
2015-01-23 20:03:55 +00:00
|
|
|
}
|
|
|
|
|
2014-12-15 11:01:48 +00:00
|
|
|
@Test(expected = IllegalStateException.class)
|
|
|
|
public void should_fail_on_unterminated_multiline_literal_string_on_single_line() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
new Toml().read("a = '''some text''");
|
2014-12-15 11:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = IllegalStateException.class)
|
|
|
|
public void should_fail_on_text_outside_multiline_string() {
|
2015-08-14 09:12:08 +00:00
|
|
|
new Toml().read("a = \"\"\" \"\"\" jdkf");
|
2014-12-15 11:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = IllegalStateException.class)
|
|
|
|
public void should_fail_on_unterminated_multiline_string() throws Exception {
|
2015-08-14 09:12:08 +00:00
|
|
|
new Toml().read("a = \"\"\"some text\"\"");
|
2014-12-15 11:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private File file(String file) {
|
|
|
|
return Utils.file(getClass(), file);
|
|
|
|
}
|
|
|
|
}
|