Skip to content

Commit

Permalink
feat(shared-behavior): exports shared behavior related functions (#34)
Browse files Browse the repository at this point in the history
* feat(shared-behavior): exports shared behavior related functions

Fixes #26
  • Loading branch information
stalniy committed Mar 11, 2018
1 parent 6ea52f4 commit 007a53f
Show file tree
Hide file tree
Showing 13 changed files with 293 additions and 113 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -12,4 +12,3 @@ before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
- sleep 3
- npm run lint
86 changes: 60 additions & 26 deletions getter.js
Expand Up @@ -353,8 +353,49 @@ Variable.EMPTY = new Variable(null, null);

var variable = Variable;

var SHARED_EXAMPLES = {};

function sharedExamplesFor(name, defs) {
if (SHARED_EXAMPLES[name]) {
throw new Error('Attempt to override "' + name + '" shared example');
}

SHARED_EXAMPLES[name] = defs;
}

function includeExamplesFor(name) {
if (!SHARED_EXAMPLES.hasOwnProperty(name)) {
throw new Error('Attempt to include not defined shared behavior "' + name + '"');
}

for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}

SHARED_EXAMPLES[name].apply(null, args);
}

function itBehavesLike(name) {
for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
args[_key2 - 1] = arguments[_key2];
}

commonjsGlobal.describe('behaves like ' + name, function () {
includeExamplesFor.apply(undefined, [name].concat(args));
});
}

var shared_behavior = {
sharedExamplesFor: sharedExamplesFor,
includeExamplesFor: includeExamplesFor,
itBehavesLike: itBehavesLike
};

var _interface$2 = createCommonjsModule(function (module) {
var Metadata = metadata.Metadata;
var sharedExamplesFor = shared_behavior.sharedExamplesFor,
includeExamplesFor = shared_behavior.includeExamplesFor,
itBehavesLike = shared_behavior.itBehavesLike;


module.exports = function (context, tracker, options) {
Expand Down Expand Up @@ -418,7 +459,14 @@ var _interface$2 = createCommonjsModule(function (module) {
}
}

return { subject: subject, def: def, get: get$$1 };
return {
subject: subject,
def: def,
get: get$$1,
sharedExamplesFor: sharedExamplesFor,
includeExamplesFor: includeExamplesFor,
itBehavesLike: itBehavesLike
};
};
});

Expand Down Expand Up @@ -571,7 +619,7 @@ function addInterface(rootSuite, options) {
context.fdescribe = tracker.wrapSuite(context.fdescribe);
commonjsGlobal.afterEach(tracker.cleanUpCurrentContext);

return context;
return ui;
}

var jasmine = {
Expand All @@ -580,13 +628,7 @@ var jasmine = {
Tracker: suite_tracker
}, options);

var context = addInterface(commonjsGlobal.jasmine.getEnv().topSuite(), config);

return {
get: context.get,
def: context.def,
subject: context.subject
};
return addInterface(commonjsGlobal.jasmine.getEnv().topSuite(), config);
}
};

Expand Down Expand Up @@ -636,23 +678,15 @@ var mocha$1 = {
return addInterface$1(rootSuite, config);
};

return Object.defineProperties(mocha.interfaces[name], {
get: {
get: function get$$1() {
return commonjsGlobal.get;
}
},
def: {
get: function get$$1() {
return commonjsGlobal.def;
}
},
subject: {
get: function get$$1() {
return commonjsGlobal.subject;
}
}
});
var getters = ['get', 'def', 'subject', 'sharedExamplesFor', 'includeExamplesFor', 'itBehavesLike'];
var defs = getters.reduce(function (all, uiName) {
all[uiName] = { get: function get$$1() {
return commonjsGlobal[uiName];
} };
return all;
}, {});

return Object.defineProperties(mocha.interfaces[name], defs);
}
};

Expand Down
86 changes: 60 additions & 26 deletions global.js
Expand Up @@ -353,8 +353,49 @@ Variable.EMPTY = new Variable(null, null);

var variable = Variable;

var SHARED_EXAMPLES = {};

function sharedExamplesFor(name, defs) {
if (SHARED_EXAMPLES[name]) {
throw new Error('Attempt to override "' + name + '" shared example');
}

SHARED_EXAMPLES[name] = defs;
}

function includeExamplesFor(name) {
if (!SHARED_EXAMPLES.hasOwnProperty(name)) {
throw new Error('Attempt to include not defined shared behavior "' + name + '"');
}

for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}

SHARED_EXAMPLES[name].apply(null, args);
}

function itBehavesLike(name) {
for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
args[_key2 - 1] = arguments[_key2];
}

commonjsGlobal.describe('behaves like ' + name, function () {
includeExamplesFor.apply(undefined, [name].concat(args));
});
}

var shared_behavior = {
sharedExamplesFor: sharedExamplesFor,
includeExamplesFor: includeExamplesFor,
itBehavesLike: itBehavesLike
};

var _interface$2 = createCommonjsModule(function (module) {
var Metadata = metadata.Metadata;
var sharedExamplesFor = shared_behavior.sharedExamplesFor,
includeExamplesFor = shared_behavior.includeExamplesFor,
itBehavesLike = shared_behavior.itBehavesLike;


module.exports = function (context, tracker, options) {
Expand Down Expand Up @@ -418,7 +459,14 @@ var _interface$2 = createCommonjsModule(function (module) {
}
}

return { subject: subject, def: def, get: get$$1 };
return {
subject: subject,
def: def,
get: get$$1,
sharedExamplesFor: sharedExamplesFor,
includeExamplesFor: includeExamplesFor,
itBehavesLike: itBehavesLike
};
};
});

Expand Down Expand Up @@ -571,7 +619,7 @@ function addInterface(rootSuite, options) {
context.fdescribe = tracker.wrapSuite(context.fdescribe);
commonjsGlobal.afterEach(tracker.cleanUpCurrentContext);

return context;
return ui;
}

var jasmine = {
Expand All @@ -580,13 +628,7 @@ var jasmine = {
Tracker: suite_tracker
}, options);

var context = addInterface(commonjsGlobal.jasmine.getEnv().topSuite(), config);

return {
get: context.get,
def: context.def,
subject: context.subject
};
return addInterface(commonjsGlobal.jasmine.getEnv().topSuite(), config);
}
};

Expand Down Expand Up @@ -636,23 +678,15 @@ var mocha$1 = {
return addInterface$1(rootSuite, config);
};

return Object.defineProperties(mocha.interfaces[name], {
get: {
get: function get$$1() {
return commonjsGlobal.get;
}
},
def: {
get: function get$$1() {
return commonjsGlobal.def;
}
},
subject: {
get: function get$$1() {
return commonjsGlobal.subject;
}
}
});
var getters = ['get', 'def', 'subject', 'sharedExamplesFor', 'includeExamplesFor', 'itBehavesLike'];
var defs = getters.reduce(function (all, uiName) {
all[uiName] = { get: function get$$1() {
return commonjsGlobal[uiName];
} };
return all;
}, {});

return Object.defineProperties(mocha.interfaces[name], defs);
}
};

Expand Down
86 changes: 60 additions & 26 deletions index.js
Expand Up @@ -353,8 +353,49 @@ Variable.EMPTY = new Variable(null, null);

var variable = Variable;

var SHARED_EXAMPLES = {};

function sharedExamplesFor(name, defs) {
if (SHARED_EXAMPLES[name]) {
throw new Error('Attempt to override "' + name + '" shared example');
}

SHARED_EXAMPLES[name] = defs;
}

function includeExamplesFor(name) {
if (!SHARED_EXAMPLES.hasOwnProperty(name)) {
throw new Error('Attempt to include not defined shared behavior "' + name + '"');
}

for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}

SHARED_EXAMPLES[name].apply(null, args);
}

function itBehavesLike(name) {
for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
args[_key2 - 1] = arguments[_key2];
}

commonjsGlobal.describe('behaves like ' + name, function () {
includeExamplesFor.apply(undefined, [name].concat(args));
});
}

var shared_behavior = {
sharedExamplesFor: sharedExamplesFor,
includeExamplesFor: includeExamplesFor,
itBehavesLike: itBehavesLike
};

var _interface$2 = createCommonjsModule(function (module) {
var Metadata = metadata.Metadata;
var sharedExamplesFor = shared_behavior.sharedExamplesFor,
includeExamplesFor = shared_behavior.includeExamplesFor,
itBehavesLike = shared_behavior.itBehavesLike;


module.exports = function (context, tracker, options) {
Expand Down Expand Up @@ -418,7 +459,14 @@ var _interface$2 = createCommonjsModule(function (module) {
}
}

return { subject: subject, def: def, get: get$$1 };
return {
subject: subject,
def: def,
get: get$$1,
sharedExamplesFor: sharedExamplesFor,
includeExamplesFor: includeExamplesFor,
itBehavesLike: itBehavesLike
};
};
});

Expand Down Expand Up @@ -571,7 +619,7 @@ function addInterface(rootSuite, options) {
context.fdescribe = tracker.wrapSuite(context.fdescribe);
commonjsGlobal.afterEach(tracker.cleanUpCurrentContext);

return context;
return ui;
}

var jasmine = {
Expand All @@ -580,13 +628,7 @@ var jasmine = {
Tracker: suite_tracker
}, options);

var context = addInterface(commonjsGlobal.jasmine.getEnv().topSuite(), config);

return {
get: context.get,
def: context.def,
subject: context.subject
};
return addInterface(commonjsGlobal.jasmine.getEnv().topSuite(), config);
}
};

Expand Down Expand Up @@ -636,23 +678,15 @@ var mocha$1 = {
return addInterface$1(rootSuite, config);
};

return Object.defineProperties(mocha.interfaces[name], {
get: {
get: function get$$1() {
return commonjsGlobal.get;
}
},
def: {
get: function get$$1() {
return commonjsGlobal.def;
}
},
subject: {
get: function get$$1() {
return commonjsGlobal.subject;
}
}
});
var getters = ['get', 'def', 'subject', 'sharedExamplesFor', 'includeExamplesFor', 'itBehavesLike'];
var defs = getters.reduce(function (all, uiName) {
all[uiName] = { get: function get$$1() {
return commonjsGlobal[uiName];
} };
return all;
}, {});

return Object.defineProperties(mocha.interfaces[name], defs);
}
};

Expand Down
10 changes: 9 additions & 1 deletion lib/interface.js
@@ -1,5 +1,6 @@
const { Metadata } = require('./metadata');
const Variable = require('./variable');
const { sharedExamplesFor, includeExamplesFor, itBehavesLike } = require('./shared_behavior');

module.exports = (context, tracker, options) => {
const get = varName => Variable.evaluate(varName, { in: tracker.currentContext });
Expand Down Expand Up @@ -45,5 +46,12 @@ module.exports = (context, tracker, options) => {
}
}

return { subject, def, get };
return {
subject,
def,
get,
sharedExamplesFor,
includeExamplesFor,
itBehavesLike
};
};

0 comments on commit 007a53f

Please sign in to comment.