Revision: 1509 Author: jasonpmorrison Date: 2006-07-10 21:46:28 -0700 (Mon, 10 Jul 2006) ViewCVS: http://svn.sourceforge.net/rubyeclipse/?rev=1509&view=rev Log Message: ----------- Update action for Mark Occurences Added Paths: ----------- branches/type_inferrence/trunk/org.rubypeople.rdt.core.tests/src/org/rubypeople/rdt/internal/ti/MarkOccurrencesTest.java Added: branches/type_inferrence/trunk/org.rubypeople.rdt.core.tests/src/org/rubypeople/rdt/internal/ti/MarkOccurrencesTest.java =================================================================== --- branches/type_inferrence/trunk/org.rubypeople.rdt.core.tests/src/org/rubypeople/rdt/internal/ti/MarkOccurrencesTest.java (rev 0) +++ branches/type_inferrence/trunk/org.rubypeople.rdt.core.tests/src/org/rubypeople/rdt/internal/ti/MarkOccurrencesTest.java 2006-07-11 04:46:28 UTC (rev 1509) @@ -0,0 +1,143 @@ +package org.rubypeople.rdt.internal.ti; + +import java.util.List; + +import org.jruby.ast.Node; +import org.jruby.lexer.yacc.ISourcePosition; +import org.rubypeople.rdt.internal.core.parser.RubyParser; +import org.rubypeople.rdt.internal.ti.util.OffsetNodeLocator; + +import junit.framework.TestCase; + +/** + * Tests related to matching occurrences. + * + * @author Jason Morrison + * + */ +public class MarkOccurrencesTest extends TestCase { + + private IOccurrencesFinder occurrencesFinder; + public void setUp() { + occurrencesFinder = new DefaultOccurrencesFinder(); + } + + private Node parse(String source) { + return new RubyParser().parse(source); + } + + private void assertOccurrencesEqual( String source, int offset, String matchName, int[][] offsets ) + { + occurrencesFinder.initialize(parse(source), offset, 0); + List <ISourcePosition> occurrences = occurrencesFinder.perform(); + assertEquals( offsets.length, occurrences.size() ); + + for ( int i = 0; i < offsets.length; i++ ) { + assertEquals( offsets[i][0], occurrences.get(i).getStartOffset() ); + assertEquals( offsets[i][1], occurrences.get(i).getEndOffset() ); + assertEquals( matchName, source.substring(occurrences.get(i).getStartOffset(), occurrences.get(i).getEndOffset())); + } + } + + /** + * Match locals within ClassNode::DefnNode + */ + public void testLocalVariableMatches() { + String source = "class Klass;def foo(x);puts x*2;end;def bar;my_var = 5;my_var = 6;puts my_var;foo(my_var);end;end"; + int[][] offsets = {{44,50},{55,61},{71,77},{82,88}}; + assertOccurrencesEqual( source, 46, "my_var", offsets ); + } + + /** + * Match locals within Kernel::DefnNode + */ + public void testLocalVariablesInKernelDefnScope() { + String source = "def foo;my_var=5;puts my_var*2;end"; + int[][] offsets = {{8,14},{22,28}}; + assertOccurrencesEqual( source, 10, "my_var", offsets ); + } + + /** + * Match locals in Kernel + */ + public void testLocalVariablesInKernelScope() { + String source = "my_var = 5;puts my_var*2;other_var = my_var * my_var"; + int[][] offsets = {{0,6},{16,22},{37,43},{46,52}}; + assertOccurrencesEqual( source, 1, "my_var", offsets ); + } + + /** + * Match instance vars inside one ClassNode across DefnNode + */ + public void testInstanceVariableMatches() { + String source = "class Klass;def foo(param);@inst_var = param;puts @inst_var;end;def bar;y @inst_var;end;end"; + int[][] offsets = {{27,36},{50,59},{74,83}}; + assertOccurrencesEqual( source, 29, "@inst_var", offsets ); + } + + /** + * Match instance vars inside two DefnNodes, each in a separate ClassNode (for the same class) + */ + public void testInstanceVariableMatchInReopenedClass() { + String source = "class Klass;def foo;@inst_var=5;end;end;class Klass;def bar;@inst_var=6;end;end"; + int[][] offsets = {{20,29},{60,69}}; + assertOccurrencesEqual( source, 23, "@inst_var", offsets ); + } + + /** + * Test matching a local variable before, inside, and after a block. + */ + public void testLocalVariableMatchesIntoBlockScope() { + String source = "class Klass;def foo;my_var = 5;5.times { puts my_var };puts my_var;end;end"; + int[][] offsets = {{20,26},{46,52},{60,66}}; + assertOccurrencesEqual( source, 23, "my_var", offsets ); + } + + /** + * Test referencing a global variable in various contexts. + * + */ + public void testGlobalVariableMatches() { + String source = "$foo = 'bar';class Klass;def foo;$foo = 5;end;def bar;puts $foo;end;end;puts $foo"; + int[][] offsets = {{0,4},{33,37},{59,63},{77,81}}; + assertOccurrencesEqual(source, 0, "$foo", offsets); + } + +//todo: Method invocation tests get into territory where a more formal approach is needed (i.e. DDP) +// Sub-goals are becoming necessary, i.e. for determining arg-type to match selectors by more +// than name, and determining receiver-type to match selectors applied to other same-typed receivers. + +// public void testMethodInvocationMatchInsideMethod() { +// String source = "class Klass;def foo;my_var = 5;y = my_var.to_s;puts y.to_s;end;end"; +// int[][] offsets = {{42,46},{54,58}}; +// assertOccurrencesEquals( source, 42, "to_s", offsets ); +// } +// +// public void testMethodInvocationMatchInKernelScope() { +// String source = "my_var = 5;y = my_var.to_s;puts y.to_s"; +// int[][] offsets = {{22,26},{34,38}}; +// assertOccurrencesEquals(source, 22, "to_s", offsets); +// } +// +// public void testStaticMethodInvocationMatchInKernelScope() { +// String source = "puts 5;puts 6;puts 7;"; +// int[][] offsets = {{0,4},{7,11},{14,18}}; +// assertOccurrencesEquals( source, 0, "puts", offsets ); +// } +// +// public void testMethodInvocationMatchAgainstMultipleInstancesOfSameType() { +// String source = "xvar = 5;yvar = 6;puts xvar.to_s;puts yvar.to_s"; +// int[][] offsets = {{28,32},{43,47}}; +// assertOccurrencesEquals(source, 28, "to_s", offsets); +// } + + /** + * Test matching against ConstNodes; specifically class occurrences + */ + public void testTypeMatches() { + String source = "f = String.new;class Klass;def foo;c = String;end;end;class MyString < String;end"; + int[][] offsets = {{4,10},{39,45},{71,77}}; + assertOccurrencesEqual( source, 5, "String", offsets ); + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |