FieldPath.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. "use strict";
  2. var assert = require("assert"),
  3. FieldPath = require("../../../lib/pipeline/FieldPath");
  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.FieldPath = {
  7. "constructor(path)": {
  8. "should throw Error if given an empty path String": function testEmpty() {
  9. assert.throws(function() {
  10. new FieldPath("");
  11. });
  12. },
  13. "should throw Error if given an empty path Array": function testEmptyVector() {
  14. assert.throws(function() {
  15. new FieldPath([]);
  16. });
  17. },
  18. "should accept simple paths as a String (without dots)": function testSimple() {
  19. var path = new FieldPath("foo");
  20. assert.equal(path.getPathLength(), 1);
  21. assert.equal(path.getFieldName(0), "foo");
  22. assert.equal(path.getPath(false), "foo");
  23. assert.equal(path.getPath(true), "$foo");
  24. },
  25. "should accept simple paths as an Array of one item": function testSimpleVector() {
  26. var path = new FieldPath(["foo"]);
  27. assert.equal(path.getPathLength(), 1);
  28. assert.equal(path.getFieldName(0), "foo");
  29. assert.equal(path.getPath(false), "foo");
  30. },
  31. "should throw Error if given a '$' String": function testDollarSign() {
  32. assert.throws(function() {
  33. new FieldPath("$");
  34. });
  35. },
  36. "should throw Error if given a '$'-prefixed String": function testDollarSignPrefix() {
  37. assert.throws(function() {
  38. new FieldPath("$a");
  39. });
  40. },
  41. "should accept paths as a String with one dot": function testDotted() {
  42. var path = new FieldPath("foo.bar");
  43. assert.equal(path.getPathLength(), 2);
  44. assert.equal(path.getFieldName(0), "foo");
  45. assert.equal(path.getFieldName(1), "bar");
  46. assert.equal(path.getPath(false), "foo.bar");
  47. assert.equal(path.getPath(true), "$foo.bar");
  48. },
  49. "should throw Error if given a path Array with items containing a dot": function testVectorWithDot() {
  50. assert.throws(function() {
  51. new FieldPath(["fo.o"]);
  52. });
  53. },
  54. "should accept paths Array of two items": function testTwoFieldVector() {
  55. var path = new FieldPath(["foo", "bar"]);
  56. assert.equal(path.getPathLength(), 2);
  57. assert.equal(path.getPath(false), "foo.bar");
  58. },
  59. "should throw Error if given a path String and 2nd field is a '$'-prefixed String": function testDollarSignPrefixSecondField() {
  60. assert.throws(function() {
  61. new FieldPath("a.$b");
  62. });
  63. },
  64. "should accept path String when it contains two dots": function testTwoDotted() {
  65. var path = new FieldPath("foo.bar.baz");
  66. assert.equal(path.getPathLength(), 3);
  67. assert.equal(path.getFieldName(0), "foo");
  68. assert.equal(path.getFieldName(1), "bar");
  69. assert.equal(path.getFieldName(2), "baz");
  70. assert.equal(path.getPath(false), "foo.bar.baz");
  71. },
  72. "should throw Error if given path String ends in a dot": function testTerminalDot() {
  73. assert.throws(function() {
  74. new FieldPath("foo.");
  75. });
  76. },
  77. "should throw Error if given path String begins in a dot": function testPrefixDot() {
  78. assert.throws(function() {
  79. new FieldPath(".foo");
  80. });
  81. },
  82. "should throw Error if given path String contains adjacent dots": function testAdjacentDots() {
  83. assert.throws(function() {
  84. new FieldPath("foo..bar");
  85. });
  86. },
  87. "should accept path String containing one letter between two dots": function testLetterBetweenDots() {
  88. var path = new FieldPath("foo.a.bar");
  89. assert.equal(path.getPathLength(), 3);
  90. assert.equal(path.getPath(false), "foo.a.bar");
  91. },
  92. "should throw Error if given path String contains a null character": function testNullCharacter() {
  93. assert.throws(function() {
  94. new FieldPath("foo.b\0r");
  95. });
  96. },
  97. "should throw Error if given path Array contains an item with a null character": function testVectorNullCharacter() {
  98. assert.throws(function() {
  99. new FieldPath(["foo", "b\0r"]);
  100. });
  101. }
  102. },
  103. "#tail()": {
  104. "should be able to get all but last part of field part of path with 2 fields": function testTail() {
  105. var path = new FieldPath("foo.bar").tail();
  106. assert.equal(path.getPathLength(), 1);
  107. assert.equal(path.getPath(), "bar");
  108. },
  109. "should be able to get all but last part of field part of path with 3 fields": function testTailThreeFields() {
  110. var path = new FieldPath("foo.bar.baz").tail();
  111. assert.equal(path.getPathLength(), 2);
  112. assert.equal(path.getPath(), "bar.baz");
  113. }
  114. }
  115. };