Skip to content

@dkandalov /gist:4023477
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Line count kata at coding dojo
import org.junit.Test
class LineCount3 {
@Test void aaa() {
assert countLines("") == 0
assert countLines("""
""") == 0
assert countLines("""
int i = 0;
""") == 1
assert countLines("""
int i = 0;
""") == 1
assert countLines("""
// int i = 0;
""") == 0
assert countLines("""
int j = 0; // int i = 0;
""") == 1
assert countLines("""
/* int i = 0; */
""") == 0
assert countLines("""
/* int i = 0;
int j = 0; */
""") == 0
assert countLines("""
/* int i = 0;
int j = 0; */ double d = 0;
""") == 1
}
@Test void examples() {
assert countLines(new File("/Users/dima/IdeaProjects/katas/src/main/scala/ru/katas/n13/java_source1.txt").readLines().join("\n")) == 3
assert countLines(new File("/Users/dima/IdeaProjects/katas/src/main/scala/ru/katas/n13/java_source2.txt").readLines().join("\n")) == 5
}
private static int countLines(String lines) {
lines = removeMultilineCommentsFrom(lines)
lines = removeSingleLineCommentsFrom(lines)
lines = removeLinesWithOnlyWhitespaces(lines)
lines = collapseSequentialNewLines(lines)
lines = lines.trim()
println(lines)
if (!lines.empty) lines.count("\n") + 1
else lines.count("\n")
}
public static String removeLinesWithOnlyWhitespaces(String lines) {
lines.replaceAll(/\s+\n/, "\n")
}
public static String collapseSequentialNewLines(String lines) {
lines.replaceAll(/\n+/, "\n")
}
public static String removeSingleLineCommentsFrom(String lines) {
lines.replaceAll(/\/\/.*/, "")
}
public static String removeMultilineCommentsFrom(lines) {
lines.replaceAll(/(?s)\/\*.*?\*\//, "")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.