PageRenderTime 6ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/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 */
16package com.google.test.metric.javascript;
17
18import org.mozilla.javascript.ast.AstRoot;
19import org.mozilla.javascript.ast.NodeVisitor;
20import org.mozilla.javascript.ast.AstNode;
21import org.mozilla.javascript.Token;
22
23import java.io.IOException;
24
25/**
26 * Main entry point for assessing cost to test Javascript
27 *
28 * @author alexeagle@google.com (Alex Eagle)
29 */
30public class JavascriptTestability {
31  private final FileRepository repository;
32
33  public JavascriptTestability(FileRepository repository) {
34    this.repository = repository;
35  }
36
37  public int calculateCost() throws IOException {
38    FunctionCountNodeVisitor v = new FunctionCountNodeVisitor();
39    for (AstRoot astRoot : repository.getAsts()) {
40      astRoot.visit(v);
41    }
42    return v.getCount();
43  }
44
45  private class FunctionCountNodeVisitor implements NodeVisitor {
46    int count = 0;
47    public boolean visit(AstNode node) {
48      if (node.getType() == Token.FUNCTION) {
49        if (node.getEnclosingFunction() != null) {
50          count++;
51        }
52      }
53      return true;
54    }
55
56    public int getCount() {
57      return count;
58    }
59  }
60}