reeeeealy basic cucumber support

This commit is contained in:
John Bintz 2012-03-09 10:05:54 -05:00
parent e9212c6743
commit bbc95564f1
11 changed files with 187 additions and 21 deletions

View File

@ -3,5 +3,6 @@ Flowerbox =
contact: (url, data...) ->
xhr = new XMLHttpRequest()
xhr.open("POST", Flowerbox.baseUrl + url, false)
xhr.setRequestHeader("Accept", "application/json")
xhr.send(JSON.stringify(data))
fail: ->

View File

@ -0,0 +1,55 @@
#= require flowerbox/cucumber/reporter
Flowerbox.Cucumber ||= {}
Flowerbox.Cucumber.features = ->
Flowerbox.Cucumber.Features ||= []
Flowerbox.Cucumber.Features.join("\n")
Flowerbox.World = (code = null) ->
->
for code in (Flowerbox.World.Code || [])
code.apply(this)
Flowerbox.Step = (type, match, code) ->
Flowerbox.World.Code ||= []
Flowerbox.World.Code.push (args..., callback) ->
this[type] match, (args..., callback) ->
pending = false
this.pending = -> pending = true
result = code.apply(this)
if result? and result.__prototype__ == Error
callback.fail(result)
else
if pending then callback.pending("pending") else callback()
null
Flowerbox.Step.files ||= {}
Flowerbox.Step.matchFile = (name) ->
for key, data of Flowerbox.Step.files
[ regexp, filename ] = data
if name.match(regexp)
return filename
null
stepGenerator = (type) ->
Flowerbox[type] = (match, code) ->
if !Flowerbox.Step.files[match.toString()]
count = 2
for line in (new Error()).stack.split('\n')
if line.match(/__F__/)
count -= 1
if count == 0
Flowerbox.Step.files[match.toString()] = [ match, line.replace(/^.*__F__/, '') ]
break
Flowerbox.Step(type, match, code)
stepGenerator(type) for type in [ 'Given', 'When', 'Then' ]

View File

@ -0,0 +1,65 @@
Flowerbox ||= {}
Flowerbox.Cucumber ||= {}
class Flowerbox.Cucumber.Reporter
hear: (event, callback) ->
switch event.getName()
when 'BeforeFeatures'
@time = (new Date()).getTime()
Flowerbox.contact("starting")
when 'AfterFeatures'
Flowerbox.contact("results", (new Date()).getTime() - @time)
when 'BeforeFeature'
@feature = event.getPayloadItem('feature')
when 'BeforeScenario'
@scenario = event.getPayloadItem('scenario')
when 'BeforeStep'
@step = event.getPayloadItem('step')
Flowerbox.contact("start_test", @step.getName())
when 'StepResult'
stepResult = event.getPayloadItem('stepResult')
type = "Given"
if @step.isOutcomeStep()
type = "Then"
else if @step.isEventStep()
type = "When"
file = Flowerbox.Step.matchFile(@step.getName()) || "unknown:0"
test = { passed_: false, message: 'skipped', splitName: [ @feature.getName(), @scenario.getName(), "#{type} #{@step.getName()}" ], trace: { stack: [ file ] } }
if stepResult.isSuccessful()
test.passed_ = true
else if stepResult.isPending()
test.message = "pending"
else if stepResult.isUndefined()
regexp = @step.getName()
regexp = regexp.replace(/"[^"]+"/g, '"([^"]+)"')
test.message = """
Step not defined. Define it with the following:
Flowerbox.#{type} /^#{regexp}$/, ->
@pending()
"""
else if stepResult.isFailed()
error = stepResult.getFailureException()
test.message = error.message
test.trace.stack = [ 'file:1' ]
Flowerbox.contact("finish_test", @step.getName(), [ { items_: [ test ] } ])
callback()

View File

@ -0,0 +1,22 @@
require 'tilt'
module Flowerbox::Delivery::Tilt
class FeatureTemplate < Tilt::Template
self.default_mime_type = 'application/javascript'
def prepare; end
def evaluate(scope, locals, &block)
<<-JS
Flowerbox.Cucumber.Features = Flowerbox.Cucumber.Features || [];
Flowerbox.Cucumber.Features.push("#{escaped_data}");
JS
end
def escaped_data
data.gsub("\n", "\\n").gsub('"', '\\"')
end
end
end

View File

@ -1,8 +1,31 @@
module Flowerbox
class Failure < BaseResult
def pending?
message == "pending"
end
def skipped?
message == "skipped"
end
def undefined?
message[%r{^Step not defined}]
end
def print_progress
case message
when "pending"
print "P".foreground(:yellow)
when "skipped"
print "-".foreground(:cyan)
else
if undefined?
print "U".foreground(:yellow)
else
print "F".foreground(:red)
end
end
end
end
end

View File

@ -1,5 +1,6 @@
require 'sinatra'
require 'json'
require 'cgi'
module Flowerbox
class Rack < Sinatra::Base

View File

@ -27,13 +27,13 @@ module Flowerbox
end
def filename
file.split(":").first
file.to_s.split(":").first
end
def line_number
return @line_number if @line_number
@line_number = file.split(":").last
@line_number = file.to_s.split(":").last
@line_number = "~#{@line_number}" if file_translated?
@line_number
end

View File

@ -49,7 +49,6 @@ module Flowerbox
end
def time=(time)
p time
@results.time = time
end

View File

@ -22,11 +22,12 @@ module Flowerbox
def configured?
File.directory?(File.join(Dir.pwd, 'node_modules/jsdom')) &&
File.directory?(File.join(Dir.pwd, 'node_modules/XMLHttpRequest'))
File.directory?(File.join(Dir.pwd, 'node_modules/XMLHttpRequest')) &&
File.directory?(File.join(Dir.pwd, 'node_modules/cucumber'))
end
def configure
system %{bash -c "mkdir -p node_modules && npm link jsdom && npm link xmlhttprequest"}
system %{bash -c "mkdir -p node_modules && npm link jsdom && npm link xmlhttprequest && npm link cucumber"}
end
def run(sprockets, spec_files, options)

View File

@ -20,8 +20,6 @@ module Flowerbox
selenium.navigate.to "http://localhost:#{server.port}/"
sleep 10
@count = 0
while @count < MAX_COUNT && !finished?
@ -44,7 +42,7 @@ module Flowerbox
<<-HTML
<html>
<head>
<title>Flowerbox - Selenium Runner</title>
<title>Flowerbox - #{Flowerbox.test_environment.name} Runner</title>
<script type="text/javascript">
console._log = console.log;
@ -56,10 +54,12 @@ console.log = function(msg) {
#{template_files.join("\n")}
</head>
<body>
<h1>Flowerbox - Selenium Runner</h1>
<h1>Flowerbox - #{Flowerbox.test_environment.name} Runner</h1>
<script type="text/javascript">
Flowerbox.environment = '#{browser}';
var context = this;
#{env}
</script>
</body>

View File

@ -1,9 +1,11 @@
module Flowerbox
module TestEnvironment
class Cucumber
class Cucumber < Base
def inject_into(sprockets)
@sprockets = sprockets
@sprockets.register_engine('.feature', Flowerbox::Delivery::Tilt::FeatureTemplate)
@sprockets.add('cucumber.js')
end
@ -14,21 +16,18 @@ module Flowerbox
runner.spec_files.each { |file| @sprockets.add(file) }
<<-JS
if (typeof context != 'undefined' && typeof jasmine == 'undefined') {
jasmine = context.jasmine;
}
context.Cucumber = context.require('./cucumber');
jasmine.getEnv().addReporter(new jasmine.FlowerboxReporter());
#{jasmine_reporters.join("\n")}
jasmine.getEnv().execute();
context.cucumber = context.Cucumber(context.Flowerbox.Cucumber.features(), context.Flowerbox.World());
context.cucumber.attachListener(new context.Flowerbox.Cucumber.Reporter());
context.cucumber.start(function() {});
JS
end
def jasmine_reporters
reporters.collect { |reporter| %{jasmine.getEnv().addReporter(new jasmine.#{reporter}());} }
end
end
end
end
module Flowerbox::Delivery::Tilt
autoload :FeatureTemplate, 'flowerbox/delivery/tilt/feature_template'
end