androidengineers.Book a session

DSL Creation and Metaprogramming

DSL Testing & Docs

article15 minHard

Test the model your DSL promises

A DSL needs ordinary behavior tests and, where useful, compile-time examples showing invalid syntax. Prefer asserting the constructed model over whitespace-sensitive output snapshots unless formatting itself is the contract.

data class Plan(val title: String, val lessons: List<String>)

fun validate(plan: Plan): Plan {
    require(plan.title.isNotBlank())
    require(plan.lessons.isNotEmpty())
    require(plan.lessons.distinct().size == plan.lessons.size)
    return plan
}

Tests should cover minimal valid configuration, nested configuration, duplicate entries, missing required fields, and mutation leakage after building. Documentation examples should compile in a test fixture so API changes do not leave stale sample syntax.

Exercise

Write tests for the validator, then connect it to a builder. Save a built result, reuse or modify the builder if supported, and verify whether the earlier result stays stable according to the documented contract.

Check: a DSL example that intentionally fails compilation must be labeled and tested separately from successful examples, rather than copied into the production source set.

Reference: Builders

YOUR LEARNING JOURNEY

0 of 110 available lessons completed

Progress saved in this browser. No account needed.
DSL Testing & Docs | Kotlin Core Programming | Android Engineers