Value.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. "use strict";
  2. var assert = require("assert"),
  3. Value = require("../../../lib/pipeline/Value");
  4. // Mocha one-liner to make these tests self-hosted
  5. 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));
  6. exports.Value = {
  7. ".constructor()": {
  8. "should throw an error when used": function() {
  9. assert.throws(function() {
  10. new Value();
  11. });
  12. }
  13. },
  14. ".coerceToBool()": {
  15. "should coerce 0 to false": function testZeroIntToBool() {
  16. assert.strictEqual(Value.coerceToBool(0), false);
  17. },
  18. "should coerce -1 to true": function testNonZeroIntToBool() {
  19. assert.strictEqual(Value.coerceToBool(-1), true);
  20. },
  21. "should coerce 0L to false": function testZeroLongToBool() {
  22. assert.strictEqual(Value.coerceToBool(0e11), false);
  23. },
  24. "should coerce 5L to true": function testNonZeroLongToBool() {
  25. assert.strictEqual(Value.coerceToBool(5e11), true);
  26. },
  27. "should coerce 0.0 to false": function testZeroDoubleToBool() {
  28. assert.strictEqual(Value.coerceToBool(0.0), false);
  29. },
  30. "should coerce -1.3 to true": function testNonZeroDoubleToBool() {
  31. assert.strictEqual(Value.coerceToBool(-1.3), true);
  32. },
  33. "should coerce {} to true": function testObjectToBool() {
  34. assert.strictEqual(Value.coerceToBool({}), true);
  35. },
  36. "should coerce [] to true": function testArrayToBool() {
  37. assert.strictEqual(Value.coerceToBool([]), true);
  38. },
  39. "should coerce Date(0) to true": function testDateToBool() {
  40. assert.strictEqual(Value.coerceToBool(new Date(0)), true);
  41. },
  42. "should coerce Regex to true": function testRegexToBool() {
  43. assert.strictEqual(Value.coerceToBool(new RegExp("")), true);
  44. },
  45. "should coerce true to true": function testTrueToBool() {
  46. assert.strictEqual(Value.coerceToBool(true), true);
  47. },
  48. "should coerce false to false": function testFalseToBool() {
  49. assert.strictEqual(Value.coerceToBool(false), false);
  50. },
  51. "should coerce null to false": function testNullToBool() {
  52. assert.strictEqual(Value.coerceToBool(null), false);
  53. },
  54. "should coerce undefined to false": function testUndefinedToBool() {
  55. assert.strictEqual(Value.coerceToBool(null), false);
  56. },
  57. },
  58. ".coerceToWholeNumber()": {
  59. "should coerce int to int": function testIntToInt() {
  60. assert.strictEqual(Value.coerceToWholeNumber(-5), -5);
  61. },
  62. "should coerce long to int": function testLongToInt() {
  63. assert.strictEqual(Value.coerceToWholeNumber(0xff00000007), 7);
  64. },
  65. "should coerce double to int": function testDoubleToInt() {
  66. assert.strictEqual(Value.coerceToWholeNumber(9.8), 9);
  67. },
  68. "should coerce null to int": function testNullToInt() {
  69. assert.strictEqual(Value.coerceToWholeNumber(null), 0);
  70. },
  71. "should coerce undefined to int": function testUndefinedToInt() {
  72. assert.strictEqual(Value.coerceToWholeNumber(undefined), 0);
  73. },
  74. "should error if coerce \"\" to int": function testStringToInt() {
  75. assert.throws(function(){
  76. Value.coerceToWholeNumber("");
  77. });
  78. },
  79. //SKIPPED: ...ToLong tests because they are the same here
  80. },
  81. ".coerceToNumber()": {
  82. "should coerce int to double": function testIntToDouble() {
  83. assert.strictEqual(Value.coerceToNumber(-5), -5.0);
  84. },
  85. "should coerce long to double": function testLongToDouble() {
  86. assert.strictEqual(Value.coerceToNumber(0x8fffffffffffffff), 0x8fffffffffffffff);
  87. },
  88. "should coerce double to double": function testDoubleToDouble() {
  89. assert.strictEqual(Value.coerceToNumber(9.8), 9.8);
  90. },
  91. "should coerce null to double": function testNullToDouble() {
  92. assert.strictEqual(Value.coerceToNumber(null), 0);
  93. },
  94. "should coerce undefined to double": function testUndefinedToDouble() {
  95. assert.strictEqual(Value.coerceToNumber(undefined), 0);
  96. },
  97. "should error if coerce \"\" to double": function testStringToDouble() {
  98. assert.throws(function() {
  99. Value.coerceToNumber("");
  100. });
  101. },
  102. },
  103. ".coerceToDate()": {
  104. "should coerce date to date": function testDateToDate() {
  105. assert.deepEqual(Value.coerceToDate(new Date(888)), new Date(888));
  106. },
  107. //SKIPPED: TimestampToDate because we don't have a Timestamp
  108. "should error if string to date": function testStringToDate() {
  109. assert.throws(function() {
  110. Value.coerceToDate("");
  111. });
  112. },
  113. },
  114. ".coerceToString()": {
  115. "should coerce double to string": function testDoubleToString() {
  116. assert.strictEqual(Value.coerceToString(-0.2), "-0.2");
  117. },
  118. "should coerce int to string": function testIntToString() {
  119. assert.strictEqual(Value.coerceToString(-4), "-4");
  120. },
  121. "should coerce long to string": function testLongToString() {
  122. assert.strictEqual(Value.coerceToString(10000e11), "1000000000000000");
  123. },
  124. "should coerce string to string": function testStringToString() {
  125. assert.strictEqual(Value.coerceToString("fO_o"), "fO_o");
  126. },
  127. //SKIPPED: TimestampToString because we don't have a Timestamp
  128. "should coerce date to string": function testDateToString() {
  129. assert.strictEqual(Value.coerceToString(new Date(1234567890 * 1000)), "2009-02-13T23:31:30");
  130. },
  131. "should coerce null to string": function testNullToString() {
  132. assert.strictEqual(Value.coerceToString(null), "");
  133. },
  134. "should coerce undefined to string": function testUndefinedToString() {
  135. assert.strictEqual(Value.coerceToString(undefined), "");
  136. },
  137. "should throw if coerce document to string": function testDocumentToString() {
  138. assert.throws(function() {
  139. Value.coerceToString({});
  140. });
  141. },
  142. },
  143. ".compare()": {
  144. "should test things": function testCompare() {
  145. // BSONObjBuilder undefinedBuilder;
  146. // undefinedBuilder.appendUndefined( "" );
  147. // BSONObj undefined = undefinedBuilder.obj();
  148. // Undefined / null.
  149. assert.strictEqual(Value.compare(undefined, undefined), 0);
  150. assert.strictEqual(Value.compare(undefined, null), -1);
  151. assert.strictEqual(Value.compare(null, null), 0);
  152. // Undefined / null with other types.
  153. assert.strictEqual(Value.compare(undefined, 1), -1);
  154. assert.strictEqual(Value.compare(undefined, "bar"), -1);
  155. assert.strictEqual(Value.compare(null, -1), -1);
  156. assert.strictEqual(Value.compare(null, "bar"), -1);
  157. // Numeric types.
  158. assert.strictEqual(Value.compare(5, 5e11 / 1e11), 0);
  159. assert.strictEqual(Value.compare(-2, -2.0), 0);
  160. assert.strictEqual(Value.compare(90e11 / 1e11, 90.0), 0);
  161. assert.strictEqual(Value.compare(5, 6e11 / 1e11), -1);
  162. assert.strictEqual(Value.compare(-2, 2.1), -1);
  163. assert.strictEqual(Value.compare(90e11 / 1e11, 89.999), 1);
  164. assert.strictEqual(Value.compare(90, 90.1), -1);
  165. assert.strictEqual(Value.compare(NaN, NaN), 0);
  166. assert.strictEqual(Value.compare(NaN, 5), -1);
  167. // strings compare between numbers and objects
  168. assert.strictEqual(Value.compare("abc", 90), 1);
  169. assert.strictEqual(Value.compare("abc", {a:"b"}), -1);
  170. // String comparison.
  171. assert.strictEqual(Value.compare("", "a"), -1);
  172. assert.strictEqual(Value.compare("a", "a"), 0);
  173. assert.strictEqual(Value.compare("a", "b"), -1);
  174. assert.strictEqual(Value.compare("aa", "b"), -1);
  175. assert.strictEqual(Value.compare("bb", "b"), 1);
  176. assert.strictEqual(Value.compare("bb", "b"), 1);
  177. assert.strictEqual(Value.compare("b-", "b"), 1);
  178. assert.strictEqual(Value.compare("b-", "ba"), -1);
  179. // With a null character.
  180. assert.strictEqual(Value.compare("a\0", "a"), 1);
  181. // Object.
  182. assert.strictEqual(Value.compare({}, {}), 0);
  183. assert.strictEqual(Value.compare({x:1}, {x:1}), 0);
  184. assert.strictEqual(Value.compare({}, {x:1}), -1);
  185. // Array.
  186. assert.strictEqual(Value.compare([], []), 0);
  187. assert.strictEqual(Value.compare([0], [1]), -1);
  188. assert.strictEqual(Value.compare([0, 0], [1]), -1);
  189. assert.strictEqual(Value.compare([0], [0, 0]), -1);
  190. assert.strictEqual(Value.compare([0], [""]), -1);
  191. //TODO: OID?
  192. // assert.strictEqual(Value.compare(OID("abcdefabcdefabcdefabcdef"), OID("abcdefabcdefabcdefabcdef")), 0);
  193. // assert.strictEqual(Value.compare(OID("abcdefabcdefabcdefabcdef"), OID("010101010101010101010101")), 1);
  194. // Bool.
  195. assert.strictEqual(Value.compare(true, true), 0);
  196. assert.strictEqual(Value.compare(false, false), 0);
  197. assert.strictEqual(Value.compare(true, false), 1);
  198. // Date.
  199. assert.strictEqual(Value.compare(new Date(555), new Date(555)), 0);
  200. assert.strictEqual(Value.compare(new Date(555), new Date(554)), 1);
  201. // Negative date.
  202. assert.strictEqual(Value.compare(new Date(0), new Date(-1)), 1);
  203. // Regex.
  204. assert.strictEqual(Value.compare(/a/, /a/), 0);
  205. assert.strictEqual(Value.compare(/a/, /a/i), -1);
  206. assert.strictEqual(Value.compare(/a/, /aa/), -1);
  207. //TODO: Timestamp?
  208. // assert.strictEqual(Value.compare(OpTime(1234), OpTime(1234)), 0);
  209. // assert.strictEqual(Value.compare(OpTime(4), OpTime(1234)), -1);
  210. // Cross-type comparisons. Listed in order of canonical types.
  211. // assert.strictEqual(Value.compare(MINKEY, undefined), -1);
  212. assert.strictEqual(Value.compare(undefined, undefined), 0);
  213. // assert.strictEqual(Value.compare(undefined, BSONUndefined), 0);
  214. assert.strictEqual(Value.compare(undefined, null), -1);
  215. assert.strictEqual(Value.compare(null, 1), -1);
  216. assert.strictEqual(Value.compare(1, 1 /*LL*/ ), 0);
  217. assert.strictEqual(Value.compare(1, 1.0), 0);
  218. assert.strictEqual(Value.compare(1, "string"), -1);
  219. // assert.strictEqual(Value.compare("string", BSONSymbol("string")), 0);
  220. assert.strictEqual(Value.compare("string", {}), -1);
  221. assert.strictEqual(Value.compare({}, []), -1);
  222. // assert.strictEqual(Value.compare([], BSONBinData("", 0, MD5Type)), -1);
  223. // assert.strictEqual(Value.compare(BSONBinData("", 0, MD5Type), OID()), -1);
  224. // assert.strictEqual(Value.compare(OID(), false), -1);
  225. // assert.strictEqual(Value.compare(false, OpTime()), -1);
  226. // assert.strictEqual(Value.compare(OpTime(), Date_t(0)), 0, );
  227. // assert.strictEqual(Value.compare(Date_t(0), BSONRegEx("")), -1);
  228. // assert.strictEqual(Value.compare(BSONRegEx(""), BSONDBRef("", OID())), -1);
  229. // assert.strictEqual(Value.compare(BSONDBRef("", OID()), BSONCode("")), -1);
  230. // assert.strictEqual(Value.compare(BSONCode(""), BSONCodeWScope("", BSONObj())), -1);
  231. // assert.strictEqual(Value.compare(BSONCodeWScope("", BSONObj()), MAXKEY), -1);
  232. },
  233. },
  234. ".consume()": {
  235. "should return an equivalent array, empty the original": function() {
  236. var inputs = [5, 6, "hi"],
  237. expected = [].concat(inputs), // copy
  238. actual = Value.consume(inputs);
  239. assert.deepEqual(actual, expected, "should equal input array");
  240. assert.notEqual(actual, inputs, "should be different array");
  241. assert.strictEqual(inputs.length, 0, "should be empty");
  242. },
  243. "should work given an empty array": function() {
  244. var inputs = [],
  245. expected = [].concat(inputs), // copy
  246. actual = Value.consume(inputs);
  247. assert.deepEqual(actual, expected, "should equal input array");
  248. assert.notEqual(actual, inputs, "should be different array");
  249. assert.strictEqual(inputs.length, 0, "should be empty");
  250. }
  251. },
  252. };