Testing@LMAX – Replacements in DSL
By Adrian Sutton
Given our DSL makes heavy use of aliases, we often have to provide a way to include the real name or ID as part of some string. For example, an audit record for a new account might be:
Created account 127322 with username someUser123.
But in our acceptance test we’d create the user with:
someUser is just an alias, the DSL creates a unique username to use and the system assigns a unique account ID that the DSL keeps track of for us. So to write a test that the new account audit record is written correctly, we need a way to build the expected audit string.
Our initial attempt was straight forward enough:
"expression: Created account <accountId> with username <username>.",
The DSL substitutes
So a little while back some of my clever colleagues changed things around to a slightly more complex but much more flexible syntax:
"expression: Created account <accountId:someUser> with username <username:someUser>."
Essentially, the replacement now contains both the type of value to insert as well as the alias to look up. This has been a minor revolution for our DSL – it’s now much easier to handle all sorts of complex cases and it’s much clearer which alias is being used for each replacement.
The biggest win though, is that because all the required information to perform the expansions is inside the one string – not requiring any extra DSL parameters – the replacement algorithm can be easily shared across all the different places in the DSL that need to support replacements. New types of things to be replaced can easily be added in one place and are then available everyone as well.
It’s surprising how much benefit you can get from such a simple little change.