ToLowerExpression_test.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. "use strict";
  2. var assert = require("assert"),
  3. ToLowerExpression = require("../../../../lib/pipeline/expressions/ToLowerExpression"),
  4. VariablesParseState = require("../../../../lib/pipeline/expressions/VariablesParseState"),
  5. VariablesIdGenerator = require("../../../../lib/pipeline/expressions/VariablesIdGenerator"),
  6. Expression = require("../../../../lib/pipeline/expressions/Expression"),
  7. utils = require("./utils"),
  8. constify = utils.constify,
  9. expressionToJson = utils.expressionToJson;
  10. // Mocha one-liner to make these tests self-hosted
  11. 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));
  12. var TestBase = function TestBase(overrides) {
  13. //NOTE: DEVIATION FROM MONGO: using this base class to make things easier to initialize
  14. for (var key in overrides)
  15. this[key] = overrides[key];
  16. },
  17. ExpectedResultBase = (function() {
  18. var klass = function ExpectedResultBase() {
  19. base.apply(this, arguments);
  20. }, base = TestBase, proto = klass.prototype = Object.create(base.prototype);
  21. proto.run = function(){
  22. var specElement = this.spec(),
  23. idGenerator = new VariablesIdGenerator(),
  24. vps = new VariablesParseState(idGenerator),
  25. expr = Expression.parseOperand(specElement, vps);
  26. assert.deepEqual(constify(specElement), expressionToJson(expr));
  27. assert.strictEqual(this.expectedResult, expr.evaluate({}));
  28. };
  29. proto.spec = function() {
  30. return {$toLower:[this.str]};
  31. };
  32. return klass;
  33. })();
  34. exports.ToLowerExpression = {
  35. "constructor()": {
  36. "should construct instance": function() {
  37. assert(new ToLowerExpression() instanceof ToLowerExpression);
  38. assert(new ToLowerExpression() instanceof Expression);
  39. },
  40. "should error if given args": function() {
  41. assert.throws(function() {
  42. new ToLowerExpression("bad stuff");
  43. });
  44. },
  45. },
  46. "#getOpName()": {
  47. "should return the correct op name; $toLower": function() {
  48. assert.equal(new ToLowerExpression().getOpName(), "$toLower");
  49. },
  50. },
  51. "#evaluate()": {
  52. "should return the lowercase version of the string if there is a null character at the beginning of the string": function NullBegin() {
  53. /** String beginning with a null character. */
  54. new ExpectedResultBase({
  55. str: "\0aB",
  56. expectedResult: "\0ab",
  57. }).run();
  58. },
  59. "should return the lowercase version of the string if there is a null character in the middle of the string": function NullMiddle() {
  60. /** String containing a null character. */
  61. new ExpectedResultBase({
  62. str: "a\0B",
  63. expectedResult: "a\0b",
  64. }).run();
  65. },
  66. "should return the lowercase version of the string if there is a null character at the end of the string": function NullEnd() {
  67. /** String ending with a null character. */
  68. new ExpectedResultBase({
  69. str: "aB\0",
  70. expectedResult: "ab\0",
  71. }).run();
  72. },
  73. },
  74. };
  75. if (!module.parent)(new (require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);