Browse Source

EAGLESIX-2353 added to NaryExpression a new utility function that ensures the number of operands falls within an allowable range

Tony Ennis 11 years ago
parent
commit
d41e397659

+ 12 - 0
lib/pipeline/expressions/NaryExpression.js

@@ -136,3 +136,15 @@ proto.checkArgLimit = function checkArgLimit(maxArgs) {
 proto.checkArgCount = function checkArgCount(reqArgs) {
 	if (this.operands.length !== reqArgs) throw new Error(this.getOpName() + ":  insufficient operands; " + reqArgs + " required, only got " + this.operands.length + "; code 15997");
 };
+
+/**
+ * Checks the current size of vpOperand; if the size does not fall within the min and max values (inclusive), fires a user assertion indicating that this must have exactly reqArgs arguments.
+ * This is meant to be used in evaluate(), *before* the evaluation takes place.
+ *
+ * @method checkArgCountRange
+ * @param min the minimum number of valid arguments
+ * @param max the maximum number of valid arguments
+ **/
+proto.checkArgCountRange = function checkArgCountRange(min, max) {
+	if (this.operands.length < min || this.operands.length > max) throw new Error(this.getOpName() + ":  insufficient operands; between " + min + " and " + max + " required, only got " + this.operands.length + "; code 15996");
+};

+ 49 - 0
test/lib/pipeline/expressions/NaryExpression.js

@@ -123,6 +123,55 @@ module.exports = {
 				testableExpr.checkArgCount(3);
 			});
 		},
+
+		"#checkArgCountRange() sans operands": {
+			"should fail with Error if there are no arguments": function(){
+				var testableExpr = new TestableExpression();
+				assert.throws(function() {
+					testableExpr.checkArgCountRange(2, 4);
+				});
+			}
+		},
+
+		"#checkArgCountRange()": {
+			before: function() {
+				this.testableExpr = new TestableExpression();
+				this.testableExpr.addOperand(new ConstantExpression("uno"));
+				this.testableExpr.addOperand(new ConstantExpression("dos"));
+				this.testableExpr.addOperand(new ConstantExpression("tres"));
+			},
+
+			"should throw Error if the number of arguments is too low": function () {
+				var t = this.testableExpr;
+				assert.throws(function() {
+					t.checkArgCountRange(4, 6);
+				});
+			},
+			"should throw Error if the number of arguments is too high": function () {
+				var t = this.testableExpr;
+				assert.throws(function() {
+					t.checkArgCountRange(1, 2);
+				});
+			},
+			"should accept if the number of arguments equals the minimum": function () {
+				var t = this.testableExpr;
+				assert.doesNotThrow(function() {
+					t.checkArgCountRange(3, 5);
+				});
+			},
+			"should accept if the number of arguments equals the maximum": function () {
+				var t = this.testableExpr;
+				assert.doesNotThrow(function() {
+					t.checkArgCountRange(1, 3);
+				});
+			},
+			"should accept if the number of arguments falls within the range": function () {
+				var t = this.testableExpr;
+				assert.doesNotThrow(function() {
+					t.checkArgCountRange(2, 4);
+				});
+			}
+		},
 		
 		//the following test case is eagerly awaiting ObjectExpression
 		"#addDependencies()": function testDependencies(){