Skip to content

Commit 2127f00

Browse files
committed
Create WordCountTestGenerator. Update WordCountTest. Refs exercism#370, exercism#331
1 parent 366fcd2 commit 2127f00

File tree

3 files changed

+87
-50
lines changed

3 files changed

+87
-50
lines changed

exercises/word-count/example.scala

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
class Phrase(phrase: String) {
2-
def wordCount = groupedWords.mapValues(_.size)
3-
4-
private def groupedWords = words.groupBy(w => w)
5-
6-
private def words = withoutPunctuation.split("\\s+")
7-
8-
private def withoutPunctuation = phrase.toLowerCase.replaceAll("[^\\w']", " ")
1+
case class WordCount(phrase: String) {
2+
def countwords = "\\w+('\\w+)*".r
3+
.findAllIn(phrase)
4+
.toSeq
5+
.map(_.toLowerCase)
6+
.groupBy(w => w)
7+
.mapValues(_.length)
98
}
Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,68 @@
1-
import org.scalatest._
1+
import org.scalatest.{Matchers, FunSuite}
22

3-
class WordCountTest extends FlatSpec with Matchers {
4-
it should "count one word" in {
5-
val phrase = new Phrase("word")
6-
phrase.wordCount should be (Map("word" -> 1))
3+
/** @version 1.0.0 */
4+
class WordCountTest extends FunSuite with Matchers {
5+
6+
test("count one word") {
7+
WordCount("word").countwords should be(Map(("word", 1)))
8+
}
9+
10+
test("count one of each word") {
11+
pending
12+
WordCount("one of each").countwords should be(
13+
Map(("one", 1), ("of", 1), ("each", 1)))
714
}
815

9-
it should "count one of each" in {
16+
test("multiple occurrences of a word") {
1017
pending
11-
val phrase = new Phrase("one of each")
12-
val counts = Map("one" -> 1, "of" -> 1, "each" -> 1)
13-
phrase.wordCount should be (counts)
18+
WordCount("one fish two fish red fish blue fish").countwords should be(
19+
Map(("blue", 1), ("two", 1), ("fish", 4), ("one", 1), ("red", 1)))
1420
}
1521

16-
it should "count multiple occurrences" in {
22+
test("handles cramped lists") {
1723
pending
18-
val phrase = new Phrase("one fish two fish red fish blue fish")
19-
val counts = Map("one" -> 1, "fish" -> 4, "two" -> 1, "red" -> 1, "blue" -> 1)
20-
phrase.wordCount should be (counts)
24+
WordCount("one,two,three").countwords should be(
25+
Map(("one", 1), ("two", 1), ("three", 1)))
2126
}
2227

23-
it should "count everything just once" in {
28+
test("handles expanded lists") {
2429
pending
25-
val phrase = new Phrase("all the kings horses and all the kings men")
26-
phrase.wordCount
27-
val counts = Map(
28-
"all" -> 2, "the" -> 2, "kings" -> 2, "horses" -> 1, "and" -> 1, "men" -> 1
29-
)
30-
phrase.wordCount should be (counts)
30+
WordCount("one,\ntwo,\nthree").countwords should be(
31+
Map(("one", 1), ("two", 1), ("three", 1)))
3132
}
3233

33-
it should "ignore punctuation" in {
34+
test("ignore punctuation") {
3435
pending
35-
val phrase = new Phrase("car : carpet as java : javascript!!&@$%^&")
36-
val counts = Map(
37-
"car" -> 1, "carpet" -> 1, "as" -> 1, "java" -> 1, "javascript" -> 1
38-
)
39-
phrase.wordCount should be (counts)
36+
WordCount("car: carpet as java: javascript!!&@$%^&").countwords should be(
37+
Map(("as", 1), ("car", 1), ("java", 1), ("carpet", 1), ("javascript", 1)))
4038
}
4139

42-
it should "handle cramped lists" in {
40+
test("include numbers") {
4341
pending
44-
val phrase = new Phrase("one,two,three")
45-
phrase.wordCount should be (Map("one" -> 1, "two" -> 1, "three" -> 1))
42+
WordCount("testing, 1, 2 testing").countwords should be(
43+
Map(("testing", 2), ("1", 1), ("2", 1)))
4644
}
4745

48-
it should "include numbers" in {
46+
test("normalize case") {
4947
pending
50-
val phrase = new Phrase("testing, 1, 2 testing")
51-
val counts = Map("testing" -> 2, "1" -> 1, "2" -> 1)
52-
phrase.wordCount should be (counts)
48+
WordCount("go Go GO Stop stop").countwords should be(
49+
Map(("go", 3), ("stop", 2)))
5350
}
5451

55-
it should "normalize case" in {
52+
test("with apostrophes") {
5653
pending
57-
val phrase = new Phrase("go Go GO")
58-
val counts = Map("go" -> 3)
59-
phrase.wordCount should be (counts)
54+
WordCount("First: don\'t laugh. Then: don\'t cry.").countwords should be(
55+
Map(("laugh", 1), ("don't", 2), ("then", 1), ("first", 1), ("cry", 1)))
6056
}
6157

62-
it should "allow apostrophes" in {
58+
test("with quotations") {
6359
pending
64-
val phrase = new Phrase("First: don't laugh. Then: don't cry.")
65-
val counts =
66-
Map("first" -> 1, "don't" -> 2, "laugh" -> 1, "then" -> 1, "cry" -> 1)
67-
phrase.wordCount should be (counts)
60+
WordCount("Joe can\'t tell between \'large\' and large.").countwords should be(
61+
Map(("can't", 1),
62+
("large", 2),
63+
("joe", 1),
64+
("between", 1),
65+
("tell", 1),
66+
("and", 1)))
6867
}
6968
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.io.File
2+
3+
import testgen.TestSuiteBuilder._
4+
import testgen._
5+
6+
object WordCountTestGenerator {
7+
def toString(expected: CanonicalDataParser.Expected): String = {
8+
expected match {
9+
case Right(m: Map[String, Int]) =>
10+
s"""Map(${m.map{case (s, i) => s"""("$s", $i)"""}.mkString(", ")})"""
11+
case _ => throw new IllegalArgumentException
12+
}
13+
}
14+
15+
def fromLabeledTest(argNames: String*): ToTestCaseData =
16+
withLabeledTest { sut =>
17+
labeledTest =>
18+
val args = escape(labeledTest.result("input").toString)
19+
val property = labeledTest.property
20+
val sutCall =
21+
s"""$sut($args).$property"""
22+
val expected = toString(labeledTest.expected)
23+
TestCaseData(labeledTest.description, sutCall, expected)
24+
}
25+
26+
def escape(raw: String): String = {
27+
import scala.reflect.runtime.universe._
28+
Literal(Constant(raw)).toString
29+
}
30+
31+
def main(args: Array[String]): Unit = {
32+
val file = new File("src/main/resources/word-count.json")
33+
34+
val code = TestSuiteBuilder.build(file, fromLabeledTest("input"))
35+
println(s"-------------")
36+
println(code)
37+
println(s"-------------")
38+
}
39+
}

0 commit comments

Comments
 (0)