Skip to content

Instantly share code, notes, and snippets.

@tobyhede
Forked from netzpirat/0_README.md
Created April 13, 2011 04:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobyhede/916955 to your computer and use it in GitHub Desktop.
Save tobyhede/916955 to your computer and use it in GitHub Desktop.

Continuous CoffeeScript testing with Guard and Jasmine

This Gist shows how to set up a Rails project to practice BDD with CoffeeScript, Guard and Jasmine. You can see this setup in action on Vimeo

  • Install Gems with Bundler with bundle install
  • Define your guards with mate Guardfile
  • Initialize Jasmine with bundle exec jasmine init
  • Configure Jasmine with mate spec/support/yasmine.ym
  • Start Guard with bundle exec guard
  • Start Jasmine with bundle exec rake jasmine
  • Open your browser with open http://localhost:8888
  • Press the LiveReload button
  • Write your specs (example app_coffeescripts_math.coffee goes to app/coffeescripts/math.coffee)
  • See the Growl notifications
  • Write your code (example spec_coffeescripts_math_spec.coffee goes to app/coffeescripts/math_spec.coffee)
  • Watch the Jasmine report

That's it

gem 'guard'
gem 'guard-coffeescript'
gem 'guard-livereload'
gem 'jasmine'
guard 'coffeescript', :output => 'public/javascripts/compiled' do
watch('^app/coffeescripts/(.*)\.coffee')
end
guard 'coffeescript', :output => 'spec/javascripts' do
watch('^spec/coffeescripts/(.*)\.coffee')
end
guard 'livereload' do
watch('^spec/javascripts/.+\.js$')
watch('^public/javascripts/compiled/.+\.js$')
end
src_dir: public/javascripts
src_files:
- underscore.js
- compiled/math.js
spec_dir: spec/javascripts
spec_files:
- **/*_spec.js
describe 'Math:', ->
describe 'fib()', ->
it 'should calculate the numbers correctly up to fib(16)', ->
fib = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
expect(Math.fib(i)).toEqual fib[i] for i in [0..16]
describe 'uuid()', ->
it 'should have the proper UUID format', ->
expect(Math.uuid()).toMatch /[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{13}/
it 'should have always the numer 4 at position 14', ->
expect(Math.uuid()).toMatch /[A-Z0-9]{8}-[A-Z0-9]{4}-4[A-Z0-9]{3}-[A-Z0-9]{4}-[A-Z0-9]{13}/
expect(Math.uuid()).toMatch /[A-Z0-9]{8}-[A-Z0-9]{4}-4[A-Z0-9]{3}-[A-Z0-9]{4}-[A-Z0-9]{13}/
expect(Math.uuid()).toMatch /[A-Z0-9]{8}-[A-Z0-9]{4}-4[A-Z0-9]{3}-[A-Z0-9]{4}-[A-Z0-9]{13}/
it 'should generate a unique uuid for 1000 generated uuids at least', ->
uuids = []
counter = 0
while counter < 1000
uuids.push Math.uuid()
counter++
expect(uuids.length).toEqual _.uniq(uuids).length
#
# Calculate the fibonacci numbers
#
# Warning: This script can go really slow if you try to calculate the number with n > 20
#
Math.fib = (n) ->
s = 0
return s if n == 0
if n == 1
s += 1
else
Math.fib(n - 1) + Math.fib(n - 2)
#
# Generate a RFC 4122 GUID
#
# See section 4.4 (Algorithms for Creating a UUID from Truly Random or
# Pseudo-Random Numbers) for generating a GUID, since we don't have
# hardware access within JavaScript.
#
# More info:
#
# - http://www.rfc-archive.org/getrfc.php?rfc=4122
# - http://www.broofa.com/2008/09/javascript-uuid-function/
# - http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
#
Math.uuid = ->
chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
uuid = new Array(36)
random = 0
for digit in [0..36]
switch digit
when 8, 13, 18, 23
uuid[digit] = '-'
when 14
uuid[digit] = '4'
else
random = 0x2000000 + (Math.random() * 0x1000000) | 0 if (random <= 0x02)
r = random & 0xf
random = random >> 4
uuid[digit] = chars[if digit == 19 then (r & 0x3) | 0x8 else r]
uuid.join('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment