/* * Run_Test_Case.bsh v1.3 - a BeanShell macro script for the * jEdit text editor - Runs the Ruby Testcase the cursor is currently in. * checks if it is a standard test, an respec or a test/spec test case */ import java.util.regex.*; void execTest(path, args) { runInSystemShell(view, "ruby " + path + " --name " + args); } void execSpec(path, args) { // testrb -rc test/unit/input_test.rb -n "has a person" runInSystemShell(view, "ruby " + path + " -rs -n \"/" + args + "/\""); } void execRSpec(path, ln) { runInSystemShell(view, "spec " + path + " -f s -l " + ln); } String find_current_method(){ curpos= textArea.getCaretPosition(); txt= textArea.getText(); off= txt.lastIndexOf(" def ", curpos); //(.*)(def[ ]+)(\\w+.*) if(off != -1){ lo= textArea.getLineOfOffset(off); eol= textArea.getLineEndOffset(lo); p= txt.lastIndexOf("(", eol); if(p != -1 && p > off) eol= p-1; return txt.substring(off+5, eol).trim(); } return null; } String find_current_spec(){ curpos= textArea.getCaretPosition(); txt= textArea.getText(); off= txt.lastIndexOf(" specify ", curpos); if(off != -1){ lo= textArea.getLineOfOffset(off); eol= textArea.getLineEndOffset(lo); p= txt.lastIndexOf("\"", eol); if(p != -1 && p > off){ eol= p-1; off= txt.lastIndexOf("\"", eol); } return txt.substring(off+1, eol+1).trim(); } return null; } int find_current_rspec(){ curpos= textArea.getCaretPosition(); lo= textArea.getLineOfOffset(curpos); txt= textArea.getText(); Pattern p = Pattern.compile(".*^\\s+it\\s+.*", Pattern.MULTILINE|Pattern.DOTALL); Matcher m = p.matcher(txt); boolean b = m.matches(); if(b){ return lo; } return -1; } void runScript() { if(buffer.isNewFile()) buffer.saveAs(view, true); else buffer.save(view, buffer.getPath()); mode = buffer.getMode().getName(); path = buffer.getPath() + " "; os = System.getProperty("os.name"); if(os.indexOf("Windows") != -1) path = "\"" + path + "\""; if(!mode.equals("ruby")) { Macros.error(view, "The current file does not appear to be a ruby file."); return; } // find the rspec it we are in ln= find_current_rspec(); if(ln >= 0){ execRSpec(path, ln); return; } // find the specify we are in spec= find_current_spec(); if(spec != null){ execSpec(path, spec); return; } // find the method we are in meth= find_current_method(); if(meth == null || !meth.startsWith("test_")){ Macros.error(view, "The current file does not appear to be a ruby testcase."); return; } execTest(path, meth); } runScript(); /* Macro index data (in DocBook format) Run_Test_Case.bsh Runs current Ruby testcase */ // end Run_Test_Case.bsh