Skip to content

Commit

Permalink
Added first set of tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzedge committed Nov 6, 2011
1 parent 4324da6 commit f190995
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
10 changes: 8 additions & 2 deletions Cakefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class BuildUtils
console.log string.as_console_message()

error_handler: (err, stdout, stderr) ->
throw err if err
console.log stdout if stdout
console.log stderr if stderr
throw err if err

process: (content, callback) ->
self = @
Expand All @@ -35,7 +35,7 @@ class BuildUtils
content = new Array()

for file, index in files then do (file, index) ->
if file.indexOf('.') isnt 0
unless file.indexOf('.') is 0
fs.readFile "./src/#{file}", 'utf8', (err, fileContent) ->
self.error_handler err
content[content.length] = fileContent
Expand All @@ -59,3 +59,9 @@ task 'minify', 'Minify the generate jquery.rambling.slider files', ->
(err, stdout, stderr) ->
utils.error_handler err, stdout, stderr
utils.log 'Done'

task 'spec', 'Run all specs', ->
utils.log 'Running specs...'
exec 'jasmine-node --coffee spec/', (err, stdout, stderr) ->
utils.error_handler err, stdout, stderr
utils.log 'Done' unless err
14 changes: 14 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ If you want to minify the existing js:

<code>cake minify</code>

### Unit testing

If you want to run the unit tests, you'll need to install <code>jasmine-node</code>. It's easily installed with:

<code>npm install -g jasmine-node</code>

To run all the tests, you can run:

<code>jasmine-node --coffee spec/</code>

Or simply:

<code>cake spec</code>

## License and copyright

Copyright (c) 2011 Edgar Gonzalez
Expand Down
18 changes: 18 additions & 0 deletions spec/array_extensions.spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require '../src/array_extensions'

describe 'Array Extensions', ->
describe 'when shuffling an array', ->
original_array = null
first_copy = null
second_copy = null

beforeEach ->
original_array = [1..100]
first_copy = original_array
second_copy = original_array.shuffle()

it 'should return a copy with shifted the positions', ->
expect(second_copy).not.toEqual original_array

it 'should let the original array untouched', ->
expect(first_copy).toEqual original_array
8 changes: 5 additions & 3 deletions src/array_extensions.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
Array::shuffle = ->
new_array = []
for i in [@length..1]
new_array[i] = @[i]
j = parseInt(Math.random() * i)
x = @[--i]
@[i] = @[j]
@[j] = x
@
new_array[i] = @[j]
new_array[j] = x
new_array

0 comments on commit f190995

Please sign in to comment.