diff --git a/lib/assets/javascripts/jasmine-gwt.js.coffee b/lib/assets/javascripts/jasmine-gwt.js.coffee index 77ccfb9..4a38997 100644 --- a/lib/assets/javascripts/jasmine-gwt.js.coffee +++ b/lib/assets/javascripts/jasmine-gwt.js.coffee @@ -1,174 +1,8 @@ -jasmine.GWT = - Step: (type, name, parameter) -> - if scenario = jasmine.GWT.currentScenario_ - scenario[type](name, parameter) - else - jasmine.GWT.Steps[type] ||= [] - jasmine.GWT.Steps[type].push([ name, parameter ]) - Hook: (type, code) -> - jasmine.GWT.Hooks[type] ||= [] - jasmine.GWT.Hooks[type].push(code) - - runHook: (type, context) -> - for code in (jasmine.GWT.Hooks[type] || []) - code.apply(context) - - Steps: {} - Hooks: {} - -class jasmine.GWT.Feature - constructor: (@name, code) -> - jasmine.GWT.currentFeature_ = this - - @background = null - code.apply(this) - - jasmine.GWT.currentFeature_ = null - - Scenario: (name, code) -> - new jasmine.GWT.Scenario(name, code, this) - - Background: (code) -> - @background = new jasmine.GWT.Background(code) - -class jasmine.GWT.Background - constructor: (code) -> - @statements = [] - - jasmine.GWT.currentScenario_ = this - - code.apply(this) - - jasmine.GWT.currentScenario_ = null - - Given: (name, code) -> - @statements.push([ 'Given', name, code ]) - When: (name, code) -> - @statements.push([ 'When', name, code ]) - Then: (name, code) -> - @statements.push([ 'Then', name, code ]) - -class jasmine.GWT.Scenario extends jasmine.GWT.Background - constructor: (scenarioName, code, @feature) -> - super(code) - - _statements = this.allStatements() - _this = this - - describe @feature.name, -> - it scenarioName, -> - jasmine.GWT.runHook('Before', _this) - - for [ type, name, param ] in _statements - _this._type = type - _this._name = name - - runCode = (param, args = []) -> - resultsIndex = jasmine.getEnv().currentSpec.results().getItems().length - - param.apply(_this, args) - - for index in [ resultsIndex...(jasmine.getEnv().currentSpec.results().getItems().length) ] - result = jasmine.getEnv().currentSpec.results().getItems()[index] - result.message = "[#{type} #{name}] " + result.message - - args = [] - - if param? - if typeof param == "function" - runCode(param) - continue - else - args = [ param ] - - found = false - - if jasmine.GWT.Steps[type] - for [ match, code ] in jasmine.GWT.Steps[type] - if match.constructor == RegExp - if result = name.match(match) - result.shift() - runCode(code, result.concat(args)) - found = true - - break - else - if name == match - runCode(code) - found = true - - break - - if !found - output = [ "", "No step defined for #{type} #{name}. Define one using the following:", '' ] - - argCount = args.length - - name = name.replace /\d+/g, (match) -> - argCount += 1 - "(\\d+)" - - name = name.replace /\"[^"]+\"/g, (match) -> - argCount += 1 - '"([^"]+)"' - - if argCount == 0 - output.push("#{type} /^#{name}$/, ->") - else - args = ("arg#{i}" for i in [ 1..(argCount) ]) - - output.push("#{type} /^#{name}$/, (#{args.join(', ')}) ->") - - output.push(" @not_defined()") - output.push("") - - fakeResult = new jasmine.ExpectationResult( - matcherName: 'steps', - passed: false, - expected: 'to be defined', - actual: "#{type} #{name}", - message: output.join("\n") - ) - - jasmine.getEnv().currentSpec.addMatcherResult(fakeResult) - - jasmine.GWT.runHook('After', _this) - - allStatements: => - allStatements = [] - - if @feature.background? - allStatements = @feature.background.statements - - allStatements.concat(@statements) - - not_defined: => - fakeResult = new jasmine.ExpectationResult( - matcherName: 'step', - passed: false, - expected: 'to be defined', - actual: "#{@_type} #{@_name}", - message: "has no code defined." - ) - - jasmine.getEnv().currentSpec.addMatcherResult(fakeResult) - -jasmine.GWT.globals = - Feature: (args...) -> - new jasmine.GWT.Feature(args...) - - Background: (args...) -> - jasmine.GWT.currentFeature_.Background(args...) - - Scenario: (args...) -> - jasmine.GWT.currentFeature_.Scenario(args...) - - Given: (args...) -> jasmine.GWT.Step('Given', args...) - When: (args...) -> jasmine.GWT.Step('When', args...) - Then: (args...) -> jasmine.GWT.Step('Then', args...) - - Before: (args...) -> jasmine.GWT.Hook('Before', args...) - After: (args...) -> jasmine.GWT.Hook('After', args...) +#= require jasmine/GWT +#= require jasmine/GWT/Feature +#= require jasmine/GWT/Background +#= require jasmine/GWT/Scenario +#= require jasmine/GWT/globals for method, code of jasmine.GWT.globals (exports ? this)[method] = code diff --git a/lib/assets/javascripts/jasmine.HeadlessReporter.VerboseGWT.js.coffee b/lib/assets/javascripts/jasmine.HeadlessReporter.VerboseGWT.js.coffee new file mode 100644 index 0000000..e075cab --- /dev/null +++ b/lib/assets/javascripts/jasmine.HeadlessReporter.VerboseGWT.js.coffee @@ -0,0 +1,16 @@ +#= require jasmine.HeadlessReporter.Verbose + +class jasmine.HeadlessReporter.VerboseGWT extends jasmine.HeadlessReporter.Verbose + colorLine: (line, color) => + parts = line.split(' ') + type = parts.shift() + + switch type + when 'Given' + type.foreground('yellow') + ' ' + parts.join(' ').foreground(color) + when 'When' + type.foreground('cyan') + ' ' + parts.join(' ').foreground(color) + when 'Then' + type.foreground('magenta') + ' ' + parts.join(' ').foreground(color) + else + line.foreground(color) diff --git a/lib/assets/javascripts/jasmine/GWT.js.coffee b/lib/assets/javascripts/jasmine/GWT.js.coffee new file mode 100644 index 0000000..11e1786 --- /dev/null +++ b/lib/assets/javascripts/jasmine/GWT.js.coffee @@ -0,0 +1,17 @@ +jasmine.GWT = + Step: (type, name, parameter) -> + if scenario = jasmine.GWT.currentScenario_ + scenario[type](name, parameter) + else + jasmine.GWT.Steps[type] ||= [] + jasmine.GWT.Steps[type].push([ name, parameter ]) + Hook: (type, code) -> + jasmine.GWT.Hooks[type] ||= [] + jasmine.GWT.Hooks[type].push(code) + + runHook: (type, context) -> + for code in (jasmine.GWT.Hooks[type] || []) + code.apply(context) + + Steps: {} + Hooks: {} diff --git a/lib/assets/javascripts/jasmine/GWT/Background.js.coffee b/lib/assets/javascripts/jasmine/GWT/Background.js.coffee new file mode 100644 index 0000000..2fadd3b --- /dev/null +++ b/lib/assets/javascripts/jasmine/GWT/Background.js.coffee @@ -0,0 +1,17 @@ +class jasmine.GWT.Background + constructor: (code) -> + @statements = [] + + jasmine.GWT.currentScenario_ = this + + code.apply(this) + + jasmine.GWT.currentScenario_ = null + + Given: (name, code) -> + @statements.push([ 'Given', name, code ]) + When: (name, code) -> + @statements.push([ 'When', name, code ]) + Then: (name, code) -> + @statements.push([ 'Then', name, code ]) + diff --git a/lib/assets/javascripts/jasmine/GWT/Feature.js.coffee b/lib/assets/javascripts/jasmine/GWT/Feature.js.coffee new file mode 100644 index 0000000..3e63318 --- /dev/null +++ b/lib/assets/javascripts/jasmine/GWT/Feature.js.coffee @@ -0,0 +1,15 @@ +class jasmine.GWT.Feature + constructor: (@name, code) -> + jasmine.GWT.currentFeature_ = this + + @background = null + code.apply(this) + + jasmine.GWT.currentFeature_ = null + + Scenario: (name, code) -> + new jasmine.GWT.Scenario(name, code, this) + + Background: (code) -> + @background = new jasmine.GWT.Background(code) + diff --git a/lib/assets/javascripts/jasmine/GWT/Scenario.js.coffee b/lib/assets/javascripts/jasmine/GWT/Scenario.js.coffee new file mode 100644 index 0000000..de5258f --- /dev/null +++ b/lib/assets/javascripts/jasmine/GWT/Scenario.js.coffee @@ -0,0 +1,118 @@ +#= require jasmine/GWT/Background +# +class jasmine.GWT.Scenario extends jasmine.GWT.Background + constructor: (scenarioName, code, @feature) -> + super(code) + + _statements = this.allStatements() + _this = this + + describe @feature.name, -> + describe scenarioName, -> + _this.spies_ = [] + + for index in [ 0..._statements.length ] + [ type, name, param ] = _statements[index] + + _this._type = type + _this._name = name + + runCode = (param, index, args = []) -> + isLast = (index + 1) == _statements.length + isFirst = (index == 0) + + it "#{type} #{name}", -> + if isFirst + jasmine.GWT.runHook('Before', _this) + + this.spyOn = (args...) -> + this.spies_ = _this.spies_ + jasmine.Spec.prototype.spyOn.apply(this, args) + + this.removeAllSpies = -> + if isLast + jasmine.Spec.prototype.removeAllSpies.apply(_this) + + param.apply(_this, args) + + if isLast + jasmine.GWT.runHook('After', _this) + + args = [] + + if param? + if typeof param == "function" + runCode(param, index) + continue + else + args = [ param ] + + found = false + + if jasmine.GWT.Steps[type] + for [ match, code ] in jasmine.GWT.Steps[type] + if match.constructor == RegExp + if result = name.match(match) + result.shift() + runCode(code, index, result.concat(args)) + found = true + + break + else + if name == match + runCode(code, index) + found = true + + break + + if !found + output = [ "", "No step defined for #{type} #{name}. Define one using the following:", '' ] + + argCount = args.length + + name = name.replace /\d+/g, (match) -> + argCount += 1 + "(\\d+)" + + name = name.replace /\"[^"]+\"/g, (match) -> + argCount += 1 + '"([^"]+)"' + + if argCount == 0 + output.push("#{type} /#{name}/, ->") + else + args = ("arg#{i}" for i in [ 1..(argCount) ]) + + output.push("#{type} /#{name}/, (#{args.join(', ')}) ->") + + output.push(" @not_defined()") + output.push("") + + fakeResult = new jasmine.ExpectationResult( + matcherName: 'steps', + passed: false, + expected: 'to be defined', + actual: "#{type} #{name}", + message: output.join("\n") + ) + + jasmine.getEnv().currentSpec.addMatcherResult(fakeResult) + + allStatements: => + allStatements = [] + + if @feature.background? + allStatements = @feature.background.statements + + allStatements.concat(@statements) + + not_defined: => + fakeResult = new jasmine.ExpectationResult( + matcherName: 'step', + passed: false, + expected: 'to be defined', + actual: "#{@_type} #{@_name}", + message: "has no code defined." + ) + + jasmine.getEnv().currentSpec.addMatcherResult(fakeResult) diff --git a/lib/assets/javascripts/jasmine/GWT/globals.js.coffee b/lib/assets/javascripts/jasmine/GWT/globals.js.coffee new file mode 100644 index 0000000..f98e431 --- /dev/null +++ b/lib/assets/javascripts/jasmine/GWT/globals.js.coffee @@ -0,0 +1,28 @@ +jasmine.GWT.globals = + Feature: (args...) -> + new jasmine.GWT.Feature(args...) + + Background: (args...) -> + jasmine.GWT.currentFeature_.Background(args...) + + Scenario: (args...) -> + jasmine.GWT.currentFeature_.Scenario(args...) + + Given: (args...) -> + @_lastType = 'Given' + jasmine.GWT.Step(@_lastType, args...) + + When: (args...) -> + @_lastType = 'When' + jasmine.GWT.Step(@_lastType, args...) + + Then: (args...) -> + @_lastType = 'Then' + jasmine.GWT.Step(@_lastType, args...) + + And: (args...) -> + jasmine.GWT.Step(@_lastType, args...) + + Before: (args...) -> jasmine.GWT.Hook('Before', args...) + After: (args...) -> jasmine.GWT.Hook('After', args...) + diff --git a/spec/javascripts/jasmine/GWT_spec.coffee b/spec/javascripts/jasmine/GWT_spec.coffee new file mode 100644 index 0000000..cc4ad6b --- /dev/null +++ b/spec/javascripts/jasmine/GWT_spec.coffee @@ -0,0 +1,2 @@ +describe 'jasmine.GWT', -> +