Instant Markdown book released

I got an opportunity to do the technical review of Instant Markdown book written by Arturo Herrero.
Instant Markdown cover

The book is divided in three parts. First part is about Markdown to HTML processor installation. You can skip it if you won’t generate HTML from manually.

Second part contains the complete language reference in case you want have it at your fingertips. What I miss from this part are examples with a side-by-side comparison of Markdown markup – generated HTML – rendered document, like in many desktop Markdown editors. You would get better grasp of the relationship between Markdown syntax, resulting HTML tags and visual representation of the document.

I enjoyed the third part the most. It tells you about cool applications of Markdown. I already knew GitHub, its comments and readme pages. But there are other useful online services when you can use Markdown to write and publish blog posts and documents.

In short, it is a quick but entertaining reading, like any book from the Instant series. On less that 50 pages, you get a complete Markdown syntax reference and some useful hints.

cut + grep + awk + sed in action; prerequisite: Git repository:

git status -s | \
cut -c4- | \
grep "$test_type.*[Test|Spec]" | \
awk 'BEGIN { FS = "/" } ; { print ($(NF)) }' | \
sed 's/\.groovy//' | tr '\n' ' '

Find all dirty Grails tests

Example – delete lines containing @SuppressWarnings('DuplicateStringLiteral') string in all test files:

sed -i '' "/@SuppressWarnings('DuplicateStringLiteral')/d" test/**/*.groovy

Find text and delete every containing line with sed

Hamcrest matchers in Spock

Working with Spock we forget about a hidden gem – Hamcrest matchers. They are assertions turned into method objects. A matcher is usually parametrized with an expected value and is executed latel on the actual one.

When can we use it? For example in a table-driven specification with expected values in the table.

An expected value can be one of the following:

  • exact value
  • ‘not available’ text
  • any number

How can we express these assertions without Hamcrest?

then:
if (exactValue) {
    assert field == exactValue
} else if (notAvailable) {
    assert field == messageSource.getMessage('na', null, LocaleContextHolder.locale)
} else if (isNumber) {
    assert field.number
} else {
    assert false, 'Wrong test configuration'
}

where:
exactValue | notAvailable | isNumber
'12345'    | null         | null
null       | true         | null
null       | null         | true

Bufff…

  • there is a lot of boilerplate code
  • the specification is misleading – what does it mean that expected exactValue is null? Is it really null? Or simply this condition should not be checked?

How can we refactor this code using Hamcrest matchers?

then:
that field, verifiedBy

where:
verifiedBy << [
    org.hamcrest.CoreMatchers.equalTo('12345')
    notAvailable(),
    isNumber(),
]

...

def notAvailable() {
    org.hamcrest.CoreMatchers.equalTo messageSource.getMessage('na', null, LocaleContextHolder.locale)
}

def isNumber() {
    [
        matches: { actual -> actual.number },
        describeTo: { Description description -> 'should be a number' }
    ] as BaseMatcher
}

Result – shorter and more expressive feature method code.

You can find the full source code with mocked messageSource in this gist. For more information about Hamcrest matchers, check Luke Daley’s article.

Nesting categories in Groovy

Recently I discovered that Groovy categories can be nested. It means, you can inject a category into another one:

@Category(GroovyObject)
class FirstCategory {
    def method1() { }
}

@Category(GroovyObject)
@Mixin(FirstCategory)
class SecondCategory {
    def method2() {
        method1()
    }
}

@Mixin(SecondCategory)
class Mixee {
    def yetAnotherMethod() {
        method2()
    }
}

Before that ‘discovery’ I was injecting all categories into the final class. My code was not expressing the explicit dependency between the second and the first category:

@Mixin([FirstCategory SecondCategory)
class Mixee

Happy mixin’!

Follow

Get every new post delivered to your Inbox.

Join 389 other followers