1
0
Fork 0

Merge pull request #916 from triAGENS/js-cleanup

Cleanup of some JavaScript modules
This commit is contained in:
Lucas Dohmen 2014-06-20 16:59:40 +02:00
commit 151f9ef6c7
7 changed files with 101 additions and 199 deletions

View File

@ -14,7 +14,7 @@ var _ = require("underscore");
// Helper function to correctly set up the prototype chain, for subclasses.
// Similar to `goog.inherits`, but uses a hash of prototype properties and
// class properties to be extended.
exports.extend = function(protoProps, staticProps) {
exports.extend = function (protoProps, staticProps) {
var parent = this;
var child;
@ -24,7 +24,7 @@ exports.extend = function(protoProps, staticProps) {
if (protoProps && _.has(protoProps, 'constructor')) {
child = protoProps.constructor;
} else {
child = function(){ return parent.apply(this, arguments); };
child = function () { return parent.apply(this, arguments); };
}
// Add static properties to the constructor function, if supplied.
@ -32,13 +32,15 @@ exports.extend = function(protoProps, staticProps) {
// Set the prototype chain to inherit from `parent`, without calling
// `parent`'s constructor function.
var Surrogate = function(){ this.constructor = child; };
var Surrogate = function () { this.constructor = child; };
Surrogate.prototype = parent.prototype;
child.prototype = new Surrogate;
child.prototype = new Surrogate();
// Add prototype properties (instance properties) to the subclass,
// if supplied.
if (protoProps) _.extend(child.prototype, protoProps);
if (protoProps) {
_.extend(child.prototype, protoProps);
}
// Set a convenience property in case the parent's prototype is needed
// later.

View File

@ -1,181 +0,0 @@
/*jslint indent: 2, nomen: true, maxlen: 100, white: true, plusplus: true, eqeq: true */
/*global require, exports, assertTrue */
////////////////////////////////////////////////////////////////////////////////
/// @brief Mini Stub and Mock Framework
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2010-2012 triagens GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is triAGENS GmbH, Cologne, Germany
///
/// @author Lucas Dohmen
/// @author Copyright 2011-2012, triAGENS GmbH, Cologne, Germany
var stub,
allow,
expect,
FunctionStub,
FunctionMock,
mockConstructor,
_ = require("underscore");
// Sorry for Yak Shaving. But I can't take it anymore.
// x = stub();
stub = function () {
'use strict';
return function() {};
};
// allow(x)
// .toReceive("functionName")
// .andReturn({ x: 1 })
FunctionStub = function(obj) {
'use strict';
this.obj = obj;
};
_.extend(FunctionStub.prototype, {
toReceive: function (functionName) {
'use strict';
this.functionName = functionName;
this.buildFunctionStub();
return this;
},
andReturn: function (returnValue) {
'use strict';
this.returnValue = returnValue;
this.buildFunctionStub();
return this;
},
buildFunctionStub: function () {
'use strict';
var returnValue = this.returnValue;
this.obj[this.functionName] = function () {
return returnValue;
};
}
});
allow = function(obj) {
'use strict';
return (new FunctionStub(obj));
};
// expect(x)
// .toReceive("functionName")
// .withArguments(5)
// .andReturn({ x: 1 })
//
// ...
//
// x.assertIsSatisfied();
FunctionMock = function(obj) {
'use strict';
this.obj = obj;
this.obj.satisfied = false;
this.obj.assertIsSatisfied = function () {
assertTrue(this.satisfied, "Mock expectation was not satisfied");
};
};
_.extend(FunctionMock.prototype, {
toReceive: function (functionName) {
'use strict';
this.functionName = functionName;
this.buildFunctionMock();
return this;
},
andReturn: function (returnValue) {
'use strict';
this.returnValue = returnValue;
this.buildFunctionMock();
return this;
},
withArguments: function () {
'use strict';
this.expectedArguments = arguments;
this.buildFunctionMock();
return this;
},
buildFunctionMock: function () {
'use strict';
var returnValue = this.returnValue,
expectedArguments = this.expectedArguments,
obj = this.obj;
this.obj[this.functionName] = function () {
if ((expectedArguments === undefined) || (_.isEqual(arguments, expectedArguments))) {
obj.satisfied = true;
}
return returnValue;
};
}
});
expect = function(obj) {
'use strict';
return (new FunctionMock(obj));
};
/* Create a Mock Constructor
*
* Give the arguments you expect to mockConstructor:
* It checks, if it was called with new and the
* correct arguments.
*
* MyProto = mockConstructor(test);
*
* a = new MyProto(test);
*
* MyProto.assertIsSatisfied();
*/
mockConstructor = function () {
'use strict';
var expectedArguments = arguments,
satisfied = false,
MockConstructor = function () {
if (this.constructor === MockConstructor) {
// Was called as a constructor
satisfied = _.isEqual(arguments, expectedArguments);
}
};
MockConstructor.assertIsSatisfied = function () {
assertTrue(satisfied);
};
return MockConstructor;
};
exports.stub = stub;
exports.allow = allow;
exports.expect = expect;
exports.mockConstructor = mockConstructor;

View File

@ -1,4 +1,4 @@
/*jslint indent: 2, nomen: true, maxlen: 120, regexp: true, todo: true */
/*jslint indent: 2, nomen: true, maxlen: 120, regexp: true */
/*global module, require, exports, print */
var runTest = require('jsunity').runTest,
@ -19,8 +19,7 @@ runJSUnityTests = function (tests) {
_.each(tests, function (file) {
if (result) {
print("\nRunning JSUnity test from file '" + file + "'");
}
else {
} else {
print("\nSkipping JSUnity test from file '" + file + "' due to previous errors");
}

View File

@ -31,7 +31,7 @@
var Model,
_ = require("underscore"),
is = require("org/arangodb/is"),
backbone_helpers = require("backbone"),
extend = require('org/arangodb/extend').extend,
metadataKeys = ['_id', '_key', '_rev'],
parseAttributes,
parseRequiredAttributes;
@ -285,7 +285,7 @@ _.extend(Model.prototype, {
/// the second object those to be defined on the prototype.
////////////////////////////////////////////////////////////////////////////////
Model.extend = backbone_helpers.extend;
Model.extend = extend;
exports.Model = Model;

View File

@ -30,7 +30,7 @@
var Repository,
_ = require("underscore"),
backbone_helpers = require("backbone");
extend = require('org/arangodb/extend').extend;
////////////////////////////////////////////////////////////////////////////////
/// @startDocuBlock JSF_foxx_repository_initializer
@ -412,7 +412,7 @@ _.extend(Repository.prototype, {
}
});
Repository.extend = backbone_helpers.extend;
Repository.extend = extend;
exports.Repository = Repository;

View File

@ -1,15 +1,97 @@
require("internal").flushModuleCache();
// Stubbing and Mocking
var stub,
allow,
FunctionStub,
mockConstructor,
_ = require("underscore");
// Sorry for Yak Shaving. But I can't take it anymore.
// x = stub();
stub = function () {
'use strict';
return function() {};
};
// allow(x)
// .toReceive("functionName")
// .andReturn({ x: 1 })
FunctionStub = function(obj) {
'use strict';
this.obj = obj;
};
_.extend(FunctionStub.prototype, {
toReceive: function (functionName) {
'use strict';
this.functionName = functionName;
this.buildFunctionStub();
return this;
},
andReturn: function (returnValue) {
'use strict';
this.returnValue = returnValue;
this.buildFunctionStub();
return this;
},
buildFunctionStub: function () {
'use strict';
var returnValue = this.returnValue;
this.obj[this.functionName] = function () {
return returnValue;
};
}
});
allow = function(obj) {
'use strict';
return (new FunctionStub(obj));
};
/* Create a Mock Constructor
*
* Give the arguments you expect to mockConstructor:
* It checks, if it was called with new and the
* correct arguments.
*
* MyProto = mockConstructor(test);
*
* a = new MyProto(test);
*
* MyProto.assertIsSatisfied();
*/
mockConstructor = function () {
'use strict';
var expectedArguments = arguments,
satisfied = false,
MockConstructor = function () {
if (this.constructor === MockConstructor) {
// Was called as a constructor
satisfied = _.isEqual(arguments, expectedArguments);
}
};
MockConstructor.assertIsSatisfied = function () {
assertTrue(satisfied);
};
return MockConstructor;
};
var jsunity = require("jsunity"),
FoxxController = require("org/arangodb/foxx").Controller,
db = require("org/arangodb").db,
_ = require("underscore"),
fakeContext,
fakeContextWithRootElement,
stub_and_mock = require("org/arangodb/stub_and_mock"),
stub = stub_and_mock.stub,
allow = stub_and_mock.allow,
mockConstructor = stub_and_mock.mockConstructor;
fakeContextWithRootElement;
fakeContext = {
prefix: "",