2012-01-11 19:30:10 +00:00
|
|
|
#= require jasmine/GWT/Background
|
|
|
|
#
|
|
|
|
class jasmine.GWT.Scenario extends jasmine.GWT.Background
|
2012-01-18 22:41:08 +00:00
|
|
|
@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
|
|
|
|
2012-01-18 22:41:08 +00:00
|
|
|
this.spyOn = (args...) ->
|
|
|
|
this.spies_ = context.spies_
|
|
|
|
jasmine.Spec.prototype.spyOn.apply(this, args)
|
2012-01-11 19:30:10 +00:00
|
|
|
|
2012-01-18 22:41:08 +00:00
|
|
|
this.removeAllSpies = ->
|
|
|
|
if position == 'last'
|
|
|
|
jasmine.Spec.prototype.removeAllSpies.apply(context)
|
2012-01-11 19:30:10 +00:00
|
|
|
|
2012-01-27 21:00:31 +00:00
|
|
|
exception = null
|
|
|
|
|
|
|
|
try
|
|
|
|
param.apply(context, args)
|
|
|
|
catch exception
|
2012-01-18 22:41:08 +00:00
|
|
|
|
|
|
|
if position == 'last'
|
|
|
|
jasmine.GWT.runHook('After', context)
|
2012-01-11 19:30:10 +00:00
|
|
|
|
2012-01-27 21:00:31 +00:00
|
|
|
throw(exception) if exception
|
|
|
|
|
2012-01-18 22:41:08 +00:00
|
|
|
@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
|
|
|
|
2012-01-18 22:41:08 +00:00
|
|
|
this.addMatcherResult(
|
|
|
|
jasmine.GWT.Scenario.generateNotDefinedResult(type, name, output.join("\n"))
|
|
|
|
)
|
2012-01-11 19:30:10 +00:00
|
|
|
|
2012-01-18 22:41:08 +00:00
|
|
|
@generateStepDefinition: (type, name, args) ->
|
|
|
|
output = []
|
2012-01-11 19:30:10 +00:00
|
|
|
|
2012-01-18 22:41:08 +00:00
|
|
|
argCount = args.length
|
2012-01-11 19:30:10 +00:00
|
|
|
|
2012-01-18 22:41:08 +00:00
|
|
|
name = name.replace /\d+/g, (match) ->
|
|
|
|
argCount += 1
|
|
|
|
"(\\d+)"
|
2012-01-11 19:30:10 +00:00
|
|
|
|
2012-01-18 22:41:08 +00:00
|
|
|
name = name.replace /\"[^"]+\"/g, (match) ->
|
|
|
|
argCount += 1
|
|
|
|
'"([^"]+)"'
|
2012-01-11 19:30:10 +00:00
|
|
|
|
2012-01-18 22:41:08 +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
|
|
|
|
2012-01-18 22:41:08 +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
|
|
|
|
2012-01-18 22:41:08 +00:00
|
|
|
constructor: (@scenarioName, @code, @feature) ->
|
|
|
|
super(@code)
|
2012-01-16 19:58:05 +00:00
|
|
|
|
2012-01-18 22:41:08 +00:00
|
|
|
run: =>
|
|
|
|
super()
|
2012-01-16 19:58:05 +00:00
|
|
|
|
2012-01-18 22:41:08 +00:00
|
|
|
jasmine.GWT.currentScenario_ = this
|
2012-01-16 19:58:05 +00:00
|
|
|
|
2012-01-18 22:41:08 +00:00
|
|
|
_statements = this.allStatements()
|
|
|
|
_this = this
|
2012-01-16 19:58:05 +00:00
|
|
|
|
2012-01-18 22:41:08 +00:00
|
|
|
describe @feature.name, ->
|
|
|
|
describe _this.scenarioName, ->
|
|
|
|
_this.spies_ = []
|
2012-01-16 19:58:05 +00:00
|
|
|
|
2012-01-18 22:41:08 +00:00
|
|
|
position = 'first'
|
2012-01-16 19:58:05 +00:00
|
|
|
|
2012-01-27 21:00:31 +00:00
|
|
|
calcPosition = (index) ->
|
|
|
|
if (index + 2) == _statements.length then 'last' else 'middle'
|
|
|
|
|
2012-01-18 22:41:08 +00:00
|
|
|
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 = []
|
|
|
|
|
2012-01-27 21:00:31 +00:00
|
|
|
codeRunner = (thing, pos, args = []) ->
|
|
|
|
jasmine.GWT.Scenario.runCode(type, name, thing, pos, _this, args)
|
2012-01-18 22:41:08 +00:00
|
|
|
|
2012-01-11 19:30:10 +00:00
|
|
|
if param?
|
|
|
|
if typeof param == "function"
|
2012-01-27 21:00:31 +00:00
|
|
|
codeRunner(param, position)
|
|
|
|
position = calcPosition(index)
|
2012-01-18 22:41:08 +00:00
|
|
|
|
2012-01-11 19:30:10 +00:00
|
|
|
continue
|
|
|
|
else
|
|
|
|
args = [ param ]
|
|
|
|
|
|
|
|
found = false
|
|
|
|
|
2012-01-18 22:41:08 +00:00
|
|
|
for [ match, code ] in (jasmine.GWT.Steps[type] || [])
|
|
|
|
if result = name.match(match)
|
2012-01-27 21:00:31 +00:00
|
|
|
codeRunner(code, position, result[1..-1].concat(args))
|
2012-01-18 22:41:08 +00:00
|
|
|
found = true
|
|
|
|
|
|
|
|
break
|
2012-01-11 19:30:10 +00:00
|
|
|
|
2012-01-18 22:41:08 +00:00
|
|
|
jasmine.GWT.Scenario.runFailure(type, name, args) if !found
|
2012-01-11 19:30:10 +00:00
|
|
|
|
2012-01-27 21:00:31 +00:00
|
|
|
position = calcPosition(index)
|
2012-01-11 19:30:10 +00:00
|
|
|
|
2012-01-18 22:41:08 +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: =>
|
2012-01-18 22:41:08 +00:00
|
|
|
fakeResult = jasmine.GWT.Scenario.generateNotDefinedResult(@_type, @_name, 'has no code defined.')
|
2012-01-11 19:30:10 +00:00
|
|
|
|
|
|
|
jasmine.getEnv().currentSpec.addMatcherResult(fakeResult)
|