querySubject.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. define([
  2. "esquery",
  3. "jstestr/assert",
  4. "jstestr/test",
  5. "./fixtures/conditional",
  6. "./fixtures/forLoop",
  7. "./fixtures/simpleFunction",
  8. "./fixtures/simpleProgram",
  9. "./fixtures/nestedFunctions",
  10. "./fixtures/bigArray"
  11. ], function (esquery, assert, test, conditional, forLoop, simpleFunction, simpleProgram, nestedFunctions, bigArray) {
  12. test.defineSuite("Query subject", {
  13. "type subject": function () {
  14. var matches = esquery(conditional, "!IfStatement Identifier");
  15. assert.contains([
  16. conditional.body[0],
  17. conditional.body[1],
  18. conditional.body[1].alternate
  19. ], matches);
  20. },
  21. "* subject": function () {
  22. var matches = esquery(forLoop, '!* > [name="foo"]');
  23. assert.contains([
  24. forLoop.body[0].test.right,
  25. forLoop.body[0].body.body[0].expression.callee
  26. ], matches);
  27. },
  28. ":nth-child subject": function () {
  29. var matches = esquery(simpleFunction, '!:nth-child(1) [name="y"]');
  30. assert.contains([
  31. simpleFunction.body[0],
  32. simpleFunction.body[0].body.body[0],
  33. simpleFunction.body[0].body.body[0].declarations[0]
  34. ], matches);
  35. },
  36. ":nth-last-child subject": function () {
  37. var matches = esquery(simpleProgram, '!:nth-last-child(1) [name="y"]');
  38. assert.contains([
  39. simpleProgram.body[3],
  40. simpleProgram.body[1].declarations[0],
  41. simpleProgram.body[3].consequent.body[0]
  42. ], matches);
  43. },
  44. "attribute literal subject": function () {
  45. var matches = esquery(simpleProgram, '![test] [name="y"]');
  46. assert.contains([
  47. simpleProgram.body[3]
  48. ], matches);
  49. },
  50. "attribute type subject": function () {
  51. var matches = esquery(nestedFunctions, '![generator=type(boolean)] > BlockStatement');
  52. assert.contains([
  53. nestedFunctions.body[0],
  54. nestedFunctions.body[0].body.body[1]
  55. ], matches);
  56. },
  57. "attribute regexp subject": function () {
  58. var matches = esquery(conditional, '![operator=/=+/] > [name="x"]');
  59. assert.contains([
  60. conditional.body[0].test,
  61. conditional.body[0].alternate.body[0].expression,
  62. conditional.body[1].test.left.left
  63. ], matches);
  64. },
  65. "field subject": function () {
  66. var matches = esquery(forLoop, '!.test');
  67. assert.contains([
  68. forLoop.body[0].test
  69. ], matches);
  70. },
  71. ":matches subject": function () {
  72. var matches = esquery(forLoop, '!:matches(*) > [name="foo"]');
  73. assert.contains([
  74. forLoop.body[0].test.right,
  75. forLoop.body[0].body.body[0].expression.callee
  76. ], matches);
  77. },
  78. ":not subject": function () {
  79. var matches = esquery(nestedFunctions, '!:not(BlockStatement) > [name="foo"]');
  80. assert.contains([
  81. nestedFunctions.body[0]
  82. ], matches);
  83. },
  84. "compound attributes subject": function () {
  85. var matches = esquery(conditional, '![left.name="x"][right.value=1]');
  86. assert.contains([
  87. conditional.body[0].test
  88. ], matches);
  89. },
  90. "decendent right subject": function () {
  91. var matches = esquery(forLoop, '* !AssignmentExpression');
  92. assert.contains([
  93. forLoop.body[0].init
  94. ], matches);
  95. },
  96. "child right subject": function () {
  97. var matches = esquery(forLoop, '* > !AssignmentExpression');
  98. assert.contains([
  99. forLoop.body[0].init
  100. ], matches);
  101. },
  102. "sibling left subject": function () {
  103. var matches = esquery(simpleProgram, "!VariableDeclaration ~ IfStatement");
  104. assert.contains([
  105. simpleProgram.body[0],
  106. simpleProgram.body[1]
  107. ], matches);
  108. },
  109. "sibling right subject": function () {
  110. var matches = esquery(simpleProgram, "!VariableDeclaration ~ !IfStatement");
  111. assert.contains([
  112. simpleProgram.body[0],
  113. simpleProgram.body[1],
  114. simpleProgram.body[3]
  115. ], matches);
  116. },
  117. "adjacent right subject": function () {
  118. var matches = esquery(simpleProgram, "!VariableDeclaration + !ExpressionStatement");
  119. assert.contains([
  120. simpleProgram.body[1],
  121. simpleProgram.body[2]
  122. ], matches);
  123. },
  124. "multiple adjacent siblings": function () {
  125. var matches = esquery(bigArray, "Identifier + Identifier");
  126. assert.contains([
  127. bigArray.body[0].expression.elements[4],
  128. bigArray.body[0].expression.elements[8]
  129. ], matches);
  130. assert.isSame(2, matches.length);
  131. },
  132. "multiple siblings": function () {
  133. var matches = esquery(bigArray, "Identifier ~ Identifier");
  134. assert.contains([
  135. bigArray.body[0].expression.elements[4],
  136. bigArray.body[0].expression.elements[7],
  137. bigArray.body[0].expression.elements[8]
  138. ], matches);
  139. assert.isSame(3, matches.length);
  140. }
  141. });
  142. });