@carbon-io/test-tube
Version:
Test framework for Carbon.io
169 lines (157 loc) • 6.63 kB
JavaScript
var _ = require('lodash')
var oo = require('@carbon-io/atom').oo(module)
/***************************************************************************************************
* @class TestContext
*/
var TestContext = oo({
_ctorName: 'TestContext',
/*****************************************************************************
* @constructs TestContext
* @description A class used to maintain state across tests in the test-suite.
* An instance of this class will be created for each top-level
* call to run a test-suite and can be used to hold user state
* (e.g., fixtures generated in {@link test-tube.Test.setup}) in
* addition to maintaining internal state to test-tube itself.
* @memberof test-tube
*/
_C: function() {
/***************************************************************************
* @property {} __testtube -- xxx
* @private
*/
this.__testtube = undefined
},
/*****************************************************************************
* @typedef InternalTestContext
* @description An object used by {@link test-tube.TestContext} to track state
* internally
* @type {Object}
* @property {Object} options --
* @property {Object} global
* @description An object that can be accessed by all tests in the test-suite
* to store or pass state during a test-suite run. When a property is
* set on this object, it will remain until the test-suite completes,
* unless it is deleted during the course of test-suite execution.
* This is accessed by tests using the {@link
* test-tube.TestContext.global} property.
* @property {Object} local
* @description An object that can be accessed by all tests in the test-suite
* to store or pass state during a single test run. If the test
* that is being executed contains sub-tests, this object will be
* stashed before running those tests and restored after they
* complete, restricting the accessibilty of this object's contents
* to a single test. This can be accessed by tests using the
* {@link test-tube.TestContext.local} property.
* @property {Object[]} localStateStack
* @description An array used to manage individual {@link
* typedef:test-tube.TestContext.InternalTestContext.local} objects
* between individual test executions. This is not accessible to
* individual tests.
* @property {test-tube.HttpTestHistory} httpHistory
* @description The current http history object used to track http requests
* and responses sent and received during the execution of an
* {@link test-tube.HttpTest} suite. This can be accessed by tests
* using the {@link test-tube.TestContext.httpHistory} property.
* @property {string} path
* @description The depth-first "path" for the test currently being executed.
* This is generated by joining the names of all tests seen in
* the traversal using the "/" character. This is not accessible
* to individual tests.
*/
/*****************************************************************************
* @method _init
* @description Initialize the context object
* @returns {undefined}
*/
_init: function() {
this.__testtube = {
options: {}, // XXX: this should be more rigorously defined
global: {},
local: undefined,
localStateStack: [],
httpHistory: undefined,
path: '/'
}
},
/*****************************************************************************
* @property {Object} global -- A free form object used to store state across
* all tests executing as part of a test-suite
*/
global: {
$property: {
get: function() {
return this.__testtube.global
},
set: function(state) {
this.__testtube.state = global
}
}
},
/*****************************************************************************
* @property {Object} local -- A free form object used to store state during
* the execution of a single test in the test-suite.
* This state will be stashed during the execution
* of any sub-tests and restored upon their
* completion.
*/
local: {
$property: {
get: function() {
return this.__testtube.local
},
set: function(local) {
this.__testtube.local = local
}
}
},
/*****************************************************************************
* @property {test-tube.HttpTestHistory} httpHistory
* @description The current HTTP request and response history. This can be used
* by sub-tests of an {@link test-tube.HttpTest}
*/
httpHistory: {
$property: {
get: function() {
return this.__testtube.httpHistory
},
set: function(httpHistory) {
this.__testtube.httpHistory = httpHistory
}
}
},
/*****************************************************************************
* @property {Object} _tt
*/
_tt: {
$property: {
get: function() {
return this.__testtube
}
}
},
/*****************************************************************************
* @method stash
* @description Stash the current value (if it exists) of {@link
* test-tube.TestContext.local} on the stack and set the new value
* of {@link test-tube.TestContext.local}
* @param {Object} [state={}]
* @description The new value of {@link test-tube.TestContext.local}
* @returns {Object} -- The new value of {@link test-tube.TestContext.local}
*/
stash: function(state) {
if (!_.isUndefined(this.local)) {
this.__testtube.localStateStack.push(this.local)
}
return this.local = state ? state : {}
},
/*****************************************************************************
* @method restore
* @description Sets {@link test-tube.TestContext.local} to the last value
* that was stashed and removes it from the stack
* @returns {Object} -- The new value of {@link test-tube.TestContext.local
*/
restore: function() {
return this.local = this.__testtube.localStateStack.pop()
}
})
module.exports = TestContext