StrcasecmpExpression_test.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. StrcasecmpExpression = require("../../../../lib/pipeline/expressions/StrcasecmpExpression"),
  5. Expression = require("../../../../lib/pipeline/expressions/Expression"),
  6. VariablesIdGenerator = require("../../../../lib/pipeline/expressions/VariablesIdGenerator"),
  7. VariablesParseState = require("../../../../lib/pipeline/expressions/VariablesParseState"),
  8. utils = require("./utils"),
  9. constify = utils.constify,
  10. expressionToJson = utils.expressionToJson;
  11. var TestBase = function TestBase(overrides) {
  12. //NOTE: DEVIATION FROM MONGO: using this base class to make things easier to initialize
  13. for (var key in overrides) //jshint ignore:line
  14. this[key] = overrides[key];
  15. },
  16. ExpectedResultBase = (function() {
  17. var klass = function ExpectedResultBase() {
  18. base.apply(this, arguments);
  19. }, base = TestBase, proto = klass.prototype = Object.create(base.prototype);
  20. proto.run = function(){
  21. this.assertResult(this.expectedResult, this.spec());
  22. this.assertResult(-this.expectedResult, this.reverseSpec());
  23. };
  24. proto.spec = function() { return {$strcasecmp:[this.a, this.b]}; };
  25. proto.reverseSpec = function() { return {$strcasecmp:[this.b, this.a]}; };
  26. proto.assertResult = function(expectedResult, spec) {
  27. var specElement = spec,
  28. idGenerator = new VariablesIdGenerator(),
  29. vps = new VariablesParseState(idGenerator),
  30. expression = Expression.parseOperand(specElement, vps);
  31. assert.deepEqual(constify(spec), expressionToJson(expression));
  32. assert.equal(expectedResult, expression.evaluate({}));
  33. };
  34. return klass;
  35. })();
  36. exports.StrcasecmpExpression = {
  37. "constructor()": {
  38. "should construct instance": function() {
  39. assert(new StrcasecmpExpression() instanceof StrcasecmpExpression);
  40. assert(new StrcasecmpExpression() instanceof Expression);
  41. },
  42. "should error if given args": function() {
  43. assert.throws(function() {
  44. new StrcasecmpExpression("bad stuff");
  45. });
  46. },
  47. },
  48. "#getOpName()": {
  49. "should return the correct op name; $strcasecmp": function(){
  50. assert.equal(new StrcasecmpExpression().getOpName(), "$strcasecmp");
  51. },
  52. },
  53. "#evaluate()": {
  54. "should return '_ab' == '_AB' (w/ null begin)": function NullBegin() {
  55. new ExpectedResultBase({
  56. a: "\0ab",
  57. b: "\0AB",
  58. expectedResult: 0,
  59. }).run();
  60. },
  61. "should return 'ab_' == 'aB_' (w/ null end)": function NullEnd() {
  62. new ExpectedResultBase({
  63. a: "ab\0",
  64. b: "aB\0",
  65. expectedResult: 0,
  66. }).run();
  67. },
  68. "should return 'a_a' < 'a_B' (w/ null middle)": function NullMiddleLt() {
  69. new ExpectedResultBase({
  70. a: "a\0a",
  71. b: "a\0B",
  72. expectedResult: -1,
  73. }).run();
  74. },
  75. "should return 'a_b' == 'a_B' (w/ null middle)": function NullMiddleEq() {
  76. new ExpectedResultBase({
  77. a: "a\0b",
  78. b: "a\0B",
  79. expectedResult: 0,
  80. }).run();
  81. },
  82. "should return 'a_c' > 'a_B' (w/ null middle)": function NullMiddleGt() {
  83. new ExpectedResultBase({
  84. a: "a\0c",
  85. b: "a\0B",
  86. expectedResult: 1,
  87. }).run();
  88. },
  89. },
  90. };