newjs 1.5.1 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (184) hide show
  1. data/History.txt +11 -0
  2. data/Manifest.txt +162 -14
  3. data/{README.txt → README.rdoc} +1 -1
  4. data/Rakefile +25 -3
  5. data/app_generators/newjs_iphone/templates/Html/test/assets/jsunittest.js +41 -13
  6. data/app_generators/newjs_iphone/templates/Html/test/assets/unittest.css +4 -0
  7. data/features/development.feature +13 -0
  8. data/features/imported_files_for_generators.feature +19 -0
  9. data/features/steps/common.rb +205 -0
  10. data/features/steps/env.rb +6 -0
  11. data/javascript_test_generators/functional_test/functional_test_generator.rb +9 -1
  12. data/javascript_test_generators/functional_test/templates/test/assets/jshoulda.js +186 -0
  13. data/javascript_test_generators/functional_test/templates/test/test.html.erb +15 -1
  14. data/javascript_test_generators/unit_test/templates/test/assets/jshoulda.js +186 -0
  15. data/javascript_test_generators/unit_test/templates/test/test.html.erb +18 -20
  16. data/javascript_test_generators/unit_test/templates/test/test_jshoulda.html.erb +45 -0
  17. data/javascript_test_generators/unit_test/unit_test_generator.rb +11 -3
  18. data/lib/newjs.rb +3 -2
  19. data/newjs_iphone_generators/page/USAGE +5 -0
  20. data/newjs_iphone_generators/page/page_generator.rb +36 -0
  21. data/newjs_iphone_generators/page/templates/src/name.css.erb +0 -0
  22. data/newjs_iphone_generators/page/templates/src/name.html.erb +13 -0
  23. data/newjs_iphone_generators/page/templates/src/name.js.erb +4 -0
  24. data/newjs_iphone_generators/page/templates/test/fixtures/name.js.erb +5 -0
  25. data/newjs_iphone_generators/page/templates/test/unit/name_test.html.erb +61 -0
  26. data/rack_generators/javascript_test/USAGE +5 -0
  27. data/rack_generators/javascript_test/javascript_test_generator.rb +88 -0
  28. data/rack_generators/javascript_test/templates/assets/jshoulda.js +186 -0
  29. data/rack_generators/javascript_test/templates/assets/jsunittest.js +1004 -0
  30. data/rack_generators/javascript_test/templates/assets/unittest.css +54 -0
  31. data/rack_generators/javascript_test/templates/ext/jquery.js +3408 -0
  32. data/{app_generators/newjs_iphone/templates/Html/test/assets → rack_generators/javascript_test/templates/ext}/prototype.js +56 -122
  33. data/rack_generators/javascript_test/templates/library.js.erb +3 -0
  34. data/rack_generators/javascript_test/templates/test.html.erb +59 -0
  35. data/rack_generators/javascript_test/templates/test_jshoulda.html.erb +48 -0
  36. data/rails_generators/javascript_test/javascript_test_generator.rb +15 -3
  37. data/rails_generators/javascript_test/templates/assets/jshoulda.js +186 -0
  38. data/rails_generators/javascript_test/templates/test_jshoulda.html.erb +48 -0
  39. data/script/console +10 -0
  40. data/script/destroy +1 -1
  41. data/tasks/vendor/update/jshoulda.rake +18 -0
  42. data/tasks/vendor/update/jsunittest.rake +18 -0
  43. data/test/test_functional_test_generator.rb +13 -0
  44. data/test/test_generator_helper.rb +10 -1
  45. data/test/test_page_generator.rb +50 -0
  46. data/test/test_rack_javascript_test.rb +166 -0
  47. data/test/{test_javascript_test_generator.rb → test_rails_javascript_test_generator.rb} +15 -1
  48. data/test/test_unit_test_generator.rb +13 -0
  49. data/vendor/jshoulda/History.txt +108 -0
  50. data/vendor/jshoulda/License.txt +20 -0
  51. data/vendor/jshoulda/README.mdown +34 -0
  52. data/vendor/jshoulda/Rakefile +120 -0
  53. data/vendor/jshoulda/config/javascript_test_autotest.yml.sample +15 -0
  54. data/vendor/jshoulda/dist/jshoulda-0.0.1.js +62 -0
  55. data/vendor/jshoulda/dist/jshoulda-0.0.2.js +72 -0
  56. data/vendor/jshoulda/dist/jshoulda-0.0.3.js +72 -0
  57. data/vendor/jshoulda/dist/jshoulda-1.0.1.js +74 -0
  58. data/vendor/jshoulda/dist/jshoulda-1.0.js +72 -0
  59. data/vendor/jshoulda/dist/jshoulda-1.1.js +110 -0
  60. data/vendor/jshoulda/dist/jshoulda-1.2.1.js +186 -0
  61. data/vendor/jshoulda/dist/jshoulda-1.2.js +141 -0
  62. data/vendor/jshoulda/dist/jshoulda-1.2.x.js +142 -0
  63. data/vendor/jshoulda/dist/jshoulda.js +186 -0
  64. data/vendor/jshoulda/jshoulda/test/basic_test.html +42 -0
  65. data/vendor/jshoulda/lib/jstest.rb +382 -0
  66. data/vendor/jshoulda/lib/protodoc.rb +36 -0
  67. data/vendor/jshoulda/script/destroy +14 -0
  68. data/vendor/jshoulda/script/generate +14 -0
  69. data/vendor/jshoulda/script/js_autotest +1 -0
  70. data/vendor/jshoulda/script/rstakeout +97 -0
  71. data/vendor/jshoulda/site/asset_defaults.yaml +6 -0
  72. data/vendor/jshoulda/site/config.yaml +3 -0
  73. data/vendor/jshoulda/site/content/content.html +27 -0
  74. data/vendor/jshoulda/site/content/content.yaml +4 -0
  75. data/vendor/jshoulda/site/content/tutorial/tutorial.html +268 -0
  76. data/vendor/jshoulda/site/content/tutorial/tutorial.yaml +4 -0
  77. data/vendor/jshoulda/site/css/blackboard.css +88 -0
  78. data/vendor/jshoulda/site/css/jshoulda_site.css +33 -0
  79. data/vendor/jshoulda/site/css/unittest.css +42 -0
  80. data/vendor/jshoulda/site/example/minimal.html +55 -0
  81. data/vendor/jshoulda/site/js/editable.js +42 -0
  82. data/vendor/jshoulda/site/layouts/default/default.html +29 -0
  83. data/vendor/jshoulda/site/layouts/default/default.yaml +4 -0
  84. data/vendor/jshoulda/site/layouts/home_test/home_test.js +18 -0
  85. data/vendor/jshoulda/site/layouts/home_test/home_test.yaml +4 -0
  86. data/vendor/jshoulda/site/lib/analytics.rb +12 -0
  87. data/vendor/jshoulda/site/lib/default.rb +2 -0
  88. data/vendor/jshoulda/site/lib/example.rb +10 -0
  89. data/vendor/jshoulda/site/lib/hash.rb +11 -0
  90. data/vendor/jshoulda/site/lib/highlight.rb +17 -0
  91. data/vendor/jshoulda/site/page_defaults.yaml +10 -0
  92. data/vendor/jshoulda/site/templates/default/default.html +1 -0
  93. data/vendor/jshoulda/site/templates/default/default.yaml +4 -0
  94. data/vendor/jshoulda/src/HEADER +8 -0
  95. data/vendor/jshoulda/src/jshoulda.js +177 -0
  96. data/vendor/jshoulda/src/jshoulda.js.erb +3 -0
  97. data/vendor/jshoulda/tasks/bundle.rake +12 -0
  98. data/vendor/jshoulda/tasks/demo.rake +9 -0
  99. data/vendor/jshoulda/tasks/deploy.rake +29 -0
  100. data/{tasks → vendor/jshoulda/tasks}/environment.rake +0 -0
  101. data/vendor/jshoulda/tasks/javascript_test_autotest_tasks.rake +45 -0
  102. data/vendor/jshoulda/tasks/site.rake +74 -0
  103. data/vendor/jshoulda/test/assets/jsunittest.js +1004 -0
  104. data/vendor/jshoulda/test/assets/prototype.js +4324 -0
  105. data/{app_generators/newjs_iphone/templates/Html/test/assets/unittest.js → vendor/jshoulda/test/assets/prototype_unittest.js} +15 -9
  106. data/vendor/jshoulda/test/assets/unittest.css +50 -0
  107. data/vendor/jshoulda/test/unit/factory_test.html +82 -0
  108. data/vendor/jshoulda/test/unit/jshoulda2_test.html +85 -0
  109. data/vendor/jshoulda/test/unit/jshoulda_prototype_test.html +87 -0
  110. data/vendor/jshoulda/test/unit/jshoulda_test.html +86 -0
  111. data/vendor/jshoulda/test/unit/merge_methods_test.html +77 -0
  112. data/vendor/jshoulda/test/unit/names_and_count.html +68 -0
  113. data/vendor/jshoulda/test/unit/no_config_test.html +57 -0
  114. data/vendor/jshoulda/test/unit/styles_test.html +77 -0
  115. data/vendor/jshoulda/test/unit/unify_runners.html +69 -0
  116. data/vendor/jshoulda/test/unit/unnamed_context_test.html +67 -0
  117. data/vendor/jsunittest/Capfile +4 -0
  118. data/vendor/jsunittest/History.txt +37 -0
  119. data/{License.txt → vendor/jsunittest/License.txt} +0 -0
  120. data/vendor/jsunittest/README.txt +21 -0
  121. data/vendor/jsunittest/Rakefile +118 -0
  122. data/vendor/jsunittest/config/deploy.rb +20 -0
  123. data/vendor/jsunittest/config/website.yml.sample +2 -0
  124. data/vendor/jsunittest/dist/jsunittest-0.7.2.js +1004 -0
  125. data/vendor/jsunittest/dist/jsunittest.js +1004 -0
  126. data/vendor/jsunittest/dist/unittest.css +54 -0
  127. data/vendor/jsunittest/lib/jstest.rb +390 -0
  128. data/vendor/jsunittest/lib/protodoc.rb +36 -0
  129. data/vendor/jsunittest/script/destroy +14 -0
  130. data/vendor/jsunittest/script/generate +14 -0
  131. data/vendor/jsunittest/script/js_autotest +1 -0
  132. data/vendor/jsunittest/script/rstakeout +97 -0
  133. data/vendor/jsunittest/script/txt2html +75 -0
  134. data/vendor/jsunittest/src/HEADER +8 -0
  135. data/vendor/jsunittest/src/ajax.js +121 -0
  136. data/vendor/jsunittest/src/assertions.js +244 -0
  137. data/vendor/jsunittest/src/common.js +191 -0
  138. data/vendor/jsunittest/src/jsunittest.js +1004 -0
  139. data/vendor/jsunittest/src/logger.js +86 -0
  140. data/vendor/jsunittest/src/message_template.js +17 -0
  141. data/vendor/jsunittest/src/orig/unittest.js +484 -0
  142. data/vendor/jsunittest/src/prototype/event.js +72 -0
  143. data/vendor/jsunittest/src/prototype/template.js +33 -0
  144. data/vendor/jsunittest/src/runner.js +120 -0
  145. data/vendor/jsunittest/src/test_case.js +106 -0
  146. data/vendor/jsunittest/tasks/deploy.rake +29 -0
  147. data/vendor/jsunittest/tasks/environment.rake +7 -0
  148. data/vendor/jsunittest/tasks/javascript_test_autotest_tasks.rake +44 -0
  149. data/vendor/jsunittest/tasks/tmbundle.rake +24 -0
  150. data/vendor/jsunittest/tasks/website.rake +53 -0
  151. data/vendor/jsunittest/test/assets/example_test.html +50 -0
  152. data/{app_generators/newjs/templates → vendor/jsunittest}/test/assets/prototype.js +0 -0
  153. data/vendor/jsunittest/test/assets/unittest.css +54 -0
  154. data/{app_generators/newjs/templates → vendor/jsunittest}/test/assets/unittest.js +0 -0
  155. data/vendor/jsunittest/test/functional/jsunittest_test.html +209 -0
  156. data/vendor/jsunittest/test/unit/assertions_test.html +202 -0
  157. data/vendor/jsunittest/test/unit/common_test.html +61 -0
  158. data/vendor/jsunittest/test/unit/logger_test.html +98 -0
  159. data/vendor/jsunittest/test/unit/message_template_test.html +44 -0
  160. data/vendor/jsunittest/test/unit/runner_test.html +77 -0
  161. data/vendor/jsunittest/test/unit/template_test.html +57 -0
  162. data/vendor/jsunittest/test/unit/test_case_test.html +97 -0
  163. data/vendor/jsunittest/website/dist/jsunittest-0.7.2.js +1004 -0
  164. data/vendor/jsunittest/website/dist/jsunittest-getting-started.tar.gz +0 -0
  165. data/vendor/jsunittest/website/dist/jsunittest-getting-started.zip +0 -0
  166. data/vendor/jsunittest/website/dist/jsunittest-getting-started/assets/jsunittest.js +1004 -0
  167. data/vendor/jsunittest/website/dist/jsunittest-getting-started/assets/unittest.css +54 -0
  168. data/vendor/jsunittest/website/dist/jsunittest-getting-started/example_test.html +50 -0
  169. data/vendor/jsunittest/website/dist/jsunittest.js +1004 -0
  170. data/vendor/jsunittest/website/images/logo_bundle.png +0 -0
  171. data/vendor/jsunittest/website/images/passing_prototypejs_unittest_tests.jpg +0 -0
  172. data/vendor/jsunittest/website/index.html +94 -0
  173. data/vendor/jsunittest/website/index.txt +80 -0
  174. data/vendor/jsunittest/website/javascripts/rounded_corners_lite.inc.js +285 -0
  175. data/vendor/jsunittest/website/stylesheets/screen.css +138 -0
  176. data/vendor/jsunittest/website/template.html.erb +46 -0
  177. data/vendor/jsunittest/website/tmbundle/JavaScript Unit Testing.tmbundle.tar.gz +0 -0
  178. metadata +189 -24
  179. data/config/hoe.rb +0 -75
  180. data/config/requirements.rb +0 -17
  181. data/lib/newjs/version.rb +0 -9
  182. data/tasks/deployment.rake +0 -34
  183. data/tasks/jsunittest/update.rake +0 -18
  184. data/tasks/website.rake +0 -17
@@ -0,0 +1,186 @@
1
+ /* jShoulda, version 1.2.1
2
+ * (c) 2008 Choan Galvez
3
+ *
4
+ * jShoulda is freely distributable under
5
+ * the terms of an MIT-style license.
6
+ * For details, see the web site: http://jshoulda.scriptia.net
7
+ *
8
+ *--------------------------------------------------------------------------*/
9
+
10
+ var jShoulda = function() {
11
+ // the test runner
12
+ var tr,
13
+ // handle all testcases with the same testrunner
14
+ unique = false,
15
+ // styles definitions
16
+ styles = {};
17
+
18
+ function dummy() {
19
+ };
20
+
21
+ function runQueue(queue, name, before, after, ext) {
22
+ var i, tests;
23
+ for (i = 0; i < queue.length; i += 1) {
24
+ tests = queue[i](name, before, after, ext);
25
+ if (tests)
26
+ tr.tests.push(tests);
27
+ }
28
+ before.pop();
29
+ after.pop();
30
+ }
31
+
32
+ function makeBatch(fn_a) {
33
+ var copy = fn_a.slice(0);
34
+ return function() {
35
+ for (var i = 0; i < copy.length; i += 1) {
36
+ copy[i].call(this);
37
+ }
38
+ };
39
+ }
40
+
41
+ function getContextAlias(prefix) {
42
+ if (prefix) prefix += ' ';
43
+ return function (name, config, args) {
44
+ var queue = Array.prototype.slice.call(arguments, 0);
45
+ var cName = '';
46
+ var obj = {};
47
+ // shift arguments if the first one is a string
48
+ if (typeof queue[0] == 'string') {
49
+ if (name) { cName += prefix; }
50
+ cName += queue.shift();
51
+ }
52
+ if (typeof queue[0] === 'object') {
53
+ obj = queue.shift();
54
+ }
55
+
56
+ obj.before = obj.setup || obj.before || dummy;
57
+ obj.after = obj.teardown || obj.after || dummy;
58
+ var beforeQueue;
59
+ var afterQueue;
60
+ var extensions = merge({}, obj || {});
61
+
62
+ return function(outerName, before, after, ext) {
63
+ // the root context gets no outerName or a configuration object
64
+ // as its first argument
65
+ var prefix = outerName;
66
+ var is_root = !!(outerName == undefined || typeof outerName == 'object');
67
+ if (is_root) {
68
+ tr = (tr && unique) ? tr : new Test.Unit.Runner({}, outerName || {});
69
+ prefix = '';
70
+ }
71
+ // shift arguments if the second one is a
72
+ // configuration object
73
+ beforeQueue = before ? before.push(obj.before) && before : [obj.before];
74
+ afterQueue = after ? after.push(obj.after) && after : [obj.after];
75
+ runQueue(queue, [prefix, cName].join(' ').replace(/^\s+/, ''), beforeQueue, afterQueue, merge(extensions, ext));
76
+ if (is_root) {
77
+ return tr;
78
+ }
79
+ return false; // do not add to the tests queue
80
+ };
81
+ };
82
+ };
83
+
84
+ function merge(sub, sup) {
85
+ for (var i in sup) {
86
+ if (i.search(/^(before|after|setup|teardown)$/) != -1) continue;
87
+ if (sup.hasOwnProperty(i)) {
88
+ sub[i] = sup[i];
89
+ }
90
+ }
91
+ return sub;
92
+ }
93
+
94
+
95
+ function getShouldAlias(connector) {
96
+ return function (name, fn) {
97
+ return function(prefix, before, after, extensions) {
98
+ var beforeBatch = makeBatch(before);
99
+ var afterBatch = makeBatch(after);
100
+ var unit = new Test.Unit.Testcase([prefix, name].join(' ' + connector + ' '), fn, beforeBatch, afterBatch);
101
+ merge(unit, extensions);
102
+ return unit;
103
+ };
104
+ };
105
+ }
106
+
107
+ function setShouldAlias(name, host) {
108
+ var connector = name;
109
+ if (typeof host == 'string') {
110
+ connector = host;
111
+ host = arguments[2];
112
+ }
113
+ host = host || window;
114
+ host[name] = getShouldAlias(connector);
115
+ return jShoulda;
116
+ }
117
+
118
+ function setContextAlias(name, host) {
119
+ var prefix = '';
120
+ if (typeof host == 'string') {
121
+ prefix = host;
122
+ host = arguments[2];
123
+ }
124
+ host = host || window;
125
+ host[name] = getContextAlias(prefix);
126
+ return jShoulda;
127
+ }
128
+
129
+ function useStyle(name) {
130
+ var style, i;
131
+ if (name in styles) {
132
+ style = styles[name];
133
+ if ('context' in style) {
134
+ for (i = 0; i < style.context.length; i += 2) {
135
+ setContextAlias(style.context[i], style.context[i+1]);
136
+ }
137
+ }
138
+ if ('should' in style) {
139
+ for (i = 0; i < style.should.length; i += 2) {
140
+ setShouldAlias(style.should[i], style.should[i+1]);
141
+ }
142
+ }
143
+ if (style.unify) unifyRunners();
144
+ }
145
+ else throw { name: 'UndefinedStyle', message: 'There is not "'+name+'" style defined' };
146
+ return jShoulda;
147
+ }
148
+
149
+ function defineStyle(name, opts, aliases) {
150
+ styles[name] = opts;
151
+ for (var i = 2; i < arguments.length; i += 1) {
152
+ styles[arguments[i]] = opts;
153
+ }
154
+ return jShoulda;
155
+ }
156
+
157
+ function unifyRunners (options) {
158
+ if (options) {
159
+ tr = options instanceof Test.Unit.Runner ? options : new Test.Unit.Runner({}, options || {});
160
+ }
161
+ unique = true;
162
+ return jShoulda;
163
+ }
164
+
165
+ return {
166
+ setShouldAlias : setShouldAlias,
167
+ setContextAlias : setContextAlias,
168
+ unifyRunners : unifyRunners,
169
+ useStyle : useStyle,
170
+ defineStyle: defineStyle
171
+ };
172
+
173
+ }();
174
+
175
+ jShoulda
176
+ .defineStyle('describe', {
177
+ context: [ 'describe', '' ],
178
+ should: [ 'it', '' ],
179
+ unify: false
180
+ }, 'it')
181
+ .defineStyle('context', {
182
+ context : [ 'context', '' ],
183
+ should : [ 'should', 'should' ],
184
+ unify : false
185
+ }, 'should')
186
+ .useStyle('should');
@@ -0,0 +1,42 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
+
4
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
+ <head>
6
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
7
+
8
+ <title>jShoulda Demo Test</title>
9
+
10
+ <script src="../assets/jsunittest.js" type="text/javascript"></script>
11
+ <script src="../assets/jshoulda.js" type="text/javascript"></script>
12
+ <link rel="stylesheet" href="../assets/unittest.css" type="text/css" media="screen" />
13
+
14
+
15
+ </head>
16
+
17
+ <body>
18
+
19
+ <h1>jShoulda Demo Test</h1>
20
+
21
+ <div id="testlog"></div>
22
+
23
+ <script type="text/javascript" charset="utf-8">
24
+ function trim(s) {
25
+ return s.replace(/^\s+|\s+$/g, '');
26
+ }
27
+ context("trim",
28
+ should("remove blanks from the start of the string", function() {
29
+ this.assertEqual('hello', trim(' hello'));
30
+ this.assertEqual('hello mamuso', trim(' hello mamuso'));
31
+ }),
32
+ should("remove blanks from the end of the string", function() {
33
+ this.assertEqual('hello', trim('hello '));
34
+ }),
35
+ should("remove blanks from both the start and the end of the string", function() {
36
+ this.assertEqual('hello', trim(' hello '));
37
+ })
38
+ )();
39
+ </script>
40
+
41
+ </body>
42
+ </html>
@@ -0,0 +1,382 @@
1
+ require 'rake/tasklib'
2
+ require 'thread'
3
+ require 'webrick'
4
+ require 'fileutils'
5
+ include FileUtils
6
+
7
+ class Browser
8
+ def supported?; true; end
9
+ def setup ; end
10
+ def open(url) ; end
11
+ def teardown ; end
12
+
13
+ def host
14
+ require 'rbconfig'
15
+ Config::CONFIG['host']
16
+ end
17
+
18
+ def macos?
19
+ host.include?('darwin')
20
+ end
21
+
22
+ def windows?
23
+ host.include?('mswin')
24
+ end
25
+
26
+ def linux?
27
+ host.include?('linux')
28
+ end
29
+
30
+ def applescript(script)
31
+ raise "Can't run AppleScript on #{host}" unless macos?
32
+ system "osascript -e '#{script}' 2>&1 >/dev/null"
33
+ end
34
+ end
35
+
36
+ class FirefoxBrowser < Browser
37
+ def initialize(path=File.join(ENV['ProgramFiles'] || 'c:\Program Files', '\Mozilla Firefox\firefox.exe'))
38
+ @path = path
39
+ end
40
+
41
+ def visit(url)
42
+ system("open -a Firefox '#{url}'") if macos?
43
+ system("#{@path} #{url}") if windows?
44
+ system("firefox #{url}") if linux?
45
+ end
46
+
47
+ def to_s
48
+ "Firefox"
49
+ end
50
+ end
51
+
52
+ class SafariBrowser < Browser
53
+ def supported?
54
+ macos?
55
+ end
56
+
57
+ def setup
58
+ applescript('tell application "Safari" to make new document')
59
+ end
60
+
61
+ def visit(url)
62
+ applescript('tell application "Safari" to set URL of front document to "' + url + '"')
63
+ end
64
+
65
+ def teardown
66
+ #applescript('tell application "Safari" to close front document')
67
+ end
68
+
69
+ def to_s
70
+ "Safari"
71
+ end
72
+ end
73
+
74
+ class IEBrowser < Browser
75
+ def setup
76
+ require 'win32ole' if windows?
77
+ end
78
+
79
+ def supported?
80
+ windows?
81
+ end
82
+
83
+ def visit(url)
84
+ if windows?
85
+ ie = WIN32OLE.new('InternetExplorer.Application')
86
+ ie.visible = true
87
+ ie.Navigate(url)
88
+ while ie.ReadyState != 4 do
89
+ sleep(1)
90
+ end
91
+ end
92
+ end
93
+
94
+ def to_s
95
+ "Internet Explorer"
96
+ end
97
+ end
98
+
99
+ class KonquerorBrowser < Browser
100
+ @@configDir = File.join((ENV['HOME'] || ''), '.kde', 'share', 'config')
101
+ @@globalConfig = File.join(@@configDir, 'kdeglobals')
102
+ @@konquerorConfig = File.join(@@configDir, 'konquerorrc')
103
+
104
+ def supported?
105
+ linux?
106
+ end
107
+
108
+ # Forces KDE's default browser to be Konqueror during the tests, and forces
109
+ # Konqueror to open external URL requests in new tabs instead of a new
110
+ # window.
111
+ def setup
112
+ cd @@configDir, :verbose => false do
113
+ copy @@globalConfig, "#{@@globalConfig}.bak", :preserve => true, :verbose => false
114
+ copy @@konquerorConfig, "#{@@konquerorConfig}.bak", :preserve => true, :verbose => false
115
+ # Too lazy to write it in Ruby... Is sed dependency so bad?
116
+ system "sed -ri /^BrowserApplication=/d '#{@@globalConfig}'"
117
+ system "sed -ri /^KonquerorTabforExternalURL=/s:false:true: '#{@@konquerorConfig}'"
118
+ end
119
+ end
120
+
121
+ def teardown
122
+ cd @@configDir, :verbose => false do
123
+ copy "#{@@globalConfig}.bak", @@globalConfig, :preserve => true, :verbose => false
124
+ copy "#{@@konquerorConfig}.bak", @@konquerorConfig, :preserve => true, :verbose => false
125
+ end
126
+ end
127
+
128
+ def visit(url)
129
+ system("kfmclient openURL #{url}")
130
+ end
131
+
132
+ def to_s
133
+ "Konqueror"
134
+ end
135
+ end
136
+
137
+ class OperaBrowser < Browser
138
+ def initialize(path='c:\Program Files\Opera\Opera.exe')
139
+ @path = path
140
+ end
141
+
142
+ def setup
143
+ if windows?
144
+ puts %{
145
+ MAJOR ANNOYANCE on Windows.
146
+ You have to shut down Opera manually after each test
147
+ for the script to proceed.
148
+ Any suggestions on fixing this is GREATLY appreciated!
149
+ Thank you for your understanding.
150
+ }
151
+ end
152
+ end
153
+
154
+ def visit(url)
155
+ applescript('tell application "Opera" to GetURL "' + url + '"') if macos?
156
+ system("#{@path} #{url}") if windows?
157
+ system("opera #{url}") if linux?
158
+ end
159
+
160
+ def to_s
161
+ "Opera"
162
+ end
163
+ end
164
+
165
+ # shut up, webrick :-)
166
+ class ::WEBrick::HTTPServer
167
+ def access_log(config, req, res)
168
+ # nop
169
+ end
170
+ end
171
+
172
+ class ::WEBrick::BasicLog
173
+ def log(level, data)
174
+ # nop
175
+ end
176
+ end
177
+
178
+ class WEBrick::HTTPResponse
179
+ alias send send_response
180
+ def send_response(socket)
181
+ send(socket) unless fail_silently?
182
+ end
183
+
184
+ def fail_silently?
185
+ @fail_silently
186
+ end
187
+
188
+ def fail_silently
189
+ @fail_silently = true
190
+ end
191
+ end
192
+
193
+ class WEBrick::HTTPRequest
194
+ def to_json
195
+ headers = []
196
+ each { |k, v| headers.push "#{k.inspect}: #{v.inspect}" }
197
+ headers = "{" << headers.join(', ') << "}"
198
+ %({ "headers": #{headers}, "body": #{body.inspect}, "method": #{request_method.inspect} })
199
+ end
200
+ end
201
+
202
+ class WEBrick::HTTPServlet::AbstractServlet
203
+ def prevent_caching(res)
204
+ res['ETag'] = nil
205
+ res['Last-Modified'] = Time.now + 100**4
206
+ res['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'
207
+ res['Pragma'] = 'no-cache'
208
+ res['Expires'] = Time.now - 100**4
209
+ end
210
+ end
211
+
212
+ class BasicServlet < WEBrick::HTTPServlet::AbstractServlet
213
+ def do_GET(req, res)
214
+ prevent_caching(res)
215
+ res['Content-Type'] = "text/plain"
216
+
217
+ req.query.each do |k, v|
218
+ res[k] = v unless k == 'responseBody'
219
+ end
220
+ res.body = req.query["responseBody"]
221
+
222
+ raise WEBrick::HTTPStatus::OK
223
+ end
224
+
225
+ def do_POST(req, res)
226
+ do_GET(req, res)
227
+ end
228
+ end
229
+
230
+ class SlowServlet < BasicServlet
231
+ def do_GET(req, res)
232
+ sleep(2)
233
+ super
234
+ end
235
+ end
236
+
237
+ class DownServlet < BasicServlet
238
+ def do_GET(req, res)
239
+ res.fail_silently
240
+ end
241
+ end
242
+
243
+ class InspectionServlet < BasicServlet
244
+ def do_GET(req, res)
245
+ prevent_caching(res)
246
+ res['Content-Type'] = "application/json"
247
+ res.body = req.to_json
248
+ raise WEBrick::HTTPStatus::OK
249
+ end
250
+ end
251
+
252
+ class NonCachingFileHandler < WEBrick::HTTPServlet::FileHandler
253
+ def do_GET(req, res)
254
+ super
255
+ set_default_content_type(res, req.path)
256
+ prevent_caching(res)
257
+ end
258
+
259
+ def set_default_content_type(res, path)
260
+ res['Content-Type'] = case path
261
+ when /\.js$/ then 'text/javascript'
262
+ when /\.html$/ then 'text/html'
263
+ when /\.css$/ then 'text/css'
264
+ else 'text/plain'
265
+ end
266
+ end
267
+ end
268
+
269
+ class JavaScriptTestTask < ::Rake::TaskLib
270
+
271
+ def initialize(name=:test, port=4711)
272
+ @name = name
273
+ @tests = []
274
+ @browsers = []
275
+ @port = port
276
+ @queue = Queue.new
277
+
278
+ @server = WEBrick::HTTPServer.new(:Port => @port) # TODO: make port configurable
279
+ @server.mount_proc("/results") do |req, res|
280
+ @queue.push({
281
+ :tests => req.query['tests'].to_i,
282
+ :assertions => req.query['assertions'].to_i,
283
+ :failures => req.query['failures'].to_i,
284
+ :errors => req.query['errors'].to_i
285
+ })
286
+ res.body = "OK"
287
+ end
288
+ @server.mount("/response", BasicServlet)
289
+ @server.mount("/slow", SlowServlet)
290
+ @server.mount("/down", DownServlet)
291
+ @server.mount("/inspect", InspectionServlet)
292
+ yield self if block_given?
293
+ define
294
+ end
295
+
296
+ def define
297
+ task @name do
298
+ trap("INT") { @server.shutdown; exit }
299
+ t = Thread.new { @server.start }
300
+
301
+ # run all combinations of browsers and tests
302
+ @browsers.each do |browser|
303
+ if browser.supported?
304
+ t0 = Time.now
305
+ results = {:tests => 0, :assertions => 0, :failures => 0, :errors => 0}
306
+ errors = []
307
+ failures = []
308
+ browser.setup
309
+ puts "\nStarted tests in #{browser}"
310
+ @tests.each do |test|
311
+ params = "resultsURL=http://localhost:#{@port}/results&t=" + ("%.6f" % Time.now.to_f)
312
+ if test.is_a?(Hash)
313
+ params << "&tests=#{test[:testcases]}" if test[:testcases]
314
+ test = test[:url]
315
+ end
316
+ browser.visit("http://localhost:#{@port}#{test}?#{params}")
317
+
318
+ result = @queue.pop
319
+ result.each { |k, v| results[k] += v }
320
+ value = "."
321
+
322
+ if result[:failures] > 0
323
+ value = "F"
324
+ failures.push(test)
325
+ end
326
+
327
+ if result[:errors] > 0
328
+ value = "E"
329
+ errors.push(test)
330
+ end
331
+
332
+ print value
333
+ end
334
+
335
+ puts "\nFinished in #{(Time.now - t0).round.to_s} seconds."
336
+ puts " Failures: #{failures.join(', ')}" unless failures.empty?
337
+ puts " Errors: #{errors.join(', ')}" unless errors.empty?
338
+ puts "#{results[:tests]} tests, #{results[:assertions]} assertions, #{results[:failures]} failures, #{results[:errors]} errors"
339
+ browser.teardown
340
+ else
341
+ puts "\nSkipping #{browser}, not supported on this OS"
342
+ end
343
+ end
344
+
345
+ @server.shutdown
346
+ t.join
347
+ end
348
+ end
349
+
350
+ def mount(path, dir=nil)
351
+ dir = Dir.pwd + path unless dir
352
+
353
+ # don't cache anything in our tests
354
+ @server.mount(path, NonCachingFileHandler, dir)
355
+ end
356
+
357
+ # test should be specified as a url or as a hash of the form
358
+ # {:url => "url", :testcases => "testFoo,testBar"}
359
+ def run(test)
360
+ @tests<<test
361
+ end
362
+
363
+ def browser(browser)
364
+ browser =
365
+ case(browser)
366
+ when :firefox
367
+ FirefoxBrowser.new
368
+ when :safari
369
+ SafariBrowser.new
370
+ when :ie
371
+ IEBrowser.new
372
+ when :konqueror
373
+ KonquerorBrowser.new
374
+ when :opera
375
+ OperaBrowser.new
376
+ else
377
+ browser
378
+ end
379
+
380
+ @browsers<<browser
381
+ end
382
+ end