From 7725840e581aa54a7aba76b309da9004e2767f39 Mon Sep 17 00:00:00 2001 From: Pat Nakajima Date: Mon, 8 Sep 2008 23:56:59 -0400 Subject: [PATCH] gemified --- .gemified | 11 ++++++++ .manifest | 37 +++++++++++++++++++++++++ js/jquery.ajax_queue.js | 59 ++++++++++++++++++++++++++++++++++++++++ js/screw.driver.js | 32 ++++++++++++++++++++++ lib/suite.rb | 34 +++++++++++++++++++---- spec/fixtures/suite.html | 2 -- spec/suite_spec.rb | 6 ++-- 7 files changed, 170 insertions(+), 11 deletions(-) create mode 100644 .gemified create mode 100644 .manifest create mode 100644 js/jquery.ajax_queue.js create mode 100644 js/screw.driver.js diff --git a/.gemified b/.gemified new file mode 100644 index 0000000..ba726c3 --- /dev/null +++ b/.gemified @@ -0,0 +1,11 @@ +--- +:homepage: http://github.com/nakajima/screw-driver +:author: Pat Nakajima +:dependencies: +- sinatra +- hpricot +:version: 0.0.1 +:name: screw-driver +:summary: Run your Screw.Unit specs from the command line. +:email: patnakajima@gmail.com +:has_rdoc: false diff --git a/.manifest b/.manifest new file mode 100644 index 0000000..d6a5d52 --- /dev/null +++ b/.manifest @@ -0,0 +1,37 @@ +README +Rakefile +bin/screwdriver +lib/browser.rb +lib/browsers/firefox.rb +lib/browsers/safari.rb +lib/ext/string.rb +lib/helpers.rb +lib/rails.rb +lib/suite.rb +js/jquery.ajax_queue.js +js/screw.driver.js +spec/browser_spec.rb +spec/fixtures/bar.js +spec/fixtures/bar_spec.js +spec/fixtures/foo.js +spec/fixtures/foo_spec.js +spec/fixtures/jquery.ajax_queue.js +spec/fixtures/public/javascripts/application.js +spec/fixtures/public/javascripts/lib.js +spec/fixtures/screw-unit/jquery-1.2.6.js +spec/fixtures/screw-unit/jquery.fn.js +spec/fixtures/screw-unit/jquery.print.js +spec/fixtures/screw-unit/screw.assets.js +spec/fixtures/screw-unit/screw.behaviors.js +spec/fixtures/screw-unit/screw.builder.js +spec/fixtures/screw-unit/screw.css +spec/fixtures/screw-unit/screw.events.js +spec/fixtures/screw-unit/screw.matchers.js +spec/fixtures/screw-unit/screw.server.js +spec/fixtures/screw.driver.js +spec/fixtures/spec_helper.js +spec/fixtures/src/great.js +spec/fixtures/suite.html +spec/rails_spec.rb +spec/spec_helper.rb +spec/suite_spec.rb \ No newline at end of file diff --git a/js/jquery.ajax_queue.js b/js/jquery.ajax_queue.js new file mode 100644 index 0000000..ae7c413 --- /dev/null +++ b/js/jquery.ajax_queue.js @@ -0,0 +1,59 @@ +// AjaxQueue - by Pat Nakajima +(function($) { + var startNextRequest = function() { + if ($.ajaxQueue.currentRequest) { return; } + if (request = $.ajaxQueue.queue.shift()) { + $.ajaxQueue.currentRequest = request; + request.perform(); + } + } + + var Request = function(url, type, options) { + this.opts = options || { }; + this.opts.url = url; + this.opts.type = type; + var oldComplete = this.opts.complete || function() { } + this.opts.complete = function(response) { + oldComplete(response); + $.ajaxQueue.currentRequest = null; + startNextRequest(); + }; + } + + Request.prototype.perform = function() { + $.ajax(this.opts); + } + + var addRequest = function(url, type, options) { + var request = new Request(url, type, options); + $.ajaxQueue.queue.push(request); + startNextRequest(); + } + + $.ajaxQueue = { + queue: [], + + currentRequest: null, + + reset: function() { + $.ajaxQueue.queue = []; + $.ajaxQueue.currentRequest = null; + }, + + post: function(url, options) { + addRequest(url, 'POST', options); + }, + + get: function(url, options) { + addRequest(url, 'GET', options); + }, + + put: function(url, options) { + addRequest(url, 'PUT', options); + }, + + "delete": function(url, options) { + addRequest(url, 'DELETE', options); + } + } +})(jQuery) \ No newline at end of file diff --git a/js/screw.driver.js b/js/screw.driver.js new file mode 100644 index 0000000..07f722e --- /dev/null +++ b/js/screw.driver.js @@ -0,0 +1,32 @@ +(function($) { + $(Screw).bind('before', function() { + $.ajaxQueue.post('/before'); + }); + + $(Screw).bind('after', function() { + $.ajaxQueue.post('/after'); + $.ajaxQueue.post('/exit'); + }); + + $(Screw).bind('loaded', function() { + $('.it').bind('passed', function(e) { + $.ajaxQueue.post('/passed'); + }); + + $('.it').bind('failed', function(e, reason) { + var specName = $(this).find('h2').text(); + + // ERROR + if (reason.fileName || reason.lineNumber || reason.line) { + return $.ajaxQueue.post('/errored', { + data: { name: specName, reason: reason } + }); + } + + // FAILURE + $.ajaxQueue.post('/failed', { + data: { name: specName, reason: reason } + }); + }); + }); +})(jQuery); \ No newline at end of file diff --git a/lib/suite.rb b/lib/suite.rb index 9acced1..bddc29b 100644 --- a/lib/suite.rb +++ b/lib/suite.rb @@ -48,7 +48,7 @@ def link_urls end def to_s - doc.to_s + doc.to_html end def browser @@ -73,10 +73,14 @@ def generate_js_urls def generate_css_urls link_urls.each { |url| generate(url, "text/css") } end + + def generate_framework_urls + ['/jquery.ajax_queue.js', 'screw.driver.js'].each { |url| generate(url, 'text/javascript') } + end def generate(url, content_type, prefix=working_directory) - prefix = load_paths.detect { |path| File.exists?(File.join(Dir.pwd, path, url)) } || '.' - path = File.join(Dir.pwd, prefix, url) + prefix = load_paths.detect { |path| File.exists?(File.join(path, url)) } || '.' + path = File.join(prefix, url) @context.send(:get, url) do headers 'Content-Type' => content_type File.read(path) @@ -125,13 +129,31 @@ def parse_args(args) @rails = options.rails @server = options.server @path = File.join(Dir.pwd, args.shift) - @load_paths = [File.dirname(@path)] - @load_paths += options.paths if options.paths + setup_load_paths(options.paths) end end + + def setup_load_paths(paths=[]) + @load_paths = paths.map { |path| File.join(Dir.pwd, path) } + @load_paths << File.join(File.dirname(__FILE__), '..', 'js') + @load_paths << File.dirname(@path) + end def doc - Hpricot(File.open(@path)) + @doc ||= begin + @doc = Hpricot(File.open(@path)) + @doc.search('script').each do |script| + case script['src'] + when "/screw-unit/screw.behaviors.js" then append_script(script, :before, 'jquery.ajax_queue.js') + when "/screw-unit/screw.events.js" then append_script(script, :after, 'screw.driver.js') + end + end + @doc + end + end + + def append_script(script, pos, name) + script.send(pos, %()) end end end diff --git a/spec/fixtures/suite.html b/spec/fixtures/suite.html index 9870618..727a74a 100644 --- a/spec/fixtures/suite.html +++ b/spec/fixtures/suite.html @@ -7,10 +7,8 @@ - - diff --git a/spec/suite_spec.rb b/spec/suite_spec.rb index a8a7978..87d29c4 100644 --- a/spec/suite_spec.rb +++ b/spec/suite_spec.rb @@ -30,7 +30,7 @@ end it "should find external script_urls" do - @suite.should have(15).script_urls + @suite.should have(13).script_urls end it "should find external link_urls" do @@ -62,12 +62,12 @@ describe "load paths" do it "should have 1 load_path by default" do - @suite.should have(1).load_paths + @suite.should have(2).load_paths end it "should append load paths" do suite = create_suite '--load-paths', 'src' - suite.should have(2).load_paths + suite.should have(3).load_paths end it "should serve files from added load paths directory" do