Skip to content

Commit

Permalink
test for mozilla#533
Browse files Browse the repository at this point in the history
  • Loading branch information
nabice committed Apr 24, 2019
1 parent 03c12c2 commit 9254382
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions testsrc/org/mozilla/javascript/tests/Issue533Test.java
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());
}
}

0 comments on commit 9254382

Please sign in to comment.