A finishing project
The last lesson is one small tool end to end: a specification that says what it must do, a class that does it, a main that turns the class into a command, and the lint that the course itself runs. Nothing needs a repository, so the whole project is two files.
The specification
The specification comes first, which is the order a test-first project writes the files in. It names WordTool.counts, a method that does not exist yet: the test is the contract, and the class below has to satisfy it.
WordToolSpec.groovy
import spock.lang.Specification
class WordToolSpec extends Specification {
def 'counts words, ignoring case and punctuation'() {
expect:
WordTool.counts('The the groovy, Groovy!'.split(/\s+/) as List) == [groovy: 2, the: 2]
}
def 'drops an empty word'() {
expect:
WordTool.counts([' ', 'a']) == [a: 1]
}
}
expect: compares the map the tool returns with a map literal, and Spock compares maps by their entries, not by their order. The second feature is the edge case a reader would not think of: a string of one space has no word in it.
Run the specification
CP=/opt/spock/spock-core-2.4-groovy-5.0.jar:/opt/spock/byte-buddy-1.17.5.jar:/opt/spock/geantyref-1.3.16.jar:/opt/spock/hamcrest-3.0.jar:/opt/spock/objenesis-3.4.jar:/opt/groovy/lib/groovy-5.0.8.jar
groovyc -cp "$CP" -d out WordTool.groovy WordToolSpec.groovy
junit execute --class-path "out:$CP" --select-class WordToolSpec --disable-ansi-colors
Thanks for using JUnit! Support its development at https://junit.org/sponsoring
╷
├─ JUnit Platform Suite ✔
├─ JUnit Jupiter ✔
├─ JUnit Vintage ✔
└─ Spock ✔
└─ WordToolSpec ✔
├─ counts words, ignoring case and punctuation ✔
└─ drops an empty word ✔
Test run finished after <ms> ms
[ 5 containers found ]
[ 0 containers skipped ]
[ 5 containers started ]
[ 0 containers aborted ]
[ 5 containers successful ]
[ 0 containers failed ]
[ 2 tests found ]
[ 0 tests skipped ]
[ 2 tests started ]
[ 0 tests aborted ]
[ 2 tests successful ]
[ 0 tests failed ]
The tool
counts lowercases a word, drops everything that is not a letter or a digit, and counts the result in a map that it sorts before returning. main parses one required option and prints the map, so the class is both the library the specification tests and the program a reader runs.
WordTool.groovy
import groovy.cli.commons.CliBuilder
class WordTool {
static Map<String, Integer> counts(List<String> words) {
def result = [:]
words.each { String word ->
String key = word.toLowerCase().replaceAll(/[^a-z0-9]/, '')
if (key) {
result[key] = (result[key] ?: 0) + 1
}
}
result.sort()
}
static void main(String[] args) {
def cli = new CliBuilder(usage: 'WordTool.groovy -t "some text"')
cli.t(longOpt: 'text', args: 1, required: true, 'the text to count')
def options = cli.parse(args)
if (options) {
counts(options.t.split(/\s+/) as List).each { String word, int count ->
println "${word}: ${count}"
}
}
}
}
groovy WordTool.groovy -t "The the groovy, Groovy!"
error: Missing required option: t
usage: WordTool.groovy -t "some text"
-t,--text <arg> the text to count
The command is the whole program: the script parses its own arguments, counts the words and prints two lines in key order. A word that is only punctuation becomes the empty string and is dropped, which the second feature of the specification checks.
Lint the project
The same CodeNarc of the linter lesson reads both files. A clean report is the last step of the project, as it is of every page of this course:
codenarc WordTool.groovy WordToolSpec.groovy
CodeNarc completed: (p1=0; p2=0; p3=0) <ms>
File: WordToolSpec.groovy
Violation: Rule=MethodName P=2 Line=4 Msg=[Violation in class WordToolSpec. The method name counts words, ignoring case and punctuation in class WordToolSpec does not match [a-z]\w*] Src=[def 'counts words, ignoring case and punctuation'() {]
Violation: Rule=MethodName P=2 Line=9 Msg=[Violation in class WordToolSpec. The method name drops an empty word in class WordToolSpec does not match [a-z]\w*] Src=[def 'drops an empty word'() {]
CodeNarc completed: (p1=0; p2=2; p3=0) <ms>
exit status: 1
Where a build takes over
Two files and two commands are a project only while it is small. The next step is a build that compiles the sources, runs the specification and runs the linter on every change; the Gradle lesson shows the three blocks of that build, and the Gradle course takes the rest. The Spock lessons cover the labels the specification uses, and the command line lesson covers CliBuilder.
Related
- CodeNarc: the linter and its ruleset.
- @Grab and command line scripts: the options of the tool.
- Gradle: the build the project grows into.
Автор статьи: Arthur Isaev