From 81aeeeedee149b62d00332fa90ec736449e5b05b Mon Sep 17 00:00:00 2001 From: Christian Williams Date: Mon, 28 Dec 2009 16:27:02 -0600 Subject: [PATCH] Refactor config file/path handling. --- jasmine-ruby.gemspec | 6 +-- lib/jasmine.rb | 3 +- lib/jasmine/config.rb | 36 ++++++++++++- lib/jasmine/jasmine_meta_spec.rb | 2 +- lib/jasmine/jasmine_runner.rb | 3 -- lib/jasmine/selenium_driver.rb | 44 +++++++++++++++ lib/jasmine/server.rb | 43 --------------- ...asmine_spec_builder.rb => spec_builder.rb} | 11 ++-- spec/config_spec.rb | 53 +++++++++++++++++++ spec/jasmine_self_test_config.rb | 15 ------ ...mine_spec.rb => jasmine_self_test_spec.rb} | 4 +- templates/public/javascripts/example.js | 2 + templates/{ => spec}/jasmine_helper.rb | 0 .../javascript/ExampleSpec.js} | 4 +- .../javascript/SpecHelper.js} | 0 15 files changed, 150 insertions(+), 76 deletions(-) delete mode 100644 lib/jasmine/jasmine_runner.rb create mode 100644 lib/jasmine/selenium_driver.rb rename lib/jasmine/{jasmine_spec_builder.rb => spec_builder.rb} (95%) create mode 100644 spec/config_spec.rb rename spec/{jasmine_spec.rb => jasmine_self_test_spec.rb} (70%) create mode 100644 templates/public/javascripts/example.js rename templates/{ => spec}/jasmine_helper.rb (100%) rename templates/{example_spec.js => spec/javascript/ExampleSpec.js} (69%) rename templates/{spec_helper.js => spec/javascript/SpecHelper.js} (100%) diff --git a/jasmine-ruby.gemspec b/jasmine-ruby.gemspec index 4d2d38a..e8a819f 100644 --- a/jasmine-ruby.gemspec +++ b/jasmine-ruby.gemspec @@ -28,12 +28,12 @@ Gem::Specification.new do |s| "lib/jasmine/jasmine_helper.rb", "lib/jasmine/jasmine_meta_spec.rb", "lib/jasmine/jasmine_runner.rb", - "lib/jasmine/jasmine_spec_builder.rb", + "lib/jasmine/spec_builder.rb", "lib/jasmine/run.html", "templates/Rakefile", - "templates/example_spec.js", + "templates/ExampleSpec.js", "templates/jasmine_helper.rb", - "templates/spec_helper.js" + "templates/SpecHelper.js" ] s.homepage = %q{http://github.com/ragaskar/jasmine} s.rdoc_options = ["--charset=UTF-8"] diff --git a/lib/jasmine.rb b/lib/jasmine.rb index 1ab5139..a4c01aa 100644 --- a/lib/jasmine.rb +++ b/lib/jasmine.rb @@ -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' \ No newline at end of file +require 'jasmine/spec_builder' \ No newline at end of file diff --git a/lib/jasmine/config.rb b/lib/jasmine/config.rb index eb5bacc..d07be03 100644 --- a/lib/jasmine/config.rb +++ b/lib/jasmine/config.rb @@ -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 \ No newline at end of file diff --git a/lib/jasmine/jasmine_meta_spec.rb b/lib/jasmine/jasmine_meta_spec.rb index 34154a2..3dd0e01 100644 --- a/lib/jasmine/jasmine_meta_spec.rb +++ b/lib/jasmine/jasmine_meta_spec.rb @@ -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, diff --git a/lib/jasmine/jasmine_runner.rb b/lib/jasmine/jasmine_runner.rb deleted file mode 100644 index da9440a..0000000 --- a/lib/jasmine/jasmine_runner.rb +++ /dev/null @@ -1,3 +0,0 @@ -module Jasmine - -end diff --git a/lib/jasmine/selenium_driver.rb b/lib/jasmine/selenium_driver.rb new file mode 100644 index 0000000..7f9bac8 --- /dev/null +++ b/lib/jasmine/selenium_driver.rb @@ -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 \ No newline at end of file diff --git a/lib/jasmine/server.rb b/lib/jasmine/server.rb index c293f78..8527abb 100644 --- a/lib/jasmine/server.rb +++ b/lib/jasmine/server.rb @@ -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 \ No newline at end of file diff --git a/lib/jasmine/jasmine_spec_builder.rb b/lib/jasmine/spec_builder.rb similarity index 95% rename from lib/jasmine/jasmine_spec_builder.rb rename to lib/jasmine/spec_builder.rb index b5ecad4..352845e 100644 --- a/lib/jasmine/jasmine_spec_builder.rb +++ b/lib/jasmine/spec_builder.rb @@ -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| diff --git a/spec/config_spec.rb b/spec/config_spec.rb new file mode 100644 index 0000000..731903a --- /dev/null +++ b/spec/config_spec.rb @@ -0,0 +1,53 @@ +require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper")) + +describe Jasmine::Config do + before(:each) do + @template_dir = File.expand_path(File.join(File.dirname(__FILE__), "../templates")) + @config = Jasmine::Config.new + @config.stub!(:src_dir).and_return(File.join(@template_dir, "public")) + @config.stub!(:spec_dir).and_return(File.join(@template_dir, "spec")) + end + + it "should provide a list of all src and spec files" do + @config.src_files.should == ['javascripts/Example.js'] + @config.spec_files.should == ['javascript/ExampleSpec.js', 'javascript/SpecHelper.js'] + end + + it "should provide a list of all spec files with full paths" do + @config.spec_files_full_paths.should == [ + File.join(@template_dir, 'spec/javascript/ExampleSpec.js'), + File.join(@template_dir, 'spec/javascript/SpecHelper.js') + ] + end + + it "should provide a list of all js files" do + @config.js_files.should == [ + 'src/javascripts/Example.js', + 'spec/javascript/ExampleSpec.js', + 'spec/javascript/SpecHelper.js', + ] + end + + it "should provide dir mappings" do + @config.mappings.should == { + '/src' => @config.src_dir, + '/spec' => @config.spec_dir + } + end + + it "should allow overriding src and spec paths" do + @config.stub!(:src_path).and_return("public") + @config.stub!(:spec_path).and_return("spekz") + + @config.js_files.should == [ + 'public/javascripts/Example.js', + 'spekz/javascript/ExampleSpec.js', + 'spekz/javascript/SpecHelper.js', + ] + + @config.mappings.should == { + '/public' => @config.src_dir, + '/spekz' => @config.spec_dir + } + end +end \ No newline at end of file diff --git a/spec/jasmine_self_test_config.rb b/spec/jasmine_self_test_config.rb index f8bd819..2736157 100644 --- a/spec/jasmine_self_test_config.rb +++ b/spec/jasmine_self_test_config.rb @@ -9,22 +9,7 @@ class JasmineSelfTestConfig < Jasmine::Config File.join(proj_root, 'src') end - def src_files - Dir.glob(File.join(src_dir, "**/*.js")) - end - def spec_dir File.join(proj_root, 'jasmine/spec') end - - def spec_files - Dir.glob(File.join(spec_dir, "**/*[Ss]pec.js")).collect { |f| f.sub("#{spec_dir}/", "spec/") } - end - - def mappings - { - "/src" => src_dir, - "/spec" => spec_dir - } - end end \ No newline at end of file diff --git a/spec/jasmine_spec.rb b/spec/jasmine_self_test_spec.rb similarity index 70% rename from spec/jasmine_spec.rb rename to spec/jasmine_self_test_spec.rb index 85666c2..fcb604e 100644 --- a/spec/jasmine_spec.rb +++ b/spec/jasmine_self_test_spec.rb @@ -1,6 +1,8 @@ +require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper")) + require 'jasmine_self_test_config' -jasmine_runner = JasmineSelfTestRunner.new +jasmine_runner = JasmineSelfTestConfig.new spec_builder = Jasmine::SpecBuilder.new(jasmine_runner) should_stop = false diff --git a/templates/public/javascripts/example.js b/templates/public/javascripts/example.js new file mode 100644 index 0000000..3fcc4e1 --- /dev/null +++ b/templates/public/javascripts/example.js @@ -0,0 +1,2 @@ +ExampleClass = function() { +}; \ No newline at end of file diff --git a/templates/jasmine_helper.rb b/templates/spec/jasmine_helper.rb similarity index 100% rename from templates/jasmine_helper.rb rename to templates/spec/jasmine_helper.rb diff --git a/templates/example_spec.js b/templates/spec/javascript/ExampleSpec.js similarity index 69% rename from templates/example_spec.js rename to templates/spec/javascript/ExampleSpec.js index 494539e..07ae14f 100644 --- a/templates/example_spec.js +++ b/templates/spec/javascript/ExampleSpec.js @@ -1,9 +1,9 @@ -describe('ExampleSuite', function () { +describe('Example', function () { it('should have a passing test', function() { expect(true).toEqual(true); }); - describe('Nested Describe', function () { + describe('nested describe', function () { it('should also have a passing test', function () { expect(true).toEqual(true); }); diff --git a/templates/spec_helper.js b/templates/spec/javascript/SpecHelper.js similarity index 100% rename from templates/spec_helper.js rename to templates/spec/javascript/SpecHelper.js