Permalink
Browse files

Added basic testing using mocha and chai

  • Loading branch information...
gudleik committed May 21, 2013
1 parent 5e5b6b1 commit dee861982e0127069603a346d529ddf0bf252494
Showing with 82 additions and 12 deletions.
  1. +15 −12 lib/assets/javascripts/autoexec_bat.coffee
  2. +65 −0 test/autoexecBatTest.coffee
  3. +2 −0 test/mocha.opts
@@ -1,45 +1,47 @@
-root = exports ? window
-
AutoexecBat =
topLevel: "App"
- debug: true
+ debug: false
# autoRequire: null
-
+
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
@@ -0,0 +1,65 @@
+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"
+
+ it "defines a function", ->
+ autoexec.App.Foo.autoexec().should.equal "do something"
+
+ it "can be executed", ->
+ autoexec.AutoexecBat.run('App.Foo').should.equal true
+
+ 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']
+
+ it "autoRequired modules are automatically loaded"
+
+
+ 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
@@ -0,0 +1,2 @@
+--compilers coffee:coffee-script
+

0 comments on commit dee8619

Please sign in to comment.