Skip to content

Commit d135749

Browse files
Merge pull request #283 from sstephenson/mocha
Switch to Mocha for testing. Requires Sinatra. rake test:start starts the test server, and from there you can either (a) visit the test page in a browser of your choice, or (b) use rake test:run with the same BROWSERS and TESTS options you're used to from the old test system. All tests have been ported over.
2 parents 0f99752 + 8156de1 commit d135749

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+14851
-6756
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.DS_Store
22
pkg
33
test/unit/tmp/*
4+
test/unit/browsers.yml
45
doc
56
tmp
67
*.pdoc.yaml

.gitmodules

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
[submodule "vendor/unittest_js"]
2-
path = vendor/unittest_js
3-
url = git://github.com/tobie/unittest_js.git
41
[submodule "vendor/caja_builder"]
52
path = vendor/caja_builder
63
url = git://github.com/tobie/unittest_js_caja_builder.git

Rakefile

Lines changed: 50 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'rake'
22
require 'rake/packagetask'
3+
require 'rbconfig'
34
require 'yaml'
45

56
module PrototypeHelper
@@ -18,6 +19,9 @@ module PrototypeHelper
1819

1920
DEFAULT_SELECTOR_ENGINE = 'sizzle'
2021

22+
host = RbConfig::CONFIG['host']
23+
IS_WINDOWS = host.include?('mswin') || host.include?('mingw32')
24+
2125
# Possible options for PDoc syntax highlighting, in order of preference.
2226
SYNTAX_HIGHLIGHTERS = [:pygments, :coderay, :none]
2327

@@ -29,7 +33,7 @@ module PrototypeHelper
2933
begin
3034
`git --version`
3135
return true
32-
rescue Error => e
36+
rescue Error
3337
return false
3438
end
3539
end
@@ -99,6 +103,26 @@ EOF
99103
})
100104
end
101105

106+
def self.require_package(name)
107+
begin
108+
require name
109+
rescue LoadError
110+
puts "You need the #{name} package. Try installing it with:\n"
111+
puts " $ gem install #{name}"
112+
exit
113+
end
114+
end
115+
116+
def self.require_phantomjs
117+
cmd = IS_WINDOWS ? "phantomjs.cmd -v" : "phantomjs -v > /dev/null 2>&1"
118+
success = system(cmd)
119+
if !success
120+
puts "\nYou need phantomjs installed to run this task. Find out how at:"
121+
puts " http://phantomjs.org/download.html"
122+
exit
123+
end
124+
end
125+
102126
def self.syntax_highlighter
103127
if ENV['SYNTAX_HIGHLIGHTER']
104128
highlighter = ENV['SYNTAX_HIGHLIGHTER'].to_sym
@@ -123,7 +147,7 @@ EOF
123147
when :coderay
124148
begin
125149
require 'coderay'
126-
rescue LoadError => e
150+
rescue LoadError
127151
if verbose
128152
puts "\nYou asked to use CodeRay, but I can't find the 'coderay' gem. Just run:\n\n"
129153
puts " $ gem install coderay"
@@ -259,109 +283,37 @@ task :clean_package_source do
259283
rm_rf File.join(PrototypeHelper::PKG_DIR, "prototype-#{PrototypeHelper::VERSION}")
260284
end
261285

262-
task :test => ['test:build', 'test:run']
286+
task :test => ['test:require', 'test:start']
263287
namespace :test do
264-
desc 'Runs all the JavaScript unit tests and collects the results'
265-
task :run => [:require] do
266-
testcases = ENV['TESTCASES']
267-
browsers_to_test = ENV['BROWSERS'] && ENV['BROWSERS'].split(',')
268-
tests_to_run = ENV['TESTS'] && ENV['TESTS'].split(',')
269-
runner = UnittestJS::WEBrickRunner::Runner.new(:test_dir => PrototypeHelper::TMP_DIR)
270-
271-
Dir[File.join(PrototypeHelper::TMP_DIR, '*_test.html')].each do |file|
272-
file = File.basename(file)
273-
test = file.sub('_test.html', '')
274-
unless tests_to_run && !tests_to_run.include?(test)
275-
runner.add_test(file, testcases)
276-
end
277-
end
278-
279-
UnittestJS::Browser::SUPPORTED.each do |browser|
280-
unless browsers_to_test && !browsers_to_test.include?(browser)
281-
runner.add_browser(browser.to_sym)
282-
end
283-
end
284-
285-
trap('INT') { runner.teardown; exit }
286-
runner.run
287-
end
288-
289-
task :build => [:clean, :dist] do
290-
builder = UnittestJS::Builder::SuiteBuilder.new({
291-
:input_dir => PrototypeHelper::TEST_UNIT_DIR,
292-
:assets_dir => PrototypeHelper::DIST_DIR
293-
})
294-
selected_tests = (ENV['TESTS'] || '').split(',')
295-
builder.collect(*selected_tests)
296-
builder.render
297-
end
298-
299-
task :clean => [:require] do
300-
UnittestJS::Builder.empty_dir!(PrototypeHelper::TMP_DIR)
288+
desc 'Starts the test server.'
289+
task :start => [:require] do
290+
path_to_app = File.join(PrototypeHelper::ROOT_DIR, 'test', 'unit', 'server.rb')
291+
require path_to_app
292+
293+
puts "Starting unit test server..."
294+
puts "Unit tests available at <http://127.0.0.1:4567/test/>\n\n"
295+
UnitTests.run!
301296
end
302297

303298
task :require do
304-
PrototypeHelper.require_unittest_js
299+
PrototypeHelper.require_package('sinatra')
305300
end
306301

307-
desc "Builds all the unit tests and starts the server. (The user can visit the tests manually in a browser at their leisure.)"
308-
task :server => [:build] do
309-
runner = UnittestJS::WEBrickRunner::Runner.new(:test_dir => PrototypeHelper::TMP_DIR)
310-
testcases = ENV['TESTCASES']
311-
312-
Dir[File.join(PrototypeHelper::TMP_DIR, '*_test.html')].each do |file|
313-
file = File.basename(file)
314-
test = file.sub('_test.html', '')
315-
runner.add_test(file, testcases)
316-
end
317-
318-
trap('INT') do
319-
puts "...server stopped."
320-
runner.teardown
321-
exit
322-
end
323-
324-
puts "Server started..."
325-
326-
runner.setup
302+
desc "Opens the test suite in several different browsers. (Does not start or stop the server; you should do that separately.)"
303+
task :run => [:require] do
304+
browsers, tests, grep = ENV['BROWSERS'], ENV['TESTS'], ENV['GREP']
305+
path_to_runner = File.join(PrototypeHelper::ROOT_DIR, 'test', 'unit', 'runner.rb')
306+
require path_to_runner
327307

328-
loop do
329-
sleep 1
330-
end
308+
Runner::run(browsers, tests, grep)
331309
end
332-
end
333-
334-
task :test_units do
335-
puts '"rake test_units" is deprecated. Please use "rake test" instead.'
336-
end
337-
338-
task :build_unit_tests do
339-
puts '"rake test_units" is deprecated. Please use "rake test:build" instead.'
340-
end
341310

342-
task :clean_tmp do
343-
puts '"rake clean_tmp" is deprecated. Please use "rake test:clean" instead.'
344-
end
345-
346-
namespace :caja do
347-
task :test => ['test:build', 'test:run']
348-
349-
namespace :test do
350-
task :run => ['rake:test:run']
351-
352-
task :build => [:require, 'rake:test:clean', :dist] do
353-
builder = UnittestJS::CajaBuilder::SuiteBuilder.new({
354-
:input_dir => PrototypeHelper::TEST_UNIT_DIR,
355-
:assets_dir => PrototypeHelper::DIST_DIR,
356-
:whitelist_dir => File.join(PrototypeHelper::TEST_DIR, 'unit', 'caja_whitelists'),
357-
:html_attrib_schema => 'html_attrib.json'
358-
})
359-
selected_tests = (ENV['TESTS'] || '').split(',')
360-
builder.collect(*selected_tests)
361-
builder.render
362-
end
311+
desc "Runs the tests in PhantomJS. (Does not start or stop the server; you should do that separately.)"
312+
task :phantom do
313+
PrototypeHelper.require_phantomjs
314+
tests, grep = ENV['TESTS'], ENV['GREP']
315+
url = "http://127.0.0.1:4567/test/#{tests}"
316+
url << "?grep=#{grep}" if grep
317+
system(%Q[phantomjs ./test/unit/phantomjs/mocha-phantomjs.js "#{url}"])
363318
end
364-
task :require => ['rake:test:require'] do
365-
PrototypeHelper.require_caja_builder
366-
end
367-
end
319+
end

0 commit comments

Comments
 (0)