queryType.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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("Type query", {
  11. "conditional": function () {
  12. var matches = esquery(conditional, "Program");
  13. assert.contains([conditional], matches);
  14. matches = esquery(conditional, "IfStatement");
  15. assert.contains([
  16. conditional.body[0],
  17. conditional.body[1],
  18. conditional.body[1].alternate
  19. ], matches);
  20. matches = esquery(conditional, "LogicalExpression");
  21. assert.contains([
  22. conditional.body[1].test,
  23. conditional.body[1].test.left
  24. ], matches);
  25. matches = esquery(conditional, "ExpressionStatement");
  26. assert.contains([
  27. conditional.body[0].consequent.body[0],
  28. conditional.body[0].alternate.body[0],
  29. conditional.body[1].consequent.body[0],
  30. conditional.body[1].alternate.consequent.body[0]
  31. ], matches);
  32. },
  33. "for loop": function () {
  34. var matches = esquery(forLoop, "Program");
  35. assert.contains([forLoop], matches);
  36. matches = esquery(forLoop, "ForStatement");
  37. assert.contains([
  38. forLoop.body[0]
  39. ], matches);
  40. matches = esquery(forLoop, "BinaryExpression");
  41. assert.contains([
  42. forLoop.body[0].test
  43. ], matches);
  44. },
  45. "simple function": function () {
  46. var matches = esquery(simpleFunction, "Program");
  47. assert.contains([simpleFunction], matches);
  48. matches = esquery(simpleFunction, "VariableDeclaration");
  49. assert.contains([
  50. simpleFunction.body[0].body.body[0]
  51. ], matches);
  52. matches = esquery(simpleFunction, "FunctionDeclaration");
  53. assert.contains([
  54. simpleFunction.body[0]
  55. ], matches);
  56. matches = esquery(simpleFunction, "ReturnStatement");
  57. assert.contains([
  58. simpleFunction.body[0].body.body[2]
  59. ], matches);
  60. },
  61. "simple program": function () {
  62. var matches = esquery(simpleProgram, "Program");
  63. assert.contains([simpleProgram], matches);
  64. matches = esquery(simpleProgram, "VariableDeclaration");
  65. assert.contains([
  66. simpleProgram.body[0],
  67. simpleProgram.body[1]
  68. ], matches);
  69. matches = esquery(simpleProgram, "AssignmentExpression");
  70. assert.contains([
  71. simpleProgram.body[2].expression,
  72. simpleProgram.body[3].consequent.body[0].expression
  73. ], matches);
  74. matches = esquery(simpleProgram, "Identifier");
  75. assert.contains([
  76. simpleProgram.body[0].declarations[0].id,
  77. simpleProgram.body[1].declarations[0].id,
  78. simpleProgram.body[2].expression.left,
  79. simpleProgram.body[2].expression.right.left,
  80. simpleProgram.body[3].test,
  81. simpleProgram.body[3].consequent.body[0].expression.left
  82. ], matches);
  83. },
  84. "# type": function () {
  85. var matches = esquery(forLoop, "#Program");
  86. assert.contains([
  87. forLoop
  88. ], matches);
  89. matches = esquery(forLoop, "#ForStatement");
  90. assert.contains([
  91. forLoop.body[0]
  92. ], matches);
  93. matches = esquery(forLoop, "#BinaryExpression");
  94. assert.contains([
  95. forLoop.body[0].test
  96. ], matches);
  97. },
  98. "case insensitive type": function () {
  99. var matches = esquery(forLoop, "Program");
  100. assert.contains([
  101. forLoop
  102. ], matches);
  103. matches = esquery(forLoop, "forStatement");
  104. assert.contains([
  105. forLoop.body[0]
  106. ], matches);
  107. matches = esquery(forLoop, "binaryexpression");
  108. assert.contains([
  109. forLoop.body[0].test
  110. ], matches);
  111. }
  112. });
  113. });