-
-
Notifications
You must be signed in to change notification settings - Fork 925
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests for new detailed source positions.
- 9.4.12.0
- 9.4.11.0
- 9.4.10.0
- 9.4.9.0
- 9.4.8.0
- 9.4.7.0
- 9.4.6.0
- 9.4.5.0
- 9.4.4.0
- 9.4.3.0
- 9.4.2.0
- 9.4.1.0
- 9.4.0.0
- 9.3.15.0
- 9.3.14.0
- 9.3.13.0
- 9.3.12.0
- 9.3.11.0
- 9.3.10.0
- 9.3.9.0
- 9.3.8.0
- 9.3.7.0
- 9.3.6.0
- 9.3.5.0
- 9.3.4.0
- 9.3.3.0
- 9.3.2.0
- 9.3.1.0
- 9.3.0.0
- 9.2.21.0
- 9.2.20.1
- 9.2.20.0
- 9.2.19.0
- 9.2.18.0
- 9.2.17.0
- 9.2.16.0
- 9.2.15.0
- 9.2.14.0
- 9.2.13.0
- 9.2.12.0
- 9.2.11.1
- 9.2.11.0
- 9.2.10.0
- 9.2.9.0
- 9.2.8.0
- 9.2.7.0
- 9.2.6.0
- 9.2.5.0
- 9.2.4.1
- 9.2.4.0
- 9.2.3.0
- 9.2.2.0
- 9.2.1.0
- 9.2.0.0
- 9.1.17.0
- 9.1.16.0
- 9.1.15.0
- 9.1.14.0
- 9.1.13.0
- 9.1.12.0
- 9.1.11.0
- 9.1.10.0
- 9.1.9.0
- 9.1.8.0
- 9.1.7.0
- 9.1.6.0
- 9.1.5.0
- 9.1.4.0
- 9.1.3.0
- 9.1.2.0
- 9.1.1.0
- 9.1.0.0
- 9.0.5.0
- 9.0.4.0
- 9.0.3.0
- 9.0.1.0
- 9.0.0.0
- 9.0.0.0.rc2
- 9.0.0.0.rc1
- 9.0.0.0.pre2
- 9.0.0.0.pre1
1 parent
43c0df9
commit 1a93885
Showing
21 changed files
with
239 additions
and
47 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
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
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
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
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
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
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
36 changes: 36 additions & 0 deletions
36
core/src/main/java/org/jruby/lexer/yacc/DetailedSourcePosition.java
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,36 @@ | ||
/* | ||
* Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. This | ||
* code is released under a tri EPL/GPL/LGPL license. You can use it, | ||
* redistribute it and/or modify it under the terms of the: | ||
* | ||
* Eclipse Public License version 1.0 | ||
* GNU General Public License version 2 | ||
* GNU Lesser General Public License version 2.1 | ||
*/ | ||
package org.jruby.lexer.yacc; | ||
|
||
public class DetailedSourcePosition extends SimpleSourcePosition { | ||
|
||
final int offset; | ||
final int length; | ||
|
||
public DetailedSourcePosition(String filename, int line, int offset, int length) { | ||
super(filename, line); | ||
this.offset = offset; | ||
this.length = length; | ||
} | ||
|
||
public int getOffset() { | ||
return offset; | ||
} | ||
|
||
public int getLength() { | ||
return length; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("%s:%d:%d:%d", getFile(), getLine() + 1, offset, length); | ||
} | ||
|
||
} |
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
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
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
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
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
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
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
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
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
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
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
164 changes: 164 additions & 0 deletions
164
core/src/test/java/org/jruby/parser/DetailedSourcePositionTest.java
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,164 @@ | ||
/* | ||
* Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. This | ||
* code is released under a tri EPL/GPL/LGPL license. You can use it, | ||
* redistribute it and/or modify it under the terms of the: | ||
* | ||
* Eclipse Public License version 1.0 | ||
* GNU General Public License version 2 | ||
* GNU Lesser General Public License version 2.1 | ||
*/ | ||
package org.jruby.parser; | ||
|
||
import junit.framework.TestCase; | ||
import org.jruby.Ruby; | ||
import org.jruby.ast.*; | ||
import org.jruby.ast.visitor.AbstractNodeVisitor; | ||
import org.jruby.lexer.yacc.ISourcePosition; | ||
import org.jruby.lexer.yacc.SimpleSourcePosition; | ||
import org.jruby.runtime.scope.ManyVarsDynamicScope; | ||
|
||
public class DetailedSourcePositionTest extends TestCase { | ||
|
||
public void testSingleLineFixnum() { | ||
final SimpleSourcePosition position = detailedSource(find(parse(" 14 "), FixnumNode.class)); | ||
assertEquals("test", position.getFile()); | ||
assertEquals(0, position.getLine()); | ||
// assertEquals(2, position.getOffset()); | ||
// assertEquals(2, position.getLength()); | ||
} | ||
|
||
public void testMultiLineFixnum() { | ||
final SimpleSourcePosition position = detailedSource(find(parse("true\n14\nfalse\n"), FixnumNode.class)); | ||
assertEquals("test", position.getFile()); | ||
assertEquals(1, position.getLine()); | ||
// assertEquals(5, position.getOffset()); | ||
// assertEquals(2, position.getLength()); | ||
} | ||
|
||
public void testSingleLineAssignment() { | ||
final SimpleSourcePosition position = detailedSource(find(parse("true\nx = 14\nfalse\n"), LocalAsgnNode.class)); | ||
assertEquals("test", position.getFile()); | ||
assertEquals(1, position.getLine()); | ||
// assertEquals(5, position.getOffset()); | ||
// assertEquals(6, position.getLength()); | ||
} | ||
|
||
public void testMultiLineAssignment() { | ||
final SimpleSourcePosition position = detailedSource(find(parse("true\nx = \n14\nfalse\n"), LocalAsgnNode.class)); | ||
assertEquals("test", position.getFile()); | ||
assertEquals(1, position.getLine()); | ||
// assertEquals(5, position.getOffset()); | ||
// assertEquals(7, position.getLength()); | ||
} | ||
|
||
public void testSingleLineIf() { | ||
final SimpleSourcePosition position = detailedSource(find(parse("true\nif true; false else true end\nfalse\n"), IfNode.class)); | ||
assertEquals("test", position.getFile()); | ||
assertEquals(1, position.getLine()); | ||
// assertEquals(5, position.getOffset()); | ||
// assertEquals(29, position.getLength()); | ||
} | ||
|
||
public void testMultiLineIf() { | ||
final SimpleSourcePosition position = detailedSource(find(parse("true\nif true\n false\nelse\n true\nend\nfalse\n"), IfNode.class)); | ||
assertEquals("test", position.getFile()); | ||
assertEquals(1, position.getLine()); | ||
// assertEquals(5, position.getOffset()); | ||
// assertEquals(31, position.getLength()); | ||
} | ||
|
||
public void testSingleLineDef() { | ||
final SimpleSourcePosition position = detailedSource(find(parse("true\ndef foo; true end\nfalse\n"), DefnNode.class)); | ||
assertEquals("test", position.getFile()); | ||
assertEquals(1, position.getLine()); | ||
// assertEquals(5, position.getOffset()); | ||
// assertEquals(18, position.getLength()); | ||
} | ||
|
||
public void testMultiLineDef() { | ||
final SimpleSourcePosition position = detailedSource(find(parse("true\ndef foo\n true\nend\nfalse\n"), DefnNode.class)); | ||
assertEquals("test", position.getFile()); | ||
assertEquals(1, position.getLine()); | ||
// assertEquals(5, position.getOffset()); | ||
// assertEquals(18, position.getLength()); | ||
} | ||
|
||
public void testSingleLineCall() { | ||
final SimpleSourcePosition position = detailedSource(find(parse("true\nFoo.bar(true, false)\nfalse\n"), CallNode.class)); | ||
assertEquals("test", position.getFile()); | ||
assertEquals(1, position.getLine()); | ||
// assertEquals(5, position.getOffset()); | ||
// assertEquals(21, position.getLength()); | ||
} | ||
|
||
public void testMultiLineCall() { | ||
final SimpleSourcePosition position = detailedSource(find(parse("true\nFoo.bar(\n true,\n false\n)\nfalse\n"), CallNode.class)); | ||
assertEquals("test", position.getFile()); | ||
assertEquals(1, position.getLine()); | ||
// assertEquals(5, position.getOffset()); | ||
// assertEquals(28, position.getLength()); | ||
} | ||
|
||
// This is the test case which motivated the need for the new detailed source position implementation | ||
|
||
public void testRegresion1() { | ||
final SimpleSourcePosition position = detailedSource(find(parse("p 42\n\n3.hello\n"), CallNode.class)); | ||
assertEquals("test", position.getFile()); | ||
assertEquals(2, position.getLine()); | ||
// assertEquals(6, position.getOffset()); | ||
// assertEquals(7, position.getLength()); | ||
} | ||
|
||
private class FoundException extends RuntimeException { | ||
|
||
private final Node node; | ||
|
||
public FoundException(Node node) { | ||
this.node = node; | ||
} | ||
|
||
public Node getNode() { | ||
return node; | ||
} | ||
|
||
} | ||
|
||
private SimpleSourcePosition detailedSource(Node node) { | ||
final ISourcePosition sourcePosition = node.getPosition(); | ||
assertTrue(sourcePosition instanceof SimpleSourcePosition); | ||
return (SimpleSourcePosition) sourcePosition; | ||
} | ||
|
||
private <T extends Node> T find(RootNode root, final Class<T> find) { | ||
try { | ||
root.accept(new AbstractNodeVisitor<Void>() { | ||
|
||
@Override | ||
protected Void defaultVisit(Node node) { | ||
if (find.isAssignableFrom(node.getClass())) { | ||
throw new FoundException(node); | ||
} | ||
|
||
visitChildren(node); | ||
|
||
return null; | ||
} | ||
|
||
}); | ||
} catch (FoundException e) { | ||
return (T) e.getNode(); | ||
} | ||
|
||
fail(); | ||
return null; | ||
} | ||
|
||
private RootNode parse(String source) { | ||
final Ruby ruby = Ruby.newInstance(); | ||
final ParserConfiguration parserConfiguration = new org.jruby.parser.ParserConfiguration(ruby, 0, false, true, true); | ||
final StaticScope staticScope = ruby.getStaticScopeFactory().newLocalScope(null); | ||
final Parser parser = new org.jruby.parser.Parser(ruby); | ||
return (RootNode) parser.parse("test", source.getBytes(), new ManyVarsDynamicScope(staticScope), parserConfiguration); | ||
} | ||
|
||
} |
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