From 6fd3181c2d61c7634879f58863a0fac9c615a8c5 Mon Sep 17 00:00:00 2001 From: Moandji Ezana Date: Wed, 13 Dec 2017 12:29:06 +0100 Subject: [PATCH] Fix crash when table array in comment after table array definition Fixes #48 --- .../toml/IdentifierConverter.java | 3 ++- .../com/moandjiezana/toml/CommentsTest.java | 24 +++++++++++++++++++ .../com/moandjiezana/toml/TableArrayTest.java | 12 ++++------ 3 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 src/test/java/com/moandjiezana/toml/CommentsTest.java diff --git a/src/main/java/com/moandjiezana/toml/IdentifierConverter.java b/src/main/java/com/moandjiezana/toml/IdentifierConverter.java index d5c246f..ecdb1e4 100644 --- a/src/main/java/com/moandjiezana/toml/IdentifierConverter.java +++ b/src/main/java/com/moandjiezana/toml/IdentifierConverter.java @@ -27,12 +27,13 @@ class IdentifierConverter { } else if (c == '=' && isKey) { terminated = true; break; - } else if (c == ']' && !isKey) { + } else if (c == ']' && !isKey && !terminated) { if (!isTableArray || s.length() > index.get() + 1 && s.charAt(index.get() + 1) == ']') { terminated = true; name.append(']'); if (isTableArray) { name.append(']'); + index.incrementAndGet(); } } } else if (terminated && c == '#') { diff --git a/src/test/java/com/moandjiezana/toml/CommentsTest.java b/src/test/java/com/moandjiezana/toml/CommentsTest.java new file mode 100644 index 0000000..19912eb --- /dev/null +++ b/src/test/java/com/moandjiezana/toml/CommentsTest.java @@ -0,0 +1,24 @@ +package com.moandjiezana.toml; + +import org.junit.Test; + +public class CommentsTest +{ + + @Test + public void should_handle_table_array_in_comment_after_table_array_definition() + { + new Toml().read("[[example]] # [[]]\n a=1"); + new Toml().read("[[example]] # [[abc]]\n a=1"); + new Toml().read("[[example]] # [[abc]]"); + } + + @Test + public void should_handle_table_array_in_comment_after_table_definition() + { + new Toml().read("[example] # [[]]"); + new Toml().read("[example] # [[abc]]"); + new Toml().read("[example] # [[]]\n a=1"); + } + +} diff --git a/src/test/java/com/moandjiezana/toml/TableArrayTest.java b/src/test/java/com/moandjiezana/toml/TableArrayTest.java index dd2d42c..5b5a013 100644 --- a/src/test/java/com/moandjiezana/toml/TableArrayTest.java +++ b/src/test/java/com/moandjiezana/toml/TableArrayTest.java @@ -2,16 +2,14 @@ package com.moandjiezana.toml; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasSize; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; - -import java.io.File; -import java.util.List; +import static org.junit.Assert.*; import org.junit.Assert; import org.junit.Test; +import java.io.File; +import java.util.List; + public class TableArrayTest { @Test @@ -119,7 +117,7 @@ public class TableArrayTest { public void should_fail_on_empty_table_array_name() { new Toml().read("[[]]"); } - + private File file(String fileName) { return new File(getClass().getResource(fileName + ".toml").getFile()); }