Value_test.js 11 KB

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