ToUpperExpression_test.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. ToUpperExpression = require("../../../../lib/pipeline/expressions/ToUpperExpression"),
  5. VariablesParseState = require("../../../../lib/pipeline/expressions/VariablesParseState"),
  6. VariablesIdGenerator = require("../../../../lib/pipeline/expressions/VariablesIdGenerator"),
  7. Expression = require("../../../../lib/pipeline/expressions/Expression"),
  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. var specElement = this.spec(),
  22. idGenerator = new VariablesIdGenerator(),
  23. vps = new VariablesParseState(idGenerator),
  24. expr = Expression.parseOperand(specElement, vps);
  25. assert.deepEqual(constify(specElement), expressionToJson(expr));
  26. assert.strictEqual(this.expectedResult, expr.evaluate({}));
  27. };
  28. proto.spec = function() {
  29. return {$toUpper:[this.str]};
  30. };
  31. return klass;
  32. })();
  33. exports.ToUpperExpression = {
  34. "constructor()": {
  35. "should construct instance": function() {
  36. assert(new ToUpperExpression() instanceof ToUpperExpression);
  37. assert(new ToUpperExpression() instanceof Expression);
  38. },
  39. "should error if given args": function() {
  40. assert.throws(function() {
  41. new ToUpperExpression("bad stuff");
  42. });
  43. },
  44. },
  45. "#getOpName()": {
  46. "should return the correct op name; $toUpper": function() {
  47. assert.equal(new ToUpperExpression().getOpName(), "$toUpper");
  48. }
  49. },
  50. "#evaluate()": {
  51. "should return the uppercase version of the string if a null character at the beginning of the string": function NullBegin() {
  52. /** String beginning with a null character. */
  53. new ExpectedResultBase({
  54. str: "\0aB",
  55. expectedResult: "\0AB",
  56. }).run();
  57. },
  58. "should return the uppercase version of the string if a null character in the middle of the string": function NullMiddle() {
  59. /** String containing a null character. */
  60. new ExpectedResultBase({
  61. str: "a\0B",
  62. expectedResult: "A\0B",
  63. }).run();
  64. },
  65. "should return the uppercase version of the string if a null character at the end of the string": function NullEnd() {
  66. /** String ending with a null character. */
  67. new ExpectedResultBase({
  68. str: "aB\0",
  69. expectedResult: "AB\0",
  70. }).run();
  71. },
  72. },
  73. };