Add rails generators. Add sources.yaml support. Fix Prototype.js collision on jasmine:ci. Version bump to 0.4.0.

This commit is contained in:
ragaskar 2010-01-26 07:55:04 -08:00
parent d2fe23b52b
commit 53305edc68
24 changed files with 267 additions and 218 deletions

View File

@ -1,5 +1,5 @@
---
:minor: 1
:build:
:patch: 3
:patch: 0
:major: 0
:build:
:minor: 4

View File

@ -10,45 +10,46 @@ def expand(*paths)
File.expand_path(File.join(*paths))
end
def rakefile_path
expand(cwd, 'templates/Rakefile')
def template_path(filepath)
expand(cwd, File.join("generators/jasmine/templates", filepath))
end
def dest_path(filepath)
expand(Dir.pwd, filepath)
end
def copy_unless_exists(relative_path, dest_path = nil)
unless File.exist?(dest_path(relative_path))
File.copy(template_path(relative_path), dest_path(dest_path || relative_path))
end
end
if ARGV[0] == 'init'
require 'ftools'
File.makedirs('spec/javascripts')
File.makedirs('spec/helpers')
File.makedirs('spec/javascripts/support')
dest_root = File.expand_path(Dir.pwd)
dest_spec = expand(dest_root, 'spec')
dest_spec_javascripts = expand(dest_root, 'spec/javascripts')
dest_spec_helpers = expand(dest_root, 'spec/helpers')
copy_unless_exists('spec/javascripts/SpecHelper.js')
copy_unless_exists('spec/javascripts/ExampleSpec.js')
copy_unless_exists('spec/javascripts/support/jasmine_config.rb')
copy_unless_exists('spec/javascripts/support/jasmine_spec.rb')
unless File.exist?(expand(dest_spec_helpers, 'spec_helper.js'))
File.copy(expand(cwd, 'templates/spec_helper.js'), dest_spec_helpers)
end
unless File.exist?(expand(dest_spec_helpers, 'jasmine_helper.rb'))
File.copy(expand(cwd, 'templates/jasmine_helper.rb'), dest_spec_helpers)
end
File.copy(expand(cwd, 'templates/example_spec.js'), dest_spec_javascripts)
rails_tasks_dir = expand(dest_root, 'lib', 'tasks')
rails_tasks_dir = dest_path('lib/tasks')
if File.exist?(rails_tasks_dir)
File.makedirs('lib/tasks/jasmine')
File.copy(rakefile_path, File.join(rails_tasks_dir, 'jasmine/jasmine.rake'))
copy_unless_exists('lib/tasks/jasmine.rake')
copy_unless_exists('spec/javascripts/support/sources-rails.yaml', 'spec/javascripts/support/sources.yaml')
else
if File.exist?(expand(dest_root, 'Rakefile'))
existing_rakefile = expand(dest_root, 'Rakefile')
load existing_rakefile
unless Rake::Task.task_defined?('jasmine')
open(existing_rakefile, 'a') do |f|
f.write(File.read(rakefile_path))
end
end
else
File.copy(rakefile_path, dest_root)
copy_unless_exists('spec/javascripts/support/sources.yaml')
if File.exist?(dest_path('Rakefile'))
load dest_path('Rakefile')
end
write_mode = Rake::Task.task_defined?('jasmine') ? 'a' : 'w'
File.open(dest_path('Rakefile'), write_mode) do |f|
f.write(File.read(template_path('lib/tasks/jasmine.rake')))
end
end
File.open(template_path('INSTALL'), 'r').each_line do |line|
puts line
end
end

View File

@ -0,0 +1,25 @@
class JasmineGenerator < Rails::Generator::Base
def manifest
record do |m|
m.directory "spec/javascripts"
m.file "spec/javascripts/SpecHelper.js", "spec/javascripts/SpecHelper.js"
m.file "spec/javascripts/ExampleSpec.js", "spec/javascripts/ExampleSpec.js"
m.directory "spec/javascripts/support"
m.file "spec/javascripts/support/jasmine_config.rb", "spec/javascripts/support/jasmine_config.rb"
m.file "spec/javascripts/support/jasmine_spec.rb", "spec/javascripts/support/jasmine_spec.rb"
m.file "spec/javascripts/support/sources-rails.yaml", "spec/javascripts/support/sources.yaml"
m.directory "lib/tasks"
m.file "lib/tasks/jasmine.rake", "lib/tasks/jasmine.rake"
m.readme "INSTALL"
end
end
def file_name
"create_blog"
end
end

View File

@ -0,0 +1,9 @@
Jasmine has been installed with example specs.
To run the server:
rake jasmine
To run the automated CI task with Selenium:
rake jasmine:ci

View File

@ -0,0 +1,23 @@
namespace :jasmine do
require 'jasmine'
desc "Run continuous integration tests"
require "spec"
require 'spec/rake/spectask'
Spec::Rake::SpecTask.new(:ci) do |t|
t.spec_opts = ["--color", "--format", "specdoc"]
t.verbose = true
t.spec_files = ['spec/javascripts/support/jasmine_spec.rb']
end
task :server do
require 'spec/javascripts/support/jasmine_config'
puts "your tests are here:"
puts " http://localhost:8888/run.html"
Jasmine::Config.new.start_server
end
end
desc "Run specs via server"
task :jasmine => ['jasmine:server']

View File

@ -0,0 +1,23 @@
require 'jasmine'
class Jasmine::Config
def project_root
File.expand_path(File.join(File.dirname(__FILE__), "..", "..", ".."))
end
# Return an array of files to include before jasmine specs. Override if needed.
# def src_files
# match_files(src_dir, "**/*.js")
# end
# Path to your JavaScript source files
# def src_dir
# File.join(project_root, "public")
# end
# Path to your JavaScript specs
# def spec_dir
# File.join(project_root, 'spec/javascripts')
# end
end

View File

@ -0,0 +1,17 @@
require 'rubygems'
require File.expand_path(File.join(File.dirname(__FILE__), 'jasmine_config'))
jasmine_config = Jasmine::Config.new
spec_builder = Jasmine::SpecBuilder.new(jasmine_config)
should_stop = false
Spec::Runner.configure do |config|
config.after(:suite) do
spec_builder.stop if should_stop
end
end
spec_builder.start
should_stop = true
spec_builder.declare_suites

View File

@ -0,0 +1,8 @@
sources:
- javascripts/prototype.js
- javascripts/effects.js
- javascripts/controls.js
- javascripts/dragdrop.js
- javascripts/application.js
src_dir: public
spec_dir: spec/javascripts

View File

@ -0,0 +1,5 @@
#sources:
# - lib/source1.js
# - lib/source2.js
#src_dir:
#spec_dir: spec/javascripts

View File

@ -5,19 +5,28 @@
Gem::Specification.new do |s|
s.name = %q{jasmine}
s.version = "0.1.3"
s.version = "0.4.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Rajan Agaskar", "Christian Williams"]
s.date = %q{2009-12-31}
s.date = %q{2010-01-26}
s.default_executable = %q{jasmine}
s.description = %q{Javascript BDD test framework}
s.email = %q{ragaskar@gmail.com}
s.executables = ["autospec", "edit_json.rb", "jasmine", "jeweler", "prettify_json.rb", "rackup", "rake", "rubyforge", "selenium-rc", "spec", "thin"]
s.executables = ["jasmine"]
s.extra_rdoc_files = [
"README.markdown"
]
s.files = [
"bin/jasmine",
"generators/jasmine/jasmine_generator.rb",
"generators/jasmine/templates/INSTALL",
"generators/jasmine/templates/lib/tasks/jasmine.rake",
"generators/jasmine/templates/spec/javascripts/ExampleSpec.js",
"generators/jasmine/templates/spec/javascripts/SpecHelper.js",
"generators/jasmine/templates/spec/javascripts/support/jasmine_config.rb",
"generators/jasmine/templates/spec/javascripts/support/jasmine_spec.rb",
"generators/jasmine/templates/spec/javascripts/support/sources-rails.yaml",
"generators/jasmine/templates/spec/javascripts/support/sources.yaml",
"jasmine/contrib/ruby/jasmine_runner.rb",
"jasmine/contrib/ruby/jasmine_spec_builder.rb",
"jasmine/contrib/ruby/run.html",
@ -29,13 +38,10 @@ Gem::Specification.new do |s|
"lib/jasmine.rb",
"lib/jasmine/base.rb",
"lib/jasmine/config.rb",
"lib/jasmine/jasmine_helper.rb",
"lib/jasmine/jasmine_meta_spec.rb",
"lib/jasmine/run.html.erb",
"lib/jasmine/selenium_driver.rb",
"lib/jasmine/server.rb",
"lib/jasmine/spec_builder.rb",
"templates/Rakefile"
"lib/jasmine/spec_builder.rb"
]
s.homepage = %q{http://github.com/pivotal/jasmine-ruby}
s.rdoc_options = ["--charset=UTF-8"]

View File

@ -3,5 +3,4 @@ require 'jasmine/config'
require 'jasmine/server'
require 'jasmine/selenium_driver'
require 'jasmine/jasmine_helper'
require 'jasmine/spec_builder'

View File

@ -1,5 +1,7 @@
module Jasmine
class Config
require 'yaml'
def initialize(options = {})
require 'selenium_rc'
@selenium_jar_path = SeleniumRC::Server.allocate.jar_path
@ -86,31 +88,60 @@ module Jasmine
Dir.glob(File.join(dir, pattern)).collect {|f| f.sub("#{dir}/", "")}.sort
end
def project_root
Dir.pwd
end
def src_dir
if simple_config['src_dir']
File.join(project_root, simple_config['src_dir'])
else
project_root
end
end
def simple_config_file
File.join(project_root, 'spec/javascripts/support/sources.yaml')
end
def simple_config
config = File.exist?(simple_config_file) ? File.open(simple_config_file) { |yf| YAML::load( yf ) } : false
config || {}
end
def src_files
match_files(src_dir, "**/*.js")
simple_config['sources'] || []
end
def src_path
"src"
end
def spec_path
"spec"
def spec_dir
if simple_config['spec_dir']
File.join(project_root, simple_config['spec_dir'])
else
File.join(project_root, 'spec/javascripts')
end
end
def spec_files
match_files(spec_dir, "**/*.js")
end
def spec_path
"/__spec__"
end
def root_path
"/__root__"
end
def mappings
{
"/" + src_path => src_dir,
"/" + spec_path => spec_dir
spec_path => spec_dir,
root_path => project_root
}
end
def js_files
src_files.collect {|f| "/" + File.join(src_path, f) } + spec_files.collect {|f| "/" + File.join(spec_path, f) }
src_files.collect {|f| "/" + f } + spec_files.collect {|f| File.join(spec_path, f) }
end
def spec_files_full_paths

View File

@ -1,48 +0,0 @@
class JasmineHelper
def self.lib_dir
File.expand_path(File.join(root, 'lib'))
end
def self.jasmine
['/lib/' + File.basename(Dir.glob("#{JasmineHelper.lib_dir}/jasmine*.js").first)] +
['/lib/json2.js',
'/lib/TrivialReporter.js']
end
def self.root
File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'jasmine'))
end
def self.spec_dir
File.expand_path('spec')
end
def self.spec_files
Dir.glob(File.join(spec_dir, "javascripts/**/*[Ss]pec.js"))
end
def self.specs
spec_files.collect {|f| f.sub(spec_dir, "/spec")}
end
def self.spec_helpers_files
Dir.glob(File.join(spec_dir, "helpers/**/*.js"))
end
def self.spec_helpers
spec_helpers_files.collect {|f| f.sub(spec_dir, "/spec")}
end
def self.meta_spec_path
File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'lib', 'jasmine', 'jasmine_meta_spec.rb'))
end
def self.files
[]
end
def self.stylesheets
[]
end
end

View File

@ -1,30 +0,0 @@
require 'rubygems'
require "selenium_rc"
require File.expand_path(File.join(File.dirname(__FILE__), "jasmine_helper.rb"))
helper_overrides = File.expand_path(File.join(Dir.pwd, "spec", "helpers", "jasmine_helper.rb"))
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__), "spec_builder"))
jasmine_runner = Jasmine::Runner.new(SeleniumRC::Server.new.jar_path,
Dir.pwd,
JasmineHelper.specs,
{ :spec_helpers => JasmineHelper.files + JasmineHelper.spec_helpers,
:stylesheets => JasmineHelper.stylesheets
})
spec_builder = Jasmine::SpecBuilder.new(JasmineHelper.spec_files, jasmine_runner)
should_stop = false
Spec::Runner.configure do |config|
config.after(:suite) do
spec_builder.stop if should_stop
end
end
spec_builder.start
should_stop = true
spec_builder.declare_suites

View File

@ -37,8 +37,8 @@ module Jasmine
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]
result = @driver.get_eval("try { eval(#{escaped_script}, window); } catch(err) { window.eval(#{escaped_script}); }")
JSON.parse("{\"result\":#{result}}")["result"]
end
end
end

View File

@ -82,7 +82,7 @@ module Jasmine
class Server
attr_reader :thin
def initialize(port, config)
@port = port
@config = config
@ -101,6 +101,7 @@ module Jasmine
thin_config["/__JASMINE_ROOT__"] = Rack::File.new(Jasmine.root)
app = Rack::Cascade.new([
Rack::URLMap.new({'/' => Rack::File.new(@config.src_dir)}),
Rack::URLMap.new(thin_config),
JsAlert.new
])

View File

@ -58,7 +58,7 @@ module Jasmine
sleep 0.1
end
@suites = eval_js('JSON.stringify(jsApiReporter.suites())')
@suites = eval_js("var result = jsApiReporter.suites(); if (window.Prototype && Object.toJSON) { Object.toJSON(result) } else { JSON.stringify(result) }")
end
def results_for(spec_id)
@ -69,7 +69,7 @@ module Jasmine
def load_results
@spec_results = {}
@spec_ids.each_slice(50) do |slice|
@spec_results.merge!(eval_js("JSON.stringify(jsApiReporter.resultsForSpecs(#{JSON.generate(slice)}))"))
@spec_results.merge!(eval_js("var result = jsApiReporter.resultsForSpecs(#{JSON.generate(slice)}); if (window.Prototype && Object.toJSON) { Object.toJSON(result) } else { JSON.stringify(result) }"))
end
@spec_results
end
@ -116,7 +116,6 @@ module Jasmine
def report_spec(spec_id)
spec_results = results_for(spec_id)
out = ""
messages = spec_results['messages'].each do |message|
case

View File

@ -2,52 +2,76 @@ 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"))
@template_dir = File.expand_path(File.join(File.dirname(__FILE__), "../generators/jasmine/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']
describe "simple_config" do
it "if sources.yaml not found" do
File.stub!(:exist?).and_return(false)
@config.src_files.should == []
@config.spec_files.should == ['javascripts/ExampleSpec.js', 'javascripts/SpecHelper.js']
@config.mappings.should == {
'/__root__' => @config.project_root,
'/__spec__' => @config.spec_dir
}
end
it "if sources.yaml is empty" do
YAML.stub!(:load).and_return(false)
@config.src_files.should == []
@config.spec_files.should == ['javascripts/ExampleSpec.js', 'javascripts/SpecHelper.js']
@config.mappings.should == {
'/__root__' => @config.project_root,
'/__spec__' => @config.spec_dir
}
end
it "using default sources.yaml" do
@config.stub!(:simple_config_file).and_return(File.join(@template_dir, 'spec/javascripts/support/sources.yaml'))
@config.src_files.should == []
@config.spec_files.should == ['javascripts/ExampleSpec.js', 'javascripts/SpecHelper.js']
@config.mappings.should == {
'/__root__' => @config.project_root,
'/__spec__' => @config.spec_dir
}
end
it "using rails sources.yaml" do
@config.stub!(:simple_config_file).and_return(File.join(@template_dir, 'spec/javascripts/support/sources-rails.yaml'))
@config.src_files.should == ['javascripts/prototype.js',
'javascripts/effects.js',
'javascripts/controls.js',
'javascripts/dragdrop.js',
'javascripts/application.js']
@config.spec_files.should == ['javascripts/ExampleSpec.js', 'javascripts/SpecHelper.js']
@config.js_files.should == [
'/javascripts/prototype.js',
'/javascripts/effects.js',
'/javascripts/controls.js',
'/javascripts/dragdrop.js',
'/javascripts/application.js',
'/__spec__/javascripts/ExampleSpec.js',
'/__spec__/javascripts/SpecHelper.js',
]
end
end
it "should provide dir mappings" do
@config.mappings.should == {
'/__root__' => @config.project_root,
'/__spec__' => @config.spec_dir
}
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')
File.join(@template_dir, 'spec/javascripts/ExampleSpec.js'),
File.join(@template_dir, 'spec/javascripts/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

View File

@ -1,15 +1,15 @@
require 'jasmine'
class JasmineSelfTestConfig < Jasmine::Config
def proj_root
def project_root
File.expand_path(File.join(File.dirname(__FILE__), ".."))
end
def src_dir
File.join(proj_root, 'src')
File.join(project_root, 'src')
end
def spec_dir
File.join(proj_root, 'jasmine/spec')
File.join(project_root, 'jasmine/spec')
end
end

View File

@ -1,30 +0,0 @@
namespace :jasmine do
require 'jasmine'
helper_overrides = File.expand_path(File.join(File.dirname(__FILE__), "spec/helpers/jasmine_helper.rb"))
if File.exist?(helper_overrides)
require helper_overrides
end
desc "Run continuous integration tests"
require "spec"
require 'spec/rake/spectask'
Spec::Rake::SpecTask.new(:ci) do |t|
t.spec_opts = ["--color", "--format", "specdoc"]
t.verbose = true
t.spec_files = [JasmineHelper.meta_spec_path]
end
task :server do
puts "your tests are here:"
puts " http://localhost:8888/run.html"
Jasmine::Server.start(8888,
File.expand_path(Dir.pwd),
lambda { JasmineHelper.specs },
{ :spec_helpers => JasmineHelper.files + JasmineHelper.spec_helpers,
:stylesheets => JasmineHelper.stylesheets
})
end
end
desc "Run specs via server"
task :jasmine => ['jasmine:server']

View File

@ -1,2 +0,0 @@
ExampleClass = function() {
};

View File

@ -1,12 +0,0 @@
class JasmineHelper
def self.files
#return a list of files you want to load before your spec defintions load
[]
end
def self.stylesheets
#return a list of stylesheets you want to load in the runner
[]
end
end