Pārlūkot izejas kodu

Merge pull request #43 from RiveraGroup/feature/mongo_2.6.5_matcher_AllElemMatchOp

Feature/mongo 2.6.5 matcher all elem match op
Chris Sexton 11 gadi atpakaļ
vecāks
revīzija
b8e0ea3a17

+ 28 - 56
lib/pipeline/matcher/AllElemMatchOp.js

@@ -2,8 +2,7 @@
 
 var MatchExpression = require('./MatchExpression');
 
-
-// Autogenerated by cport.py on 2013-09-17 14:37
+// From expression_array.h
 var AllElemMatchOp = module.exports = function AllElemMatchOp(){
 	base.call(this);
 	this._matchType = 'ALL';
@@ -11,26 +10,15 @@ var AllElemMatchOp = module.exports = function AllElemMatchOp(){
 	this._list = [];
 }, klass = AllElemMatchOp, base =  MatchExpression , proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
 
-// DEPENDENCIES
 var errors = require("../../Errors.js"),
 	ErrorCodes = errors.ErrorCodes,
 	ElementPath = require('./ElementPath.js');
 
-// File: expression_array.h lines: 175-175
-//         ElementPath _elementPath;
-
+// ElementPath _elementPath
 proto._elementPath = undefined;
-
-
-// File: expression_array.h lines: 176-176
-//         std::vector< const ArrayMatchingMatchExpression* > _list;
-
+// std::vector< MatchExpression* > _list;
 proto._list = undefined;
-
-
-// File: expression_array.h lines: 174-174
-//         StringData _path;
-
+// StringData _path;
 proto._path = undefined;
 
 /**
@@ -40,18 +28,16 @@ proto._path = undefined;
  * @param anArray
  *
  */
-proto._allMatch = function _allMatch( anArray ){ //  const BSONObj& anArray
-	// File: expression_array.cpp lines: 208-215
-	if(this._list.length === 0) { return false; }
+proto._allMatch = function _allMatch(anArray) {
+	if (this._list.length === 0) return false;
 
 	for (var i = 0; i < this._list.length; i++) {
-		if( ! this._list[i].matchesArray( anArray, null ) ) { return false; }
+		if (!this._list[i].matchesArray(anArray, null)) return false;
 	}
 
 	return true;
 };
 
-
 /**
  *
  * This method adds a new expression to the internal array of expression
@@ -59,13 +45,11 @@ proto._allMatch = function _allMatch( anArray ){ //  const BSONObj& anArray
  * @param expr
  *
  */
-proto.add = function add( expr ){//  const ArrayMatchingMatchExpression* expr
-	// File: expression_array.cpp lines: 184-186
+proto.add = function add(expr) {
 	if (!expr) throw new Error("AllElemMatchOp:add#68 failed to verify expr");
 	this._list.push(expr);
 };
 
-
 /**
  *
  * Writes a debug string for this object
@@ -73,15 +57,13 @@ proto.add = function add( expr ){//  const ArrayMatchingMatchExpression* expr
  * @param level
  *
  */
-proto.debugString = function debugString( level ){ //   StringBuilder& debug, int level
-	// File: expression_array.cpp lines: 219-224
+proto.debugString = function debugString(level) {
 	console.debug(this._debugAddSpace(level) + this._path + " AllElemMatchOp: " + this._path + '\n');
 	for (var i = 0; i < this._list.length; i++) {
 		this._list[i].debugString(level +1);
 	}
 };
 
-
 /**
  *
  * checks if this expression is == to the other
@@ -89,29 +71,27 @@ proto.debugString = function debugString( level ){ //   StringBuilder& debug, in
  * @param other
  *
  */
-proto.equivalent = function equivalent( other ){//  const MatchExpression* other
-// File: expression_array.cpp lines: 227-242
+proto.equivalent = function equivalent(other) {
 	if (this.matchType() != other.matchType()) {
 		return false;
 	}
 
-	if( this._path != other._path ) {
+	if (this._path != other._path) {
 		return false;
 	}
 
-	if( this._list.length != other._list.length ) {
+	if (this._list.length != other._list.length) {
 		return false;
 	}
+
 	for (var i = 0; i < this._list.length; i++) {
-		if ( !this._list[i].equivalent( other._list[i] ) ) {
+		if (!this._list[i].equivalent(other._list[i])) {
 			return false;
 		}
 	}
 	return true;
 };
 
-
-
 /**
  *
  * gets the specified item from the list
@@ -119,12 +99,10 @@ proto.equivalent = function equivalent( other ){//  const MatchExpression* other
  * @param i
  *
  */
-proto.getChild = function getChild( i ){ //  size_t i
-// File: expression_array.h lines: 167-166
+proto.getChild = function getChild(i) {
 	return this._list[i];
 };
 
-
 /**
  *
  * Initialize the necessary items
@@ -132,11 +110,10 @@ proto.getChild = function getChild( i ){ //  size_t i
  * @param path
  *
  */
-proto.init = function init( path ){ //  const StringData& path
-// File: expression_array.cpp lines: 177-181
+proto.init = function init(path) {
 	this._path = path;
-	var s = this._elementPath.init( this._path );
-	this._elementPath.setTraverseLeafArray( false );
+	var s = this._elementPath.init(this._path);
+	this._elementPath.setTraverseLeafArray(false);
 	return s;
 };
 
@@ -148,17 +125,18 @@ proto.init = function init( path ){ //  const StringData& path
  * @param details
  *
  */
-proto.matches = function matches(doc, details){
-	// File: expression_array.cpp lines: 189-198
+proto.matches = function matches(doc, details) {
 	var self = this,
 		checker = function(element) {
-			if (!(element instanceof Array))
+			if (!(element instanceof Array)) {
 				return false;
+			}
 
 			//var amIRoot = (element.length === 0);
 
-			if (self._allMatch(element))
+			if (self._allMatch(element)) {
 				return true;
+			}
 
 			/*
 			if (!amIRoot && details && details.needRecord() {
@@ -177,15 +155,13 @@ proto.matches = function matches(doc, details){
  * @param e
  *
  */
-proto.matchesSingleElement = function matchesSingleElement( e ){ //  const BSONElement& e
-	// File: expression_array.cpp lines: 201-205
+proto.matchesSingleElement = function matchesSingleElement(e) {
 	if (!(e instanceof Array)) {
 		return false;
 	}
 	return this._allMatch(e);
 };
 
-
 /**
  *
  * return the length of the internal array
@@ -193,8 +169,7 @@ proto.matchesSingleElement = function matchesSingleElement( e ){ //  const BSONE
  * @param
  *
  */
-proto.numChildren = function numChildren( /*  */ ){
-// File: expression_array.h lines: 166-165
+proto.numChildren = function numChildren() {
 	return this._list.length;
 };
 
@@ -206,8 +181,7 @@ proto.numChildren = function numChildren( /*  */ ){
  * @param
  *
  */
-proto.path = function path( /*  */ ){
-// File: expression_array.h lines: 169-168
+proto.path = function path() {
 	return this._path;
 };
 
@@ -219,11 +193,9 @@ proto.path = function path( /*  */ ){
  * @param
  *
  */
-proto.shallowClone = function shallowClone( /*  */ ){
-// File: expression_array.h lines: 145-152
+proto.shallowClone = function shallowClone() {
 	var e = new AllElemMatchOp();
-	e.init( this._path );
+	e.init(this._path);
 	e._list = this._list.slice(0);
 	return e;
 };
-

+ 0 - 131
test/lib/pipeline/matcher/AllElemMatchOp.js

@@ -1,131 +0,0 @@
-"use strict";
-var assert = require("assert"),
-	EqualityMatchExpression = require("../../../../lib/pipeline/matcher/EqualityMatchExpression.js"),
-	ElemMatchObjectMatchExpression = require("../../../../lib/pipeline/matcher/ElemMatchObjectMatchExpression.js"),
-	ElemMatchValueMatchExpression = require("../../../../lib/pipeline/matcher/ElemMatchValueMatchExpression.js"),
-	AndMatchExpression = require("../../../../lib/pipeline/matcher/AndMatchExpression.js"),
-	LTMatchExpression = require("../../../../lib/pipeline/matcher/LTMatchExpression.js"),
-	GTMatchExpression = require("../../../../lib/pipeline/matcher/GTMatchExpression.js"),
-	AllElemMatchOp = require("../../../../lib/pipeline/matcher/AllElemMatchOp.js");
-
-
-module.exports = {
-	"AllElemMatchOp": {
-		"Should match an element": function() {
-			var baseOperanda1={"a":1},
-				eqa1 = new EqualityMatchExpression();
-
-			assert.strictEqual(eqa1.init("a", baseOperanda1.a).code, 'OK');
-
-			var baseOperandb1={"b":1},
-				eqb1 = new EqualityMatchExpression(),
-				and1 = new AndMatchExpression(),
-				elemMatch1 = new ElemMatchObjectMatchExpression();
-
-			assert.strictEqual(eqb1.init("b", baseOperandb1.b).code, 'OK');
-
-			and1.add(eqa1);
-			and1.add(eqb1);
-			// and1 = { a : 1, b : 1 }
-
-			elemMatch1.init("x", and1);
-			// elemMatch1 = { x : { $elemMatch : { a : 1, b : 1 } } }
-
-			var baseOperanda2={"a":2},
-				eqa2 = new EqualityMatchExpression();
-
-			assert.strictEqual(eqa2.init("a", baseOperanda2.a).code, 'OK');
-
-			var baseOperandb2={"b":2},
-				eqb2 = new EqualityMatchExpression(),
-				and2 = new AndMatchExpression(),
-				elemMatch2 = new ElemMatchObjectMatchExpression(),
-				op = new AllElemMatchOp();
-
-			assert.strictEqual(eqb2.init("b", baseOperandb2.b).code, 'OK');
-
-			and2.add(eqa2);
-			and2.add(eqb2);
-
-			elemMatch2.init("x", and2);
-			// elemMatch2 = { x : { $elemMatch : { a : 2, b : 2 } } }
-
-			op.init("");
-			op.add(elemMatch1);
-			op.add(elemMatch2);
-
-			var nonArray={"x":4},
-				emptyArray={"x":[]},
-				nonObjArray={"x":[4]},
-				singleObjMatch={"x":[{"a":1, "b":1}]},
-				otherObjMatch={"x":[{"a":2, "b":2}]},
-				bothObjMatch={"x":[{"a":1, "b":1}, {"a":2, "b":2}]},
-				noObjMatch={"x":[{"a":1, "b":2}, {"a":2, "b":1}]};
-
-			assert.ok(!op.matchesSingleElement(nonArray.x));
-			assert.ok(!op.matchesSingleElement(emptyArray.x));
-			assert.ok(!op.matchesSingleElement(nonObjArray.x));
-			assert.ok(!op.matchesSingleElement(singleObjMatch.x));
-			assert.ok(!op.matchesSingleElement(otherObjMatch.x));
-			assert.ok(op.matchesSingleElement(bothObjMatch.x));
-			assert.ok(!op.matchesSingleElement(noObjMatch.x));
-		},
-
-		"Should match things": function() {
-			var baseOperandgt1={"$gt":1},
-				gt1 = new GTMatchExpression();
-
-			assert.strictEqual(gt1.init("", baseOperandgt1.$gt).code, 'OK');
-
-			var baseOperandlt1={"$lt":10},
-				lt1 = new LTMatchExpression(),
-				elemMatch1 = new ElemMatchValueMatchExpression();
-
-			assert.strictEqual(lt1.init("", baseOperandlt1.$lt).code, 'OK');
-
-			elemMatch1.init("x");
-			elemMatch1.add(gt1);
-			elemMatch1.add(lt1);
-
-			var baseOperandgt2={"$gt":101},
-				gt2 = new GTMatchExpression();
-
-			assert.strictEqual(gt2.init("", baseOperandgt2.$gt).code, 'OK');
-
-			var baseOperandlt2={"$lt":110},
-				lt2 = new LTMatchExpression(),
-				elemMatch2 = new ElemMatchValueMatchExpression(),
-				op = new AllElemMatchOp();
-
-			assert.strictEqual(lt2.init("", baseOperandlt2.$lt).code, 'OK');
-
-			elemMatch2.init("x");
-			elemMatch2.add(gt2);
-			elemMatch2.add(lt2);
-
-			op.init("x");
-			op.add(elemMatch1);
-			op.add(elemMatch2);
-
-
-			var nonArray={"x":4},
-				emptyArray={"x":[]},
-				nonNumberArray={"x":["q"]},
-				singleMatch={"x":[5]},
-				otherMatch={"x":[105]},
-				bothMatch={"x":[5,105]},
-				neitherMatch={"x":[0,200]};
-
-			assert.ok(!op.matches(nonArray, null));
-			assert.ok(!op.matches(emptyArray, null));
-			assert.ok(!op.matches(nonNumberArray, null));
-			assert.ok(!op.matches(singleMatch, null));
-			assert.ok(!op.matches(otherMatch, null));
-			assert.ok(op.matches(bothMatch, null));
-			assert.ok(!op.matches(neitherMatch, null));
-		},
-	}
-};
-
-if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);
-

+ 128 - 0
test/lib/pipeline/matcher/AllElemMatchOp_test.js

@@ -0,0 +1,128 @@
+"use strict";
+var assert = require("assert"),
+	EqualityMatchExpression = require("../../../../lib/pipeline/matcher/EqualityMatchExpression.js"),
+	ElemMatchObjectMatchExpression = require("../../../../lib/pipeline/matcher/ElemMatchObjectMatchExpression.js"),
+	ElemMatchValueMatchExpression = require("../../../../lib/pipeline/matcher/ElemMatchValueMatchExpression.js"),
+	AndMatchExpression = require("../../../../lib/pipeline/matcher/AndMatchExpression.js"),
+	LTMatchExpression = require("../../../../lib/pipeline/matcher/LTMatchExpression.js"),
+	GTMatchExpression = require("../../../../lib/pipeline/matcher/GTMatchExpression.js"),
+	AllElemMatchOp = require("../../../../lib/pipeline/matcher/AllElemMatchOp.js");
+
+// Mocha one-liner to make these tests self-hosted
+if(!module.parent)return(require.cache[__filename]=null,(new(require("mocha"))({ui:"exports",reporter:"spec",grep:process.env.TEST_GREP})).addFile(__filename).run(process.exit));
+
+exports.AllElemMatchOp = {
+	"Should match an element": function() {
+		var baseOperanda1={"a":1},
+			eqa1 = new EqualityMatchExpression();
+
+		assert.strictEqual(eqa1.init("a", baseOperanda1.a).code, 'OK');
+
+		var baseOperandb1={"b":1},
+			eqb1 = new EqualityMatchExpression(),
+			and1 = new AndMatchExpression(),
+			elemMatch1 = new ElemMatchObjectMatchExpression();
+
+		assert.strictEqual(eqb1.init("b", baseOperandb1.b).code, 'OK');
+
+		and1.add(eqa1);
+		and1.add(eqb1);
+		// and1 = { a : 1, b : 1 }
+
+		elemMatch1.init("x", and1);
+		// elemMatch1 = { x : { $elemMatch : { a : 1, b : 1 } } }
+
+		var baseOperanda2={"a":2},
+			eqa2 = new EqualityMatchExpression();
+
+		assert.strictEqual(eqa2.init("a", baseOperanda2.a).code, 'OK');
+
+		var baseOperandb2={"b":2},
+			eqb2 = new EqualityMatchExpression(),
+			and2 = new AndMatchExpression(),
+			elemMatch2 = new ElemMatchObjectMatchExpression(),
+			op = new AllElemMatchOp();
+
+		assert.strictEqual(eqb2.init("b", baseOperandb2.b).code, 'OK');
+
+		and2.add(eqa2);
+		and2.add(eqb2);
+
+		elemMatch2.init("x", and2);
+		// elemMatch2 = { x : { $elemMatch : { a : 2, b : 2 } } }
+
+		op.init("");
+		op.add(elemMatch1);
+		op.add(elemMatch2);
+
+		var nonArray={"x":4},
+			emptyArray={"x":[]},
+			nonObjArray={"x":[4]},
+			singleObjMatch={"x":[{"a":1, "b":1}]},
+			otherObjMatch={"x":[{"a":2, "b":2}]},
+			bothObjMatch={"x":[{"a":1, "b":1}, {"a":2, "b":2}]},
+			noObjMatch={"x":[{"a":1, "b":2}, {"a":2, "b":1}]};
+
+		assert.ok(!op.matchesSingleElement(nonArray.x));
+		assert.ok(!op.matchesSingleElement(emptyArray.x));
+		assert.ok(!op.matchesSingleElement(nonObjArray.x));
+		assert.ok(!op.matchesSingleElement(singleObjMatch.x));
+		assert.ok(!op.matchesSingleElement(otherObjMatch.x));
+		assert.ok(op.matchesSingleElement(bothObjMatch.x));
+		assert.ok(!op.matchesSingleElement(noObjMatch.x));
+	},
+
+	"Should match things": function() {
+		var baseOperandgt1={"$gt":1},
+			gt1 = new GTMatchExpression();
+
+		assert.strictEqual(gt1.init("", baseOperandgt1.$gt).code, 'OK');
+
+		var baseOperandlt1={"$lt":10},
+			lt1 = new LTMatchExpression(),
+			elemMatch1 = new ElemMatchValueMatchExpression();
+
+		assert.strictEqual(lt1.init("", baseOperandlt1.$lt).code, 'OK');
+
+		elemMatch1.init("x");
+		elemMatch1.add(gt1);
+		elemMatch1.add(lt1);
+
+		var baseOperandgt2={"$gt":101},
+			gt2 = new GTMatchExpression();
+
+		assert.strictEqual(gt2.init("", baseOperandgt2.$gt).code, 'OK');
+
+		var baseOperandlt2={"$lt":110},
+			lt2 = new LTMatchExpression(),
+			elemMatch2 = new ElemMatchValueMatchExpression(),
+			op = new AllElemMatchOp();
+
+		assert.strictEqual(lt2.init("", baseOperandlt2.$lt).code, 'OK');
+
+		elemMatch2.init("x");
+		elemMatch2.add(gt2);
+		elemMatch2.add(lt2);
+
+		op.init("x");
+		op.add(elemMatch1);
+		op.add(elemMatch2);
+
+
+		var nonArray={"x":4},
+			emptyArray={"x":[]},
+			nonNumberArray={"x":["q"]},
+			singleMatch={"x":[5]},
+			otherMatch={"x":[105]},
+			bothMatch={"x":[5,105]},
+			neitherMatch={"x":[0,200]};
+
+		assert.ok(!op.matches(nonArray, null));
+		assert.ok(!op.matches(emptyArray, null));
+		assert.ok(!op.matches(nonNumberArray, null));
+		assert.ok(!op.matches(singleMatch, null));
+		assert.ok(!op.matches(otherMatch, null));
+		assert.ok(op.matches(bothMatch, null));
+		assert.ok(!op.matches(neitherMatch, null));
+	}
+};