FeatureFileParser { public static String stripComments(final String line) { String trimmed = null; if (line != null) { final int idx = line.trim().indexOf("#"); if (idx >= 0) { boolean doTrim = false; if (idx == 0) { doTrim = true; } final String[] splitByQuotes = line.split("\"[^\"]*\"|'[^']*'"); for (final String split : splitByQuotes) { if (split.contains("#") ) { doTrim = true; break; } } if (doTrim) { trimmed = line.trim().substring(0, idx).trim(); } else { trimmed = line.trim(); } } else { trimmed = line.trim(); } } return trimmed; }  FeatureFile loadFeatureFile(final File featureFile); FeatureFile getFeatureFile(FileContents fileContents); static String stripComments(final String line);  }
@Test public void testCommentEscaping() { final String line1 = "hello this is a test with no comments"; final String line2 = "# hello this is a comment"; final String line3 = "hello this is a # trailing comment"; final String line4 = " \"hello\" this is an unquoted # \"hash\""; final String line5 = " hello this is a quoted \" # \"hash"; final String line6 = " hello this is a quoted '#' hash"; final String line7 = " \"hello\" this is a quoted '#' hash"; final String line8 = " 'hello' this is a non 'quoted' #' hash"; Assert.assertThat(FeatureFileParser.stripComments(line1), is("hello this is a test with no comments")); Assert.assertThat(FeatureFileParser.stripComments(line2), is("")); Assert.assertThat(FeatureFileParser.stripComments(line3), is("hello this is a")); Assert.assertThat(FeatureFileParser.stripComments(line4), is("\"hello\" this is an unquoted")); Assert.assertThat(FeatureFileParser.stripComments(line5), is("hello this is a quoted \" # \"hash")); Assert.assertThat(FeatureFileParser.stripComments(line6), is("hello this is a quoted '#' hash")); Assert.assertThat(FeatureFileParser.stripComments(line7), is("\"hello\" this is a quoted '#' hash")); Assert.assertThat(FeatureFileParser.stripComments(line8), is("'hello' this is a non 'quoted'")); }