Updated JSpec
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent 5729da5 · Browse files · 4 files·+117 added−66 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+117 added−66 removed
- tests:+117 added−66 removed
Provenance
- Commit
d85d834- Tree
c863803- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
5729da5
4 files changed+117 added−66 removed
modified:spec/lib/jspec.jquery.js+10 added-3 removed
22 unmodified lines232425262727282930313 unmodified lines353637383939404142434445464748494950515253543 unmodified lines58596061626364656667686970718 unmodified lines22 unmodified lines }, // --- Matchers matchers : { matchers : { have_tag : "jQuery(expected, actual).length === 1", have_one : "alias have_tag", have_tags : "jQuery(expected, actual).length > 1", have_many : "alias have_tags",3 unmodified lines have_text : "jQuery(actual).text() === expected", have_value : "jQuery(actual).val() === expected", be_enabled : "!jQuery(actual).attr('disabled')", have_class : "jQuery(actual).hasClass(expected)", be_animated : "jQuery(actual).queue().length > 0", be_visible : function(actual) { return jQuery(actual).css('display') != 'none' && jQuery(actual).css('visibility') != 'hidden' && jQuery(actual).attr('type') != 'hidden' }, be_hidden : function(actual) { return !JSpec.does(actual, 'be_visible') },
have_classes : function(actual) { return !JSpec.any(JSpec.toArray(arguments, 1), function(arg){ return !JSpec.does(actual, 'have_class', arg) })3 unmodified lines return value ? jQuery(actual).attr(attr) == value: jQuery(actual).attr(attr) }, have_event_handlers : function(actual, expected) { return jQuery(actual).data('events') ? jQuery(actual).data('events').hasOwnProperty(expected) : false }, 'be disabled selected checked' : function(attr) { return 'jQuery(actual).attr("' + attr + '")' }, 8 unmodified linesmodified:spec/lib/jspec.js+55 added-42 removed
2 unmodified lines3 4 ;(function(){5 6 JSpec = {7- version : '4.0.0',7+ version : '4.2.1',8 assert : true,9 cache : {},10 suites : [],11 modules : [],247 unmodified lines259 })260 },261 262 ProxyAssertion : function(object, method, times, negate) {263- var self = this264- var old = object[method]263+ var self = this,264+ old = object[method]265 266 // Proxy267 268 object[method] = function(){269- args = toArray(arguments)270- result = old.apply(object, args)269+ var args = toArray(arguments),270+ result = old.apply(object, args)271 self.calls.push({ args : args, result : result })272 return result273 }274 618 unmodified lines893 * @api public894 */895 896 color : function(string, color) {897+ if (option('disableColors')) {898+ return string899+ } else {897- return "\u001B[" + {898- bold : 1,899- black : 30,900- red : 31,901- green : 32,902- yellow : 33,903- blue : 34,904- magenta : 35,905- cyan : 36,906- white : 37907- }[color] + 'm' + string + "\u001B[0m"900+ return "\u001B[" + {901+ bold : 1,902+ black : 30,903+ red : 31,904+ green : 32,905+ yellow : 33,906+ blue : 34,907+ magenta : 35,908+ cyan : 36,909+ white : 37910+ }[color] + 'm' + string + "\u001B[0m"911+ }912 },913 914 /**915 * Default matcher message callback.43 unmodified lines959 * @api public960 */961 962 normalizeMatcherBody : function(body) {963+ var captures964 switch (body.constructor) {965 case String:966 if (captures = body.match(/^alias (\w+)/)) return JSpec.matchers[last(captures)]967 if (body.length < 4) body = 'actual ' + body + ' expected'107 unmodified lines1075 }1076 },1077 1078 /**1079+ * Parse an XML String and return a 'document'.1080+ *1081+ * @param {string} text1082+ * @return {document}1083+ * @api public1084+ */1085+1086+ parseXML : function(text) {1087+ var xmlDoc1088+ if (window.DOMParser)1089+ xmlDoc = (new DOMParser()).parseFromString(text, "text/xml")1090+ else {1091+ xmlDoc = new ActiveXObject("Microsoft.XMLDOM")1092+ xmlDoc.async = "false"1093+ xmlDoc.loadXML(text)1094+ }1095+ return xmlDoc1096+ },1097+1098+ /**1099 * Escape HTML.1100 *1101 * @param {string} html1102 * @return {string}48 unmodified lines1151 expect : function(actual) {1152 function assert(matcher, args, negate) {1153 var expected = toArray(args, 1)1154 matcher.negate = negate 1130- assertion = new JSpec.Assertion(matcher, actual, expected, negate)1155+ var assertion = new JSpec.Assertion(matcher, actual, expected, negate)1156 hook('beforeAssertion', assertion)1157 if (matcher.defer) assertion.run()1158 else JSpec.currentSpec.assertions.push(assertion.run().report()), hook('afterAssertion', assertion)1159 return assertion.result109 unmodified lines1269 * @api public1270 */1271 1272 destub : function(object, method) {1273+ var captures1274 if (method) {1275 if (object['__prototype__' + method])1276 delete object[method]1277 else356 unmodified lines1634 })1635 },1636 1637 /**1612- * Throw a JSpec related error.1613- *1614- * @param {string} message1615- * @param {Exception} e1616- * @api public1617- */1618-1619- error : function(message, e) {1620- throw (message ? message : '') + e.toString() + 1621- (e.line ? ' near line ' + e.line : '')1622- },1623- 1624- /**1638 * Ad-hoc POST request for JSpec server usage.1639 *1640 * @param {string} uri1641 * @param {string} data145 unmodified lines1787 be_within : "actual >= expected[0] && actual <= last(expected)",1788 have_length_within : "actual.length >= expected[0] && actual.length <= last(expected)",1789 1790 receive : { defer : true, match : function(actual, method, times) {1778- proxy = new JSpec.ProxyAssertion(actual, method, times, this.negate)1791+ var proxy = new JSpec.ProxyAssertion(actual, method, times, this.negate)1792 JSpec.currentSpec.assertions.push(proxy)1793 return proxy1794 }},1795 4 unmodified lines1800 return !actual.length1801 },1802 1803 include : function(actual) {1791- for (state = true, i = 1; i < arguments.length; i++) {1792- arg = arguments[i]1804+ for (var state = true, i = 1; i < arguments.length; i++) {1805+ var arg = arguments[i]1806 switch (actual.constructor) {1807 case String: 1808 case Number:1809 case RegExp:38 unmodified lines1848 case String : return 'exception of ' + puts(expected[i])1849 case Function : return expected[i].name || 'Error'1850 }1851 }1839- exception = message_for(1) + (expected[2] ? ' and ' + message_for(2) : '')1852+ var exception = message_for(1) + (expected[2] ? ' and ' + message_for(2) : '')1853 return 'expected ' + exception + (negate ? ' not ' : '' ) +1854 ' to be thrown, but ' + (this.e ? 'got ' + puts(this.e) : 'nothing was')1855 }},1856 1857 have : function(actual, length, property) {1845- return actual[property].length == length1858+ return actual[property] == null ? false : actual[property].length == length1859 },1860 1861 have_at_least : function(actual, length, property) {1849- return actual[property].length >= length1862+ return actual[property] == null ? (length === 0) : actual[property].length >= length1863 },1864 1865 have_at_most :function(actual, length, property) {1853- return actual[property].length <= length1866+ return actual[property] == null || actual[property].length <= length1867 },1868 1869 have_within : function(actual, range, property) {1857- length = actual[property].length1870+ var length = actual[property] == undefined ? 0 : actual[property].length1871 return length >= range.shift() && length <= range.pop()1872 },1873 1874 have_prop : function(actual, property, value) {1862- return actual[property] == null || 1875+ return actual[property] === undefined ||1876 actual[property] instanceof Function ? false:1864- value == null ? true:1877+ value === undefined ? true:1878 does(actual[property], 'eql', value)1879 },1880 1881 have_property : function(actual, property, value) {1869- return actual[property] == null ||1882+ return actual[property] === undefined ||1883 actual[property] instanceof Function ? false:1871- value == null ? true:1884+ value === undefined ? true:1885 value === actual[property]1886 }1887 })1888 1 unmodified lineadded:spec/lib/jspec.nodejs.js+18 added
123456789101112131415161718
// JSpec - node - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
JSpec.include({ name: 'node', // --- Matchers matchers : { have_enumerable_property: 'actual.propertyIsEnumerable(expected)', have_writable_property: 'Object.getOwnPropertyDescriptor(actual, expected).writable === true', have_configurable_property: 'Object.getOwnPropertyDescriptor(actual, expected).configurable === true', have_keys: 'does(Object.keys(actual), "eql", expected)', have_prototype: 'Object.getPrototypeOf(actual) === expected' }})
modified:spec/lib/jspec.xhr.js+34 added-21 removed
1 2 // JSpec - XHR - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)3 4 (function(){5- 5+6 var lastRequest7- 7+8 // --- Original XMLHttpRequest9- 10- var OriginalXMLHttpRequest = 'XMLHttpRequest' in this ? 9+10+ var OriginalXMLHttpRequest = 'XMLHttpRequest' in this ?11 XMLHttpRequest :12 function(){}13 var OriginalActiveXObject = 'ActiveXObject' in this ?14 ActiveXObject :15 undefined16- 16+17 // --- MockXMLHttpRequest18 19 var MockXMLHttpRequest = function() {20 this.requestHeaders = {}21 }22- 22+23 MockXMLHttpRequest.prototype = {24 status: 0,25 async: true,26 readyState: 0,27+ responseXML: null,28 responseText: '',29 abort: function(){},30 onreadystatechange: function(){},31 3 unmodified lines35 36 getAllResponseHeaders : function(){37 return this.responseHeaders38 },38- 39+40 /**41 * Return case-insensitive value for header _name_.42 */43 44 getResponseHeader : function(name) {45 return this.responseHeaders[name.toLowerCase()]46 },46- 47+48 /**49 * Set case-insensitive _value_ for header _name_.50 */51 52 setRequestHeader : function(name, value) {53 this.requestHeaders[name.toLowerCase()] = value54 },54- 55+56 /**57 * Open mock request.58 */59 5 unmodified lines65 this.method = method.toUpperCase()66 if (async != undefined) this.async = async67 if (this.async) this.onreadystatechange()68 },68- 69+70 /**71 * Send request _data_.72 */73 3 unmodified lines77 this.readyState = 478 if (this.method == 'HEAD') this.responseText = null79 this.responseHeaders['content-length'] = (this.responseText || '').length80 if(this.async) this.onreadystatechange()81+ this.populateResponseXML()82 lastRequest = function(){83 return self84 }83- }85+ },86+87+ /**88+ * Parse request body and populate responseXML if response-type is xml89+ * Based on the standard specification : http://www.w3.org/TR/XMLHttpRequest/90+ */91+ populateResponseXML: function() {92+ var type = this.getResponseHeader("content-type")93+ if (!type || !this.responseText || !type.match(/(text\/xml|application\/xml|\+xml$)/g))94+ return95+ this.responseXML = JSpec.parseXML(this.responseText)96+ }97 }85- 98+99 // --- Response status codes87- 100+101 JSpec.statusCodes = {102 100: 'Continue',103 101: 'Switching Protocols',104 200: 'OK',35 unmodified lines140 503: 'Service Unavailable',141 504: 'Gateway Timeout',142 505: 'HTTP Version Not Supported'143 }131- 144+145 /**146 * Mock XMLHttpRequest requests.147 *148 * mockRequest().and_return('some data', 'text/plain', 200, { 'X-SomeHeader' : 'somevalue' })149 *150 * @return {hash}151 * @api public152 */140- 153+154 function mockRequest() {155 return { and_return : function(body, type, status, headers) {156 XMLHttpRequest = MockXMLHttpRequest157 ActiveXObject = false7 unmodified lines165 statusText: JSpec.statusCodes[status]166 })167 }}168 }156- 169+170 /**171 * Unmock XMLHttpRequest requests.172 *173 * @api public174 */162- 175+176 function unmockRequest() {177 XMLHttpRequest = OriginalXMLHttpRequest178 ActiveXObject = OriginalActiveXObject179 }167- 180+181 JSpec.include({182 name: 'Mock XHR',183 184 // --- Utilities7 unmodified lines192 193 afterSpec : function() {194 unmockRequest()195 },183- 196+197 // --- DSLs185- 198+199 DSLs : {200 snake : {201 mock_request: mockRequest,202 unmock_request: unmockRequest,1 unmodified line204 }205 }206 207 })195-})()208+})()