diff --git a/src/JohnnyScript.java b/src/JohnnyScript.java index 977e21c..fa13f31 100644 --- a/src/JohnnyScript.java +++ b/src/JohnnyScript.java @@ -11,6 +11,7 @@ public class JohnnyScript { private static final String OUTPUT_EXTENSION = ".ram"; + private static final String LINE_COMMENT_DELIMITER = "//"; public static void main(String[] args) throws IOException { @@ -25,7 +26,7 @@ private static List compileCode(List lines) { for (String line: lines) { String compiledLine = compile(line); - if (compiledLine != null) { + if (!compiledLine.equals("")) { compiled.add(compiledLine); } } @@ -34,10 +35,13 @@ private static List compileCode(List lines) { } private static String compile(String line) { - if(line.startsWith("//")){ - return null; + if (line.contains(LINE_COMMENT_DELIMITER)) { + int commentStart = line.indexOf(LINE_COMMENT_DELIMITER); + String nonComment = line.substring(0, commentStart); + return nonComment.trim(); } - else return line; + else + return line.trim(); } /** diff --git a/test/JohnnyScriptTest.java b/test/JohnnyScriptTest.java index 02b1f34..3ef0b2d 100644 --- a/test/JohnnyScriptTest.java +++ b/test/JohnnyScriptTest.java @@ -91,4 +91,30 @@ public void commentLine() throws Exception { } + @Test + public void commentInline() throws Exception { + List testCode = new ArrayList<>(); + testCode.add("add 0"); + testCode.add("add 1 //Test comment"); + testCode.add("add 0"); + Files.write(inputPath, testCode); + + JohnnyScript.main(new String[]{validFile}); + + assertTrue("Output file not generated or incorrectly named", Files.exists(outputPath)); + + List outLines = Files.readAllLines(outputPath); + + testCode = new ArrayList<>(); + testCode.add("add 0"); + testCode.add("add 1"); + testCode.add("add 0"); + + assertEquals(testCode.size(), outLines.size()); + for (int i = 0; i < testCode.size(); i++) { + assertEquals("Line " + i, testCode.get(i),outLines.get(i)); + } + + } + } \ No newline at end of file