Debian Codegraph @

rhino/testsrc/org/mozilla/javascript/tests/GeneratedClassNameTest.java

raw symbols SCIP
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 package org.mozilla.javascript.tests;
6
7 import org.mozilla.javascript.Context;
8 import org.mozilla.javascript.ContextAction;
9 import org.mozilla.javascript.ContextFactory;
10 import org.mozilla.javascript.Script;
11
12 import junit.framework.TestCase;
13
14 /**
15 * Takes care that the class name of the generated class "looks like"
16 * the provided script name.
17 * See https://bugzilla.mozilla.org/show_bug.cgi?id=460283
18 * @author Marc Guillemot
19 */
20 public class GeneratedClassNameTest extends TestCase
21 {
22 public void testGeneratedClassName() throws Exception {
23 doTest("myScript_js", "myScript.js");
24 doTest("foo", "foo");
25 doTest("c", "");
26 doTest("_1", "1");
27 doTest("_", "_");
28 doTest("unnamed_script", null);
29 doTest("some_dir_some_foo_js", "some/dir/some/foo.js");
30 doTest("some_dir_some_foo_js", "some\\dir\\some\\foo.js");
31 doTest("_12_foo_34_js", "12 foo 34.js");
32 }
33
34 private void doTest(final String expectedName, final String scriptName)
35 throws Exception
36 {
37 final Script script = (Script) ContextFactory.getGlobal().call(
38 new ContextAction() {
39 public Object run(final Context context) {
40 return context.compileString("var f = 1", scriptName, 1, null);
41 }
42 });
43
44 // remove serial number
45 String name = script.getClass().getSimpleName();
46 assertEquals(expectedName, name.substring(0, name.lastIndexOf('_')));
47 }
48 }