245 lines
8.3 KiB
JavaScript
245 lines
8.3 KiB
JavaScript
getJasmineRequireObj().base = function(j$, jasmineGlobal) {
|
|
j$.unimplementedMethod_ = function() {
|
|
throw new Error('unimplemented method');
|
|
};
|
|
|
|
/**
|
|
* Maximum object depth the pretty printer will print to.
|
|
* Set this to a lower value to speed up pretty printing if you have large objects.
|
|
* @name jasmine.MAX_PRETTY_PRINT_DEPTH
|
|
*/
|
|
j$.MAX_PRETTY_PRINT_DEPTH = 8;
|
|
/**
|
|
* Maximum number of array elements to display when pretty printing objects.
|
|
* This will also limit the number of keys and values displayed for an object.
|
|
* Elements past this number will be ellipised.
|
|
* @name jasmine.MAX_PRETTY_PRINT_ARRAY_LENGTH
|
|
*/
|
|
j$.MAX_PRETTY_PRINT_ARRAY_LENGTH = 50;
|
|
/**
|
|
* Maximum number of charasters to display when pretty printing objects.
|
|
* Characters past this number will be ellipised.
|
|
* @name jasmine.MAX_PRETTY_PRINT_CHARS
|
|
*/
|
|
j$.MAX_PRETTY_PRINT_CHARS = 1000;
|
|
/**
|
|
* Default number of milliseconds Jasmine will wait for an asynchronous spec to complete.
|
|
* @name jasmine.DEFAULT_TIMEOUT_INTERVAL
|
|
*/
|
|
j$.DEFAULT_TIMEOUT_INTERVAL = 5000;
|
|
|
|
j$.getGlobal = function() {
|
|
return jasmineGlobal;
|
|
};
|
|
|
|
/**
|
|
* Get the currently booted Jasmine Environment.
|
|
*
|
|
* @name jasmine.getEnv
|
|
* @function
|
|
* @return {Env}
|
|
*/
|
|
j$.getEnv = function(options) {
|
|
var env = j$.currentEnv_ = j$.currentEnv_ || new j$.Env(options);
|
|
//jasmine. singletons in here (setTimeout blah blah).
|
|
return env;
|
|
};
|
|
|
|
j$.isArray_ = function(value) {
|
|
return j$.isA_('Array', value);
|
|
};
|
|
|
|
j$.isObject_ = function(value) {
|
|
return !j$.util.isUndefined(value) && value !== null && j$.isA_('Object', value);
|
|
};
|
|
|
|
j$.isString_ = function(value) {
|
|
return j$.isA_('String', value);
|
|
};
|
|
|
|
j$.isNumber_ = function(value) {
|
|
return j$.isA_('Number', value);
|
|
};
|
|
|
|
j$.isFunction_ = function(value) {
|
|
return j$.isA_('Function', value);
|
|
};
|
|
|
|
j$.isAsyncFunction_ = function(value) {
|
|
return j$.isA_('AsyncFunction', value);
|
|
};
|
|
|
|
j$.isTypedArray_ = function(value) {
|
|
return j$.isA_('Float32Array', value) ||
|
|
j$.isA_('Float64Array', value) ||
|
|
j$.isA_('Int16Array', value) ||
|
|
j$.isA_('Int32Array', value) ||
|
|
j$.isA_('Int8Array', value) ||
|
|
j$.isA_('Uint16Array', value) ||
|
|
j$.isA_('Uint32Array', value) ||
|
|
j$.isA_('Uint8Array', value) ||
|
|
j$.isA_('Uint8ClampedArray', value);
|
|
};
|
|
|
|
j$.isA_ = function(typeName, value) {
|
|
return j$.getType_(value) === '[object ' + typeName + ']';
|
|
};
|
|
|
|
j$.getType_ = function(value) {
|
|
return Object.prototype.toString.apply(value);
|
|
};
|
|
|
|
j$.isDomNode = function(obj) {
|
|
return obj.nodeType > 0;
|
|
};
|
|
|
|
j$.isMap = function(obj) {
|
|
return typeof jasmineGlobal.Map !== 'undefined' && obj.constructor === jasmineGlobal.Map;
|
|
};
|
|
|
|
j$.isSet = function(obj) {
|
|
return typeof jasmineGlobal.Set !== 'undefined' && obj.constructor === jasmineGlobal.Set;
|
|
};
|
|
|
|
j$.isPromise = function(obj) {
|
|
return typeof jasmineGlobal.Promise !== 'undefined' && obj.constructor === jasmineGlobal.Promise;
|
|
};
|
|
|
|
j$.fnNameFor = function(func) {
|
|
if (func.name) {
|
|
return func.name;
|
|
}
|
|
|
|
var matches = func.toString().match(/^\s*function\s*(\w*)\s*\(/) ||
|
|
func.toString().match(/^\s*\[object\s*(\w*)Constructor\]/);
|
|
|
|
return matches ? matches[1] : '<anonymous>';
|
|
};
|
|
|
|
/**
|
|
* Get a matcher, usable in any {@link matchers|matcher} that uses Jasmine's equality (e.g. {@link matchers#toEqual|toEqual}, {@link matchers#toContain|toContain}, or {@link matchers#toHaveBeenCalledWith|toHaveBeenCalledWith}),
|
|
* that will succeed if the actual value being compared is an instance of the specified class/constructor.
|
|
* @name jasmine.any
|
|
* @function
|
|
* @param {Constructor} clazz - The constructor to check against.
|
|
*/
|
|
j$.any = function(clazz) {
|
|
return new j$.Any(clazz);
|
|
};
|
|
|
|
/**
|
|
* Get a matcher, usable in any {@link matchers|matcher} that uses Jasmine's equality (e.g. {@link matchers#toEqual|toEqual}, {@link matchers#toContain|toContain}, or {@link matchers#toHaveBeenCalledWith|toHaveBeenCalledWith}),
|
|
* that will succeed if the actual value being compared is not `null` and not `undefined`.
|
|
* @name jasmine.anything
|
|
* @function
|
|
*/
|
|
j$.anything = function() {
|
|
return new j$.Anything();
|
|
};
|
|
|
|
/**
|
|
* Get a matcher, usable in any {@link matchers|matcher} that uses Jasmine's equality (e.g. {@link matchers#toEqual|toEqual}, {@link matchers#toContain|toContain}, or {@link matchers#toHaveBeenCalledWith|toHaveBeenCalledWith}),
|
|
* that will succeed if the actual value being compared contains at least the keys and values.
|
|
* @name jasmine.objectContaining
|
|
* @function
|
|
* @param {Object} sample - The subset of properties that _must_ be in the actual.
|
|
*/
|
|
j$.objectContaining = function(sample) {
|
|
return new j$.ObjectContaining(sample);
|
|
};
|
|
|
|
/**
|
|
* Get a matcher, usable in any {@link matchers|matcher} that uses Jasmine's equality (e.g. {@link matchers#toEqual|toEqual}, {@link matchers#toContain|toContain}, or {@link matchers#toHaveBeenCalledWith|toHaveBeenCalledWith}),
|
|
* that will succeed if the actual value is a `String` that matches the `RegExp` or `String`.
|
|
* @name jasmine.stringMatching
|
|
* @function
|
|
* @param {RegExp|String} expected
|
|
*/
|
|
j$.stringMatching = function(expected) {
|
|
return new j$.StringMatching(expected);
|
|
};
|
|
|
|
/**
|
|
* Get a matcher, usable in any {@link matchers|matcher} that uses Jasmine's equality (e.g. {@link matchers#toEqual|toEqual}, {@link matchers#toContain|toContain}, or {@link matchers#toHaveBeenCalledWith|toHaveBeenCalledWith}),
|
|
* that will succeed if the actual value is an `Array` that contains at least the elements in the sample.
|
|
* @name jasmine.arrayContaining
|
|
* @function
|
|
* @param {Array} sample
|
|
*/
|
|
j$.arrayContaining = function(sample) {
|
|
return new j$.ArrayContaining(sample);
|
|
};
|
|
|
|
/**
|
|
* Get a matcher, usable in any {@link matchers|matcher} that uses Jasmine's equality (e.g. {@link matchers#toEqual|toEqual}, {@link matchers#toContain|toContain}, or {@link matchers#toHaveBeenCalledWith|toHaveBeenCalledWith}),
|
|
* that will succeed if the actual value is an `Array` that contains all of the elements in the sample in any order.
|
|
* @name jasmine.arrayWithExactContents
|
|
* @function
|
|
* @param {Array} sample
|
|
*/
|
|
j$.arrayWithExactContents = function(sample) {
|
|
return new j$.ArrayWithExactContents(sample);
|
|
};
|
|
|
|
/**
|
|
* Create a bare {@link Spy} object. This won't be installed anywhere and will not have any implementation behind it.
|
|
* @name jasmine.createSpy
|
|
* @function
|
|
* @param {String} [name] - Name to give the spy. This will be displayed in failure messages.
|
|
* @param {Function} [originalFn] - Function to act as the real implementation.
|
|
* @return {Spy}
|
|
*/
|
|
j$.createSpy = function(name, originalFn) {
|
|
return j$.Spy(name, originalFn);
|
|
};
|
|
|
|
j$.isSpy = function(putativeSpy) {
|
|
if (!putativeSpy) {
|
|
return false;
|
|
}
|
|
return putativeSpy.and instanceof j$.SpyStrategy &&
|
|
putativeSpy.calls instanceof j$.CallTracker;
|
|
};
|
|
|
|
/**
|
|
* Create an object with multiple {@link Spy}s as its members.
|
|
* @name jasmine.createSpyObj
|
|
* @function
|
|
* @param {String} [baseName] - Base name for the spies in the object.
|
|
* @param {String[]|Object} methodNames - Array of method names to create spies for, or Object whose keys will be method names and values the {@link Spy#and#returnValue|returnValue}.
|
|
* @return {Object}
|
|
*/
|
|
j$.createSpyObj = function(baseName, methodNames) {
|
|
var baseNameIsCollection = j$.isObject_(baseName) || j$.isArray_(baseName);
|
|
|
|
if (baseNameIsCollection && j$.util.isUndefined(methodNames)) {
|
|
methodNames = baseName;
|
|
baseName = 'unknown';
|
|
}
|
|
|
|
var obj = {};
|
|
var spiesWereSet = false;
|
|
|
|
if (j$.isArray_(methodNames)) {
|
|
for (var i = 0; i < methodNames.length; i++) {
|
|
obj[methodNames[i]] = j$.createSpy(baseName + '.' + methodNames[i]);
|
|
spiesWereSet = true;
|
|
}
|
|
} else if (j$.isObject_(methodNames)) {
|
|
for (var key in methodNames) {
|
|
if (methodNames.hasOwnProperty(key)) {
|
|
obj[key] = j$.createSpy(baseName + '.' + key);
|
|
obj[key].and.returnValue(methodNames[key]);
|
|
spiesWereSet = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!spiesWereSet) {
|
|
throw 'createSpyObj requires a non-empty array or object of method names to create spies for';
|
|
}
|
|
|
|
return obj;
|
|
};
|
|
};
|