Sunday, January 31, 2010

Gospecify: Basic setup of the project's structure

During the last two days I played a bit with GoSpecify by Samuel Tesla, a BDD framework for the Go Language. I'm coming from 4 years of intense Ruby development and, you know, I could not live without a good BDD framework. After watching Samuel Google TechTalk I decided to explore BDD under Go. The first problem I faced and solved was with basic files and folders setup. In this post I'll describe how I managed the issue using project structure and makefiles taken from gospecify project itself.

First of all, let's create the project folder
mkdir fib
Yeah, you guessed right, we'll deal with yet another Fibonacci sequence implementation ;)

Now, move in fib folder and create a basic project structure
cd fib
mkdir src
mkdir spec
As you guess src will contain source files and spec will contain the specs. Now let's go creating a Makefile in the project base path. Create an empty Makefile using your favourite text editor and copy/paste the code below:
These are the up-level's rules with which we will interact. Names are self-explanatory however I'll describe the corresponding actions below:
make clean
Clean up project folder removing intermediate and backup files

make test
Run the specs

make format
Format the source files using gofmt

make package
Build the package
As you can see, some of the rules above assume there is another Makefile inside src/ folder. Thus, move on src/ and create the following Makefile:
Note that we have to set the PACKAGE variable with the name of the package (fib in this case). Also, note that the testpackage rule in the Makefile above will create a package named test$(PACKAGE).a. So, in our case, we will get src/testfib.a. We will import this package in our test code to actually run the specs.

At this point, the basic project structure and makefiles are done. Now, we can populate src/ folder with an empty source file (with just the basic package clause within):
cd src
echo "package fib" > fib.go
Now, following BDD convention (test-first approach), let's write our test. Enter in spec/ folder and create spec/fib_spec.go with the code below:
The spec above tell us how we expect the system behaves before actually implement it. In particular, we setup two seed values N0 and N1 and then we query fib about the next value in the sequence using fib.Next() method. For the first five calls we expect the sequence: 1, 1, 2, 3, 5.

If we try to run the specs at this point we get an error of course (we haven't an implementation yet):
$ make
...
fib_spec.go:9: undefined: testfib.Fib
...
Open the text editor and paste in src/fib.go the code below:
If you're new to Go, take your time reading the code. Also consider that I'm still learning this language from the ground up so this could not be the best idiomatic implementation (waiting for suggestions in this case). BTW, for the purpose of this post, our code behave as we expect. In fact, running the specs
$ make test
.
Passing: 1 Failing: 0 Pending: 0 Errors: 0
they all pass.

Well, that's all for now! You can browse the source files used in this post on github.

5 comments:

  1. Hey, I just found this via the new Go blog.

    I notice that you're reusing the makefiles from gospecify for your project. One thing you can do to simplify your build process is eliminate the testpackage dance that they do. I did that so that I could use an installed version of gospecify to test my development version of gospecify. They need to have different package names in order to actually be separate. For any other project, you could just leave the testpackage target out and build your code normally in the src directory.

    ReplyDelete
  2. Hi Samuel, thank you very much for pointing it out. I cleaned up the makefiles as you suggested. Now they're a lot more readable.

    ReplyDelete
  3. Hmm , very interesting thoughts! I think In order for more interested people to read your thoughts, you should post it on Twitter, and don`t forget to buy twitter followers and quickly increase their amount.

    ReplyDelete