FieldPath_test.js 4.3 KB

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