Browse Source

EAGLESIX-2651: Value: add tests; fix issues found

Kyle P Davis 11 years ago
parent
commit
b8d380a56a
2 changed files with 295 additions and 73 deletions
  1. 32 18
      lib/pipeline/Value.js
  2. 263 55
      test/lib/pipeline/Value.js

+ 32 - 18
lib/pipeline/Value.js

@@ -35,9 +35,11 @@ klass.coerceToBool = function coerceToBool(value) {
  *  These currently assert if called on an unconvertible type.
  *  TODO: decided how to handle unsupported types.
  */
-//SKIPPED: coerceToInt -- use coerceToNumber
-//SKIPPED:  coerceToLong -- use coerceToNumber
-//SKIPPED:  coerceToDouble -- use coerceToNumber
+klass.coerceToWholeNumber = function coerceToInt(value) {
+	return klass.coerceToNumber(value) | 0;
+};
+klass.coerceToInt = klass.coerceToWholeNumber;
+klass.coerceToLong = klass.coerceToWholeNumber;
 klass.coerceToNumber = function coerceToNumber(value) {
 	if (value === null) return 0;
 	switch (typeof(value)) {
@@ -59,8 +61,8 @@ klass.coerceToNumber = function coerceToNumber(value) {
 		throw new Error("can't convert from BSON type " + typeof(value) + " to int; codes 16003, 16004, 16005");
 	}
 };
+klass.coerceToDouble = klass.coerceToNumber;
 klass.coerceToDate = function coerceToDate(value) {
-	//TODO: Support Timestamp BSON type?
 	if (value instanceof Date) return value;
 	throw new Error("can't convert from BSON type " + typeof(value) + " to Date; uassert code 16006");
 };
@@ -68,16 +70,28 @@ klass.coerceToDate = function coerceToDate(value) {
 //SKIPPED: coerceToTm -- not required; just use Date
 //SKIPPED: tmToISODateString -- not required; just use Date
 klass.coerceToString = function coerceToString(value) {
-	if (value === null) return "";
-	switch (typeof(value)) {
-	case "undefined":
-		return "";
-	case "number":
-		return value.toString();
-	case "string":
-		return value;
-	default:
-		throw new Error("can't convert from BSON type " + typeof(value) + " to String; uassert code 16007");
+	var type = typeof(value);
+	if (type == "object") type = value === null ? "null" : value.constructor.name;
+	switch (type) {
+		//TODO: BSON numbers?
+		case "number":
+			return value.toString();
+
+		//TODO: BSON Code?
+		//TODO: BSON Symbol?
+		case "string":
+			return value;
+
+		//TODO: BSON Timestamp?
+		case "Date":
+			return value.toISOString().split(".")[0];
+
+		case "null":
+		case "undefined":
+			return "";
+
+		default:
+			throw new Error("can't convert from BSON type " + typeof(value) + " to String; uassert code 16007");
 	}
 };
 //SKIPPED: coerceToTimestamp
@@ -116,10 +130,10 @@ klass.compare = function compare(l, r) {
 		return klass.cmp(l,r);
 	}
 	// Compare MinKey and MaxKey cases
-	if(l.constructor && l.constructor.name in {'MinKey':1,'MaxKey':1} ){
-		if(l.constructor.name == r.constructor.name) {
+	if (l instanceof Object && ["MinKey", "MaxKey"].indexOf(l.constructor.name) !== -1) {
+		if (l.constructor.name == r.constructor.name) {
 			return 0;
-		} else if (l.constructor.name === 'MinKey'){
+		} else if (l.constructor.name === "MinKey") {
 			return -1;
 		} else {
 			return 1; // Must be MaxKey, which is greater than everything but MaxKey (which r cannot be)
@@ -138,7 +152,7 @@ klass.compare = function compare(l, r) {
 	case "number":
 		throw new Error("number types should have been handled earlier!");
 	case "string":
-		return klass.cmp(l,r);
+		return klass.cmp(l, r);
 	case "boolean":
 		return l == r ? 0 : l ? 1 : -1;
 	case "undefined": //NOTE: deviation from mongo code: we are comparing null to null or undefined to undefined (otherwise the ret stuff above would have caught it)

+ 263 - 55
test/lib/pipeline/Value.js

@@ -9,7 +9,7 @@ exports.Value = {
 
 	".constructor()": {
 
-		"should throw an error when used": function ctorThrows() {
+		"should throw an error when used": function() {
 			assert.throws(function() {
 				new Value();
 			});
@@ -19,101 +19,309 @@ exports.Value = {
 
 	".coerceToBool()": {
 
-		"should be tested": function(){
-			assert.equal("TESTS", "NO");
+		"should coerce 0 to false": function testZeroIntToBool() {
+			assert.strictEqual(Value.coerceToBool(0), false);
 		},
 
+		"should coerce -1 to true": function testNonZeroIntToBool() {
+			assert.strictEqual(Value.coerceToBool(-1), true);
+		},
+
+		"should coerce 0L to false": function testZeroLongToBool() {
+			assert.strictEqual(Value.coerceToBool(0e11), false);
+		},
+
+		"should coerce 5L to true": function testNonZeroLongToBool() {
+			assert.strictEqual(Value.coerceToBool(5e11), true);
+		},
+
+		"should coerce 0.0 to false": function testZeroDoubleToBool() {
+			assert.strictEqual(Value.coerceToBool(0.0), false);
+		},
+
+		"should coerce -1.3 to true": function testNonZeroDoubleToBool() {
+			assert.strictEqual(Value.coerceToBool(-1.3), true);
+		},
+
+		"should coerce {} to true": function testObjectToBool() {
+			assert.strictEqual(Value.coerceToBool({}), true);
+		},
+
+		"should coerce [] to true": function testArrayToBool() {
+			assert.strictEqual(Value.coerceToBool([]), true);
+		},
+
+		"should coerce Date(0) to true": function testDateToBool() {
+			assert.strictEqual(Value.coerceToBool(new Date(0)), true);
+		},
+
+		"should coerce Regex to true": function testRegexToBool() {
+			assert.strictEqual(Value.coerceToBool(new RegExp("")), true);
+		},
+
+		"should coerce true to true": function testTrueToBool() {
+			assert.strictEqual(Value.coerceToBool(true), true);
+		},
+
+		"should coerce false to false": function testFalseToBool() {
+			assert.strictEqual(Value.coerceToBool(false), false);
+		},
+
+		"should coerce null to false": function testNullToBool() {
+			assert.strictEqual(Value.coerceToBool(null), false);
+		},
+
+		"should coerce undefined to false": function testUndefinedToBool() {
+			assert.strictEqual(Value.coerceToBool(null), false);
+		},
+
+	},
+
+	".coerceToWholeNumber()": {
+
+		"should coerce int to int": function testIntToInt() {
+			assert.strictEqual(Value.coerceToWholeNumber(-5), -5);
+		},
+
+		"should coerce long to int": function testLongToInt() {
+			assert.strictEqual(Value.coerceToWholeNumber(0xff00000007), 7);
+		},
+
+		"should coerce double to int": function testDoubleToInt() {
+			assert.strictEqual(Value.coerceToWholeNumber(9.8), 9);
+		},
+
+		"should coerce null to int": function testNullToInt() {
+			assert.strictEqual(Value.coerceToWholeNumber(null), 0);
+		},
+
+		"should coerce undefined to int": function testUndefinedToInt() {
+			assert.strictEqual(Value.coerceToWholeNumber(undefined), 0);
+		},
+
+		"should error if coerce \"\" to int": function testStringToInt() {
+			assert.throws(function(){
+				Value.coerceToWholeNumber("");
+			});
+		},
+
+		//SKIPPED: ...ToLong tests because they are the same here
+
 	},
 
 	".coerceToNumber()": {
 
-		"should be tested": function(){
-			assert.equal("TESTS", "NO");
+		"should coerce int to double": function testIntToDouble() {
+			assert.strictEqual(Value.coerceToNumber(-5), -5.0);
+		},
+
+		"should coerce long to double": function testLongToDouble() {
+			assert.strictEqual(Value.coerceToNumber(0x8fffffffffffffff), 0x8fffffffffffffff);
+		},
+
+		"should coerce double to double": function testDoubleToDouble() {
+			assert.strictEqual(Value.coerceToNumber(9.8), 9.8);
+		},
+
+		"should coerce null to double": function testNullToDouble() {
+			assert.strictEqual(Value.coerceToNumber(null), 0);
+		},
+
+		"should coerce undefined to double": function testUndefinedToDouble() {
+			assert.strictEqual(Value.coerceToNumber(undefined), 0);
+		},
+
+		"should error if coerce \"\" to double": function testStringToDouble() {
+			assert.throws(function() {
+				Value.coerceToNumber("");
+			});
 		},
 
 	},
 
 	".coerceToDate()": {
 
-		"should be tested": function(){
-			assert.equal("TESTS", "NO");
+		"should coerce date to date": function testDateToDate() {
+			assert.deepEqual(Value.coerceToDate(new Date(888)), new Date(888));
+		},
+
+		//SKIPPED: TimestampToDate because we don't have a Timestamp
+
+		"should error if string to date": function testStringToDate() {
+			assert.throws(function() {
+				Value.coerceToDate("");
+			});
 		},
 
 	},
 
 	".coerceToString()": {
 
-		"should be tested": function(){
-			assert.equal("TESTS", "NO");
+		"should coerce double to string": function testDoubleToString() {
+			assert.strictEqual(Value.coerceToString(-0.2), "-0.2");
 		},
 
-	},
-
-	".cmp()": {
+		"should coerce int to string": function testIntToString() {
+			assert.strictEqual(Value.coerceToString(-4), "-4");
+		},
 
-		"should be tested": function(){
-			assert.equal("TESTS", "NO");
+		"should coerce long to string": function testLongToString() {
+			assert.strictEqual(Value.coerceToString(10000e11), "1000000000000000");
 		},
 
-	},
+		"should coerce string to string": function testStringToString() {
+			assert.strictEqual(Value.coerceToString("fO_o"), "fO_o");
+		},
 
-	".compare()": {
+		//SKIPPED: TimestampToString because we don't have a Timestamp
 
-		"follows canonical type order when types differ": function throwsWhenDiffTypes() {
-			var a = 5,
-				b = "hi",
-				actual = Value.compare(a,b);
-			assert.equal(actual, -1);
+		"should coerce date to string": function testDateToString() {
+			assert.strictEqual(Value.coerceToString(new Date(1234567890 * 1000)), "2009-02-13T23:31:30");
 		},
 
-		"should compare two numbers": function comparesNumbers() {
-			var a = 5,
-				b = 6,
-				actual = Value.compare(a,b);
-			assert.equal(actual, -1);
+		"should coerce null to string": function testNullToString() {
+			assert.strictEqual(Value.coerceToString(null), "");
 		},
 
-		"should compare two strings": function comparesStrings() {
-			var a = "a",
-				b = "b",
-				actual = Value.compare(b,a);
-			assert.equal(actual, 1);
+		"should coerce undefined to string": function testUndefinedToString() {
+			assert.strictEqual(Value.coerceToString(undefined), "");
 		},
 
-		"should detect the same object": function compareSameObj() {
-			var a = {},
-				b = a,
-				actual = Value.compare(a,b);
-			assert.equal(actual, 0);
-		}
+		"should throw if coerce document to string": function testDocumentToString() {
+			assert.throws(function() {
+				Value.coerceToString({});
+			});
+		},
 
 	},
 
-	".consume()": {
+	".compare()": {
 
-		"should return an equivalent array, empty the original":function works() {
-			var testArray = [5,6,"hi"],
-				result = Value.consume(testArray);
-			assert.deepEqual([5,6,"hi"], result); // tests that insides were copied
-			assert.notEqual(testArray, result);   // tests that a new array was returned
-			assert.equal(testArray.length, 0);    // tests that the old array was emptied
+		"should test things": function testCompare() {
+            // BSONObjBuilder undefinedBuilder;
+            // undefinedBuilder.appendUndefined( "" );
+            // BSONObj undefined = undefinedBuilder.obj();
+
+            // Undefined / null.
+            assert.strictEqual(Value.compare(undefined, undefined), 0);
+            assert.strictEqual(Value.compare(undefined, null), -1);
+            assert.strictEqual(Value.compare(null, null), 0);
+
+            // Undefined / null with other types.
+			assert.strictEqual(Value.compare(undefined, 1), -1);
+			assert.strictEqual(Value.compare(undefined, "bar"), -1);
+			assert.strictEqual(Value.compare(null, -1), -1);
+			assert.strictEqual(Value.compare(null, "bar"), -1);
+
+            // Numeric types.
+            assert.strictEqual(Value.compare(5, 5e11 / 1e11), 0);
+            assert.strictEqual(Value.compare(-2, -2.0), 0);
+            assert.strictEqual(Value.compare(90e11 / 1e11, 90.0), 0);
+            assert.strictEqual(Value.compare(5, 6e11 / 1e11), -1);
+            assert.strictEqual(Value.compare(-2, 2.1), -1);
+            assert.strictEqual(Value.compare(90e11 / 1e11, 89.999), 1);
+            assert.strictEqual(Value.compare(90, 90.1), -1);
+            assert.strictEqual(Value.compare(NaN, NaN), 0);
+            assert.strictEqual(Value.compare(NaN, 5), -1);
+
+            // strings compare between numbers and objects
+            assert.strictEqual(Value.compare("abc", 90), 1);
+            assert.strictEqual(Value.compare("abc", {a:"b"}), -1);
+
+            // String comparison.
+            assert.strictEqual(Value.compare("", "a"), -1);
+			assert.strictEqual(Value.compare("a", "a"), 0);
+			assert.strictEqual(Value.compare("a", "b"), -1);
+			assert.strictEqual(Value.compare("aa", "b"), -1);
+			assert.strictEqual(Value.compare("bb", "b"), 1);
+			assert.strictEqual(Value.compare("bb", "b"), 1);
+			assert.strictEqual(Value.compare("b-", "b"), 1);
+			assert.strictEqual(Value.compare("b-", "ba"), -1);
+            // With a null character.
+            assert.strictEqual(Value.compare("a\0", "a"), 1);
+
+            // Object.
+            assert.strictEqual(Value.compare({}, {}), 0);
+            assert.strictEqual(Value.compare({x:1}, {x:1}), 0);
+            assert.strictEqual(Value.compare({}, {x:1}), -1);
+
+            // Array.
+            assert.strictEqual(Value.compare([], []), 0);
+			assert.strictEqual(Value.compare([0], [1]), -1);
+			assert.strictEqual(Value.compare([0, 0], [1]), -1);
+			assert.strictEqual(Value.compare([0], [0, 0]), -1);
+			assert.strictEqual(Value.compare([0], [""]), -1);
+
+            //TODO: OID?
+            // assert.strictEqual(Value.compare(OID("abcdefabcdefabcdefabcdef"), OID("abcdefabcdefabcdefabcdef")), 0);
+            // assert.strictEqual(Value.compare(OID("abcdefabcdefabcdefabcdef"), OID("010101010101010101010101")), 1);
+
+            // Bool.
+            assert.strictEqual(Value.compare(true, true), 0);
+            assert.strictEqual(Value.compare(false, false), 0);
+            assert.strictEqual(Value.compare(true, false), 1);
+
+            // Date.
+            assert.strictEqual(Value.compare(new Date(555), new Date(555)), 0);
+            assert.strictEqual(Value.compare(new Date(555), new Date(554)), 1);
+            // Negative date.
+            assert.strictEqual(Value.compare(new Date(0), new Date(-1)), 1);
+
+            // Regex.
+            assert.strictEqual(Value.compare(/a/, /a/), 0);
+            assert.strictEqual(Value.compare(/a/, /a/i), -1);
+            assert.strictEqual(Value.compare(/a/, /aa/), -1);
+
+            //TODO: Timestamp?
+            // assert.strictEqual(Value.compare(OpTime(1234), OpTime(1234)), 0);
+            // assert.strictEqual(Value.compare(OpTime(4), OpTime(1234)), -1);
+
+            // Cross-type comparisons. Listed in order of canonical types.
+            // assert.strictEqual(Value.compare(MINKEY, undefined), -1);
+            assert.strictEqual(Value.compare(undefined, undefined), 0);
+            // assert.strictEqual(Value.compare(undefined, BSONUndefined), 0);
+            assert.strictEqual(Value.compare(undefined, null), -1);
+            assert.strictEqual(Value.compare(null, 1), -1);
+			assert.strictEqual(Value.compare(1, 1 /*LL*/ ), 0);
+            assert.strictEqual(Value.compare(1, 1.0), 0);
+            assert.strictEqual(Value.compare(1, "string"), -1);
+            // assert.strictEqual(Value.compare("string", BSONSymbol("string")), 0);
+            assert.strictEqual(Value.compare("string", {}), -1);
+            assert.strictEqual(Value.compare({}, []), -1);
+            // assert.strictEqual(Value.compare([], BSONBinData("", 0, MD5Type)), -1);
+            // assert.strictEqual(Value.compare(BSONBinData("", 0, MD5Type), OID()), -1);
+            // assert.strictEqual(Value.compare(OID(), false), -1);
+            // assert.strictEqual(Value.compare(false, OpTime()), -1);
+            // assert.strictEqual(Value.compare(OpTime(), Date_t(0)), 0, );
+            // assert.strictEqual(Value.compare(Date_t(0), BSONRegEx("")), -1);
+            // assert.strictEqual(Value.compare(BSONRegEx(""), BSONDBRef("", OID())), -1);
+            // assert.strictEqual(Value.compare(BSONDBRef("", OID()), BSONCode("")), -1);
+            // assert.strictEqual(Value.compare(BSONCode(""), BSONCodeWScope("", BSONObj())), -1);
+            // assert.strictEqual(Value.compare(BSONCodeWScope("", BSONObj()), MAXKEY), -1);
 		},
 
-		"should work given an empty array":function worksWhenEmpty() {
-			var testArray = [],
-				result = Value.consume(testArray);
-			assert.deepEqual([], result);
-			assert.equal(testArray.length, 0);
-		}
-
 	},
 
-	".canonicalize()": {
+	".consume()": {
 
-		"should be tested": function(){
-			assert.equal("TESTS", "NO");
+		"should return an equivalent array, empty the original": function() {
+			var inputs = [5, 6, "hi"],
+				expected = [].concat(inputs), // copy
+				actual = Value.consume(inputs);
+			assert.deepEqual(actual, expected, "should equal input array");
+			assert.notEqual(actual, inputs, "should be different array");
+			assert.strictEqual(inputs.length, 0, "should be empty");
 		},
 
-	}
+		"should work given an empty array": function() {
+			var inputs = [],
+				expected = [].concat(inputs), // copy
+				actual = Value.consume(inputs);
+			assert.deepEqual(actual, expected, "should equal input array");
+			assert.notEqual(actual, inputs, "should be different array");
+			assert.strictEqual(inputs.length, 0, "should be empty");
+		}
+
+	},
 
 };