jasmine-gwt/lib/assets/javascripts/jasmine/GWT/Scenario.js.coffee

136 lines
3.5 KiB
CoffeeScript
Raw Normal View History

2012-01-11 19:30:10 +00:00
#= require jasmine/GWT/Background
#
class jasmine.GWT.Scenario extends jasmine.GWT.Background
@runCode: (type, name, param, position, context, args = []) ->
it "#{type} #{name}", ->
if position == 'first'
jasmine.GWT.runHook('World', context)
jasmine.GWT.runHook('Before', context)
2012-01-11 19:30:10 +00:00
this.spyOn = (args...) ->
this.spies_ = context.spies_
jasmine.Spec.prototype.spyOn.apply(this, args)
2012-01-11 19:30:10 +00:00
this.removeAllSpies = ->
if position == 'last'
jasmine.Spec.prototype.removeAllSpies.apply(context)
2012-01-11 19:30:10 +00:00
exception = null
try
param.apply(context, args)
catch exception
if position == 'last'
jasmine.GWT.runHook('After', context)
2012-01-11 19:30:10 +00:00
throw(exception) if exception
@runFailure: (type, name, args) ->
it "#{type} #{name}", ->
output = [ "", "No step defined for #{type} #{name}. Define one using the following:", '' ]
output = output.concat(jasmine.GWT.Scenario.generateStepDefinition(type, name, args))
output.push("")
2012-01-11 19:30:10 +00:00
this.addMatcherResult(
jasmine.GWT.Scenario.generateNotDefinedResult(type, name, output.join("\n"))
)
2012-01-11 19:30:10 +00:00
@generateStepDefinition: (type, name, args) ->
output = []
2012-01-11 19:30:10 +00:00
argCount = args.length
2012-01-11 19:30:10 +00:00
name = name.replace /\d+/g, (match) ->
argCount += 1
"(\\d+)"
2012-01-11 19:30:10 +00:00
name = name.replace /\"[^"]+\"/g, (match) ->
argCount += 1
'"([^"]+)"'
2012-01-11 19:30:10 +00:00
if argCount == 0
output.push("#{type} /^#{name}$/, ->")
else
args = ("arg#{i}" for i in [ 1..(argCount) ])
2012-01-11 19:30:10 +00:00
output.push("#{type} /^#{name}$/, (#{args.join(', ')}) ->")
output.push(" @not_defined()")
output
@generateNotDefinedResult: (type, name, message) ->
new jasmine.ExpectationResult(
matcherName: 'step',
passed: false,
expected: 'to be defined',
actual: "#{type} #{name}",
message: message
)
2012-01-16 19:58:05 +00:00
constructor: (@scenarioName, @code, @feature) ->
super(@code)
2012-01-16 19:58:05 +00:00
run: =>
super()
2012-01-16 19:58:05 +00:00
jasmine.GWT.currentScenario_ = this
2012-01-16 19:58:05 +00:00
_statements = this.allStatements()
_this = this
2012-01-16 19:58:05 +00:00
describe @feature.name, ->
describe _this.scenarioName, ->
_this.spies_ = []
2012-01-16 19:58:05 +00:00
position = 'first'
2012-01-16 19:58:05 +00:00
calcPosition = (index) ->
if (index + 2) == _statements.length then 'last' else 'middle'
for index in [ 0..._statements.length ]
[ type, name, param ] = _statements[index]
[ _this._type, _this._name ] = [ type, name ]
2012-01-16 19:58:05 +00:00
2012-01-11 19:30:10 +00:00
args = []
codeRunner = (thing, pos, args = []) ->
jasmine.GWT.Scenario.runCode(type, name, thing, pos, _this, args)
2012-01-11 19:30:10 +00:00
if param?
if typeof param == "function"
codeRunner(param, position)
position = calcPosition(index)
2012-01-11 19:30:10 +00:00
continue
else
args = [ param ]
found = false
for [ match, code ] in (jasmine.GWT.Steps[type] || [])
if result = name.match(match)
codeRunner(code, position, result[1..-1].concat(args))
found = true
break
2012-01-11 19:30:10 +00:00
jasmine.GWT.Scenario.runFailure(type, name, args) if !found
2012-01-11 19:30:10 +00:00
position = calcPosition(index)
2012-01-11 19:30:10 +00:00
jasmine.GWT.currentScenario_ = null
2012-01-11 19:30:10 +00:00
allStatements: =>
allStatements = []
if @feature.background?
allStatements = @feature.background.statements
allStatements.concat(@statements)
not_defined: =>
fakeResult = jasmine.GWT.Scenario.generateNotDefinedResult(@_type, @_name, 'has no code defined.')
2012-01-11 19:30:10 +00:00
jasmine.getEnv().currentSpec.addMatcherResult(fakeResult)