public
Last active

Line count kata at coding dojo

  • Download Gist
LineCountTest.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
package linecount
 
import org.scalatest._
import matchers.ShouldMatchers
 
class LineCountTest extends FlatSpec with ShouldMatchers {
 
"the Linecounter" should "think an empty string has zero lines" in {
countLinesIn("") should equal(0)
}
 
it should "think a string with spaces has zero lines" in {
countLinesIn("") should equal(0)
}
 
it should "count all the lines with text" in {
countLinesIn(
"""while (true) do durgg();;
|aa;
|ee + 33;}""".stripMargin) should equal(3)
}
 
it should "ignore empty lines" in {
countLinesIn(
"""while (true) do durgg();
|
|{
| 1 ++
|}
""".stripMargin) should equal(4)
}
 
it should "ignore inline comments" in {
countLinesIn(
"""while (true) do durgg();
|
|// ignore me
|22 ++ // but not me
|{
| i++
|}
""".stripMargin) should equal(5)
}
 
it should "ignore block comments" in {
countLinesIn(
"""while (true) do durgg();
|
|/** */
|
|/* comment
|more comment
|end comment *//***///inline
|
|/* don't ignore the command after this */ aCommand(); /* even with this comment after it */ //and this inline one
|{
| i++
|}
""".stripMargin) should equal(5)
}
 
it should "ignore nested block comments" in {
countLinesIn(
"""while (true) do durgg();
|
| /* comment 1 /* /*
| comment 2
| */
| */
| */ <--
| */
|
|/* comment
|more comment
|end comment *//***///inline
|
|{
| i++
|}
""".stripMargin) should equal(7)
}
 
it should "correctly count Nigel's first file" in {
countLinesIn(
"""// This file contains 3 lines of code
|public interface Dave {
| /**
|* count the number of lines in a file
|*/
| int countLines(File inFile); // not the real signature!
|}
""".stripMargin) should equal(3)
}
 
it should "correctly count Nigel's second file" in {
countLinesIn(
"""/*****
|* This is a test program with 5 lines of code
|* \/* no nesting allowed!
|//*****//***/// Slightly pathological comment ending...
|
| public class Hello {
| public static final void main(String [] args) { // gotta love Java
| // Say hello
| System./*wait*/out./*for*/println/*it*/("Hello/*");
| }
|
|}
""".stripMargin) should equal(5)
}
 
 
def countLinesIn(text: String): Int = {
val lines = removeBlockComments(text).split("\\n")
lines.filter(notEmptyOrInlineComment).length
}
 
val notEmptyOrInlineComment: (String => Boolean) = line => {
val trimmedLine = line.trim()
trimmedLine.length > 0 && !trimmedLine.startsWith("//")
}
 
def removeBlockComments(text: String) = {
val regexForBlockComments = "/\\*(\\n|.)*?\\*/"
text.replaceAll(regexForBlockComments, "")
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.