coat - COntract And Test
Than coat project was born. The idea is to create a DSL to describe class contract and having a preprocessor creating:
- the class skeleton code with both pre-conditions and post-conditions for each method
- a lightweight documentation for each method extracted from the contract
- the test case to drive BDD
The coat language
The coat language will be a descriptive language with English keywords formatted using indentation like Python programming language.
The language will be:
- easy (both to read than to use)
- compact (very few keywords, I promise... just to describe class contract with the world, I don't want to get into your implementation details)
A classic that never goes old fashioned is the hello world project.
Since coat is not born to produce bytecode or native executable machine code, the goal is to describe how it has to behave a class that writes "Hello world" as standard output.
Let's look at our first coat program.
\# As in ruby, this is a comment. We will follow the same naming convention \# used in ruby to translate filenames in class. contract HelloWorld: \# Classes can have pre and post conditions as well. \# Pre conditions can be environment variables or command line switches \# Usually post conditions deal with output or they can be empty if there is \# no particular output or if your class is not design to be executed from command \# line as example. pre: none \# Here "read", "from" and "stout" are coat keywords to be used to build the \# BDD spec file. post: read "Hello world" from stdout \# We declare here the public apis for the HelloWorld class. \# An empty newline will be the methods separator api: \# A public say_hello method here with pre and post conditions. I don't \# care about how you would like to achieve it. True to be told, it's fine if \# after coat preprocessing, your hello_world.rb file won't to anything. \# \# Remember, you must look for hello_world_spec.rb and use BDD. def say_hello pre: none post: "Hello world"
This would be the contract for an "Hello world" project. Now it's time to run coat creating class and tests.
$ coat hello_world.coat
Two file will be created in the same directory you launched coat:
- hello_world.rb containing the "HelloWorld" class skeleton
- hello_world_spec.rb containing the test cases for the "HelloWorld" class
Then you can start your BDD or TDD (both of them are fine) work over the "Hello World" project.