diff --git a/src/main/java/com/github/hexomod/macro/Preprocessor.java b/src/main/java/com/github/hexomod/macro/Preprocessor.java index e1d2e12..0383257 100644 --- a/src/main/java/com/github/hexomod/macro/Preprocessor.java +++ b/src/main/java/com/github/hexomod/macro/Preprocessor.java @@ -242,7 +242,7 @@ String commentLine(String line, Map keywords) { if (trimLine.startsWith(keywords.get("comment"))) { return line; } else { - return StringUtils.repeat(" ", indent) + keywords.get("comment") + " " + trimLine; + return line.substring(0, indent) + keywords.get("comment") + " " + trimLine; } } @@ -250,7 +250,7 @@ String uncommentLine(String line, Map keywords) { int indent = getIndentSize(line); String trimLine = line.trim(); if (trimLine.startsWith(keywords.get("comment"))) { - return StringUtils.repeat(" ", indent) + trimLine.substring(keywords.get("comment").length()).trim(); + return line.substring(0, indent) + trimLine.substring(keywords.get("comment").length()).trim(); } else { return line; } diff --git a/src/test/java/com/github/hexomod/macro/PreprocessorTest.java b/src/test/java/com/github/hexomod/macro/PreprocessorTest.java index bc0fb62..34a8a99 100644 --- a/src/test/java/com/github/hexomod/macro/PreprocessorTest.java +++ b/src/test/java/com/github/hexomod/macro/PreprocessorTest.java @@ -23,13 +23,18 @@ */ package com.github.hexomod.macro; +import org.junit.BeforeClass; import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; +import java.util.stream.Collectors; +import static com.github.hexomod.macro.Preprocessor.HASH_KEYWORDS; import static com.github.hexomod.macro.Preprocessor.SLASH_KEYWORDS; import static org.junit.Assert.*; @@ -440,4 +445,50 @@ public void processLines_simple_ifdef_elseif_t_f_f() { assertTrue(lines.get(4).compareTo(preprocessor.commentLine(testLine1, SLASH_KEYWORDS))==0); assertTrue(lines.get(6).compareTo(preprocessor.commentLine(testLine, SLASH_KEYWORDS))==0); } -} \ No newline at end of file + + @RunWith(Parameterized.class) + public static class CommentTests { + static Preprocessor preprocessor; + @BeforeClass + public static void setup() { + preprocessor = new Preprocessor(Collections.emptyMap()); + } + + @Parameterized.Parameter(0) public String plain; + @Parameterized.Parameter(1) public String commented; + @Parameterized.Parameter(2) public Map keywords; + @Parameterized.Parameters + public static Collection data() { + try { + BufferedReader reader = new BufferedReader( + new InputStreamReader(PreprocessorTest.class.getResource("preprocessor_comment_data.csv").openStream()) + ); + List data = reader.lines().map(line -> { + String[] parts = line.split("\\|"); + if ("SLASH".equals(parts[2])) { + return new Object[]{parts[0], parts[1], SLASH_KEYWORDS}; + } else if ("HASH".equals(parts[2])) { + return new Object[]{parts[0], parts[1], HASH_KEYWORDS}; + } else { + throw new RuntimeException(); + } + }).collect(Collectors.toList()); + + reader.close(); + return data; + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Test + public void commentLine() { + assertEquals("Trying to comment \"" + plain + "\"", commented, preprocessor.commentLine(plain, keywords)); + } + + @Test + public void uncommentLine() { + assertEquals("Trying to uncomment \"" + commented + "\"", plain, preprocessor.uncommentLine(commented, keywords)); + } + } +} diff --git a/src/test/resources/com/github/hexomod/macro/preprocessor_comment_data.csv b/src/test/resources/com/github/hexomod/macro/preprocessor_comment_data.csv new file mode 100644 index 0000000..b947ea3 --- /dev/null +++ b/src/test/resources/com/github/hexomod/macro/preprocessor_comment_data.csv @@ -0,0 +1,14 @@ +String message = "0";|/// String message = "0";|SLASH +String message = "1";|### String message = "1";|HASH + String message = "2";| /// String message = "2";|SLASH + String message = "3";| ### String message = "3";|HASH + String message = "4";| /// String message = "4";|SLASH + String message = "5";| ### String message = "5";|HASH + String message = "6";| /// String message = "6";|SLASH + String message = "7";| ### String message = "7";|HASH + String message = "8";| /// String message = "8";|SLASH + String message = "9";| ### String message = "9";|HASH +// comment|/// // comment|SLASH +# comment|### # comment|HASH + // comment| /// // comment|SLASH + # comment| ### # comment|HASH