FieldPath.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. "use strict";
  2. var assert = require("assert"),
  3. FieldPath = require("../../../lib/pipeline/FieldPath");
  4. module.exports = {
  5. "FieldPath": {
  6. "constructor(path)": {
  7. "should throw Error if given an empty path String": function empty() {
  8. assert.throws(function() {
  9. new FieldPath("");
  10. });
  11. },
  12. "should throw Error if given an empty path Array": function emptVector() {
  13. assert.throws(function() {
  14. new FieldPath([]);
  15. });
  16. },
  17. "should accept simple paths as a String (without dots)": function simple() {
  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 simpleVector() {
  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. assert.equal(path.getPath(true), "$foo");
  30. },
  31. "should throw Error if given a '$' String": function dollarSign() {
  32. assert.throws(function() {
  33. new FieldPath("$");
  34. });
  35. },
  36. "should throw Error if given a '$'-prefixed String": function dollarSignPrefix() {
  37. assert.throws(function() {
  38. new FieldPath("$a");
  39. });
  40. },
  41. "should accept paths as a String with one dot": function dotted() {
  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 vectorWithDot() {
  50. assert.throws(function() {
  51. new FieldPath(["fo.o"]);
  52. });
  53. },
  54. "should accept paths Array of two items": function twoFieldVector() {
  55. var path = new FieldPath(["foo", "bar"]);
  56. assert.equal(path.getPathLength(), 2);
  57. assert.equal(path.getFieldName(0), "foo");
  58. assert.equal(path.getFieldName(1), "bar");
  59. assert.equal(path.getPath(false), "foo.bar");
  60. assert.equal(path.getPath(true), "$foo.bar");
  61. },
  62. "should throw Error if given a path String and 2nd field is a '$'-prefixed String": function dollarSignPrefixSecondField() {
  63. assert.throws(function() {
  64. new FieldPath("a.$b");
  65. });
  66. },
  67. "should accept path String when it contains two dots": function twoDotted() {
  68. var path = new FieldPath("foo.bar.baz");
  69. assert.equal(path.getPathLength(), 3);
  70. assert.equal(path.getFieldName(0), "foo");
  71. assert.equal(path.getFieldName(1), "bar");
  72. assert.equal(path.getFieldName(2), "baz");
  73. assert.equal(path.getPath(false), "foo.bar.baz");
  74. assert.equal(path.getPath(true), "$foo.bar.baz");
  75. },
  76. "should throw Error if given path String ends in a dot": function terminalDot() {
  77. assert.throws(function() {
  78. new FieldPath("foo.");
  79. });
  80. },
  81. "should throw Error if given path String begins in a dot": function prefixDot() {
  82. assert.throws(function() {
  83. new FieldPath(".foo");
  84. });
  85. },
  86. "should throw Error if given path String contains adjacent dots": function adjacentDots() {
  87. assert.throws(function() {
  88. new FieldPath("foo..bar");
  89. });
  90. },
  91. "should accept path String containing one letter between two dots": function letterBetweenDots() {
  92. var path = new FieldPath("foo.a.bar");
  93. assert.equal(path.getPathLength(), 3);
  94. assert.equal(path.getFieldName(0), "foo");
  95. assert.equal(path.getFieldName(1), "a");
  96. assert.equal(path.getFieldName(2), "bar");
  97. assert.equal(path.getPath(false), "foo.a.bar");
  98. assert.equal(path.getPath(true), "$foo.a.bar");
  99. },
  100. "should throw Error if given path String contains a null character": function nullCharacter() {
  101. assert.throws(function() {
  102. new FieldPath("foo.b\0r");
  103. });
  104. },
  105. "should throw Error if given path Array contains an item with a null character": function vectorNullCharacter() {
  106. assert.throws(function() {
  107. new FieldPath(["foo", "b\0r"]);
  108. });
  109. }
  110. },
  111. "#tail()": {
  112. "should be able to get all but last part of field part of path with 2 fields": function tail() {
  113. var path = new FieldPath("foo.bar").tail();
  114. assert.equal(path.getPathLength(), 1);
  115. assert.equal(path.getPath(), "bar");
  116. },
  117. "should be able to get all but last part of field part of path with 3 fields": function tailThreeFields() {
  118. var path = new FieldPath("foo.bar.baz").tail();
  119. assert.equal(path.getPathLength(), 2);
  120. assert.equal(path.getPath(), "bar.baz");
  121. }
  122. }
  123. }
  124. };
  125. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run();