Permalink
Browse files

Merge branch 'tests'

* tests:
  private method test
  auterequire test
  Added notes about testing
  Added basic testing using mocha and chai
  Added rake task to run test suite
  Ignore node_modules
  ignore binstubs
  • Loading branch information...
2 parents a9b214c + a8e8a45 commit a745aa7722611a63a059163ad40e995292272652 @gudleik gudleik committed May 21, 2013
Showing with 123 additions and 13 deletions.
  1. +2 −0 .gitignore
  2. +12 −0 README.md
  3. +5 −0 Rakefile
  4. +2 −0 autoexec_bat.gemspec
  5. +16 −13 lib/assets/javascripts/autoexec_bat.coffee
  6. +84 −0 test/autoexecBatTest.coffee
  7. +2 −0 test/mocha.opts
View
2 .gitignore
@@ -15,3 +15,5 @@ spec/reports
test/tmp
test/version_tmp
tmp
+/bin/
+/node_modules/
View
12 README.md
@@ -105,6 +105,18 @@ For Rails 3:
jQuery ->
$('[data-autoexec]').autoexec()
+## Testing
+
+Install mocha and chai:
+
+ # npm install -g mocha coffee-script
+ # npm install chai jack
+
+And run the test suite:
+
+ # mocha -R spec --watch (or)
+ # rake test
+
## Contributing
1. Fork it
View
5 Rakefile
@@ -5,3 +5,8 @@ desc 'Compile coffee source to JavaScript (requires coffee binary)'
task :compile do
`coffee -o . lib/assets/javascripts/autoexec_bat.coffee`
end
+
+desc 'Test'
+task :test do
+ system "mocha -R spec"
+end
View
2 autoexec_bat.gemspec
@@ -14,4 +14,6 @@ Gem::Specification.new do |gem|
gem.name = "autoexec_bat"
gem.require_paths = ["lib"]
gem.version = AutoexecBat::VERSION
+
+ gem.add_development_dependency "rake"
end
View
29 lib/assets/javascripts/autoexec_bat.coffee
@@ -1,45 +1,47 @@
-root = exports ? window
-
AutoexecBat =
topLevel: "App"
- debug: true
- # autoRequire: null
-
+ debug: false
+ autoRequire: false
+
log: (msg) ->
console?.log msg if AutoexecBat.debug
+ require: (dependencies) ->
+ (AutoexecBat.initializeModule(lib) for lib in dependencies) if dependencies
+
define: (name) ->
dependencies = []
dependencies.push AutoexecBat.autoRequire if AutoexecBat.autoRequire && name != AutoexecBat.autoRequire
for arg in arguments
- if typeof arg is "object"
+ if typeof arg is "object"
for module in arg
dependencies.push module unless module in dependencies && module != name
- else if typeof arg is "function"
+ else if typeof arg is "function"
block = arg
block ?= -> # empty function
- top = AutoexecBat.topLevel
+ top = AutoexecBat.topLevel
target = AutoexecBat.namespace name
target.name = name
target.loaded = false
target.autoexec = -> # make sure the autoexec function exists
target.dependencies = dependencies
target.init = (options) ->
+ options or=
+ idempotent: false
+ callee: null
+
unless options.idempotent and @loaded
- require dependencies
+ AutoexecBat.require dependencies
@autoexec(options.callee) if typeof @autoexec is 'function'
if options.idempotent
@autoexec = -> AutoexecBat.log "Module already initialized"
@loaded = true
- block target, top
-
- require: (dependencies) ->
- (AutoexecBat.initializeModule(lib) for lib in dependencies) if dependencies
+ block target, top
namespace: (name) ->
target = root
@@ -66,6 +68,7 @@ AutoexecBat =
# Globals
+root = exports ? window
root.AutoexecBat = AutoexecBat
root.define = AutoexecBat.define
root.require = AutoexecBat.require
View
84 test/autoexecBatTest.coffee
@@ -0,0 +1,84 @@
+chai = require 'chai'
+jack = require 'jack'
+
+chai.use jack.chai
+should = chai.should()
+expect = chai.expect
+
+autoexec = require '../lib/assets/javascripts/autoexec_bat'
+
+describe 'AutoexecBat', ->
+
+ describe "global variables", ->
+ it "should define AutoexecBat", -> autoexec.AutoexecBat.should.be.a 'object'
+ it "should have a define function", -> autoexec.define.should.be.a 'function'
+ it "should have a require function", -> autoexec.require.should.be.a 'function'
+ it "should have a namespace function", -> autoexec.namespace.should.be.a 'function'
+
+ describe "namespace", ->
+
+ it "declares function namespaces", ->
+ autoexec.namespace('App.Foo.Bar').should.equal autoexec.App.Foo.Bar
+ autoexec.namespace('App.Foo').should.equal autoexec.App.Foo
+ autoexec.namespace('App.Foo').should.be.a 'object'
+
+ describe "define", ->
+ beforeEach ->
+ autoexec.define 'App.Foo', (exports) ->
+ exports.autoexec = ->
+ "do something"
+
+ privateMethod = ->
+ 'I am hidden'
+
+ it "defines a function that can be called", ->
+ autoexec.App.Foo.autoexec().should.equal "do something"
+
+ it "can be executed", ->
+ autoexec.AutoexecBat.run('App.Foo').should.equal true
+
+ it "private methods are unavailable", ->
+ expect(autoexec.App.Foo.privateMethod).to.not.be.ok
+
+ it "should not be loaded", ->
+ autoexec.App.Foo.loaded.should.equal false
+
+ describe "dependencies", ->
+ it "should be empty when not defined", ->
+ autoexec.App.Foo.dependencies.should.be.empty
+
+ it "can be specified", ->
+ autoexec.define 'App.Bar', ["App.Foo"], (exports) ->
+ autoexec.App.Bar.dependencies.should.deep.equal(["App.Foo"])
+
+ it "ignores non-existing dependencies", ->
+ autoexec.define 'App.Bar', ["App.Bullshit"], (exports) ->
+ autoexec.App.Bar.dependencies.should =~ ['App.Bullshit']
+
+ describe "autoRequire", ->
+
+ it "should be disabled by default", ->
+ autoexec.AutoexecBat.autoRequire.should.not.be.ok
+ autoexec.App.Foo.dependencies.should.be.empty
+
+ it "autoRequired modules are automatically added to the dependency list", ->
+ autoexec.AutoexecBat.autoRequire = "App.Always"
+ autoexec.define "App.Bar", -> exports.autoexec = -> 'something here'
+ autoexec.App.Bar.dependencies.should.include "App.Always"
+
+ it "has support for idempotency"
+
+ it "knows the callee"
+
+ describe "require", ->
+ beforeEach ->
+ autoexec.define 'App.Foo', (exports) ->
+ exports.autoexec = -> "do something"
+
+ it "loads the modules"
+
+ it "ignores undefined modules", ->
+ autoexec.require(["App.BareFoot"])
+
+ it "ignores empty argument", ->
+ autoexec.require()
View
2 test/mocha.opts
@@ -0,0 +1,2 @@
+--compilers coffee:coffee-script
+

0 comments on commit a745aa7

Please sign in to comment.