Skip to content
Myr

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

4 files+117 added−66 removed

modified:spec/lib/jspec.jquery.js+10 added-3 removed
modified:spec/lib/jspec.js+55 added-42 removed
2 unmodified lines
34;(function(){56 JSpec = {7 version : '4.0.0',7 version : '4.2.1',8 assert : true,9 cache : {},10 suites : [],11 modules : [],
247 unmodified lines
259 })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 lines
893 * @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 lines
959 * @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 lines
1075 }1076 },10771078 /**1079 * Parse an XML String and return a 'document'.1080 *1081 * @param {string} text1082 * @return {document}1083 * @api public1084 */10851086 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 },10971098 /**1099 * Escape HTML.1100 *1101 * @param {string} html1102 * @return {string}
48 unmodified lines
1151 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.result
109 unmodified lines
1269 * @api public1270 */1271 1272 destub : function(object, method) {1273 var captures1274 if (method) {1275 if (object['__prototype__' + method])1276 delete object[method]1277 else
356 unmodified lines
1634 })1635 },16361637 /**1612 * Throw a JSpec related error.1613 *1614 * @param {string} message1615 * @param {Exception} e1616 * @api public1617 */16181619 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} data
145 unmodified lines
1787 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 lines
1800 return !actual.length1801 },18021803 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 lines
1848 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 line
added:spec/lib/jspec.nodejs.js+18 added
modified:spec/lib/jspec.xhr.js+34 added-21 removed
12// JSpec - XHR - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)34(function(){5 56 var lastRequest7 78 // --- Original XMLHttpRequest9 10 var OriginalXMLHttpRequest = 'XMLHttpRequest' in this ? 910 var OriginalXMLHttpRequest = 'XMLHttpRequest' in this ?11 XMLHttpRequest :12 function(){}13 var OriginalActiveXObject = 'ActiveXObject' in this ?14 ActiveXObject :15 undefined16 1617 // --- MockXMLHttpRequest1819 var MockXMLHttpRequest = function() {20 this.requestHeaders = {}21 }22 2223 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 lines
3536 getAllResponseHeaders : function(){37 return this.responseHeaders38 },38 3940 /**41 * Return case-insensitive value for header _name_.42 */4344 getResponseHeader : function(name) {45 return this.responseHeaders[name.toLowerCase()]46 },46 4748 /**49 * Set case-insensitive _value_ for header _name_.50 */5152 setRequestHeader : function(name, value) {53 this.requestHeaders[name.toLowerCase()] = value54 },54 5556 /**57 * Open mock request.58 */59
5 unmodified lines
65 this.method = method.toUpperCase()66 if (async != undefined) this.async = async67 if (this.async) this.onreadystatechange()68 },68 6970 /**71 * Send request _data_.72 */73
3 unmodified lines
77 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 },8687 /**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 9899 // --- Response status codes87 100101 JSpec.statusCodes = {102 100: 'Continue',103 101: 'Switching Protocols',104 200: 'OK',
35 unmodified lines
140 503: 'Service Unavailable',141 504: 'Gateway Timeout',142 505: 'HTTP Version Not Supported'143 }131 144145 /**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 153154 function mockRequest() {155 return { and_return : function(body, type, status, headers) {156 XMLHttpRequest = MockXMLHttpRequest157 ActiveXObject = false
7 unmodified lines
165 statusText: JSpec.statusCodes[status]166 })167 }}168 }156 169170 /**171 * Unmock XMLHttpRequest requests.172 *173 * @api public174 */162 175176 function unmockRequest() {177 XMLHttpRequest = OriginalXMLHttpRequest178 ActiveXObject = OriginalActiveXObject179 }167 180181 JSpec.include({182 name: 'Mock XHR',183184 // --- Utilities
7 unmodified lines
192193 afterSpec : function() {194 unmockRequest()195 },183 196197 // --- DSLs185 198199 DSLs : {200 snake : {201 mock_request: mockRequest,202 unmock_request: unmockRequest,
1 unmodified line
204 }205 }206207 })195})()208})()