diff options
Diffstat (limited to 'enigma/src/test/java')
| -rw-r--r-- | enigma/src/test/java/cuchaz/enigma/ConfigTest.java | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/enigma/src/test/java/cuchaz/enigma/ConfigTest.java b/enigma/src/test/java/cuchaz/enigma/ConfigTest.java new file mode 100644 index 0000000..a44f037 --- /dev/null +++ b/enigma/src/test/java/cuchaz/enigma/ConfigTest.java | |||
| @@ -0,0 +1,86 @@ | |||
| 1 | package cuchaz.enigma; | ||
| 2 | |||
| 3 | import org.junit.Test; | ||
| 4 | |||
| 5 | import cuchaz.enigma.config.ConfigContainer; | ||
| 6 | |||
| 7 | import static org.junit.Assert.assertEquals; | ||
| 8 | |||
| 9 | public class ConfigTest { | ||
| 10 | |||
| 11 | @Test | ||
| 12 | public void serialize() { | ||
| 13 | ConfigContainer cc = new ConfigContainer(); | ||
| 14 | cc.data().setString("a", "a"); | ||
| 15 | cc.data().section("a").section("b").section("c").setString("a", "abcd"); | ||
| 16 | cc.data().section("a").section("b").section("c").setBool("b", true); | ||
| 17 | cc.data().section("a").section("b").section("c").setInt("c", 5); | ||
| 18 | cc.data().section("a").section("b").section("c").setDouble("d", 3.5); | ||
| 19 | cc.data().section("a").section("b").section("c").setRgbColor("e", 0x123456); | ||
| 20 | assertEquals("a=a\n" + | ||
| 21 | "\n" + | ||
| 22 | "[a][b][c]\n" + | ||
| 23 | "a=abcd\n" + | ||
| 24 | "b=true\n" + | ||
| 25 | "c=5\n" + | ||
| 26 | "d=3.5\n" + | ||
| 27 | "e=#123456\n", | ||
| 28 | cc.serialize()); | ||
| 29 | } | ||
| 30 | |||
| 31 | @Test | ||
| 32 | public void deserialize() { | ||
| 33 | ConfigContainer cc = new ConfigContainer(); | ||
| 34 | cc.data().setString("a", "a"); | ||
| 35 | cc.data().section("a").section("b").section("c").setString("a", "abcd"); | ||
| 36 | cc.data().section("a").section("b").section("c").setBool("b", true); | ||
| 37 | cc.data().section("a").section("b").section("c").setInt("c", 5); | ||
| 38 | cc.data().section("a").section("b").section("c").setDouble("d", 3.5); | ||
| 39 | cc.data().section("a").section("b").section("c").setRgbColor("e", 0x123456); | ||
| 40 | assertEquals(ConfigContainer.parse("a=a\n" + | ||
| 41 | "\n" + | ||
| 42 | "[a][b][c]\n" + | ||
| 43 | "a=abcd\n" + | ||
| 44 | "b=true\n" + | ||
| 45 | "c=5\n" + | ||
| 46 | "d=3.5\n" + | ||
| 47 | "e=#123456\n").data(), cc.data()); | ||
| 48 | } | ||
| 49 | |||
| 50 | @Test | ||
| 51 | public void weirdChars() { | ||
| 52 | ConfigContainer cc = new ConfigContainer(); | ||
| 53 | String thing = "\\[],\\,./'\"`~!@#$%^&*()_+-=|}{\n\\\\\r\b\u0000\uffff\u1234"; | ||
| 54 | cc.data().section(thing).setString(thing, thing); | ||
| 55 | cc.data().section(thing).setArray("arr", new String[] { thing, thing, thing, thing }); | ||
| 56 | |||
| 57 | assertEquals( | ||
| 58 | "[\\\\[\\],\\\\,./'\"`~!@#$%^&*()_+-=|}{\\n\\\\\\\\\\u000d\\u0008\\u0000\\uffff\\u1234]\n" + | ||
| 59 | "\\\\\\[],\\\\,./'\"`~!@#$%^&*()_+-\\=|}{\\n\\\\\\\\\\u000d\\u0008\\u0000\\uffff\\u1234=\\\\[],\\\\,./'\"`~!@#$%^&*()_+-=|}{\\n\\\\\\\\\\u000d\\u0008\\u0000\\uffff\\u1234\n" + | ||
| 60 | "arr=\\\\\\\\[]\\\\,\\\\\\\\\\\\,./'\"`~!@#$%^&*()_+-=|}{\\n\\\\\\\\\\\\\\\\\\u000d\\u0008\\u0000\\uffff\\u1234,\\\\\\\\[]\\\\,\\\\\\\\\\\\,./'\"`~!@#$%^&*()_+-=|}{\\n\\\\\\\\\\\\\\\\\\u000d\\u0008\\u0000\\uffff\\u1234,\\\\\\\\[]\\\\,\\\\\\\\\\\\,./'\"`~!@#$%^&*()_+-=|}{\\n\\\\\\\\\\\\\\\\\\u000d\\u0008\\u0000\\uffff\\u1234,\\\\\\\\[]\\\\,\\\\\\\\\\\\,./'\"`~!@#$%^&*()_+-=|}{\\n\\\\\\\\\\\\\\\\\\u000d\\u0008\\u0000\\uffff\\u1234\n", | ||
| 61 | cc.serialize()); | ||
| 62 | |||
| 63 | ConfigContainer cc1 = ConfigContainer.parse(cc.serialize()); | ||
| 64 | assertEquals(cc.data(), cc1.data()); | ||
| 65 | |||
| 66 | cc1 = ConfigContainer.parse(cc1.serialize()); | ||
| 67 | assertEquals(cc.data(), cc1.data()); | ||
| 68 | } | ||
| 69 | |||
| 70 | @Test | ||
| 71 | public void syntaxErrors() { | ||
| 72 | assertEquals("", ConfigContainer.parse("abcde").serialize()); | ||
| 73 | assertEquals("", ConfigContainer.parse("what\\=?").serialize()); | ||
| 74 | |||
| 75 | assertEquals("[a]\nb=c\n", ConfigContainer.parse("[a] what is this\nb=c").serialize()); | ||
| 76 | assertEquals("b=c\n", ConfigContainer.parse("[a][ what is this\nb=c").serialize()); | ||
| 77 | assertEquals("", ConfigContainer.parse("[").serialize()); | ||
| 78 | assertEquals("[a]\na=b\nc=d\n", ConfigContainer.parse("[a]\na=b\n[\nc=d").serialize()); | ||
| 79 | |||
| 80 | |||
| 81 | // not technically syntax errors but never something that gets generated | ||
| 82 | assertEquals("", ConfigContainer.parse("[a]").serialize()); | ||
| 83 | assertEquals("", ConfigContainer.parse("[a]\n[b]").serialize()); | ||
| 84 | } | ||
| 85 | |||
| 86 | } | ||