PageRenderTime 27ms CodeModel.GetById 19ms app.highlight 5ms RepoModel.GetById 2ms app.codeStats 0ms

/javascript/src/test/java/com/google/test/metric/javascript/JavascriptParsingTest.java

http://testability-explorer.googlecode.com/
Java | 49 lines | 24 code | 5 blank | 20 comment | 0 complexity | bb35864f14a4d8838f97608315b673c9 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 junit.framework.TestCase;
19import org.mozilla.javascript.Parser;
20import org.mozilla.javascript.ast.AstRoot;
21import org.mozilla.javascript.ast.FunctionNode;
22
23import java.io.InputStreamReader;
24import java.io.Reader;
25
26/**
27 * Basic tests that the Javascript parser produces an Abstract Syntax Tree
28 *
29 * @author alexeagle@google.com (Alex Eagle)
30 */
31public class JavascriptParsingTest extends TestCase {
32
33  public void testThatParsingIsPossible() throws Exception {
34    String js = "function a() {};";
35    Parser parser = new Parser();
36    AstRoot ast = parser.parse(js, "source.js", 1);
37    assertEquals("a", ((FunctionNode)ast.getFirstChild()).getFunctionName().getIdentifier());
38  }
39
40  public void testParseComplexStuff() throws Exception {
41    Reader source = new InputStreamReader(
42        this.getClass().getResourceAsStream("browser_debug.js"));
43    Parser parser = new Parser();
44    AstRoot ast = parser.parse(source, "browser_debug.js", 1);
45    String debugString = ast.debugPrint();
46    assertTrue(debugString.contains("getRequiresAndProvides"));
47    assertTrue(debugString.contains("ARRAYLIT"));
48  }
49}