queryAttribute.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. define([
  2. "esquery",
  3. "jstestr/assert",
  4. "jstestr/test",
  5. "./fixtures/conditional",
  6. "./fixtures/forLoop",
  7. "./fixtures/simpleFunction",
  8. "./fixtures/simpleProgram"
  9. ], function (esquery, assert, test, conditional, forLoop, simpleFunction, simpleProgram) {
  10. test.defineSuite("Attribute query", {
  11. "conditional": function () {
  12. var matches = esquery(conditional, "[name=\"x\"]");
  13. assert.contains([
  14. conditional.body[0].test.left,
  15. conditional.body[0].alternate.body[0].expression.left,
  16. conditional.body[1].test.left.left.left,
  17. conditional.body[1].test.right
  18. ], matches);
  19. matches = esquery(conditional, "[callee.name=\"foo\"]");
  20. assert.contains([
  21. conditional.body[0].consequent.body[0].expression
  22. ], matches);
  23. matches = esquery(conditional, "[operator]");
  24. assert.contains([
  25. conditional.body[0].test,
  26. conditional.body[0].alternate.body[0].expression,
  27. conditional.body[1].test,
  28. conditional.body[1].test.left,
  29. conditional.body[1].test.left.left
  30. ], matches);
  31. matches = esquery(conditional, "[prefix=true]");
  32. assert.contains([
  33. conditional.body[1].consequent.body[0].expression.right
  34. ], matches);
  35. },
  36. "for loop": function () {
  37. var matches = esquery(forLoop, "[operator=\"=\"]");
  38. assert.contains([
  39. forLoop.body[0].init
  40. ], matches);
  41. matches = esquery(forLoop, "[object.name=\"foo\"]");
  42. assert.contains([
  43. forLoop.body[0].test.right
  44. ], matches);
  45. matches = esquery(forLoop, "[operator]");
  46. assert.contains([
  47. forLoop.body[0].init,
  48. forLoop.body[0].test,
  49. forLoop.body[0].update
  50. ], matches);
  51. },
  52. "simple function": function () {
  53. var matches = esquery(simpleFunction, "[kind=\"var\"]");
  54. assert.contains([
  55. simpleFunction.body[0].body.body[0]
  56. ], matches);
  57. matches = esquery(simpleFunction, "[id.name=\"foo\"]");
  58. assert.contains([
  59. simpleFunction.body[0]
  60. ], matches);
  61. matches = esquery(simpleFunction, "[left]");
  62. assert.contains([
  63. simpleFunction.body[0].body.body[0].declarations[0].init
  64. ], matches);
  65. },
  66. "simple program": function () {
  67. var matches = esquery(simpleProgram, "[kind=\"var\"]");
  68. assert.contains([
  69. simpleProgram.body[0],
  70. simpleProgram.body[1]
  71. ], matches);
  72. matches = esquery(simpleProgram, "[id.name=\"y\"]");
  73. assert.contains([
  74. simpleProgram.body[1].declarations[0]
  75. ], matches);
  76. matches = esquery(simpleProgram, "[body]");
  77. assert.contains([
  78. simpleProgram,
  79. simpleProgram.body[3].consequent
  80. ], matches);
  81. },
  82. "conditional regexp": function () {
  83. var matches = esquery(conditional, "[name=/x|foo/]");
  84. assert.contains([
  85. conditional.body[0].test.left,
  86. conditional.body[0].consequent.body[0].expression.callee,
  87. conditional.body[0].alternate.body[0].expression.left
  88. ], matches);
  89. },
  90. "simple function regexp": function () {
  91. var matches = esquery(simpleFunction, "[name=/x|foo/]");
  92. assert.contains([
  93. simpleFunction.body[0].id,
  94. simpleFunction.body[0].params[0],
  95. simpleFunction.body[0].body.body[0].declarations[0].init.left
  96. ], matches);
  97. },
  98. "simple function numeric": function () {
  99. var matches = esquery(simpleFunction, "FunctionDeclaration[params.0.name=x]");
  100. assert.contains([
  101. simpleFunction.body[0]
  102. ], matches);
  103. },
  104. "simple program regexp": function () {
  105. var matches = esquery(simpleProgram, "[name=/[asdfy]/]");
  106. assert.contains([
  107. simpleProgram.body[1].declarations[0].id,
  108. simpleProgram.body[3].test,
  109. simpleProgram.body[3].consequent.body[0].expression.left
  110. ], matches);
  111. },
  112. "for loop regexp": function () {
  113. var matches = esquery(forLoop, "[name=/i|foo/]");
  114. assert.contains([
  115. forLoop.body[0].init.left,
  116. forLoop.body[0].test.left,
  117. forLoop.body[0].test.right.object,
  118. forLoop.body[0].update.argument,
  119. forLoop.body[0].body.body[0].expression.callee.object,
  120. forLoop.body[0].body.body[0].expression.callee.property
  121. ], matches);
  122. },
  123. "nonexistent attribute regexp": function () {
  124. var matches = esquery(conditional, '[foobar=/./]');
  125. assert.isSame(0, matches.length);
  126. },
  127. "not string": function () {
  128. var matches = esquery(conditional, '[name!="x"]');
  129. assert.contains([
  130. conditional.body[0].consequent.body[0].expression.callee,
  131. conditional.body[1].consequent.body[0].expression.left
  132. ], matches);
  133. },
  134. "not type": function () {
  135. var matches = esquery(conditional, '[value!=type(number)]');
  136. assert.contains([
  137. conditional.body[1].test.left.left.right,
  138. conditional.body[1].test.left.right,
  139. conditional.body[1].alternate
  140. ], matches);
  141. },
  142. "not regexp": function () {
  143. var matches = esquery(conditional, '[name!=/x|y/]');
  144. assert.contains([
  145. conditional.body[0].consequent.body[0].expression.callee
  146. ], matches);
  147. },
  148. "less than": function () {
  149. var matches = esquery(conditional, "[body.length<2]");
  150. assert.contains([
  151. conditional.body[0].consequent,
  152. conditional.body[0].alternate,
  153. conditional.body[1].consequent,
  154. conditional.body[1].alternate.consequent
  155. ], matches);
  156. },
  157. "greater than": function () {
  158. var matches = esquery(conditional, "[body.length>1]");
  159. assert.contains([
  160. conditional
  161. ], matches);
  162. },
  163. "less than or equal": function () {
  164. var matches = esquery(conditional, "[body.length<=2]");
  165. assert.contains([
  166. conditional,
  167. conditional.body[0].consequent,
  168. conditional.body[0].alternate,
  169. conditional.body[1].consequent,
  170. conditional.body[1].alternate.consequent
  171. ], matches);
  172. },
  173. "greater than or equal": function () {
  174. var matches = esquery(conditional, "[body.length>=1]");
  175. assert.contains([
  176. conditional,
  177. conditional.body[0].consequent,
  178. conditional.body[0].alternate,
  179. conditional.body[1].consequent,
  180. conditional.body[1].alternate.consequent
  181. ], matches);
  182. },
  183. "attribute type": function () {
  184. var matches = esquery(conditional, "[test=type(object)]");
  185. assert.contains([
  186. conditional.body[0],
  187. conditional.body[1],
  188. conditional.body[1].alternate
  189. ], matches);
  190. matches = esquery(conditional, "[value=type(boolean)]");
  191. assert.contains([
  192. conditional.body[1].test.left.right,
  193. conditional.body[1].alternate.test
  194. ], matches);
  195. }
  196. });
  197. });