forked from mozilla/rhino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
package org.mozilla.javascript.tests; | ||
|
||
import org.junit.*; | ||
import org.mozilla.javascript.CompilerEnvirons; | ||
import org.mozilla.javascript.Parser; | ||
import org.mozilla.javascript.ast.AstNode; | ||
import org.mozilla.javascript.ast.AstRoot; | ||
import org.mozilla.javascript.ast.Comment; | ||
|
||
import java.util.SortedSet; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Tests position of Comment node in source code. | ||
*/ | ||
public class Issue533Test { | ||
private static final String SOURCE_URI = "issue533test.js"; | ||
|
||
private Parser parser; | ||
|
||
@Before | ||
public void setUp() { | ||
CompilerEnvirons compilerEnv = new CompilerEnvirons(); | ||
compilerEnv.setRecordingComments(true); | ||
parser = new Parser(compilerEnv); | ||
} | ||
|
||
@Test | ||
public void testGetPosition() { | ||
String script = "function a() {\n //testtest\n function b() {\n //password\n }\n}"; | ||
AstRoot root = parser.parse(script, SOURCE_URI, 0); | ||
SortedSet<Comment> comments = root.getComments(); | ||
assertEquals(2, comments.size()); | ||
for(Comment comment:comments) { | ||
assertEquals(comment.getValue(), getFromSource(script, comment)); | ||
} | ||
} | ||
|
||
private String getFromSource(String source, AstNode node) { | ||
return source.substring(node.getAbsolutePosition(), node.getAbsolutePosition() + node.getLength()); | ||
} | ||
} |