Refactor config file/path handling.

This commit is contained in:
Christian Williams
2009-12-28 16:27:02 -06:00
parent 2045226ba7
commit 81aeeeedee
15 changed files with 150 additions and 76 deletions

View File

@@ -1,6 +1,7 @@
require 'jasmine/base'
require 'jasmine/config'
require 'jasmine/server'
require 'jasmine/selenium_driver'
require 'jasmine/jasmine_helper'
require 'jasmine/jasmine_spec_builder'
require 'jasmine/spec_builder'

View File

@@ -16,7 +16,7 @@ module Jasmine
def start
start_servers
@client = Jasmine::SimpleClient.new("localhost", @selenium_server_port, "*#{@browser}", "http://localhost:#{@jasmine_server_port}/")
@client = Jasmine::SeleniumDriver.new("localhost", @selenium_server_port, "*#{@browser}", "http://localhost:#{@jasmine_server_port}/")
@client.connect
end
@@ -83,8 +83,40 @@ module Jasmine
raise "You need to declare a spec_files method in #{self.class}!"
end
def match_files(dir, pattern)
dir = File.expand_path(dir)
Dir.glob(File.join(dir, pattern)).collect {|f| f.sub("#{dir}/", "")}.sort
end
def src_files
match_files(src_dir, "**/*.js")
end
def src_path
"src"
end
def spec_path
"spec"
end
def spec_files
match_files(spec_dir, "**/*.js")
end
def mappings
{
"/" + src_path => src_dir,
"/" + spec_path => spec_dir
}
end
def js_files
src_files + spec_files
src_files.collect {|f| File.join(src_path, f) } + spec_files.collect {|f| File.join(spec_path, f) }
end
def spec_files_full_paths
spec_files.collect {|spec_file| File.join(spec_dir, spec_file) }
end
end
end

View File

@@ -6,7 +6,7 @@ if File.exist?(helper_overrides)
require helper_overrides
end
require File.expand_path(File.join(File.dirname(__FILE__), "jasmine_runner.rb"))
require File.expand_path(File.join(File.dirname(__FILE__), "jasmine_spec_builder"))
require File.expand_path(File.join(File.dirname(__FILE__), "spec_builder"))
jasmine_runner = Jasmine::Runner.new(SeleniumRC::Server.new.jar_path,
Dir.pwd,

View File

@@ -1,3 +0,0 @@
module Jasmine
end

View File

@@ -0,0 +1,44 @@
module Jasmine
class SeleniumDriver
def initialize(selenium_host, selenium_port, selenium_browser_start_command, http_address)
require 'selenium/client'
@driver = Selenium::Client::Driver.new(
selenium_host,
selenium_port,
selenium_browser_start_command,
http_address
)
@http_address = http_address
end
def tests_have_finished?
@driver.get_eval("window.jasmine.getEnv().currentRunner.finished") == "true"
end
def connect
@driver.start
@driver.open("/")
end
def disconnect
@driver.stop
end
def run
until tests_have_finished? do
sleep 0.1
end
puts @driver.get_eval("window.results()")
failed_count = @driver.get_eval("window.jasmine.getEnv().currentRunner.results().failedCount").to_i
failed_count == 0
end
def eval_js(script)
escaped_script = "'" + script.gsub(/(['\\])/) { '\\' + $1 } + "'"
result = @driver.get_eval(" try { eval(#{escaped_script}, window); } catch(err) { window.eval(#{escaped_script}); }")
JSON.parse("[#{result}]")[0]
end
end
end

View File

@@ -127,47 +127,4 @@ module Jasmine
thin.stop
end
end
class SimpleClient
def initialize(selenium_host, selenium_port, selenium_browser_start_command, http_address)
require 'selenium/client'
@driver = Selenium::Client::Driver.new(
selenium_host,
selenium_port,
selenium_browser_start_command,
http_address
)
@http_address = http_address
end
def tests_have_finished?
@driver.get_eval("window.jasmine.getEnv().currentRunner.finished") == "true"
end
def connect
@driver.start
@driver.open("/")
end
def disconnect
@driver.stop
end
def run
until tests_have_finished? do
sleep 0.1
end
puts @driver.get_eval("window.results()")
failed_count = @driver.get_eval("window.jasmine.getEnv().currentRunner.results().failedCount").to_i
failed_count == 0
end
def eval_js(script)
escaped_script = "'" + script.gsub(/(['\\])/) { '\\' + $1 } + "'"
result = @driver.get_eval(" try { eval(#{escaped_script}, window); } catch(err) { window.eval(#{escaped_script}); }")
JSON.parse("[#{result}]")[0]
end
end
end

View File

@@ -1,12 +1,13 @@
require 'enumerator'
module Jasmine
module Jasmine
class SpecBuilder
attr_accessor :suites
def initialize(runner)
@spec_files = runner.spec_files
@runner = runner
def initialize(config)
@config = config
@spec_files = config.spec_files
@runner = config
@spec_ids = []
end
@@ -31,7 +32,7 @@ module Jasmine
example_name_parts = []
previous_indent_level = 0
@spec_files.each do |filename|
@config.spec_files_full_paths.each do |filename|
line_number = 1
File.open(filename, "r") do |file|
file.readlines.each do |line|