Skip to content

Commit

Permalink
Fix/parent access (#33)
Browse files Browse the repository at this point in the history
* fix(variable): fixes access to parent variable inside child override

Relates to #30

* chore(rollup): reuses builtin rollup options instead of env vars

* fix(variable): fixes access to parent variable inside child override declaration

Also improve performance by decreasing amount of `afterEach` callbacks. Currently there is a single `afterEach` on root level instead of a single `afterEach` for each describe/context

Fixes #30

* fix(variable): fixes parent variable caching

During evaluation of redefined variable parent variable currently will not be cached

Fixes #29

* chore(eslint): removes unused variable

* refactor(metadata): removes dependency on suite
  • Loading branch information
stalniy committed Mar 6, 2018
1 parent 75dbc57 commit 6ea52f4
Show file tree
Hide file tree
Showing 11 changed files with 302 additions and 284 deletions.
141 changes: 69 additions & 72 deletions getter.js
Expand Up @@ -129,14 +129,13 @@ var toConsumableArray = function (arr) {
};

var LAZY_VARS_FIELD = symbol.for('__lazyVars');
var noop = function noop() {};

var VariableMetadata = function () {
function VariableMetadata(name, definition) {
function VariableMetadata(name, definition, metadata) {
classCallCheck(this, VariableMetadata);

this.value = definition;
this.parent = null;
this.parent = metadata;
this.names = defineProperty({}, name, true);
}

Expand All @@ -154,9 +153,7 @@ var VariableMetadata = function () {
}, {
key: 'evaluate',
value: function evaluate() {
var value = this.value;

return typeof value === 'function' ? value() : value;
return typeof this.value === 'function' ? this.value() : this.value;
}
}]);
return VariableMetadata;
Expand Down Expand Up @@ -184,7 +181,7 @@ var Metadata = function () {
value: function setVirtual(context, metadata) {
var virtualMetadata = Object.create(metadata);

virtualMetadata.releaseVars = noop;
virtualMetadata.values = {};
context[LAZY_VARS_FIELD] = virtualMetadata;
}
}]);
Expand All @@ -201,17 +198,18 @@ var Metadata = function () {
createClass(Metadata, [{
key: 'getVar',
value: function getVar(name) {
if (!this.values.hasOwnProperty(name)) {
if (!this.values.hasOwnProperty(name) && this.defs[name]) {
this.hasValues = true;
if (this.defs[name] instanceof VariableMetadata) {
this.values[name] = this.defs[name].evaluate();
} else {
this.values[name] = undefined;
}
this.values[name] = this.evaluate(name);
}

return this.values[name];
}
}, {
key: 'evaluate',
value: function evaluate(name) {
return this.defs[name].evaluate();
}
}, {
key: 'addChild',
value: function addChild(child) {
Expand All @@ -226,7 +224,7 @@ var Metadata = function () {
}

this.defined = true;
this.defs[name] = new VariableMetadata(name, definition);
this.defs[name] = new VariableMetadata(name, definition, this);

return this;
}
Expand All @@ -243,6 +241,18 @@ var Metadata = function () {
this.hasValues = false;
}
}
}, {
key: 'lookupMetadataFor',
value: function lookupMetadataFor(varName) {
var varMeta = this.defs[varName];
var definedIn = varMeta.parent;

if (!varMeta || !definedIn.parent.defs[varName]) {
throw new Error('Unknown parent variable "' + varName + '".');
}

return definedIn.parent;
}
}]);
return Metadata;
}();
Expand All @@ -268,6 +278,10 @@ var Variable = function () {
}, {
key: 'evaluate',
value: function evaluate(varName, options) {
if (!options.in) {
throw new Error('It looke like you are trying to evaluate "' + varName + '" too early');
}

var variable = Variable.fromStack(options.in);

if (variable.isSame(varName)) {
Expand All @@ -278,7 +292,7 @@ var Variable = function () {
variable = Variable.allocate(varName, options);
return variable.value();
} finally {
variable.release();
variable.pullFromStack();
}
}
}, {
Expand All @@ -293,7 +307,7 @@ var Variable = function () {

this.name = varName;
this.context = context;
this.meta = context ? Metadata$1.of(context) : null;
this.evaluationMeta = context ? Metadata$1.of(context) : null;
}

createClass(Variable, [{
Expand All @@ -304,8 +318,7 @@ var Variable = function () {
}, {
key: 'value',
value: function value() {
// console.log('<-------', this.context.result.description)
return Metadata$1.of(this.context).getVar(this.name);
return this.evaluationMeta.getVar(this.name);
}
}, {
key: 'addToStack',
Expand All @@ -316,33 +329,22 @@ var Variable = function () {
return this;
}
}, {
key: 'release',
value: function release() {
key: 'pullFromStack',
value: function pullFromStack() {
this.context[CURRENTLY_RETRIEVED_VAR_FIELD].pop();
}
}, {
key: 'valueInParentContext',
value: function valueInParentContext(varOrAliasName) {
var prevMeta = this.meta;
var meta = this.evaluationMeta;

try {
this.meta = this.getParentMetadataFor(varOrAliasName);
return this.meta.getVar(varOrAliasName);
this.evaluationMeta = meta.lookupMetadataFor(varOrAliasName);
return this.evaluationMeta.evaluate(varOrAliasName);
} finally {
this.meta = prevMeta;
this.evaluationMeta = meta;
}
}
}, {
key: 'getParentMetadataFor',
value: function getParentMetadataFor(varName) {
var metadata$$1 = this.meta;

if (!metadata$$1 || !metadata$$1.defs[varName]) {
throw new Error('Unknown parent variable "' + varName + '".');
}

return metadata$$1.parent;
}
}]);
return Variable;
}();
Expand Down Expand Up @@ -428,25 +430,13 @@ var SuiteTracker = function () {
classCallCheck(this, SuiteTracker);

this.state = { currentlyDefinedSuite: config.rootSuite };
this.suiteTracker = config.suiteTracker || this.createSuiteTracker();
this.suiteTracker = config.suiteTracker;
this.suites = [];
this.cleanUpCurrentContext = this.cleanUpCurrentContext.bind(this);
this.cleanUpCurrentAndRestorePrevContext = this.cleanUpCurrentAndRestorePrevContext.bind(this);
}

createClass(SuiteTracker, [{
key: 'createSuiteTracker',
value: function createSuiteTracker() {
return {
before: function before(tracker, suite) {
suite.beforeAll(tracker.registerSuite);
},
after: function after(tracker, suite) {
suite.beforeAll(tracker.cleanUp);
suite.afterEach(tracker.cleanUp);
suite.afterAll(tracker.cleanUpAndRestorePrev);
}
};
}
}, {
key: 'wrapSuite',
value: function wrapSuite(describe) {
var tracker = this;
Expand Down Expand Up @@ -482,24 +472,13 @@ var SuiteTracker = function () {
}, {
key: 'execute',
value: function execute(defineTests, suite, args) {
var tracker = this.buildRuntimeTrackerFor(suite);

this.suiteTracker.before(tracker, suite);
this.suiteTracker.before(this, suite);
defineTests.apply(suite, args);

if (Metadata$2.of(suite)) {
this.suiteTracker.after(tracker, suite);
this.suiteTracker.after(this, suite);
}
}
}, {
key: 'buildRuntimeTrackerFor',
value: function buildRuntimeTrackerFor(suite) {
return {
registerSuite: this.registerSuite.bind(this, suite),
cleanUp: this.cleanUp.bind(this, suite),
cleanUpAndRestorePrev: this.cleanUpAndRestorePrev.bind(this, suite)
};
}
}, {
key: 'isRoot',
value: function isRoot(suite) {
Expand Down Expand Up @@ -543,10 +522,15 @@ var SuiteTracker = function () {
}
}
}, {
key: 'cleanUpAndRestorePrev',
value: function cleanUpAndRestorePrev(context) {
key: 'cleanUpCurrentContext',
value: function cleanUpCurrentContext() {
this.cleanUp(this.currentContext);
}
}, {
key: 'cleanUpCurrentAndRestorePrevContext',
value: function cleanUpCurrentAndRestorePrevContext() {
this.cleanUpCurrentContext();
this.state.currentTestContext = this.state.prevTestContext;
return this.cleanUp(context);
}
}, {
key: 'currentContext',
Expand All @@ -566,13 +550,12 @@ var suite_tracker = SuiteTracker;

function createSuiteTracker() {
return {
before: function before(tracker) {
commonjsGlobal.beforeAll(tracker.registerSuite);
commonjsGlobal.afterEach(tracker.cleanUp);
commonjsGlobal.afterAll(tracker.cleanUpAndRestorePrev);
before: function before(tracker, suite) {
commonjsGlobal.beforeAll(tracker.registerSuite.bind(tracker, suite));
commonjsGlobal.afterAll(tracker.cleanUpCurrentAndRestorePrevContext);
},
after: function after(tracker) {
commonjsGlobal.beforeAll(tracker.cleanUp);
commonjsGlobal.beforeAll(tracker.cleanUpCurrentContext);
}
};
}
Expand All @@ -586,6 +569,7 @@ function addInterface(rootSuite, options) {
context.describe = tracker.wrapSuite(context.describe);
context.xdescribe = tracker.wrapSuite(context.xdescribe);
context.fdescribe = tracker.wrapSuite(context.fdescribe);
commonjsGlobal.afterEach(tracker.cleanUpCurrentContext);

return context;
}
Expand All @@ -611,9 +595,22 @@ var jest = jasmine;
// eslint-disable-line


function createSuiteTracker$1() {
return {
before: function before(tracker, suite) {
suite.beforeAll(tracker.registerSuite.bind(tracker, suite));
},
after: function after(tracker, suite) {
suite.beforeAll(tracker.cleanUpCurrentContext);
suite.afterAll(tracker.cleanUpCurrentAndRestorePrevContext);
}
};
}

function addInterface$1(rootSuite, options) {
var tracker = new options.Tracker({ rootSuite: rootSuite });
var tracker = new options.Tracker({ rootSuite: rootSuite, suiteTracker: createSuiteTracker$1() });

rootSuite.afterEach(tracker.cleanUpCurrentContext);
rootSuite.on('pre-require', function (context) {
var ui = _interface$2(context, tracker, options);
var describe = context.describe;
Expand Down

0 comments on commit 6ea52f4

Please sign in to comment.