run-suite.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * Module dependencies
  3. */
  4. var util = require('util');
  5. var _ = require('@sailshq/lodash');
  6. var getDisplayType = require('../../lib/get-display-type');
  7. module.exports = function runSuite( testSuite, runTestFn ){
  8. // Run each test.
  9. _.each(testSuite, function (test){
  10. describeAndExecuteTest(test, runTestFn);
  11. });
  12. };
  13. /**
  14. * Describe a mocha test based on the provided definition.
  15. *
  16. * @param {[type]} test [description]
  17. * @return {[type]} [description]
  18. */
  19. function describeAndExecuteTest(test, runTestFn){
  20. if (test.strictEq && test.isNew) {
  21. throw new Error('INVALID TEST: `isNew` and `strictEq` are mutually exclusive opposites- cannot use them together. For reference, this is test:\n'+util.inspect(test, false, null));
  22. }
  23. var actualDisplayName = getDisplayVal(test.actual);
  24. describe((function _determineDescribeMsg(){
  25. var msg = '';
  26. if (test._meta) {
  27. msg += '['+test._meta+']';
  28. }
  29. else {
  30. msg += ' ';
  31. }
  32. if (!_.isUndefined(test.example)) {
  33. msg += 'with a '+getDisplayType(test.example)+' example ('+getDisplayVal(test.example)+')';
  34. }
  35. else if (!_.isUndefined(test.typeclass)) {
  36. msg += 'with typeclass===`'+test.typeclass+'`';
  37. }
  38. else {
  39. msg +='with example===`undefined`';
  40. }
  41. return msg;
  42. })(), function suite (){
  43. if (test.error) {
  44. it(util.format('should error when %s is provided', actualDisplayName), function (done){
  45. runTestFn(test, done);
  46. });
  47. return;
  48. }
  49. var itMsg = 'should ';
  50. if (test.strictEq) {
  51. itMsg+='maintain strict equality (===) when ' + actualDisplayName + ' is provided';
  52. }
  53. else if (test.isNew) {
  54. if (test.hasOwnProperty('result')) {
  55. itMsg+='convert ' + actualDisplayName + ' into a new (!== original) value '+util.inspect(test.result, false, null);
  56. }
  57. else {
  58. itMsg+='take ' + actualDisplayName + ' and yield a copy (which !== original)';
  59. }
  60. }
  61. else {
  62. itMsg+='convert ' + actualDisplayName + ' into '+getDisplayVal(test.result) + ' (a '+getDisplayType(test.result)+')';
  63. }
  64. it(itMsg, function (done){
  65. runTestFn(test, done);
  66. });
  67. });
  68. }
  69. function getDisplayVal(v){
  70. if (_.isDate(v)) {
  71. return 'a Date';
  72. }
  73. if (_.isFunction(v)) {
  74. return v.toString();
  75. }
  76. if (_.isError(v)) {
  77. return 'an Error';
  78. }
  79. if (_.isRegExp(v)) {
  80. return 'a RegExp';
  81. }
  82. if (!_.isPlainObject(v) && !_.isArray(v)) {
  83. return getDisplayType(v);
  84. }
  85. return util.inspect(v,false,null);
  86. }