Updated to JSpec 4.0.0
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent 1c0e2ce · Browse files · 3 files·+213 added−109 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+213 added−109 removed
- tests:+213 added−109 removed
Provenance
- Commit
ce4fe24- Tree
eae03e3- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
1c0e2ce
3 files changed+213 added−109 removed
modified:spec/lib/jspec.jquery.js+5 added-4 removed
23 unmodified lines24252627282829303132323334343535363738394032 unmodified lines23 unmodified lines // --- Matchers matchers : { have_tag : "jQuery(expected, actual).length == 1", have_tag : "jQuery(expected, actual).length === 1", have_one : "alias have_tag", have_tags : "jQuery(expected, actual).length > 1", have_many : "alias have_tags", have_any : "alias have_tags", have_child : "jQuery(actual).children(expected).length == 1", have_child : "jQuery(actual).children(expected).length === 1", have_children : "jQuery(actual).children(expected).length > 1", have_text : "jQuery(actual).text() == expected", have_value : "jQuery(actual).val() == expected", 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_visible : function(actual) {32 unmodified linesmodified:spec/lib/jspec.js+206 added-105 removed
2 unmodified lines3 4 ;(function(){5 6 JSpec = {7- version : '3.2.1',7+ version : '4.0.0',8 assert : true,9 cache : {},10 suites : [],11 modules : [],12 allSuites : [],13+ sharedBehaviors: [],14 matchers : {},15 stubbed : [],16 options : {},17 request : 'XMLHttpRequest' in this ? XMLHttpRequest : null,50 unmodified lines68 if (JSpec.cache[path]) return JSpec.cache[path]69 return JSpec.cache[path] = 70 JSpec.tryLoading(JSpec.options.fixturePath + '/' + path) ||71 JSpec.tryLoading(JSpec.options.fixturePath + '/' + path + '.html')72+ },73+ 74+ /**75+ * Load json fixture at _path_.76+ *77+ * JSON fixtures are resolved as:78+ *79+ * - <path>80+ * - <path>.json81+ *82+ * @param {string} path83+ * @return {object}84+ * @api public85+ */86+ 87+ json_fixture: function(path) {88+ if (!JSpec.cache['json:' + path])89+ JSpec.cache['json:' + path] =90+ JSpec.tryLoading(JSpec.options.fixturePath + '/' + path) ||91+ JSpec.tryLoading(JSpec.options.fixturePath + '/' + path + '.json')92+ try {93+ return eval('(' + JSpec.cache['json:' + path] + ')')94+ } catch (e) {95+ throw 'json_fixture("' + path + '"): ' + e96+ }97 }98 },99 100 // --- Objects16 unmodified lines117 JSpec.post(uri, {118 stats: JSpec.stats,119 options: options,120 results: map(results.allSuites, function(suite) {95- if (suite.hasSpecs())121+ if (suite.isExecutable())122 return {123 description: suite.description,124 specs: map(suite.specs, function(spec) {125 return {25 unmodified lines151 * @api public152 */153 154 DOM : function(results, options) {129- var id = option('reportToId') || 'jspec'130- var report = document.getElementById(id)131- var failuresOnly = option('failuresOnly')132- var classes = results.stats.failures ? 'has-failures' : ''155+ var id = option('reportToId') || 'jspec',156+ report = document.getElementById(id),157+ failuresOnly = option('failuresOnly'),158+ classes = results.stats.failures ? 'has-failures' : ''159 if (!report) throw 'JSpec requires the element #' + id + ' to output its reports'160 161 function bodyContents(body) {162 return JSpec.7 unmodified lines170 <span class="failures">Failures: <em>' + results.stats.failures + '</em></span> \171 <span class="passes">Duration: <em>' + results.duration + '</em> ms</span> \172 </div><table class="suites">' + map(results.allSuites, function(suite) {173 var displaySuite = failuresOnly ? suite.ran && !suite.passed() : suite.ran148- if (displaySuite && suite.hasSpecs())174+ if (displaySuite && suite.isExecutable())175 return '<tr class="description"><td colspan="2">' + escape(suite.description) + '</td></tr>' +176 map(suite.specs, function(i, spec) {177 return '<tr class="' + (i % 2 ? 'odd' : 'even') + '">' +178 (spec.requiresImplementation() ?16 unmodified lines195 * @api public196 */197 198 Terminal : function(results, options) {173- failuresOnly = option('failuresOnly')199+ var failuresOnly = option('failuresOnly')200 print(color("\n Passes: ", 'bold') + color(results.stats.passes, 'green') + 201 color(" Failures: ", 'bold') + color(results.stats.failures, 'red') +202 color(" Duration: ", 'bold') + color(results.duration, 'green') + " ms \n")203 2 unmodified lines206 }207 208 each(results.allSuites, function(suite) {209 var displaySuite = failuresOnly ? suite.ran && !suite.passed() : suite.ran184- if (displaySuite && suite.hasSpecs()) {210+ if (displaySuite && suite.isExecutable()) {211 print(color(' ' + suite.description, 'bold'))212 each(suite.specs, function(spec){213 var assertionsGraph = inject(spec.assertions, '', function(graph, assertion){214 return graph + color('.', assertion.passed ? 'green' : 'red')10 unmodified lines225 }226 })227 228 quit(results.stats.failures)203- },204-205- /**206- * Console reporter.207- *208- * @api public209- */210-211- Console : function(results, options) {212- console.log('')213- console.log('Passes: ' + results.stats.passes + ' Failures: ' + results.stats.failures)214- each(results.allSuites, function(suite) {215- if (suite.ran) {216- console.group(suite.description)217- each(suite.specs, function(spec){218- var assertionCount = spec.assertions.length + ':'219- if (spec.requiresImplementation())220- console.warn(spec.description)221- else if (spec.passed())222- console.log(assertionCount + ' ' + spec.description)223- else 224- console.error(assertionCount + ' ' + spec.description + ', ' + spec.failure().message)225- })226- console.groupEnd()227- }228- })229- }229+ }230 },231 232 Assertion : function(matcher, actual, expected, negate) {233 extend(this, {183 unmodified lines417 * @param {function} body418 * @api private419 */420 421- Suite : function(description, body) {421+ Suite : function(description, body, isShared) {422 var self = this423 extend(this, {424 body: body,425 description: description,426 suites: [],427+ sharedBehaviors: [],428 specs: [],429 ran: false,429- hooks: { 'before' : [], 'after' : [], 'before_each' : [], 'after_each' : [] },430+ shared: isShared, 431+ hooks: { 'before' : [], 'after' : [], 432+ 'before_each' : [], 'after_each' : [],433+ 'before_nested' : [], 'after_nested' : []},434 431- // Add a spec to the suite435+ // Add a spec to the suite436 437 addSpec : function(description, body) {438 var spec = new JSpec.Spec(description, body)439 this.specs.push(spec)440 JSpec.stats.specs++ // TODO: abstract441 spec.suite = this442 },443 440- // Add a hook to the suite444+ // Add a before hook to the suite445 446+ addBefore : function(options, body) {447+ body.options = options || {}448+ this.befores.push(body)449+ },450+451+ // Add an after hook to the suite452+453+ addAfter : function(options, body) {454+ body.options = options || {}455+ this.afters.unshift(body)456+ },457+458+ // Add a hook to the suite459+ 460 addHook : function(hook, body) {461 this.hooks[hook].push(body)462 },463 464 // Add a nested suite465 448- addSuite : function(description, body) {449- var suite = new JSpec.Suite(description, body)466+ addSuite : function(description, body, isShared) {467+ var suite = new JSpec.Suite(description, body, isShared)468 JSpec.allSuites.push(suite)469 suite.name = suite.description470 suite.description = this.description + ' ' + suite.description471 this.suites.push(suite)472 suite.suite = this473 },474 457- // Invoke a hook in context to this suite475+ // Invoke a hook in context to this suite476 477 hook : function(hook) {478+ if (hook != 'before' && hook != 'after') 460- if (this.suite) this.suite.hook(hook)479+ if (this.suite) this.suite.hook(hook)480+481 each(this.hooks[hook], function(body) {482 JSpec.evalBody(body, "Error in hook '" + hook + "', suite '" + self.description + "': ")483 })484 },465-485+ 486 // Check if nested suites are present487 488 hasSuites : function() {489 return this.suites.length 10 unmodified lines500 passed : function() {501 return !any(this.specs, function(spec){502 return !spec.passed() 503 })484- }504+ },505+506+ isShared : function(){507+ return this.shared508+ },509+510+ isExecutable : function() {511+ return !this.isShared() && this.hasSpecs()512+ }513 })514 },515 516 /**139 unmodified lines656 return JSpec.expect(actual)657 },658 659 describe : function(description, body) {632- return JSpec.currentSuite.addSuite(description, body)660+ return JSpec.currentSuite.addSuite(description, body, false)661 },662 663 it : function(description, body) {664 return JSpec.currentSuite.addSpec(description, body)1 unmodified line666 667 before : function(body) {668 return JSpec.currentSuite.addHook('before', body)669 },642-670+ 671 after : function(body) {672 return JSpec.currentSuite.addHook('after', body)673 },646-674+ 675 before_each : function(body) {676 return JSpec.currentSuite.addHook('before_each', body)677 },650-678+ 679 after_each : function(body) {680 return JSpec.currentSuite.addHook('after_each', body)681 },682+683+ before_nested : function(body) {684+ return JSpec.currentSuite.addHook('before_nested', body)685+ },686+ 687+ after_nested : function(body){688+ return JSpec.currentSuite.addhook('after_nested', body)689+ },690 691+ shared_behaviors_for : function(description, body){692+ return JSpec.currentSuite.addSuite(description, body, true)693+ },694+695 should_behave_like : function(description) {696 return JSpec.shareBehaviorsOf(description)697 }698 }68 unmodified lines767 */768 769 evalHook : function(module, name, args) {770 hook('evaluatingHookBody', module, name)731- try { return module[name].apply(module, args) }732- catch(e) { error('Error in hook ' + module.name + '.' + name + ': ', e) }771+ return module[name].apply(module, args)772 },773 774 /**775 * Same as hook() however accepts only one _arg_ which is14 unmodified lines790 })791 },792 793 /**755- * Find a suite by its description or name.794+ * Find a shared example suite by its description or name.795+ * First searches parent tree of suites for shared behavior796+ * before falling back to global scoped nested behaviors.797 *798 * @param {string} description799 * @return {Suite}800 * @api private801 */802 762- findSuite : function(description) {763- return find(this.allSuites, function(suite){764- return suite.name == description || suite.description == description765- })803+ findSharedBehavior : function(description) {804+ var behavior805+ return (behavior = JSpec.findLocalSharedBehavior(description))806+ ? behavior807+ : JSpec.findGlobalSharedBehavior(description)808 },809+810+ /**811+ * Find a shared example suite within the current suite's812+ * parent tree by its description or name.813+ *814+ * @param {string} description815+ * @return {Suite}816+ * @api private817+ */818+ 819+ findLocalSharedBehavior : function(description) {820+ var behavior,821+ currentSuite = JSpec.currentSuite.suite822+ while (currentSuite)823+ if (behavior = find(currentSuite.suites, JSpec.suiteDescriptionPredicate(description)))824+ return behavior825+ else826+ currentSuite = currentSuite.suite827+ },828+ 829+ /**830+ * Find a shared example suite within the global831+ * scope by its description or name.832+ *833+ * @param {string} description834+ * @return {Suite}835+ * @api private836+ */837+ 838+ findGlobalSharedBehavior : function(description) {839+ return find(JSpec.suites, JSpec.suiteDescriptionPredicate(description))840+ },841 842 /**843+ * Build a predicate that will match a suite based on name or description844+ *845+ * @param {string} description846+ * @return {function}847+ * @api private848+ */849+ 850+ suiteDescriptionPredicate : function(description) {851+ return function(suite){852+ return suite.name === description ||853+ suite.description === description854+ }855+ },856+857+ /**858 * Share behaviors (specs) of the given suite with859 * the current suite.860 *861 * @param {string} description862 * @api public863 */864 865 shareBehaviorsOf : function(description) {777- if (suite = this.findSuite(description)) this.copySpecs(suite, this.currentSuite)778- else throw 'failed to share behaviors. ' + puts(description) + ' is not a valid Suite name'866+ var suite = JSpec.findSharedBehavior(description)867+ if (suite)868+ JSpec.evalBody(suite.body)869+ else870+ throw new Error("failed to find shared behaviors named `" + description + "'")871 },872 781- /**782- * Copy specs from one suite to another. 783- *784- * @param {Suite} fromSuite785- * @param {Suite} toSuite786- * @api public787- */873 789- copySpecs : function(fromSuite, toSuite) {790- each(fromSuite.specs, function(spec){791- spec.assertions = []792- toSuite.specs.push(spec)793- })794- },795- 874 /**875 * Convert arguments to an array.876 *877 * @param {object} arguments500 unmodified lines1378 * @api public1379 */1380 1381 describe : function(description, body) {1304- var suite = new JSpec.Suite(description, body)1382+ var suite = new JSpec.Suite(description, body, false)1383 hook('addingSuite', suite)1384 this.allSuites.push(suite)1385 this.suites.push(suite)1386 },1387 1388 /**1389+ * Add a shared example suite to JSpec.1390+ *1391+ * @param {string} description1392+ * @param {body} function1393+ * @api public1394+ */1395+ 1396+ shared_behaviors_for : function(description, body) {1397+ var suite = new JSpec.Suite(description, body, true)1398+ hook('addingSuite', suite)1399+ this.allSuites.push(suite)1400+ this.suites.push(suite)1401+ },1402+1403+ /**1404 * Return the contents of a function body.1405 *1406 * @param {function} body1407 * @return {string}17 unmodified lines1425 var dsl = this.DSL || this.DSLs.snake1426 var matchers = this.matchers1427 var context = this.context || this.defaultContext1428 var contents = this.contentsOf(body)1336- hook('evaluatingBody', dsl, matchers, context, contents)1337- try { with (dsl){ with (context) { with (matchers) { eval(contents) }}} }1338- catch(e) { error(errorMessage, e) }1429+ hook('evaluatingBody', dsl, matchers, context, contents)1430+ with (dsl){ with (context) { with (matchers) { eval(contents) }}}1431 },1432 1433 /**1434 * Pre-process a string of JSpec.11 unmodified lines1446 replace(/\r\n|\n|\r/g, '\n').1447 split('__END__')[0].1448 replace(/([\w\.]+)\.(stub|destub)\((.*?)\)$/gm, '$2($1, $3)').1449 replace(/describe\s+(.*?)$/gm, 'describe($1, function(){').1450+ replace(/shared_behaviors_for\s+(.*?)$/gm, 'shared_behaviors_for($1, function(){').1451 replace(/^\s+it\s+(.*?)$/gm, ' it($1, function(){').1359- replace(/^ *(before_each|after_each|before|after)(?= |\n|$)/gm, 'JSpec.currentSuite.addHook("$1", function(){').1452+ replace(/^ *(before_nested|after_nested|before_each|after_each|before|after)(?= |\n|$)/gm, 'JSpec.currentSuite.addHook("$1", function(){').1453 replace(/^\s*end(?=\s|$)/gm, '});').1454 replace(/-\{/g, 'function(){').1455 replace(/(\d+)\.\.(\d+)/g, function(_, a, b){ return range(a, b) }).1456 replace(/\.should([_\.]not)?[_\.](\w+)(?: |;|$)(.*)$/gm, '.should$1_$2($3)').54 unmodified lines1511 * @api public1512 */1513 1514 runSuite : function(suite) {1515+ if (!suite.isShared())1516+ {1422- this.currentSuite = suite1423- this.evalBody(suite.body)1424- suite.ran = true1425- hook('beforeSuite', suite), suite.hook('before')1426- each(suite.specs, function(spec) {1427- hook('beforeSpec', spec)1428- suite.hook('before_each')1429- JSpec.runSpec(spec)1430- hook('afterSpec', spec)1431- suite.hook('after_each')1432- })1433- if (suite.hasSuites()) {1434- each(suite.suites, function(suite) {1435- JSpec.runSuite(suite)1436- })1437- }1438- hook('afterSuite', suite), suite.hook('after')1439- this.stats.suitesFinished++1440- },1517+ this.currentSuite = suite1518+ this.evalBody(suite.body)1519+ suite.ran = true1520+ hook('beforeSuite', suite), suite.hook('before'), suite.hook('before_nested')1521+ each(suite.specs, function(spec) {1522+ hook('beforeSpec', spec)1523+ suite.hook('before_each')1524+ JSpec.runSpec(spec)1525+ hook('afterSpec', spec)1526+ suite.hook('after_each')1527+ })1528+ if (suite.hasSuites()) {1529+ each(suite.suites, function(suite) {1530+ JSpec.runSuite(suite)1531+ })1532+ }1533+ hook('afterSuite', suite), suite.hook('after_nested'), suite.hook('after')1534+ this.stats.suitesFinished++1535+ } 1536+ },1537 1538 /**1539 * Report a failure for the current spec.1540 *75 unmodified lines1616 * @api public1617 */1618 1619 error : function(message, e) {1524- if ('stack' in e)1525- require('sys').puts(e.stack + '\n')1620 throw (message ? message : '') + e.toString() + 1621 (e.line ? ' near line ' + e.line : '')1622 },1623 91 unmodified lines1715 request.status.toString().charAt(0) == 2)) 1716 return request.responseText1717 }1718 else1625- error("failed to load `" + file + "'")1719+ throw new Error("failed to load `" + file + "'")1720 },1721 1722 /**1723 * Load, pre-process, and evaluate a file.8 unmodified lines1732 eval('with (JSpec){' + this.preprocess(this.load(file)) + '}')1733 return this1734 }1735 }1642-1736+ 1737+ // --- Node.js support1738+ 1739+ if (typeof GLOBAL === 'object' && typeof exports === 'object')1740+ quit = process.exit,1741+ print = require('sys').puts,1742+ readFile = require('fs').readFileSync1743+ 1744 // --- Utility functions1745 1645- var main = this1646- var find = JSpec.any1647- var utils = 'haveStopped stub hookImmutable hook destub map any last pass fail range each option inject select \1746+ var main = this,1747+ find = JSpec.any,1748+ utils = 'haveStopped stub hookImmutable hook destub map any last pass fail range each option inject select \1749 error escape extend puts query strip color does addMatchers callIterator toArray equal'.split(/\s+/)1750 while (utils.length) eval('var ' + utils[0] + ' = JSpec.' + utils.shift())1751 if (!main.setTimeout) main.setTimeout = function(callback){ callback() }1752 119 unmodified lines1872 value === actual[property]1873 }1874 })1875 1775-})()1876+})()modified:spec/lib/jspec.xhr.js+2 added
1 unmodified line2 // JSpec - XHR - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)3 4 (function(){5 6+ var lastRequest7+ 8 // --- Original XMLHttpRequest9 10 var OriginalXMLHttpRequest = 'XMLHttpRequest' in this ? 11 XMLHttpRequest :184 unmodified lines