ConnectionMatchers.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import {
  2. pick
  3. } from 'min-dash';
  4. import {
  5. getDi
  6. } from 'lib/util/ModelUtil';
  7. var POSITION_ATTRS = [ 'x', 'y' ];
  8. function getPoint(point) {
  9. return pick(point, POSITION_ATTRS);
  10. }
  11. function getPoints(waypoints) {
  12. return waypoints.map(getPoint);
  13. }
  14. export default function(chai, utils) {
  15. var Assertion = chai.Assertion;
  16. function inspect(obj) {
  17. return utils.inspect(obj).replace(/\n /g, '');
  18. }
  19. /**
  20. * A simple waypoints matcher, that verifies a connection
  21. * consists of the correct connection points.
  22. *
  23. * Does not take the original docking into account.
  24. *
  25. * @example
  26. *
  27. * expect(connection).to.have.waypoints([ { x: 100, y: 100 }, { x: 0, y: 0 } ]);
  28. *
  29. * @param {Connection|Array<Point>} exp
  30. */
  31. Assertion.addMethod('waypoints', function(exp) {
  32. var obj = this._obj;
  33. expect(obj).to.have.property('waypoints');
  34. assertWaypoints(this, obj.id + '#waypoints', getPoints(obj.waypoints), getPoints(exp));
  35. });
  36. /**
  37. * A simple waypoints matcher, that verifies a connection
  38. * consists of the correct DI waypoints.
  39. *
  40. * Does not take the original docking into account.
  41. *
  42. * @example
  43. *
  44. * expect(connection).to.have.diWaypoints([ { x: 100, y: 100 }, { x: 0, y: 0 } ]);
  45. *
  46. * @param {Connection|Point[]} exp
  47. */
  48. Assertion.addMethod('diWaypoints', function(exp) {
  49. var obj = this._obj;
  50. var di = getDi(obj);
  51. expect(di).to.exist;
  52. expect(di).to.have.property('waypoint');
  53. assertWaypoints(this, di + '#waypoint', getPoints(di.waypoint), getPoints(exp));
  54. });
  55. /**
  56. * A simple waypoints matcher, that verifies a connection
  57. * has the given start docking.
  58. *
  59. * @example
  60. *
  61. * expect(connection).to.have.startDocking({ x: 100, y: 100 });
  62. *
  63. * @param {Point} exp
  64. */
  65. Assertion.addMethod('startDocking', function(exp) {
  66. var obj = this._obj;
  67. var startPoint = obj.waypoints[0],
  68. startDocking = startPoint && startPoint.original;
  69. var matches = utils.eql(startDocking, exp);
  70. var startDockingStr = inspect(startDocking),
  71. expectedStartDockingStr = inspect(exp);
  72. var theAssert = new Assertion(startDocking);
  73. // transfer negate status
  74. utils.transferFlags(this, theAssert, false);
  75. theAssert.assert(
  76. matches,
  77. 'expected <' + obj.id + '> to have startDocking ' +
  78. expectedStartDockingStr + ' but got ' + startDockingStr
  79. );
  80. });
  81. /**
  82. * A simple waypoints matcher, that verifies a connection
  83. * has the given start docking.
  84. *
  85. * @example
  86. *
  87. * expect(connection).to.have.endDocking({ x: 100, y: 100 });
  88. *
  89. * @param {Point} exp
  90. */
  91. Assertion.addMethod('endDocking', function(exp) {
  92. var obj = this._obj;
  93. var endPoint = obj.waypoints[obj.waypoints.length - 1],
  94. endDocking = endPoint && endPoint.original;
  95. var matches = utils.eql(endDocking, exp);
  96. var endDockingStr = inspect(endDocking),
  97. expectedEndDockingStr = inspect(exp);
  98. var theAssert = new Assertion(endDocking);
  99. // transfer negate status
  100. utils.transferFlags(this, theAssert, false);
  101. theAssert.assert(
  102. matches,
  103. 'expected <' + obj.id + '> to have endDocking ' +
  104. expectedEndDockingStr + ' but got ' + endDockingStr
  105. );
  106. });
  107. // helpers ////////////////
  108. function assertWaypoints(self, desc, waypoints, expectedWaypoints) {
  109. var matches = utils.eql(waypoints, expectedWaypoints);
  110. var waypointsStr = inspect(waypoints),
  111. expectedWaypointsStr = inspect(expectedWaypoints);
  112. var theAssert = new Assertion(waypoints);
  113. // transfer negate status
  114. utils.transferFlags(self, theAssert, false);
  115. theAssert.assert(
  116. matches,
  117. 'expected <' + desc + '> ' +
  118. 'to equal \n ' + expectedWaypointsStr +
  119. '\nbut got\n ' + waypointsStr,
  120. 'expected <' + desc + '> ' +
  121. 'not to equal \n ' + expectedWaypoints,
  122. expectedWaypoints
  123. );
  124. }
  125. }