Skip to content

Instantly share code, notes, and snippets.

@dchelimsky
Forked from garybernhardt/gist:305174
Created February 16, 2010 09:57
Show Gist options
  • Save dchelimsky/305419 to your computer and use it in GitHub Desktop.
Save dchelimsky/305419 to your computer and use it in GitHub Desktop.
# Here are a couple of alternative suggestions for PSpec (see http://gist.github.com/305174).
# I'm not in love with any of these ideas, but hopefully they'll provide some food for thought.
#
# I like using strings for test names, so I like the 'with xxx' syntax, but I think that
# the 'with describe' doesn't makke sense - how about 'with subject':
with subject('calculator'):
with expectation('adds positive integers'):
expect(Calculator.add(1, 1)) == 2
with expectation('adds negative integers'):
expect(Calculator.add(-1, -2)) == -3
with expectation('adds positive and negative integers'):
expect(Calculator.add(1, -2)) == -1
# that's admittedly verbose, but could yield output like this:
#
# calculator
# adds positive integers
# adds negative integers
# adds positive and negative integers
# If deeper nesting is possible, then we could have this:
with subject('calculator'):
with context('adding integers'):
with expectation('adds positive integers'):
expect(Calculator.add(1, 1)) == 2
with expectation('adds negative integers'):
expect(Calculator.add(-1, -2)) == -3
with expectation('adds positive and negative integers'):
expect(Calculator.add(1, -2)) == -1
with context('multiplying integers'):
with expectation('multiplies positive integers'):
expect(Calculator.multiply(2, 3)) == 6
# etc
# Again, verbose, but output could be:
# calculator
# adding integers
# adds positive integers
# adds negative integers
# adds positive and negative integers
# multiplying integers
# multiplies positive integers
# Perhaps we just need a shorter word for expectation: goal?
with subject('calculator'):
with context('adding integers'):
with goal('adds positive integers'):
expect(Calculator.add(1, 1)) == 2
with goal('adds negative integers'):
expect(Calculator.add(-1, -2)) == -3
with goal('adds positive and negative integers'):
expect(Calculator.add(1, -2)) == -1
with context('multiplying integers'):
with goal('multiplies positive integers'):
expect(Calculator.multiply(2, 3)) == 6
# Another thought is to have the expect method actually do the work of the
# lowest level of this nesting:
with subject('calculator'):
with context('adding integers'):
expect(Calculator.add(1, 1)) == 2
expect(Calculator.add(-1, -2)) == -3
expect(Calculator.add(1, -2)) == -1
with context('multiplying integers'):
expect(Calculator.multiply(2, 3)) == 6
# Not sure if that's even possible, but if the source line could be read in,
# then the output could be:
# calculator
# adding integers
# expect(Calculator.add(1, 1)) == 2
# expect(Calculator.add(-1, -2)) == -3
# expect(Calculator.add(1, -2)) == -1
# multiplying integers
# expect(Calculator.multiply(2, 3)) == 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment