queryMatches.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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("Pseudo matches query", {
  11. "conditional matches": function () {
  12. var matches = esquery(conditional, ":matches(IfStatement)");
  13. assert.contains([
  14. conditional.body[0],
  15. conditional.body[1].alternate
  16. ], matches);
  17. },
  18. "for loop matches": function () {
  19. var matches = esquery(forLoop, ":matches(BinaryExpression, MemberExpression)");
  20. assert.contains([
  21. forLoop.body[0].test,
  22. forLoop.body[0].body.body[0].expression.callee
  23. ], matches);
  24. },
  25. "simple function matches": function () {
  26. var matches = esquery(simpleFunction, ':matches([name="foo"], ReturnStatement)');
  27. assert.contains([
  28. simpleFunction.body[0].id,
  29. simpleFunction.body[0].body.body[2]
  30. ], matches);
  31. },
  32. "simple program matches": function () {
  33. var matches = esquery(simpleProgram, ":matches(AssignmentExpression, BinaryExpression)");
  34. assert.contains([
  35. simpleProgram.body[2].expression,
  36. simpleProgram.body[3].consequent.body[0].expression,
  37. simpleProgram.body[2].expression.right
  38. ], matches);
  39. },
  40. "implicit matches": function () {
  41. var matches = esquery(simpleProgram, "AssignmentExpression, BinaryExpression, NonExistant");
  42. assert.contains([
  43. simpleProgram.body[2].expression,
  44. simpleProgram.body[3].consequent.body[0].expression,
  45. simpleProgram.body[2].expression.right
  46. ], matches);
  47. }
  48. });
  49. });