FieldPath.js 4.7 KB

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