Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/com/github/hexomod/macro/Preprocessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,15 @@ String commentLine(String line, Map<String, String> 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;
}
}

String uncommentLine(String line, Map<String, String> 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;
}
Expand Down
61 changes: 56 additions & 5 deletions src/test/java/com/github/hexomod/macro/PreprocessorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down Expand Up @@ -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);
}
}

@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<String, String> keywords;
@Parameterized.Parameters
public static Collection<Object[]> data() {
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(PreprocessorTest.class.getResource("preprocessor_comment_data.csv").openStream())
);
List<Object[]> 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));
}
}
}
Original file line number Diff line number Diff line change
@@ -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