Wednesday, 18 February 2009

A couple of Specs

Here are a couple of snippets of sample code for lancelot.

First up, a standalone function:
import lancelot

def fib(ordinal=0):
    '''Simple fibonacci generator'''

def specify_fib_zero_to_five():
    '''First five fibonacci numbers should be 1,2,3,5,8'''
    spec = lancelot.Spec(fib)

Next, a standalone class:
import lancelot

class Stack:
    '''A simple stack with push, pop and peek'''

def behavior_of_stack_with_values():
    '''Should be able to peek() and pop() values after push()'''
    stack = lancelot.Spec(Stack, given=new_stack)

Finally, a class whose behaviour involves collaborations with other classes:
import lancelot

class Observable:
    '''Simple class that sends notifications to its observers'''

def observable_observer_behaviour():
    '''Added Observers should receive Observer notifications.'''
    observer = lancelot.MockSpec()
    observable = Observable()
    spec = lancelot.Spec(observable)

No comments:

Post a Comment