Created
November 20, 2011 04:35
-
-
Save cpsubrian/1379800 to your computer and use it in GitHub Desktop.
Parse readable string vows into should.js assertions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
This is what the below example produces. | |
### | |
vows = require 'vows' | |
should = require 'should' | |
# Create the test suite. | |
suite = vows.describe('QuickVows') | |
# Test quickVows | |
suite.addBatch | |
'Testing quickVows': | |
topic: {name: 'myTopic', size: 15, colors: ['blue', 'green']} | |
'it should have property name': (topic) -> | |
topic.should.have.property('name') | |
'it should not have property age': (topic) -> | |
topic.should.not.have.property('age') | |
'the name should equal myTopic': (topic) -> | |
topic.name.should.equal('myTopic') | |
'its size should equal 15': (topic) -> | |
topic.size.should.equal(15) | |
'colors should be an instanceof Array': (topic) -> | |
topic.colors.should.be.an.instanceof(Array) | |
'the colors should eql ["blue", "green"]': (topic) -> | |
topic.colors.should.eql(['blue', 'green']) | |
# Export the test suite | |
suite.export module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
Example usage of quick-vows.coffee. | |
### | |
vows = require 'vows' | |
quickVows = require './quick-vows' | |
# Create the test suite. | |
suite = vows.describe('QuickVows') | |
# Test quickVows | |
suite.addBatch | |
'Testing quickVows': quickVows | |
topic: {name: 'myTopic', size: 15, colors: ['blue', 'green']} | |
'it should have property': 'name' | |
'it should not have property': 'age' | |
'the name should equal': 'myTopic' | |
'its size should equal': 15 | |
'colors should be an instanceof': Array | |
'the colors should eql': ['blue', 'green'] | |
# Export the test suite | |
suite.export module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
♢ QuickVows | |
Testing quickVows | |
✓ it should have property 'name' | |
✓ it should not have property 'age' | |
✓ the name should equal 'myTopic' | |
✓ its size should equal '15' | |
✓ colors should be an instanceof 'function Array() { [native cod...' | |
✓ the colors should eql 'blue,green' | |
✓ OK » 6 honored (0.049s) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
Parse string vows into should.js assertions. | |
Example Usage: | |
vows.describe('quickVows Example').addBatch | |
'My context description': quickVows | |
topic: {name: 'myTopic', size: 15, colors: ['blue', 'green']} | |
'it should have property': 'name' | |
'the name should equal': 'myTopic' | |
'its size should equal': 15 | |
'colors should be an instanceof': Array | |
'the colors should eql': ['blue', 'green'] | |
In the vows property keys, 'the', 'it' and 'its' (at the beginning of the vow) | |
will be replaced with the topic. The above example translates directly to: | |
vows.describe('quickVows Example').addBatch | |
'My context description': | |
topic: {name: 'myTopic', size: 15, colors: ['blue', 'green']} | |
"the name should equal 'myTopic': (topic) -> | |
topic.name.should.equal 'myTopic' | |
"the size should equal '15': (topic) -> | |
topic.size.should.equal 15 | |
"the colors should be an instanceof 'function Array() { [native code] }": (topic) -> | |
topic.colors.should.be.an.instanceof Array | |
"the colors should eql 'blue,green'": (topic) -> | |
topic.colors.should.eql ['blue', 'green'] | |
Pretty cool huh? | |
### | |
module.exports = (convert) -> | |
context = {} | |
for own vow, arg of convert | |
do (vow, arg) -> | |
if vow is 'topic' | |
context.topic = arg | |
else | |
argString = "#{arg}" | |
argString = argString.replace(/\n*/g, '') | |
argString = argString.substr(0, 30) + '...' if argString.length > 30 | |
context["#{vow} '#{argString}'"] = (topic) -> | |
obj = topic | |
scope = {} | |
for part in vow.split(' ') | |
if not /^(the|it|its)$/.test part | |
obj = obj[part] | |
if part is 'should' | |
scope = obj | |
if typeof obj is 'function' | |
obj.call scope, arg | |
else if /^(empty|arguments|ok|true|false)$/.test part | |
# The assertion has already been made. | |
else | |
throw new Error 'quickVow did not resolve into a function' | |
context |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment