Переглянути джерело

REFS DEVOPS-269 Added v2.5 compatibility

David Aebersold 12 роки тому
батько
коміт
9af03795aa

+ 27 - 21
lib/pipeline/expressions/SubtractExpression.js

@@ -2,37 +2,43 @@
 
 /** 
  * A $subtract pipeline expression.
- * @see evaluate 
+ * @see evaluateInternal
  * @class SubtractExpression
  * @namespace mungedb-aggregate.pipeline.expressions
  * @module mungedb-aggregate
  * @constructor
  **/
-var SubtractExpression = module.exports = function SubtractExpression(){
-	if (arguments.length !== 0) throw new Error("zero args expected");
-	base.call(this);
-}, klass = SubtractExpression, base = require("./NaryExpression"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
+var SubtractExpression = module.exports = function SubtractExpression() {
+		this.fixedAirty(2);
+		if (arguments.length !== 0) throw new Error("zero args expected");
+		base.call(this);
+}, klass = SubtractExpression,
+		base = require("./NaryExpression"),
+		proto = klass.prototype = Object.create(base.prototype, {
+				constructor: {
+						value: klass
+				}
+		});
 
 // DEPENDENCIES
-var Value = require("../Value");
+var Value = require("../Value"),
+		Expression = require("./Expression");
 
 // PROTOTYPE MEMBERS
-proto.getOpName = function getOpName(){
-	return "$subtract";
-};
-
-proto.addOperand = function addOperand(expr) {
-	this.checkArgLimit(2);
-	base.prototype.addOperand.call(this, expr);
+proto.getOpName = function getOpName() {
+		return "$subtract";
 };
 
 /** 
-* Takes an array that contains a pair of numbers and subtracts the second from the first, returning their difference. 
-**/
-proto.evaluate = function evaluate(doc) {
-	this.checkArgCount(2);
-	var left = this.operands[0].evaluate(doc),
-		right = this.operands[1].evaluate(doc);
-	if (left instanceof Date || right instanceof Date) throw new Error("$subtract does not support dates; code 16376");
-	return left - right;
+ * Takes an array that contains a pair of numbers and subtracts the second from the first, returning their difference.
+ **/
+proto.evaluateInternal = function evaluateInternal(doc) {
+		this.checkArgCount(2);
+		var left = this.operands[0].evaluateInternal(doc),
+				right = this.operands[1].evaluateInternal(doc);
+		if (left instanceof Date || right instanceof Date) throw new Error("$subtract does not support dates; code 16376");
+		return left - right;
 };
+
+/** Register Expression */
+Expression.registerExpression("$subtract", SubtractExpression.parse);

+ 26 - 29
test/lib/pipeline/expressions/SubtractExpression.js

@@ -1,48 +1,45 @@
 "use strict";
 var assert = require("assert"),
-	SubtractExpression = require("../../../../lib/pipeline/expressions/SubtractExpression"),
-	Expression = require("../../../../lib/pipeline/expressions/Expression");
+		SubtractExpression = require("../../../../lib/pipeline/expressions/SubtractExpression"),
+		Expression = require("../../../../lib/pipeline/expressions/Expression");
 
 
 module.exports = {
 
-	"SubtractExpression": {
+		"SubtractExpression": {
 
-		"constructor()": {
+				"constructor()": {
 
-			"should not throw Error when constructing without args": function testConstructor(){
-				assert.doesNotThrow(function(){
-					new SubtractExpression();
-				});
-			}
+						"should not throw Error when constructing without args": function testConstructor() {
+								assert.doesNotThrow(function() {
+										new SubtractExpression();
+								});
+						}
 
-		},
+				},
 
-		"#getOpName()": {
+				"#getOpName()": {
 
-			"should return the correct op name; $subtract": function testOpName(){
-				assert.equal(new SubtractExpression().getOpName(), "$subtract");
-			}
+						"should return the correct op name; $subtract": function testOpName() {
+								assert.equal(new SubtractExpression().getOpName(), "$subtract");
+						}
 
-		},
+				},
 
-		"#getFactory()": {
+				"#evaluateInternal()": {
 
-			"should return the constructor for this class": function factoryIsConstructor(){
-				assert.strictEqual(new SubtractExpression().getFactory(), undefined);
-			}
+						"should return the result of subtraction between two numbers": function testStuff() {
+								assert.strictEqual(Expression.parseOperand({
+										$subtract: ["$a", "$b"]
+								}).evaluateInternal({
+										a: 35636364,
+										b: -0.5656
+								}), 35636364 - (-0.5656));
+						}
+				}
 
-		},
-
-		"#evaluate()": {
-
-			"should return the result of subtraction between two numbers": function testStuff(){
-				assert.strictEqual(Expression.parseOperand({$subtract:["$a", "$b"]}).evaluate({a:35636364, b:-0.5656}), 35636364-(-0.5656));
-			}
 		}
 
-	}
-
 };
 
-if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);
+if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);