js-test-driver-rails 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. data/Gemfile +2 -1
  2. data/README +40 -0
  3. data/Rakefile +1 -0
  4. data/VERSION +1 -1
  5. data/lib/js_test_driver.rb +5 -1
  6. data/lib/js_test_driver/config.rb +84 -7
  7. data/lib/js_test_driver/html_fixture.rb +40 -0
  8. data/lib/js_test_driver/runner.rb +21 -3
  9. data/test/fixtures/a.html +1 -0
  10. data/test/fixtures/b.html +1 -0
  11. data/test/fixtures/baz/a.html +1 -0
  12. data/test/fixtures/c.html +1 -0
  13. data/test/fixtures/foo/a.html +1 -0
  14. data/test/fixtures/foo/bar/a.html +1 -0
  15. data/test/fixtures/hello.txt +1 -0
  16. data/test/test_helper.rb +19 -0
  17. data/test/unit/config_test.rb +75 -2
  18. data/test/unit/html_fixture_test.rb +70 -0
  19. data/vendor/VERSIONS +3 -0
  20. data/vendor/jasmine-jstd-adapter/MIT.LICENSE +22 -0
  21. data/vendor/jasmine-jstd-adapter/README.md +25 -0
  22. data/vendor/jasmine-jstd-adapter/jsTestDriver.conf +6 -0
  23. data/vendor/jasmine-jstd-adapter/lib/jasmine/jasmine-0.11.1.js +2343 -0
  24. data/vendor/jasmine-jstd-adapter/lib/jstestdriver/JsTestDriver.jar +0 -0
  25. data/vendor/jasmine-jstd-adapter/server.sh +1 -0
  26. data/vendor/jasmine-jstd-adapter/src-test/tests.js +89 -0
  27. data/vendor/jasmine-jstd-adapter/src/JasmineAdapter.js +111 -0
  28. data/vendor/jasmine-jstd-adapter/test.sh +1 -0
  29. data/vendor/jasmine/Gemfile +6 -0
  30. data/vendor/jasmine/MIT.LICENSE +20 -0
  31. data/vendor/jasmine/README.markdown +28 -0
  32. data/vendor/jasmine/Rakefile +178 -0
  33. data/vendor/jasmine/cruise_config.rb +21 -0
  34. data/vendor/jasmine/example/SpecRunner.html +27 -0
  35. data/vendor/jasmine/example/spec/PlayerSpec.js +58 -0
  36. data/vendor/jasmine/example/spec/SpecHelper.js +9 -0
  37. data/vendor/jasmine/example/src/Player.js +22 -0
  38. data/vendor/jasmine/example/src/Song.js +7 -0
  39. data/vendor/jasmine/images/fail-16.png +0 -0
  40. data/vendor/jasmine/images/fail.png +0 -0
  41. data/vendor/jasmine/images/go-16.png +0 -0
  42. data/vendor/jasmine/images/go.png +0 -0
  43. data/vendor/jasmine/images/pending-16.png +0 -0
  44. data/vendor/jasmine/images/pending.png +0 -0
  45. data/vendor/jasmine/images/question-bk.png +0 -0
  46. data/vendor/jasmine/images/questionbk-16.png +0 -0
  47. data/vendor/jasmine/images/spinner.gif +0 -0
  48. data/vendor/jasmine/lib/jasmine-html.js +182 -0
  49. data/vendor/jasmine/lib/jasmine.css +166 -0
  50. data/vendor/jasmine/lib/jasmine.js +2421 -0
  51. data/vendor/jasmine/lib/json2.js +478 -0
  52. data/vendor/jasmine/spec/runner.html +80 -0
  53. data/vendor/jasmine/spec/suites/BaseSpec.js +27 -0
  54. data/vendor/jasmine/spec/suites/CustomMatchersSpec.js +97 -0
  55. data/vendor/jasmine/spec/suites/EnvSpec.js +158 -0
  56. data/vendor/jasmine/spec/suites/ExceptionsSpec.js +107 -0
  57. data/vendor/jasmine/spec/suites/JsApiReporterSpec.js +103 -0
  58. data/vendor/jasmine/spec/suites/MatchersSpec.js +795 -0
  59. data/vendor/jasmine/spec/suites/MockClockSpec.js +38 -0
  60. data/vendor/jasmine/spec/suites/MultiReporterSpec.js +45 -0
  61. data/vendor/jasmine/spec/suites/NestedResultsSpec.js +54 -0
  62. data/vendor/jasmine/spec/suites/PrettyPrintSpec.js +93 -0
  63. data/vendor/jasmine/spec/suites/QueueSpec.js +23 -0
  64. data/vendor/jasmine/spec/suites/ReporterSpec.js +56 -0
  65. data/vendor/jasmine/spec/suites/RunnerSpec.js +267 -0
  66. data/vendor/jasmine/spec/suites/SpecRunningSpec.js +1253 -0
  67. data/vendor/jasmine/spec/suites/SpecSpec.js +124 -0
  68. data/vendor/jasmine/spec/suites/SpySpec.js +201 -0
  69. data/vendor/jasmine/spec/suites/SuiteSpec.js +120 -0
  70. data/vendor/jasmine/spec/suites/TrivialReporterSpec.js +235 -0
  71. data/vendor/jasmine/spec/suites/UtilSpec.js +40 -0
  72. data/vendor/jasmine/spec/suites/WaitsForBlockSpec.js +87 -0
  73. data/vendor/jasmine/src/Block.js +22 -0
  74. data/vendor/jasmine/src/Env.js +264 -0
  75. data/vendor/jasmine/src/JsApiReporter.js +102 -0
  76. data/vendor/jasmine/src/Matchers.js +354 -0
  77. data/vendor/jasmine/src/MultiReporter.js +35 -0
  78. data/vendor/jasmine/src/NestedResults.js +80 -0
  79. data/vendor/jasmine/src/PrettyPrinter.js +122 -0
  80. data/vendor/jasmine/src/Queue.js +99 -0
  81. data/vendor/jasmine/src/Reporter.js +31 -0
  82. data/vendor/jasmine/src/Runner.js +77 -0
  83. data/vendor/jasmine/src/Spec.js +242 -0
  84. data/vendor/jasmine/src/Suite.js +82 -0
  85. data/vendor/jasmine/src/WaitsBlock.js +13 -0
  86. data/vendor/jasmine/src/WaitsForBlock.js +52 -0
  87. data/vendor/jasmine/src/base.js +589 -0
  88. data/vendor/jasmine/src/html/TrivialReporter.js +182 -0
  89. data/vendor/jasmine/src/html/jasmine.css +166 -0
  90. data/vendor/jasmine/src/mock-timeout.js +183 -0
  91. data/vendor/jasmine/src/util.js +67 -0
  92. data/vendor/jasmine/src/version.json +5 -0
  93. metadata +102 -5
  94. data/test/unit/jsTestDriver.conf +0 -2
data/Gemfile CHANGED
@@ -3,4 +3,5 @@ source "http://rubygems.org"
3
3
  gem "bundler", "~> 0.9.26"
4
4
  gem "jeweler"
5
5
  gem "mocha"
6
- gem "rake"
6
+ gem "rake"
7
+ gem "json"
data/README CHANGED
@@ -4,6 +4,8 @@ To take advantage of it, you should create a js_test_driver.rb file in your RAIL
4
4
 
5
5
  The file may contain following directives:
6
6
 
7
+ # the paths are relative to the current directory, but you can use absolute paths too
8
+ # this is different from the JsTestDriver which does not allow you to use absolute paths in the config file
7
9
  includes 'foo', 'bar', 'public/javascripts/*.js' # files to be included
8
10
  excludes 'public/javascripts/fail.js' # files to be excluded, useful with globbing
9
11
 
@@ -14,6 +16,44 @@ browser 'firefox' # you can specify the default browsers which will be captured
14
16
 
15
17
  Note, that this is a ruby file, so the file/browser list can be generated dynamically - it's completely up to you.
16
18
 
19
+ For example in our project we examine a list of known browsers to determine the ones installed on the developer's system,
20
+ and define only those that were found.
21
+
22
+ Similarly we get the list of .js files to include from our asset packaging solution.
23
+
24
+ js-test-driver-rails also allows you to define HTML fixtures easily.
25
+
26
+ Imagine you have a directory structure like this:
27
+
28
+ RAILS_ROOT:
29
+ test/
30
+ js/
31
+ fixtures/
32
+ foo/
33
+ a.html
34
+ a.html
35
+
36
+ Then by defining the fixtures directory in your config file:
37
+ fixtures "test/js/fixtures"
38
+
39
+ At runtime, your tests will have access to the contents of the fixture files:
40
+ htmlFixtures.all["a"] // contents of the test/js/fixtures/a.html
41
+ htmlFixtures.all["foo/a"] // contents of the test/js/fixtures/foo/a.html
42
+
43
+ You can customize the namespace and fixture container name:
44
+ fixtures "test/js/fixtures", :name => "html", :namespace => "myApp"
45
+
46
+ By default JsTestDriver comes with a pretty simple Test::Unit like testing framework.
47
+ However it supports a couple of other frameworks, including Jasmine (http://pivotal.github.com/jasmine/), which
48
+ is an RSpec-like BDD framework.
49
+
50
+ If you want to user Jasmine, simply add:
51
+ enable_jasmine
52
+
53
+ in your config file, somewhere before including your test files and you are golden:)
54
+
55
+ # the fixtures will be accessible through myApp.html["<fixture name goes here>"]
56
+
17
57
  This gem comes with 3 rake tasks:
18
58
 
19
59
  # start the js test driver server
data/Rakefile CHANGED
@@ -25,6 +25,7 @@ Jeweler::Tasks.new do |gem|
25
25
  gem.add_development_dependency "mocha"
26
26
  gem.add_development_dependency "jeweler"
27
27
  gem.add_dependency "rake"
28
+ gem.add_dependency "json"
28
29
  gem.files = FileList["[A-Z]*", "{bin,generators,lib,test,vendor}/**/*"]
29
30
  end
30
31
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -1,2 +1,6 @@
1
+ require 'json'
2
+ require 'fileutils'
3
+
1
4
  require File.expand_path(File.join(File.dirname(__FILE__), 'js_test_driver', 'config'))
2
- require File.expand_path(File.join(File.dirname(__FILE__), 'js_test_driver', 'runner'))
5
+ require File.expand_path(File.join(File.dirname(__FILE__), 'js_test_driver', 'runner'))
6
+ require File.expand_path(File.join(File.dirname(__FILE__), 'js_test_driver', 'html_fixture'))
@@ -13,22 +13,21 @@ module JsTestDriver
13
13
  self.attributes = attributes
14
14
  end
15
15
 
16
- # Adds a file to be loaded, the path *must* be relative to the yml config file (which is placed in the RAILS_ROOT
17
- # by default)
16
+ # Adds a file to be loaded, the path *must* be relative to the root_dir (which is the current dir by default)
18
17
  #
19
18
  # JsTestDriver supports globbing
20
19
  def includes(*paths)
21
20
  paths.each do |path|
22
- self.included_files << path
21
+ self.included_files << File.expand_path(path)
23
22
  end
24
23
  end
25
24
 
26
25
  # Files specified here will not be loaded, it's useful when combined with globbing in includes
27
26
  #
28
- # paths should be relative to RAILS_ROOT
27
+ # paths should be relative to root_dir
29
28
  def excludes(*paths)
30
29
  paths.each do |path|
31
- self.excluded_files << path
30
+ self.excluded_files << File.expand_path(path)
32
31
  end
33
32
  end
34
33
 
@@ -42,6 +41,34 @@ module JsTestDriver
42
41
  end
43
42
  end
44
43
 
44
+ # Defines a HTML fixture directory
45
+ #
46
+ # the first argument is the directory to scan for html fixtures
47
+ # you can pass also :name and :namespace arguments to define the name and the namespace of the fixture
48
+ #
49
+ # the fixtures will be accessible through:
50
+ # namespace.name["file_name_without the html extension"]
51
+ #
52
+ # by default the namespace is called htmlFixtures
53
+ # and the fixture name is called all
54
+ def fixtures(directory, opts = {})
55
+ fixture = JsTestDriver::HtmlFixture.new(directory, opts[:name], opts[:namespace])
56
+ if html_fixtures.detect{|f| f.name == fixture.name && f.namespace == fixture.namespace}
57
+ raise ArgumentError.new("Fixture #{fixture.namespace}.#{fixture.name} already defined!")
58
+ end
59
+ html_fixtures << fixture
60
+ end
61
+
62
+ # Includes the bundled with the gem jasmine js file and an adapter for jasmine
63
+ #
64
+ # There's no hacks or modifications here, so this method is just for the users convenience
65
+ def enable_jasmine
66
+ this_directory = File.dirname(__FILE__)
67
+ vendor_directory = File.expand_path(File.join('..', '..', 'vendor'), this_directory)
68
+ includes File.join(vendor_directory, "jasmine", "lib", "jasmine.js")
69
+ includes File.join(vendor_directory, "jasmine-jstd-adapter", "src", "*.js")
70
+ end
71
+
45
72
  # config variable which has a regular setter,
46
73
  # but also can be set by calling the "getter" with an argument
47
74
  # and if called without an argument the getter will return the passed block
@@ -85,6 +112,10 @@ module JsTestDriver
85
112
  @excludes ||= []
86
113
  end
87
114
 
115
+ def html_fixtures
116
+ @html_fixtures ||= []
117
+ end
118
+
88
119
  attr_writer :browsers
89
120
 
90
121
  def browsers
@@ -93,8 +124,8 @@ module JsTestDriver
93
124
 
94
125
  def to_s
95
126
  hash = {'server' => server}
96
- hash['load'] = included_files unless included_files.empty?
97
- hash['exclude'] = excluded_files unless excluded_files.empty?
127
+ hash['load'] = loaded_files unless loaded_files.empty?
128
+ hash['exclude'] = map_paths(excluded_files) unless excluded_files.empty?
98
129
  return hash.to_yaml
99
130
  end
100
131
 
@@ -104,8 +135,54 @@ module JsTestDriver
104
135
  return config
105
136
  end
106
137
 
138
+ attr_writer :config_dir
139
+
140
+ # this is where the config files are saved (ex. RAILS_ROOT/.js_test_driver)
141
+ def config_dir
142
+ @config_dir ||= File.expand_path(".")
143
+ end
144
+
145
+ def save_fixtures
146
+ html_fixtures.each do |fixture|
147
+ path = fixture_file_name(fixture)
148
+ FileUtils.mkdir_p(File.dirname(path))
149
+ puts File.dirname(path)
150
+ File.open(path, "w+") do |f|
151
+ f.puts fixture.to_s
152
+ end
153
+ end
154
+ end
155
+
107
156
  private
108
157
 
158
+ def fixture_file_name(fixture)
159
+ File.expand_path(File.join(config_dir, "fixtures", fixture.namespace, "#{fixture.name}.js"))
160
+ end
161
+
162
+ def loaded_files
163
+ files = included_files + html_fixtures.collect { |fixture| fixture_file_name(fixture) }
164
+
165
+ map_paths(files)
166
+ end
167
+
168
+ def path_relative_to_config_dir(path)
169
+ source = File.expand_path(path).split(File::SEPARATOR)
170
+ config = File.expand_path(config_dir).split(File::SEPARATOR)
171
+
172
+ while source.first == config.first && !source.empty? && !config.empty?
173
+ source.shift
174
+ config.shift
175
+ end
176
+
177
+ parts = (['..'] * config.size) + source
178
+
179
+ return File.join(*parts)
180
+ end
181
+
182
+ def map_paths(files)
183
+ files.map{|file| path_relative_to_config_dir(file)}
184
+ end
185
+
109
186
  def attributes=(values)
110
187
  values.each do |attr, value|
111
188
  self.send("#{attr}=", value)
@@ -0,0 +1,40 @@
1
+ module JsTestDriver
2
+ # This is a class that given a directory name, puts all its *.html children
3
+ # into a javascript file, so that they can later be used in the tests
4
+ class HtmlFixture
5
+
6
+ attr_reader :name, :namespace
7
+
8
+ def initialize(directory_name, name = nil, namespace = nil)
9
+ @name = name || "all"
10
+ @namespace = namespace || "htmlFixtures"
11
+ @data = {}
12
+
13
+ load_data(directory_name)
14
+ end
15
+
16
+ def to_h
17
+ @data
18
+ end
19
+
20
+ def to_s
21
+ <<JS
22
+ if (!#{namespace}) { #{namespace} = {}; }
23
+ #{namespace}.#{name} = #{self.to_h.to_json};
24
+ JS
25
+ end
26
+
27
+ private
28
+
29
+ def load_data(directory_name)
30
+ full_path = File.expand_path(directory_name)
31
+ files = Dir["#{full_path}/**/*.html"]
32
+
33
+ files.each do |file|
34
+ name = file.gsub(/^#{full_path}\//, '').gsub(/\.html$/, '')
35
+ @data[name] = File.read(file)
36
+ end
37
+ end
38
+
39
+ end
40
+ end
@@ -13,6 +13,15 @@ module JsTestDriver
13
13
  @config ||= parse_config
14
14
  end
15
15
 
16
+ attr_writer :generated_files_dir
17
+
18
+ # this is where the generated files will be saved, by default this is `pwd`/.js_test_driver
19
+ #
20
+ # the generated files are the config yml file and the fixture files
21
+ def generated_files_dir
22
+ @generated_files_dir || default_generated_files_dir
23
+ end
24
+
16
25
  # this is the path of the config file, by default its `pwd`/config/js_test_driver.rb
17
26
  attr_writer :config_path
18
27
 
@@ -27,7 +36,8 @@ module JsTestDriver
27
36
  @jar_path ||= default_jar_path
28
37
  end
29
38
 
30
- # this is where the config yml file will be saved, by default its /tmp/js_test_driver.(contents MD5).yml
39
+ # this is where the config yml file will be saved, by default its saved in the generated files
40
+ # directory under the name jsTestDriver.conf
31
41
  attr_writer :tmp_path
32
42
 
33
43
  def config_yml_path
@@ -88,7 +98,9 @@ module JsTestDriver
88
98
  else
89
99
  warn("Could not find JS Test Driver config: '#{config_path}', assuming empty config file!")
90
100
  end
91
- JsTestDriver::Config.parse(source)
101
+ config = JsTestDriver::Config.parse(source)
102
+ config.config_dir = generated_files_dir
103
+ return config
92
104
  end
93
105
 
94
106
  def default_config_path
@@ -103,13 +115,19 @@ module JsTestDriver
103
115
  end
104
116
 
105
117
  def default_config_yml_path
106
- return File.expand_path("jsTestDriver.conf")
118
+ return File.expand_path("jsTestDriver.conf", generated_files_dir)
119
+ end
120
+
121
+ def default_generated_files_dir
122
+ return File.expand_path(".js_test_driver")
107
123
  end
108
124
 
109
125
  private
110
126
 
111
127
  def save_config_file(path)
128
+ Dir.mkdir(generated_files_dir) unless File.exists?(generated_files_dir)
112
129
  File.open(path, "w+") { |f| f.puts config.to_s }
130
+ config.save_fixtures
113
131
  end
114
132
 
115
133
  def attributes=(values)
@@ -0,0 +1 @@
1
+ <p>This is the file <b>a.html</b></p>
@@ -0,0 +1 @@
1
+ <p>This is the file <b>b.html</b></p>
@@ -0,0 +1 @@
1
+ <p>This is the file <b>baz/a.html</b></p>
@@ -0,0 +1 @@
1
+ <p>This is the file <b>c.html</b></p>
@@ -0,0 +1 @@
1
+ <p>This is the file <b>foo/a.html</b></p>
@@ -0,0 +1 @@
1
+ <p>This is the file <b>foo/bar/a.html</b></p>
@@ -0,0 +1 @@
1
+ This file is not a HTML file, so it will not be used as a fixture.
@@ -7,3 +7,22 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'js_test
7
7
 
8
8
  require 'test/unit'
9
9
  require 'mocha'
10
+
11
+ class Test::Unit::TestCase
12
+
13
+ def setup
14
+ clean_up_saved_config_files
15
+ end
16
+
17
+ def fixture_dir
18
+ File.expand_path('fixtures', File.dirname(__FILE__))
19
+ end
20
+
21
+ def clean_up_saved_config_files
22
+ root_dir = File.expand_path('..', File.dirname(__FILE__))
23
+ Dir["#{root_dir}/**/.js_test_driver"].each do |file|
24
+ FileUtils.rm_rf(file)
25
+ end
26
+ end
27
+
28
+ end
@@ -145,10 +145,83 @@ module JsTestDriver
145
145
  config = JsTestDriver::Config.parse(str)
146
146
 
147
147
  # then
148
- assert_equal ['a', 'b', 'c'], config.included_files
149
- assert_equal ['d'], config.excluded_files
148
+ assert_equal ['a', 'b', 'c'].map{|p| File.expand_path(p)}, config.included_files
149
+ assert_equal [File.expand_path('d')], config.excluded_files
150
150
  assert_equal 'http://example.com:666', config.server
151
151
  end
152
152
 
153
+ def test_given_a_config_path_should_expand_the_includes
154
+ # given
155
+ config = given_an_empty_config
156
+ config.config_dir = "/foo/bbb/ccc"
157
+ pwd = File.expand_path('.')
158
+ config.includes "a/a", "/b/b", "../c"
159
+
160
+ # then
161
+ assert_config_includes config, 'load' => ["../../..#{pwd}/a/a", '../../../b/b', "../../..#{File.expand_path('../c')}"]
162
+ end
163
+
164
+ def test_config_with_html_fixtures
165
+ # given
166
+ config = given_an_empty_config
167
+ config.config_dir = File.expand_path("configs")
168
+
169
+ # when
170
+ config.fixtures "fixture/directory", :name => "fixture_name", :namespace => "fixture_namespace"
171
+
172
+ # then
173
+ assert_config_includes config, 'load' => ["fixtures/fixture_namespace/fixture_name.js"]
174
+ end
175
+
176
+ def test_should_save_fixtures
177
+ # given
178
+ config = given_an_empty_config
179
+ config.config_dir = "tmp/stuff"
180
+ config.fixtures fixture_dir, :name => "fixture_name", :namespace => "fixture_namespace"
181
+
182
+ # when
183
+ config.save_fixtures
184
+
185
+ # then
186
+ name = "tmp/stuff/fixtures/fixture_namespace/fixture_name.js"
187
+ assert File.exists?(name)
188
+ assert_equal File.read(name), config.html_fixtures.first.to_s
189
+ ensure
190
+ FileUtils.rm_rf("tmp/stuff")
191
+ end
192
+
193
+ def test_should_raise_argument_error_if_the_same_fixture_defined_twice
194
+ # given
195
+ config = given_an_empty_config
196
+ config.fixtures "fixture/directory"
197
+
198
+ assert_raises(ArgumentError) do
199
+ config.fixtures "fixture/some_other_directory"
200
+ end
201
+ end
202
+
203
+ def test_should_not_raise_argument_error_for_two_fixtures_with_different_names
204
+ # given
205
+ config = given_an_empty_config
206
+ config.fixtures "fixture/directory"
207
+
208
+ assert_nothing_raised do
209
+ config.fixtures "fixture/some_other_directory", :name => "some_other_name"
210
+ end
211
+ end
212
+
213
+ def test_enable_jasmine_adds_jasmine_and_jasmine_adapter
214
+ # given
215
+ config = given_an_empty_config
216
+
217
+ # when
218
+ config.enable_jasmine
219
+
220
+ # then
221
+ assert_equal 2, config.included_files.size
222
+ assert File.exists?(config.included_files[0])
223
+ assert Dir[config.included_files[1]].size > 0
224
+ end
225
+
153
226
  end
154
227
  end