commit da7741c3b81d65a17d6988e7209d47d24ee8f89b Author: John Bintz Date: Fri Feb 18 11:33:47 2011 -0500 kind of working, yay diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3390d99 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*.gem +.bundle +Gemfile.lock +pkg/* +.project diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..643f583 --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source "http://rubygems.org" + +# Specify your gem's dependencies in jasmine-mozrepl.gemspec +gemspec diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..14cfe0b --- /dev/null +++ b/Rakefile @@ -0,0 +1,2 @@ +require 'bundler' +Bundler::GemHelper.install_tasks diff --git a/jasmine-mozrepl.gemspec b/jasmine-mozrepl.gemspec new file mode 100644 index 0000000..5be302c --- /dev/null +++ b/jasmine-mozrepl.gemspec @@ -0,0 +1,23 @@ +# -*- encoding: utf-8 -*- +$:.push File.expand_path("../lib", __FILE__) +require "jasmine-mozrepl/version" + +Gem::Specification.new do |s| + s.name = "jasmine-mozrepl" + s.version = Jasmine::MozRepl::VERSION + s.platform = Gem::Platform::RUBY + s.authors = ["John Bintz"] + s.email = ["john@coswellproductions.com"] + s.homepage = "" + s.summary = %q{Watch a Jasmine test suite in Firefox} + s.description = %q{Using MozRepl, watch and get success/failure information from a Jasmine test suite} + + s.rubyforge_project = "jasmine-mozrepl" + + s.files = `git ls-files`.split("\n") + s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") + s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } + s.require_paths = ["lib"] + + s.add_dependency 'rainbow' +end diff --git a/lib/jasmine-mozrepl.rb b/lib/jasmine-mozrepl.rb new file mode 100644 index 0000000..36531fe --- /dev/null +++ b/lib/jasmine-mozrepl.rb @@ -0,0 +1,86 @@ +require 'net/telnet' +require 'rainbow' + +module Jasmine + module MozRepl + def repl_reload(options = {}) + options = { + :host => '127.0.0.1', + :port => 4242, + :jasmine_matcher => 'localhost:8888', + :echo_to_console => false + }.merge(options) + + repl = Net::Telnet.new('Host' => options[:host], 'Port' => options[:port]) + repl.print(<<-ENDREPL) +var targetWindow = null; +for (var i = 0; i < window.length; ++i) { + if (window[i].location.href.match(/#{options[:jasmine_matcher]}/)) { + targetWindow = window[i]; + break; + } +} + +var result = null; +var descriptionNode = null; + +function hasFinishedAt() { + var as = targetWindow.document.getElementsByTagName('span'); + for (var i = 0; i < as.length; ++i) { + + if (as[i].innerHTML.match(/Finished at/)) { + return true; + } + } + return false; +} + +function getDescription() { + var descriptionNode = null; + + var as = targetWindow.document.getElementsByTagName('a'); + for (var i = 0; i < as.length; ++i) { + if (as[i].className.match(/description/)) { + descriptionNode = as[i].innerHTML; + break; + } + } + repl.print(descriptionNode); +} + +function domReady() { + var count = 10; + + var countdown; + countdown = function() { + if (hasFinishedAt()) { + getDescription(); + } else { + if (count > 0) { + repl.print(count); + setTimeout(countdown, 1000); + count--; + } + } + }; + + countdown(); +} + +if (targetWindow) { + targetWindow.location = targetWindow.location; + setTimeout(domReady, 1000); +} + ENDREPL + message = nil + + matcher = %r{\d+ specs?, (\d+) .+ in .+s} + repl.waitfor(matcher) { |output| message = output[matcher] } + repl.close + + puts message.foreground(($1 == "0") ? :green : :red) if options[:echo_to_console] + + message + end + end +end diff --git a/lib/jasmine-mozrepl/version.rb b/lib/jasmine-mozrepl/version.rb new file mode 100644 index 0000000..08782cf --- /dev/null +++ b/lib/jasmine-mozrepl/version.rb @@ -0,0 +1,5 @@ +module Jasmine + module MozRepl + VERSION = "0.0.1" + end +end