Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Unit test lib reorganizing.

  • Loading branch information...
commit 41ef42e0335ad0d881e6c5afe77d2e8a639d481b 1 parent 26ca9c3
@tobie tobie authored
View
32 Rakefile
@@ -5,6 +5,8 @@ PROTOTYPE_ROOT = File.expand_path(File.dirname(__FILE__))
PROTOTYPE_SRC_DIR = File.join(PROTOTYPE_ROOT, 'src')
PROTOTYPE_DIST_DIR = File.join(PROTOTYPE_ROOT, 'dist')
PROTOTYPE_PKG_DIR = File.join(PROTOTYPE_ROOT, 'pkg')
+PROTOTYPE_TEST_DIR = File.join(PROTOTYPE_ROOT, 'test')
+PROTOTYPE_TMP_DIR = File.join(PROTOTYPE_TEST_DIR, 'unit', 'tmp')
PROTOTYPE_VERSION = '1.6.0.2'
task :default => [:dist, :dist_helper, :package, :clean_package_source]
@@ -50,23 +52,20 @@ task :test => [:dist, :test_units]
require 'test/lib/jstest'
desc "Runs all the JavaScript unit tests and collects the results"
-JavaScriptTestTask.new(:test_units) do |t|
+JavaScriptTestTask.new(:test_units => [:build_unit_tests]) do |t|
testcases = ENV['TESTCASES']
tests_to_run = ENV['TESTS'] && ENV['TESTS'].split(',')
browsers_to_test = ENV['BROWSERS'] && ENV['BROWSERS'].split(',')
- tmp_dir = "test/unit/tmp"
t.mount("/dist")
t.mount("/test")
- Dir.mkdir(tmp_dir) unless File.exist?(tmp_dir)
+ Dir.mkdir(PROTOTYPE_TMP_DIR) unless File.exist?(PROTOTYPE_TMP_DIR)
- Dir["test/unit/*_test.js"].each do |file|
- TestBuilder.new(file).render
- test_file = File.basename(file, ".js")
- test_name = test_file.sub("_test", "")
+ Dir["test/unit/tmp/*_test.html"].each do |file|
+ test_name = File.basename(file).sub("_test.html", "")
unless tests_to_run && !tests_to_run.include?(test_name)
- t.run("/#{tmp_dir}/#{test_file}.html", testcases)
+ t.run("/#{file}", testcases)
end
end
@@ -74,3 +73,20 @@ JavaScriptTestTask.new(:test_units) do |t|
t.browser(browser.to_sym) unless browsers_to_test && !browsers_to_test.include?(browser)
end
end
+
+task :build_unit_tests do
+ Dir[File.join('test', 'unit', '*_test.js')].each do |file|
+ PageBuilder.new(file, 'prototype.erb').render
+ end
+end
+
+task :clean_package_source do
+ rm_rf File.join(PROTOTYPE_PKG_DIR, "prototype-#{PROTOTYPE_VERSION}")
+end
+
+desc 'Generates an empty tmp directory for building tests.'
+task :clean_tmp do
+ puts 'Generating an empty tmp directory for building tests.'
+ FileUtils.rm_rf(PROTOTYPE_TMP_DIR) if File.exist?(PROTOTYPE_TMP_DIR)
+ Dir.mkdir(PROTOTYPE_TMP_DIR)
+end
View
0  test/test.css → test/lib/assets/test.css
File renamed without changes
View
0  test/lib/unittest.js → test/lib/assets/unittest.js
File renamed without changes
View
37 test/lib/jstest.rb
@@ -426,17 +426,17 @@ def summary
end
end
-class TestBuilder
- UNITTEST_DIR = File.expand_path('test')
- FIXTURES_DIR = File.join(UNITTEST_DIR, 'unit', 'fixtures')
- TMP_DIR = File.join(UNITTEST_DIR, 'unit', 'tmp')
- TEMPLATE = File.join(UNITTEST_DIR, 'lib', 'template.erb')
+class PageBuilder
+ UNITTEST_DIR = File.expand_path('test')
+ FIXTURES_DIR = File.join(UNITTEST_DIR, 'unit', 'fixtures')
+ TMP_DIR = File.join(UNITTEST_DIR, 'unit', 'tmp')
+ TEMPLATES_DIR = File.join(UNITTEST_DIR, 'lib', 'templates')
- def initialize(filename, template = TEMPLATE)
+ def initialize(filename, template = 'default.erb')
@filename = filename
- @template = template
+ @template = File.join(self.class::TEMPLATES_DIR, template)
@js_filename = File.basename(@filename)
- @basename = @js_filename.sub("_test.js", "")
+ @basename = @js_filename.sub('_test.js', '')
end
def html_fixtures
@@ -452,18 +452,25 @@ def external_fixtures(extension)
end
def render
- @title = @basename.gsub("_", " ").strip.capitalize
+ @title = @basename.gsub('_', ' ').strip.capitalize
@html_fixtures = html_fixtures
- @js_fixtures_filename = external_fixtures("js")
- @css_fixtures_filename = external_fixtures("css")
+ @js_fixtures_filename = external_fixtures('js')
+ @css_fixtures_filename = external_fixtures('css')
- File.open(destination, "w+") do |file|
- file << ERB.new(IO.read(@template), nil, "%").result(binding)
+ File.open(destination, 'w+') do |file|
+ file << ERB.new(IO.read(@template), nil, '%').result(binding)
end
end
def destination
- filename = File.basename(@filename, ".js")
- File.join(TMP_DIR, "#{filename}.html")
+ name_file(:ext => 'html')
+ end
+
+ def name_file(options = {})
+ prefix = options[:prefix] ? "#{options[:prefix]}_" : ""
+ suffix = options[:suffix] ? "_#{options[:suffix]}" : ""
+ ext = options[:ext] ? options[:ext] : "js"
+ filename = File.basename(@filename, '.js')
+ File.join(TMP_DIR, "#{prefix}#{filename}#{suffix}.#{ext}")
end
end
View
35 test/lib/templates/default.erb
@@ -0,0 +1,35 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+ <title>Unit test file | <%= @title %></title>
+ <meta http-equiv="content-type" content="text/html; charset=utf-8" />
+ <script src="../../../dist/prototype.js" type="text/javascript"></script>
+ <script src="../../lib/assets/unittest.js" type="text/javascript"></script>
+ <link rel="stylesheet" href="../../lib/assets/test.css" type="text/css" />
+
+ <% if @css_fixtures_filename %>
+ <link rel="stylesheet" href="../fixtures/<%= @css_fixtures_filename %>" type="text/css" charset="utf-8" />
+ <% end %>
+
+ <% if @js_fixtures_filename %>
+ <script src="../fixtures/<%= @js_fixtures_filename %>" type="text/javascript" charset="utf-8"></script>
+ <% end %>
+
+ <script src="../<%= @js_filename %>" type="text/javascript"></script>
+</head>
+<body>
+<h1>Unit test file</h1>
+<h2><%= @title %></h2>
+
+<!-- This file is programmatically generated. Do not attempt to modify it. Instead, modify <%= @fixtures_filename %> -->
+
+<!-- Log output start -->
+<div id="testlog"></div>
+<!-- Log output end -->
+
+<!-- HTML Fixtures start -->
+<%= @html_fixtures %>
+<!-- HTML Fixtures end -->
+</body>
+</html>
View
6 test/lib/template.erb → test/lib/templates/prototype.erb
@@ -9,8 +9,8 @@
var originalElement = window.Element;
</script>
<script src="../../../dist/prototype.js" type="text/javascript"></script>
- <script src="../../lib/unittest.js" type="text/javascript"></script>
- <link rel="stylesheet" href="../../test.css" type="text/css" />
+ <script src="../../lib/assets/unittest.js" type="text/javascript"></script>
+ <link rel="stylesheet" href="../../lib/assets/test.css" type="text/css" />
<% if @css_fixtures_filename %>
<link rel="stylesheet" href="../fixtures/<%= @css_fixtures_filename %>" type="text/css" charset="utf-8" />
@@ -39,4 +39,4 @@
</html>
<script type="text/javascript" charset="utf-8">
eventResults.endOfDocument = true;
-</script>
+</script>
Please sign in to comment.
Something went wrong with that request. Please try again.