1 7 8 package org.netbeans.modules.ruby; 9 10 import java.io.File ; 11 import javax.swing.text.Document ; 12 import junit.framework.TestCase; 13 import org.jruby.ast.Node; 14 import org.jruby.parser.RubyParserResult; 15 import org.netbeans.api.gsf.Error; 16 import org.netbeans.api.gsf.ParseEvent; 17 import org.netbeans.api.gsf.ParseListener; 18 import org.netbeans.api.gsf.ParserFile; 19 import org.netbeans.api.gsf.ParserResult; 20 import org.netbeans.junit.NbTestCase; 21 import org.netbeans.spi.gsf.DefaultParseListener; 22 import org.netbeans.spi.gsf.DefaultParserFile; 23 import org.netbeans.spi.gsf.DefaultParserFile; 24 import org.openide.cookies.EditorCookie; 25 import org.openide.filesystems.FileObject; 26 import org.openide.filesystems.FileUtil; 27 import org.openide.loaders.DataObject; 28 29 33 public class TypeAnalyzerTest extends NbTestCase { 34 35 public TypeAnalyzerTest(String testName) { 36 super(testName); 37 } 38 39 protected void setUp() throws Exception { 40 super.setUp(); 41 } 42 43 protected void tearDown() throws Exception { 44 super.tearDown(); 45 } 46 47 private ParserResult parse(FileObject fileObject) { 48 RubyParser parser = new RubyParser(); 49 int caretOffset = -1; 50 51 ParserFile file = new DefaultParserFile(fileObject, null, false); 52 String sequence = ""; 53 ParseListener listener = new DefaultParseListener(); 54 try { 55 DataObject dobj = DataObject.find(fileObject); 56 EditorCookie cookie = dobj.getCookie(EditorCookie.class); 57 Document doc = cookie.openDocument(); 58 sequence = doc.getText(0, doc.getLength()); 59 } catch (Exception ex) { 60 fail(ex.toString()); 61 } 62 ParserResult result = parser.parseBuffer(file, caretOffset, sequence, listener, false); 63 return result; 64 } 65 66 private FileObject getTestFile(String relFilePath) { 67 File wholeInputFile = new File (getDataDir(), relFilePath); 68 if (!wholeInputFile.exists()) { 69 NbTestCase.fail("File " + wholeInputFile + " not found."); 70 } 71 FileObject fo = FileUtil.toFileObject(wholeInputFile); 72 assertNotNull(fo); 73 74 return fo; 75 } 76 77 private Node getRootNode(String relFilePath) { 78 FileObject fileObject = getTestFile(relFilePath); 79 ParserResult result = parse(fileObject); 80 assertNotNull(result); 81 RubyParseResult rpr = (RubyParseResult)result; 82 Node root = rpr.getRootNode(); 83 84 return root; 85 } 86 87 public void testGetType() { 88 Node root = getRootNode("testfiles/types.rb"); 89 TypeAnalyzer instance = new TypeAnalyzer(root, 745); 90 assertEquals("Integer", instance.getType("x")); 91 assertEquals("File", instance.getType("y")); 92 assertEquals("Hash", instance.getType("$baz")); 93 assertEquals("Fixnum", instance.getType("@bar")); 94 assertEquals("Array", instance.getType("@foo")); 95 96 } 98 } 99 | Popular Tags |