Skip to content

Commit 0e1c454

Browse files
committed
Cache simulated platform objects for tests
1 parent fe8fc4a commit 0e1c454

File tree

1 file changed

+62
-54
lines changed

1 file changed

+62
-54
lines changed

test/test.js

Lines changed: 62 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -111,61 +111,69 @@
111111
*
112112
* @private
113113
* @param {Object} options The options object to simulate environment objects.
114-
* @returns {Object} The modified string.
114+
* @returns {Object} The simulated platform object.
115115
*/
116116
var getPlatform = (function() {
117-
var code,
118-
result,
119-
xhr;
120-
121-
// for browsers
122-
if (root.document && !root.phantom) {
123-
if (isHostType(root, 'ActiveXObject')) {
124-
xhr = new ActiveXObject('Microsoft.XMLHTTP');
125-
} else if (isHostType(root, 'XMLHttpRequest')) {
126-
xhr = new XMLHttpRequest;
127-
}
128-
each(document.getElementsByTagName('script'), function(element) {
129-
var src = element.src;
130-
if (/platform\.js$/.test(src)) {
131-
xhr.open('get', src + '?t=' + (+new Date), false);
132-
xhr.send(null);
133-
code = xhr.responseText;
117+
// provides a cache for platform objects
118+
var cache = {};
119+
var getPlatform = (function() {
120+
var code,
121+
result,
122+
xhr;
123+
124+
// for browsers
125+
if (root.document && !root.phantom) {
126+
if (isHostType(root, 'ActiveXObject')) {
127+
xhr = new ActiveXObject('Microsoft.XMLHTTP');
128+
} else if (isHostType(root, 'XMLHttpRequest')) {
129+
xhr = new XMLHttpRequest;
134130
}
135-
});
136-
}
137-
// for Narwhal and Rhino
138-
else if (typeof readFile == 'function') {
139-
code = readFile('../platform.js');
140-
}
141-
// for Node.js, PhantomJS, and RingoJS
142-
else if (typeof require == 'function') {
143-
code = (require('fs').readFileSync || require('fs').read)('../platform.js');
144-
}
145-
return Function('options',
146-
('return ' +
147-
/\(function[\s\S]+?(?=if\s*\(typeof define)/.exec(code)[0] +
148-
' return parse()}.call(this))')
149-
.replace('/internal|\\n/i.test(toString.toString())', '!me.likeChrome')
150-
.replace(/\broot\s*=[^\n]+?(;\n)/, 'root=options$1')
151-
.replace(/\boldRoot\s*=[^\n]+?(;\n)/, 'oldRoot=options$1')
152-
.replace(/\bvar thisBinding\s*=[^\n]+?(;\n)/, '')
153-
.replace(/\bfreeGlobal\s*=(?:.|\n)+?(;\n)\s*if[^}]+\}/, 'freeGlobal=options.global$1')
154-
.replace(/\buserAgent\s*=[^\n]+?(;\n)/, 'userAgent=me.ua$1')
155-
.replace(/\b(?:thisBinding|root)\b/g, 'me')
156-
.replace(/([^.])\bsystem\b/g, '$1me.system')
157-
.replace(/\bgetClassOf\(opera\)/g, 'opera&&opera["[[Class]]"]')
158-
.replace(/\b(?:Environment|Java|RuntimeObject|ScriptBridgingProxyObject)\b/g, 'Object')
159-
.replace(/\bnav\.appMinorVersion/g, 'me.appMinorVersion')
160-
.replace(/\bnav\.cpuClass/g, 'me.cpuClass')
161-
.replace(/\bnav\.platform/g, 'me.platform')
162-
.replace(/\bexports\b/g, 'me.exports')
163-
.replace(/\bexternal/g, 'me.external')
164-
.replace(/\bprocess\b/g, 'me.process')
165-
.replace(/\brequire\b/g, 'me.require')
166-
.replace(/\bdoc\.documentMode/g, 'me.mode'));
131+
each(document.getElementsByTagName('script'), function(element) {
132+
var src = element.src;
133+
if (/platform\.js$/.test(src)) {
134+
xhr.open('get', src + '?t=' + (+new Date), false);
135+
xhr.send(null);
136+
code = xhr.responseText;
137+
}
138+
});
139+
}
140+
// for Narwhal and Rhino
141+
else if (typeof readFile == 'function') {
142+
code = readFile('../platform.js');
143+
}
144+
// for Node.js, PhantomJS, and RingoJS
145+
else if (typeof require == 'function') {
146+
code = (require('fs').readFileSync || require('fs').read)('../platform.js');
147+
}
148+
return Function('options',
149+
('return ' +
150+
/\(function[\s\S]+?(?=if\s*\(typeof define)/.exec(code)[0] +
151+
' return parse()}.call(this))')
152+
.replace('/internal|\\n/i.test(toString.toString())', '!me.likeChrome')
153+
.replace(/\broot\s*=[^\n]+?(;\n)/, 'root=options$1')
154+
.replace(/\boldRoot\s*=[^\n]+?(;\n)/, 'oldRoot=options$1')
155+
.replace(/\bvar thisBinding\s*=[^\n]+?(;\n)/, '')
156+
.replace(/\bfreeGlobal\s*=(?:.|\n)+?(;\n)\s*if[^}]+\}/, 'freeGlobal=options.global$1')
157+
.replace(/\buserAgent\s*=[^\n]+?(;\n)/, 'userAgent=me.ua$1')
158+
.replace(/\b(?:thisBinding|root)\b/g, 'me')
159+
.replace(/([^.])\bsystem\b/g, '$1me.system')
160+
.replace(/\bgetClassOf\(opera\)/g, 'opera&&opera["[[Class]]"]')
161+
.replace(/\b(?:Environment|Java|RuntimeObject|ScriptBridgingProxyObject)\b/g, 'Object')
162+
.replace(/\bnav\.appMinorVersion/g, 'me.appMinorVersion')
163+
.replace(/\bnav\.cpuClass/g, 'me.cpuClass')
164+
.replace(/\bnav\.platform/g, 'me.platform')
165+
.replace(/\bexports\b/g, 'me.exports')
166+
.replace(/\bexternal/g, 'me.external')
167+
.replace(/\bprocess\b/g, 'me.process')
168+
.replace(/\brequire\b/g, 'me.require')
169+
.replace(/\bdoc\.documentMode/g, 'me.mode'));
170+
}());
171+
return function(name, options) {
172+
return cache[name] || (cache[name] = getPlatform(options));
173+
};
167174
}());
168175

176+
169177
/*--------------------------------------------------------------------------*/
170178

171179
/**
@@ -1883,7 +1891,7 @@
18831891
each(['description', 'layout', 'manufacturer', 'name', 'os', 'prerelease', 'product', 'version'], function(name) {
18841892
test('has the correct `platform.' + name + '` property', function() {
18851893
forOwn(Tests, function(value, key) {
1886-
var platform = getPlatform(value);
1894+
var platform = getPlatform(key, value);
18871895
value = name == 'description' ? key : value[name];
18881896
value = value ? interpolate(value, { 'alpha': '\u03b1', 'beta': '\u03b2', ' ': ' ' }) : null;
18891897
equal(platform && String(platform[name]), String(value), String(platform));
@@ -1892,15 +1900,15 @@
18921900
});
18931901

18941902
test('has correct null values', function() {
1895-
forOwn(Tests, function(value) {
1896-
forOwn(getPlatform(value), function(value, key) {
1903+
forOwn(Tests, function(value, key) {
1904+
forOwn(getPlatform(key, value), function(value, key) {
18971905
!value && strictEqual(value, null, 'platform.' + key);
18981906
});
18991907
});
19001908
});
19011909

19021910
test('handles no user agent', function() {
1903-
forOwn(getPlatform({}), function(value, key) {
1911+
forOwn(getPlatform('', {}), function(value, key) {
19041912
if (typeof value != 'function') {
19051913
equal(String(value), 'null', 'platform.' + key);
19061914
}

0 commit comments

Comments
 (0)