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