honkster-js-test-server 0.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. data/CHANGES +40 -0
  2. data/Gemfile +21 -0
  3. data/Gemfile.lock +60 -0
  4. data/README.markdown +9 -0
  5. data/Rakefile +67 -0
  6. data/bin/jasmine-server +9 -0
  7. data/bin/js-test-client +8 -0
  8. data/bin/js-test-server +9 -0
  9. data/bin/screw-unit-server +9 -0
  10. data/lib/js_test_server.rb +29 -0
  11. data/lib/js_test_server/client.rb +23 -0
  12. data/lib/js_test_server/client/runner.rb +130 -0
  13. data/lib/js_test_server/configuration.rb +69 -0
  14. data/lib/js_test_server/server.rb +14 -0
  15. data/lib/js_test_server/server/app.rb +10 -0
  16. data/lib/js_test_server/server/resources.rb +14 -0
  17. data/lib/js_test_server/server/resources/file.rb +58 -0
  18. data/lib/js_test_server/server/resources/framework_file.rb +15 -0
  19. data/lib/js_test_server/server/resources/implementations_deprecation.rb +8 -0
  20. data/lib/js_test_server/server/resources/not_found.rb +25 -0
  21. data/lib/js_test_server/server/resources/remote_control.rb +80 -0
  22. data/lib/js_test_server/server/resources/resource.rb +12 -0
  23. data/lib/js_test_server/server/resources/spec_file.rb +47 -0
  24. data/lib/js_test_server/server/resources/web_root.rb +17 -0
  25. data/lib/js_test_server/server/runner.rb +77 -0
  26. data/lib/js_test_server/server/standalone.ru +1 -0
  27. data/lib/js_test_server/server/views.rb +12 -0
  28. data/lib/js_test_server/server/views/dir.html.rb +22 -0
  29. data/lib/js_test_server/server/views/frameworks.rb +3 -0
  30. data/lib/js_test_server/server/views/not_found.html.rb +13 -0
  31. data/lib/js_test_server/server/views/page.html.rb +40 -0
  32. data/lib/js_test_server/server/views/remote_control_subscriber.rb +17 -0
  33. data/lib/js_test_server/server/views/suite.html.rb +54 -0
  34. data/lib/js_test_server/server/views/suites.rb +6 -0
  35. data/lib/js_test_server/server/views/suites/jasmine.html.rb +30 -0
  36. data/lib/js_test_server/server/views/suites/screw_unit.html.rb +44 -0
  37. data/public/js_test_server.js +565 -0
  38. data/public/js_test_server/jasmine_driver.js +63 -0
  39. data/public/js_test_server/remote_control.js +28 -0
  40. data/public/js_test_server/screw_unit_driver.js +31 -0
  41. data/scratch.rb +8 -0
  42. data/spec/frameworks/jasmine/cruise_config.rb +21 -0
  43. data/spec/frameworks/jasmine/spec/jasmine_helper.rb +44 -0
  44. data/spec/frameworks/jasmine/spec/jasmine_spec.rb +31 -0
  45. data/spec/functional/functional_spec_helper.rb +55 -0
  46. data/spec/functional/functional_spec_server_starter.rb +69 -0
  47. data/spec/functional/jasmine/jasmine_functional_spec.rb +27 -0
  48. data/spec/functional/screw-unit/screw_unit_functional_spec.rb +27 -0
  49. data/spec/functional_suite.rb +16 -0
  50. data/spec/spec_helpers/be_http.rb +32 -0
  51. data/spec/spec_helpers/example_group.rb +41 -0
  52. data/spec/spec_helpers/fake_deferrable.rb +3 -0
  53. data/spec/spec_helpers/fake_selenium_driver.rb +16 -0
  54. data/spec/spec_helpers/mock_session.rb +30 -0
  55. data/spec/spec_helpers/show_test_exceptions.rb +22 -0
  56. data/spec/spec_helpers/wait_for.rb +11 -0
  57. data/spec/spec_suite.rb +3 -0
  58. data/spec/unit/js_test_core/client/runner_spec.rb +198 -0
  59. data/spec/unit/js_test_core/configuration_spec.rb +44 -0
  60. data/spec/unit/js_test_core/resources/file_spec.rb +79 -0
  61. data/spec/unit/js_test_core/resources/framework_file_spec.rb +58 -0
  62. data/spec/unit/js_test_core/resources/implementations_deprecation_spec.rb +16 -0
  63. data/spec/unit/js_test_core/resources/not_found_spec.rb +49 -0
  64. data/spec/unit/js_test_core/resources/remote_control_spec.rb +117 -0
  65. data/spec/unit/js_test_core/resources/spec_file_spec.rb +147 -0
  66. data/spec/unit/js_test_core/resources/web_root_spec.rb +26 -0
  67. data/spec/unit/js_test_core/server/server_spec.rb +103 -0
  68. data/spec/unit/unit_spec_helper.rb +34 -0
  69. data/spec/unit_suite.rb +10 -0
  70. data/vendor/lucky-luciano/lib/lucky_luciano.rb +5 -0
  71. data/vendor/lucky-luciano/lib/lucky_luciano/resource.rb +142 -0
  72. data/vendor/lucky-luciano/lib/lucky_luciano/resource/path.rb +24 -0
  73. data/vendor/lucky-luciano/lib/lucky_luciano/rspec.rb +4 -0
  74. data/vendor/lucky-luciano/lib/lucky_luciano/rspec/be_http.rb +32 -0
  75. data/vendor/lucky-luciano/spec/lucky_luciano/resource_spec.rb +276 -0
  76. data/vendor/lucky-luciano/spec/spec_helper.rb +48 -0
  77. data/vendor/lucky-luciano/spec/spec_suite.rb +4 -0
  78. metadata +146 -0
@@ -0,0 +1,40 @@
1
+ class JsTestServer::Server::Views::Page < Erector::Widget
2
+ def content(&block)
3
+ rawtext %Q{<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">}
4
+ html :xmlns => "http://www.w3.org/1999/xhtml", :"xml:lang" => "en" do
5
+ head do
6
+ meta :"http-equiv" => "Content-Type", :content => "text/html;charset=UTF-8"
7
+ title title_text
8
+ head_content
9
+ end
10
+ body do
11
+ body_content(&block)
12
+ end
13
+ end
14
+ end
15
+
16
+ protected
17
+
18
+ def head_content
19
+ end
20
+
21
+ def title_text
22
+ "Js Test Server"
23
+ end
24
+
25
+ def body_content(&block)
26
+ yield(self)
27
+ end
28
+
29
+ def javascript(params={})
30
+ if params[:src]
31
+ script({:type => "text/javascript"}.merge(params))
32
+ else
33
+ super
34
+ end
35
+ end
36
+
37
+ def path
38
+ helpers.rack_request.path_info
39
+ end
40
+ end
@@ -0,0 +1,17 @@
1
+ module JsTestServer
2
+ module Server
3
+ module Views
4
+ class RemoteControlSubscriber < JsTestServer::Server::Views::Page
5
+ def head_content
6
+ javascript :src => "/js_test_server.js"
7
+ javascript :src => "/js_test_server/remote_control.js"
8
+ javascript "JsTestServer.RemoteControl.start();"
9
+ end
10
+
11
+ def body_content
12
+ a "Commands", :href => Resources::RemoteControl.path("commands"), :id => "commands"
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,54 @@
1
+ class JsTestServer::Server::Views::Suite < JsTestServer::Server::Views::Page
2
+ class << self
3
+ def project_js_files
4
+ @@project_js_files ||= []
5
+ end
6
+
7
+ def project_js_files=(files)
8
+ @@project_js_files = files
9
+ end
10
+
11
+ def project_css_files
12
+ @@project_css_files ||= []
13
+ end
14
+
15
+ def project_css_files=(files)
16
+ @@project_css_files = files
17
+ end
18
+ end
19
+
20
+ needs :spec_files
21
+ attr_reader :spec_files
22
+ protected
23
+
24
+ def title_text
25
+ "Js Test Core Suite"
26
+ end
27
+
28
+ def head_content
29
+ project_js_files
30
+ project_css_files
31
+ spec_script_elements
32
+ end
33
+
34
+ def spec_script_elements
35
+ spec_files.each do |file|
36
+ script :type => "text/javascript", :src => file
37
+ end
38
+ end
39
+
40
+ def project_js_files
41
+ self.class.project_js_files.each do |file|
42
+ script :src => file, :type => "text/javascript"
43
+ end
44
+ end
45
+
46
+ def project_css_files
47
+ self.class.project_css_files.each do |file|
48
+ link :href => file, :type => "text/css", :media => "screen", :rel => "stylesheet"
49
+ end
50
+ end
51
+
52
+ def body_content
53
+ end
54
+ end
@@ -0,0 +1,6 @@
1
+ module JsTestServer::Server::Views::Suites
2
+ end
3
+
4
+ Dir["#{File.dirname(__FILE__)}/suites/*.html.rb"].each do |file|
5
+ require file
6
+ end
@@ -0,0 +1,30 @@
1
+ class JsTestServer::Server::Views::Suites::Jasmine < JsTestServer::Server::Views::Suite
2
+ needs :spec_files, :framework_path
3
+ attr_reader :spec_files, :framework_path
4
+
5
+ def title_text
6
+ "Jasmine suite"
7
+ end
8
+
9
+ def head_content
10
+ core_js_files
11
+ project_js_files
12
+ link :rel => "stylesheet", :href => "/framework/jasmine.css"
13
+ project_css_files
14
+ spec_script_elements
15
+ javascript "JsTestServer.JasmineDriver.init();"
16
+
17
+ end
18
+
19
+ def core_js_files
20
+ jasmine_file = File.basename(Dir["#{framework_path}/jasmine*.js"].sort.last)
21
+ javascript :src => "/framework/#{jasmine_file}"
22
+ javascript :src => "/framework/TrivialReporter.js"
23
+ javascript :src => "/js_test_server.js"
24
+ javascript :src => "/js_test_server/jasmine_driver.js"
25
+ end
26
+
27
+ def body_content
28
+ div :id => "jasmine_content"
29
+ end
30
+ end
@@ -0,0 +1,44 @@
1
+ class JsTestServer::Server::Views::Suites::ScrewUnit < JsTestServer::Server::Views::Suite
2
+ class << self
3
+ attr_accessor :jquery_js_file
4
+ end
5
+
6
+ needs :spec_files, :framework_path
7
+ attr_reader :spec_files, :framework_path
8
+
9
+ def title_text
10
+ "Screw Unit suite"
11
+ end
12
+
13
+ def head_content
14
+ core_js_files
15
+ project_js_files
16
+ link :rel => "stylesheet", :href => "/framework/screw.css"
17
+ project_css_files
18
+
19
+ spec_script_elements
20
+ end
21
+
22
+ def core_js_files
23
+ javascript :src => jquery_js_file
24
+ javascript :src => "/js_test_server.js"
25
+ javascript :src => "/framework/jquery.fn.js"
26
+ javascript :src => "/framework/jquery.print.js"
27
+ javascript :src => "/framework/screw.builder.js"
28
+ javascript :src => "/framework/screw.matchers.js"
29
+ javascript :src => "/framework/screw.events.js"
30
+ javascript :src => "/framework/screw.behaviors.js"
31
+ javascript :src => "/js_test_server/screw_unit_driver.js"
32
+ end
33
+
34
+ def jquery_js_file
35
+ self.class.jquery_js_file || (
36
+ (jquery_path = Dir["#{framework_path}/jquery-*.js"].sort.last) &&
37
+ "/framework/#{File.basename(jquery_path)}"
38
+ )
39
+ end
40
+
41
+ def body_content
42
+ div :id => "screw_unit_content"
43
+ end
44
+ end
@@ -0,0 +1,565 @@
1
+ (function() {
2
+ function JsTestServer() {
3
+ }
4
+ window.JsTestServer = JsTestServer;
5
+
6
+ JsTestServer.status = function() {
7
+ throw "You must implement the JsTestServer.status method";
8
+ };
9
+
10
+ JsTestServer.Assets = {};
11
+ JsTestServer.Assets.use_cache_buster = false; // TODO: NS/CTI - make this configurable from the UI.
12
+ var required_paths = [];
13
+ var included_stylesheets = {};
14
+ var cache_buster = parseInt(new Date().getTime() / (1 * 1000));
15
+
16
+ function tag(name, attributes) {
17
+ var html = "<" + name;
18
+ for (var attribute in attributes) {
19
+ html += (" " + attribute + "='" + attributes[attribute]) + "'";
20
+ }
21
+ html += "></";
22
+ html += name;
23
+ html += ">";
24
+ return html;
25
+ }
26
+
27
+ JsTestServer.Assets.require = function(javascript_path, onload) {
28
+ if (!required_paths[javascript_path]) {
29
+ var full_path = javascript_path + ".js";
30
+ if (JsTestServer.Assets.use_cache_buster) {
31
+ full_path += '?' + cache_buster;
32
+ }
33
+ document.write(tag("script", {src: full_path, type: 'text/javascript'}));
34
+ if (onload) {
35
+ var scripts = document.getElementsByTagName('script');
36
+ scripts[scripts.length - 1].onload = onload;
37
+ }
38
+ required_paths[javascript_path] = true;
39
+ }
40
+ };
41
+
42
+ JsTestServer.Assets.stylesheet = function(stylesheet_path) {
43
+ if (!included_stylesheets[stylesheet_path]) {
44
+ var full_path = stylesheet_path + ".css";
45
+ if (JsTestServer.Assets.use_cache_buster) {
46
+ full_path += '?' + cache_buster;
47
+ }
48
+ document.write(tag("link", {rel: 'stylesheet', type: 'text/css', href: full_path}));
49
+ included_stylesheets[stylesheet_path] = true;
50
+ }
51
+ };
52
+
53
+ JsTestServer.xhrGet = function(url, onComplete) {
54
+ var request;
55
+ try {
56
+ request = new XMLHttpRequest();
57
+ } catch(e) {
58
+ try {
59
+ request = new ActiveXObject("Msxml2.XMLHTTP");
60
+ } catch(e) {
61
+ request = new ActiveXObject("Microsoft.XMLHTTP");
62
+ }
63
+ };
64
+
65
+ request.onreadystatechange = function() {
66
+ if (request.readyState == 4) {
67
+ if (request.status == 200) {
68
+ onComplete(request);
69
+ }
70
+ }
71
+ };
72
+
73
+ request.open("get", url, true);
74
+ request.send(null);
75
+ };
76
+
77
+ window.require = JsTestServer.Assets.require;
78
+ window.stylesheet = JsTestServer.Assets.stylesheet;
79
+ })();
80
+
81
+
82
+ //////////////////////////////////////////////////////////////////
83
+ // Inlining json2.js library because there should only be one js script tag
84
+ // and its simpler to inline rather than cat the js together on the server.
85
+ //////////////////////////////////////////////////////////////////
86
+
87
+ /*
88
+ http://www.JSON.org/json2.js
89
+ 2009-06-29
90
+
91
+ Public Domain.
92
+
93
+ NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
94
+
95
+ See http://www.JSON.org/js.html
96
+
97
+ This file creates a global JSON object containing two methods: stringify
98
+ and parse.
99
+
100
+ JSON.stringify(value, replacer, space)
101
+ value any JavaScript value, usually an object or array.
102
+
103
+ replacer an optional parameter that determines how object
104
+ values are stringified for objects. It can be a
105
+ function or an array of strings.
106
+
107
+ space an optional parameter that specifies the indentation
108
+ of nested structures. If it is omitted, the text will
109
+ be packed without extra whitespace. If it is a number,
110
+ it will specify the number of spaces to indent at each
111
+ level. If it is a string (such as '\t' or '&nbsp;'),
112
+ it contains the characters used to indent at each level.
113
+
114
+ This method produces a JSON text from a JavaScript value.
115
+
116
+ When an object value is found, if the object contains a toJSON
117
+ method, its toJSON method will be called and the result will be
118
+ stringified. A toJSON method does not serialize: it returns the
119
+ value represented by the name/value pair that should be serialized,
120
+ or undefined if nothing should be serialized. The toJSON method
121
+ will be passed the key associated with the value, and this will be
122
+ bound to the object holding the key.
123
+
124
+ For example, this would serialize Dates as ISO strings.
125
+
126
+ Date.prototype.toJSON = function (key) {
127
+ function f(n) {
128
+ // Format integers to have at least two digits.
129
+ return n < 10 ? '0' + n : n;
130
+ }
131
+
132
+ return this.getUTCFullYear() + '-' +
133
+ f(this.getUTCMonth() + 1) + '-' +
134
+ f(this.getUTCDate()) + 'T' +
135
+ f(this.getUTCHours()) + ':' +
136
+ f(this.getUTCMinutes()) + ':' +
137
+ f(this.getUTCSeconds()) + 'Z';
138
+ };
139
+
140
+ You can provide an optional replacer method. It will be passed the
141
+ key and value of each member, with this bound to the containing
142
+ object. The value that is returned from your method will be
143
+ serialized. If your method returns undefined, then the member will
144
+ be excluded from the serialization.
145
+
146
+ If the replacer parameter is an array of strings, then it will be
147
+ used to select the members to be serialized. It filters the results
148
+ such that only members with keys listed in the replacer array are
149
+ stringified.
150
+
151
+ Values that do not have JSON views, such as undefined or
152
+ functions, will not be serialized. Such values in objects will be
153
+ dropped; in arrays they will be replaced with null. You can use
154
+ a replacer function to replace those with JSON values.
155
+ JSON.stringify(undefined) returns undefined.
156
+
157
+ The optional space parameter produces a stringification of the
158
+ value that is filled with line breaks and indentation to make it
159
+ easier to read.
160
+
161
+ If the space parameter is a non-empty string, then that string will
162
+ be used for indentation. If the space parameter is a number, then
163
+ the indentation will be that many spaces.
164
+
165
+ Example:
166
+
167
+ text = JSON.stringify(['e', {pluribus: 'unum'}]);
168
+ // text is '["e",{"pluribus":"unum"}]'
169
+
170
+
171
+ text = JSON.stringify(['e', {pluribus: 'unum'}], null, '\t');
172
+ // text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'
173
+
174
+ text = JSON.stringify([new Date()], function (key, value) {
175
+ return this[key] instanceof Date ?
176
+ 'Date(' + this[key] + ')' : value;
177
+ });
178
+ // text is '["Date(---current time---)"]'
179
+
180
+
181
+ JSON.parse(text, reviver)
182
+ This method parses a JSON text to produce an object or array.
183
+ It can throw a SyntaxError exception.
184
+
185
+ The optional reviver parameter is a function that can filter and
186
+ transform the results. It receives each of the keys and values,
187
+ and its return value is used instead of the original value.
188
+ If it returns what it received, then the structure is not modified.
189
+ If it returns undefined then the member is deleted.
190
+
191
+ Example:
192
+
193
+ // Parse the text. Values that look like ISO date strings will
194
+ // be converted to Date objects.
195
+
196
+ myData = JSON.parse(text, function (key, value) {
197
+ var a;
198
+ if (typeof value === 'string') {
199
+ a =
200
+ /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
201
+ if (a) {
202
+ return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],
203
+ +a[5], +a[6]));
204
+ }
205
+ }
206
+ return value;
207
+ });
208
+
209
+ myData = JSON.parse('["Date(09/09/2001)"]', function (key, value) {
210
+ var d;
211
+ if (typeof value === 'string' &&
212
+ value.slice(0, 5) === 'Date(' &&
213
+ value.slice(-1) === ')') {
214
+ d = new Date(value.slice(5, -1));
215
+ if (d) {
216
+ return d;
217
+ }
218
+ }
219
+ return value;
220
+ });
221
+
222
+
223
+ This is a reference implementation. You are free to copy, modify, or
224
+ redistribute.
225
+
226
+ This code should be minified before deployment.
227
+ See http://javascript.crockford.com/jsmin.html
228
+
229
+ USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO
230
+ NOT CONTROL.
231
+ */
232
+
233
+ /*jslint evil: true */
234
+
235
+ /*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply,
236
+ call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,
237
+ getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join,
238
+ lastIndex, length, parse, prototype, push, replace, slice, stringify,
239
+ test, toJSON, toString, valueOf
240
+ */
241
+
242
+ // Create a JSON object only if one does not already exist. We create the
243
+ // methods in a closure to avoid creating global variables.
244
+
245
+ (function(JsTestServer) {
246
+ var JSON = JSON || {};
247
+ JsTestServer.JSON = JSON;
248
+
249
+ (function () {
250
+
251
+ function f(n) {
252
+ // Format integers to have at least two digits.
253
+ return n < 10 ? '0' + n : n;
254
+ }
255
+
256
+ if (typeof Date.prototype.toJSON !== 'function') {
257
+
258
+ Date.prototype.toJSON = function (key) {
259
+
260
+ return isFinite(this.valueOf()) ?
261
+ this.getUTCFullYear() + '-' +
262
+ f(this.getUTCMonth() + 1) + '-' +
263
+ f(this.getUTCDate()) + 'T' +
264
+ f(this.getUTCHours()) + ':' +
265
+ f(this.getUTCMinutes()) + ':' +
266
+ f(this.getUTCSeconds()) + 'Z' : null;
267
+ };
268
+
269
+ String.prototype.toJSON =
270
+ Number.prototype.toJSON =
271
+ Boolean.prototype.toJSON = function (key) {
272
+ return this.valueOf();
273
+ };
274
+ }
275
+
276
+ var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
277
+ escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
278
+ gap,
279
+ indent,
280
+ meta = { // table of character substitutions
281
+ '\b': '\\b',
282
+ '\t': '\\t',
283
+ '\n': '\\n',
284
+ '\f': '\\f',
285
+ '\r': '\\r',
286
+ '"' : '\\"',
287
+ '\\': '\\\\'
288
+ },
289
+ rep;
290
+
291
+
292
+ function quote(string) {
293
+
294
+ // If the string contains no control characters, no quote characters, and no
295
+ // backslash characters, then we can safely slap some quotes around it.
296
+ // Otherwise we must also replace the offending characters with safe escape
297
+ // sequences.
298
+
299
+ escapable.lastIndex = 0;
300
+ return escapable.test(string) ?
301
+ '"' + string.replace(escapable, function (a) {
302
+ var c = meta[a];
303
+ return typeof c === 'string' ? c :
304
+ '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
305
+ }) + '"' :
306
+ '"' + string + '"';
307
+ }
308
+
309
+
310
+ function str(key, holder) {
311
+
312
+ // Produce a string from holder[key].
313
+
314
+ var i, // The loop counter.
315
+ k, // The member key.
316
+ v, // The member value.
317
+ length,
318
+ mind = gap,
319
+ partial,
320
+ value = holder[key];
321
+
322
+ // If the value has a toJSON method, call it to obtain a replacement value.
323
+
324
+ if (value && typeof value === 'object' &&
325
+ typeof value.toJSON === 'function') {
326
+ value = value.toJSON(key);
327
+ }
328
+
329
+ // If we were called with a replacer function, then call the replacer to
330
+ // obtain a replacement value.
331
+
332
+ if (typeof rep === 'function') {
333
+ value = rep.call(holder, key, value);
334
+ }
335
+
336
+ // What happens next depends on the value's type.
337
+
338
+ switch (typeof value) {
339
+ case 'string':
340
+ return quote(value);
341
+
342
+ case 'number':
343
+
344
+ // JSON numbers must be finite. Encode non-finite numbers as null.
345
+
346
+ return isFinite(value) ? String(value) : 'null';
347
+
348
+ case 'boolean':
349
+ case 'null':
350
+
351
+ // If the value is a boolean or null, convert it to a string. Note:
352
+ // typeof null does not produce 'null'. The case is included here in
353
+ // the remote chance that this gets fixed someday.
354
+
355
+ return String(value);
356
+
357
+ // If the type is 'object', we might be dealing with an object or an array or
358
+ // null.
359
+
360
+ case 'object':
361
+
362
+ // Due to a specification blunder in ECMAScript, typeof null is 'object',
363
+ // so watch out for that case.
364
+
365
+ if (!value) {
366
+ return 'null';
367
+ }
368
+
369
+ // Make an array to hold the partial results of stringifying this object value.
370
+
371
+ gap += indent;
372
+ partial = [];
373
+
374
+ // Is the value an array?
375
+
376
+ if (Object.prototype.toString.apply(value) === '[object Array]') {
377
+
378
+ // The value is an array. Stringify every element. Use null as a placeholder
379
+ // for non-JSON values.
380
+
381
+ length = value.length;
382
+ for (i = 0; i < length; i += 1) {
383
+ partial[i] = str(i, value) || 'null';
384
+ }
385
+
386
+ // Join all of the elements together, separated with commas, and wrap them in
387
+ // brackets.
388
+
389
+ v = partial.length === 0 ? '[]' :
390
+ gap ? '[\n' + gap +
391
+ partial.join(',\n' + gap) + '\n' +
392
+ mind + ']' :
393
+ '[' + partial.join(',') + ']';
394
+ gap = mind;
395
+ return v;
396
+ }
397
+
398
+ // If the replacer is an array, use it to select the members to be stringified.
399
+
400
+ if (rep && typeof rep === 'object') {
401
+ length = rep.length;
402
+ for (i = 0; i < length; i += 1) {
403
+ k = rep[i];
404
+ if (typeof k === 'string') {
405
+ v = str(k, value);
406
+ if (v) {
407
+ partial.push(quote(k) + (gap ? ': ' : ':') + v);
408
+ }
409
+ }
410
+ }
411
+ } else {
412
+
413
+ // Otherwise, iterate through all of the keys in the object.
414
+
415
+ for (k in value) {
416
+ if (Object.hasOwnProperty.call(value, k)) {
417
+ v = str(k, value);
418
+ if (v) {
419
+ partial.push(quote(k) + (gap ? ': ' : ':') + v);
420
+ }
421
+ }
422
+ }
423
+ }
424
+
425
+ // Join all of the member texts together, separated with commas,
426
+ // and wrap them in braces.
427
+
428
+ v = partial.length === 0 ? '{}' :
429
+ gap ? '{\n' + gap + partial.join(',\n' + gap) + '\n' +
430
+ mind + '}' : '{' + partial.join(',') + '}';
431
+ gap = mind;
432
+ return v;
433
+ }
434
+ }
435
+
436
+ // If the JSON object does not yet have a stringify method, give it one.
437
+
438
+ if (typeof JSON.stringify !== 'function') {
439
+ JSON.stringify = function (value, replacer, space) {
440
+
441
+ // The stringify method takes a value and an optional replacer, and an optional
442
+ // space parameter, and returns a JSON text. The replacer can be a function
443
+ // that can replace values, or an array of strings that will select the keys.
444
+ // A default replacer method can be provided. Use of the space parameter can
445
+ // produce text that is more easily readable.
446
+
447
+ var i;
448
+ gap = '';
449
+ indent = '';
450
+
451
+ // If the space parameter is a number, make an indent string containing that
452
+ // many spaces.
453
+
454
+ if (typeof space === 'number') {
455
+ for (i = 0; i < space; i += 1) {
456
+ indent += ' ';
457
+ }
458
+
459
+ // If the space parameter is a string, it will be used as the indent string.
460
+
461
+ } else if (typeof space === 'string') {
462
+ indent = space;
463
+ }
464
+
465
+ // If there is a replacer, it must be a function or an array.
466
+ // Otherwise, throw an error.
467
+
468
+ rep = replacer;
469
+ if (replacer && typeof replacer !== 'function' &&
470
+ (typeof replacer !== 'object' ||
471
+ typeof replacer.length !== 'number')) {
472
+ throw new Error('JSON.stringify');
473
+ }
474
+
475
+ // Make a fake root object containing our value under the key of ''.
476
+ // Return the result of stringifying the value.
477
+
478
+ return str('', {'': value});
479
+ };
480
+ }
481
+
482
+
483
+ // If the JSON object does not yet have a parse method, give it one.
484
+
485
+ if (typeof JSON.parse !== 'function') {
486
+ JSON.parse = function (text, reviver) {
487
+
488
+ // The parse method takes a text and an optional reviver function, and returns
489
+ // a JavaScript value if the text is a valid JSON text.
490
+
491
+ var j;
492
+
493
+ function walk(holder, key) {
494
+
495
+ // The walk method is used to recursively walk the resulting structure so
496
+ // that modifications can be made.
497
+
498
+ var k, v, value = holder[key];
499
+ if (value && typeof value === 'object') {
500
+ for (k in value) {
501
+ if (Object.hasOwnProperty.call(value, k)) {
502
+ v = walk(value, k);
503
+ if (v !== undefined) {
504
+ value[k] = v;
505
+ } else {
506
+ delete value[k];
507
+ }
508
+ }
509
+ }
510
+ }
511
+ return reviver.call(holder, key, value);
512
+ }
513
+
514
+
515
+ // Parsing happens in four stages. In the first stage, we replace certain
516
+ // Unicode characters with escape sequences. JavaScript handles many characters
517
+ // incorrectly, either silently deleting them, or treating them as line endings.
518
+
519
+ cx.lastIndex = 0;
520
+ if (cx.test(text)) {
521
+ text = text.replace(cx, function (a) {
522
+ return '\\u' +
523
+ ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
524
+ });
525
+ }
526
+
527
+ // In the second stage, we run the text against regular expressions that look
528
+ // for non-JSON patterns. We are especially concerned with '()' and 'new'
529
+ // because they can cause invocation, and '=' because it can cause mutation.
530
+ // But just to be safe, we want to reject all unexpected forms.
531
+
532
+ // We split the second stage into 4 regexp operations in order to work around
533
+ // crippling inefficiencies in IE's and Safari's regexp engines. First we
534
+ // replace the JSON backslash pairs with '@' (a non-JSON character). Second, we
535
+ // replace all simple value tokens with ']' characters. Third, we delete all
536
+ // open brackets that follow a colon or comma or that begin the text. Finally,
537
+ // we look to see that the remaining characters are only whitespace or ']' or
538
+ // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
539
+
540
+ if (/^[\],:{}\s]*$/.
541
+ test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').
542
+ replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
543
+ replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
544
+
545
+ // In the third stage we use the eval function to compile the text into a
546
+ // JavaScript structure. The '{' operator is subject to a syntactic ambiguity
547
+ // in JavaScript: it can begin a block or an object literal. We wrap the text
548
+ // in parens to eliminate the ambiguity.
549
+
550
+ j = eval('(' + text + ')');
551
+
552
+ // In the optional fourth stage, we recursively walk the new structure, passing
553
+ // each name/value pair to a reviver function for possible transformation.
554
+
555
+ return typeof reviver === 'function' ?
556
+ walk({'': j}, '') : j;
557
+ }
558
+
559
+ // If the text is not JSON parseable, then a SyntaxError is thrown.
560
+
561
+ throw new SyntaxError('JSON.parse');
562
+ };
563
+ }
564
+ }());
565
+ })(JsTestServer);