/javascript/src/main/java/com/google/test/metric/javascript/JavascriptTestability.java

http://testability-explorer.googlecode.com/ · Java · 60 lines · 33 code · 7 blank · 20 comment · 5 complexity · fa8e640d2e237592aa9b500b9f84f469 MD5 · raw file

  1. /*
  2. * Copyright 2007 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.test.metric.javascript;
  17. import org.mozilla.javascript.ast.AstRoot;
  18. import org.mozilla.javascript.ast.NodeVisitor;
  19. import org.mozilla.javascript.ast.AstNode;
  20. import org.mozilla.javascript.Token;
  21. import java.io.IOException;
  22. /**
  23. * Main entry point for assessing cost to test Javascript
  24. *
  25. * @author alexeagle@google.com (Alex Eagle)
  26. */
  27. public class JavascriptTestability {
  28. private final FileRepository repository;
  29. public JavascriptTestability(FileRepository repository) {
  30. this.repository = repository;
  31. }
  32. public int calculateCost() throws IOException {
  33. FunctionCountNodeVisitor v = new FunctionCountNodeVisitor();
  34. for (AstRoot astRoot : repository.getAsts()) {
  35. astRoot.visit(v);
  36. }
  37. return v.getCount();
  38. }
  39. private class FunctionCountNodeVisitor implements NodeVisitor {
  40. int count = 0;
  41. public boolean visit(AstNode node) {
  42. if (node.getType() == Token.FUNCTION) {
  43. if (node.getEnclosingFunction() != null) {
  44. count++;
  45. }
  46. }
  47. return true;
  48. }
  49. public int getCount() {
  50. return count;
  51. }
  52. }
  53. }