Revision: 1578 Author: jasonpmorrison Date: 2006-08-21 01:31:48 -0700 (Mon, 21 Aug 2006) ViewCVS: http://svn.sourceforge.net/rubyeclipse/?rev=1578&view=rev Log Message: ----------- * Tests for initial DataFlowTypeInferrer. Added Paths: ----------- branches/type_inferrence/trunk/org.rubypeople.rdt.core.tests/src/org/rubypeople/rdt/internal/ti/DataFlowTypeInferrerTest.java Added: branches/type_inferrence/trunk/org.rubypeople.rdt.core.tests/src/org/rubypeople/rdt/internal/ti/DataFlowTypeInferrerTest.java =================================================================== --- branches/type_inferrence/trunk/org.rubypeople.rdt.core.tests/src/org/rubypeople/rdt/internal/ti/DataFlowTypeInferrerTest.java (rev 0) +++ branches/type_inferrence/trunk/org.rubypeople.rdt.core.tests/src/org/rubypeople/rdt/internal/ti/DataFlowTypeInferrerTest.java 2006-08-21 08:31:48 UTC (rev 1578) @@ -0,0 +1,162 @@ +package org.rubypeople.rdt.internal.ti; + +import java.util.List; + +import junit.framework.TestCase; + +/** + * @author Jason + * + */ +public class DataFlowTypeInferrerTest extends TestCase { + + private ITypeInferrer inferrer; + public void setUp() { + inferrer = createTypeInferrer(); + } + + /** + * Shortcut for testing that a particular type is the only one inferred, + * and is inferred with 100% confidence + * @param guesses + * @param type + */ + private void assertInfersTypeWithoutDoubt(List<ITypeGuess> guesses, String type) { + assertEquals(1, guesses.size()); + ITypeGuess guess = guesses.get(0); + assertEquals(type, guess.getType()); + assertEquals(100, guess.getConfidence()); + } + + /** + * Shortcut for testing that two types are inferred, each with 50% confidence + * @param guesses + * @param type + * @param type2 + */ + + private void assertInfersTypeFiftyFifty( List<ITypeGuess> guesses, String type1, String type2 ) { + assertEquals(2, guesses.size()); + assertEquals( guesses.get(0).getType(), type1 ); + assertEquals( guesses.get(1).getType(), type2 ); + assertEquals( guesses.get(0).getConfidence(), 50 ); + assertEquals( guesses.get(1).getConfidence(), 50 ); + } + + + public void testFixnum() throws Exception { + assertInfersTypeWithoutDoubt(inferrer.infer("5", 0), "Fixnum"); + } + + public void testString() throws Exception { + assertInfersTypeWithoutDoubt(inferrer.infer("'string'", 3), "String"); + } + + public void testFixnumAssignment() throws Exception { + assertInfersTypeWithoutDoubt(inferrer.infer("var=5", 1), "Fixnum"); + } + + public void testLocalVariableAfterAssignment() throws Exception { + assertInfersTypeWithoutDoubt(inferrer.infer("x=5;x", 4), "Fixnum"); + } + + public void testLocalVariableAfterAssignmentInsideScope() throws Exception { + assertInfersTypeWithoutDoubt(inferrer.infer("module M;x=5;x;end", 13), "Fixnum"); + } + + public void testLocalVariableAfterAssignmentWithOverwrite() throws Exception { + +//FIXME: Currently returns sum of types (i.e. [String, Fixnum]). Try and track last-asgn-before-infer-point +//OLD: +// assertInfersTypeWithoutDoubt(inferrer.infer("x=5;x='foo';x", 12), "String"); +// assertInfersTypeWithoutDoubt(inferrer.infer("x=5;y='foo';x", 12), "Fixnum"); +//NEW (temporary): + assertInfersTypeFiftyFifty( inferrer.infer("x=5;x='foo';x", 12), "Fixnum", "String"); + } + + public void testLocalVariableAssignmentToLocalVariable() throws Exception { + String script = "x=5;y=x;x;y"; + assertInfersTypeWithoutDoubt(inferrer.infer(script, 8), "Fixnum"); // "x" + assertInfersTypeWithoutDoubt(inferrer.infer(script, 10), "Fixnum"); // "y" + } + + public void testLocalVariableAssignmentToLocalVariableTwice() throws Exception { + String script = "x=5;y=x;z=y;z;y;x"; + assertInfersTypeWithoutDoubt(inferrer.infer(script, 12), "Fixnum"); // "z" + assertInfersTypeWithoutDoubt(inferrer.infer(script, 14), "Fixnum"); // "y" + assertInfersTypeWithoutDoubt(inferrer.infer(script, 16), "Fixnum"); // "x" + } + + //FIXME: Will need access to core stubs +// public void testLocalVariableAssignmentToWellKnownMethodCall() throws Exception { +// assertInfersTypeWithoutDoubt(inferrer.infer("x=5.to_s;x", 9), "String"); +// } + + public void testLocalVariableAssignmentToClassInstantiation() throws Exception { + assertInfersTypeWithoutDoubt(inferrer.infer("x=Regexp.new;x", 13), "Regexp"); + } + + public void testInstVarAssignment() throws Exception { + assertInfersTypeWithoutDoubt(inferrer.infer("@x=5;@x", 6), "Fixnum"); + } + + public void testClassVarAssignment() throws Exception { + assertInfersTypeWithoutDoubt(inferrer.infer("class Klass;@@x=5;@@x;end", 20), "Fixnum"); + } + + public void testInstVarAssignmentInDifferentClassesWithSameName() throws Exception { + assertInfersTypeWithoutDoubt(inferrer.infer("class X;@var=5;@var;end;class Y;@var='string';@var;end", 16), "Fixnum"); + assertInfersTypeWithoutDoubt(inferrer.infer("class X;@var=5;@var;end;class Y;@var='string';@var;end", 48), "String"); + } + public void testGlobalVarAssignment() throws Exception { + assertInfersTypeWithoutDoubt(inferrer.infer("$x=5;$x", 6), "Fixnum"); + } + + public void testGlobalVarAssignmentInDifferentClassesWithSameName() throws Exception { + assertInfersTypeFiftyFifty( inferrer.infer("class X;$var=5;$var;end;class Y;$var='string';$var;end", 16 ), "Fixnum", "String"); + } + + public void testArg() throws Exception { + assertInfersTypeWithoutDoubt( inferrer.infer("def foo(var);var;end;foo(5)", 14), "Fixnum" ); + } + + public void testArgTwoDegree() throws Exception { + assertInfersTypeWithoutDoubt( inferrer.infer("def foo(var);var;end;def bar(var);foo(var);end;foo(5)", 14), "Fixnum" ); + } + + public void testArgTwoWay() throws Exception { + assertInfersTypeFiftyFifty( inferrer.infer("def foo(var);var;end;foo(5);foo('baz');", 14), "Fixnum", "String" ); + } + + public void testLocalVariableAssignmentWithSameNameAcrossTwoScopes() throws Exception { + String script = "module M;x=5;x;end;module N;x='foo';x;end"; + assertInfersTypeWithoutDoubt( inferrer.infer( script, 13 ), "Fixnum" ); + assertInfersTypeWithoutDoubt( inferrer.infer( script, 36 ), "String" ); + } + + public void testMethodRetval() throws Exception { + String script = "def foo;return 'baz';end;my_var = foo;my_var"; + assertInfersTypeWithoutDoubt(inferrer.infer( script, 40), "String" ); + } + + public void testInstanceObjectMethodRetval() throws Exception { + String script = "class X;def foo;return 'bar';end;end;my_instance = X.new;my_var = my_instance.foo;my_var"; + assertInfersTypeWithoutDoubt(inferrer.infer( script, 85), "String" ); + } + +//TODO: No dice yet on implicit retvals +// public void testMethodImplicitRetval() throws Exception { +// String script = "def foo;'baz';end;my_var = foo;my_var"; +// assertInfersTypeWithoutDoubt(inferrer.infer( script, 33), "String" ); +// } + + /** + * Override this method in subclasses so that we can test any + * implementation of ITypeInferrer the same way. + * @return an implementation of ITypeInferrer + */ + protected ITypeInferrer createTypeInferrer() { + return new DataFlowTypeInferrer(); + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |