Sha256: c39991abffaf086c8a8efe558661a249b61467bc7facea24fc5f2add2dd0ef28

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

// ==========================================================================
// Project:   CoreTest Unit Testing Library
// Copyright: ©2010 Apple Inc. All rights reserved.
// License:   Licened under MIT license (see license.js)
// ==========================================================================

var Ct    = require('./core'),
    utils = require('./utils'),
    trimmer = (/^test\s*/) ;

require('./system/plan'); // add Ct.Plan
    
/** 
  @file 
  
  Implements the CommonJS API to run a suite of tests.  This looks on the 
  passed object hash and builds a "plan" which can be run to the standard 
  logger.  Looks for a property called "logger" to select the logger.  
  
  Otherwise will use the default logger defined on CoreTest.
*/

// generates a test.  Assume sync unless returns a promise (that implements
// 'then'() in which case add a listener)
function testFor(func) {
  return function(t, done) {
    var ret = func(t);
    if (ret && ('function' === typeof ret.then)) {
      ret.then(function() { done(); }, function(e) { done(e); });
    } else done();
  };
}

// iterates through a single hash and adds any tests. recursively called for
// any nested subgroups.  subgroups are treated as modules
function addTests(mod, tests) {
  
  var key, value ;
  for(key in tests) {

    // limit processing to spec...
    if ((key==='test') || (key.indexOf('test')!==0)) continue;
    
    value = tests[key];
    switch(utils.typeOf(value)) {
      case utils.T_HASH:
        addTests(mod.module(key), value);
        break;
        
      case utils.T_FUNCTION:
        mod.test(key, testFor(value));
        break;
        
      default:
        throw utils.fmt("test '%@' must be a function or hash (was %@)", key, utils.typeOf(value));
    }
  }
}

exports.run = function(tests) {
  
  var plan = new Ct.Plan(tests.name || 'unnamed plan');
  if (tests.logger) plan.logger(tests.logger);
 
  // add tests to a default module
  addTests(plan.module(), tests);
  
  Ct.run(plan);
};

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spade-0.0.1 packages/core-test/lib/test.js