[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Some Elisp code for working with Beanshell
From: |
William Bland |
Subject: |
Some Elisp code for working with Beanshell |
Date: |
Tue, 27 Jul 2004 10:30:14 -0700 |
Hello,
I use Beanshell very often, from inside Emacs, to test my Java
code. I wrote a bit of Elisp code that takes a buffer containing a
Beanshell session and turns it into Java code for a unit test. I'm
sending it to the list in the hope that other people will find it
useful. For example, the following Beanshell session:
cd ~/
/opt/sun-jdk-1.4.2.04/bin/java -classpath
/usr/share/emacs/site-lisp/jde/java/bsh-commands:/opt/sun-jdk-1.4.2.04/lib/tools.jar:/usr/share/emacs/site-lisp/jde/java/lib/checkstyle-all.jar:/usr/share/emacs/site-lisp/jde/java/lib/jakarta-regexp.jar:/usr/share/emacs/site-lisp/jde/java/lib/jde.jar:/usr/share/emacs/site-lisp/jde/java/lib/bsh.jar
bsh.Interpreter
BeanShell 1.2.7 - by Pat Niemeyer (address@hidden)
bsh % import java.util.*;
bsh % HashMap h = new HashMap();
bsh % h.put("colour", "blue");
bsh % show();
<true>
bsh % h.get("colour");
<blue>
bsh % Math.abs(-1);
<1>
bsh %
gets transformed, with a single key-stroke and no extra input from me,
into the following Java code:
import java.util.*;
import junit.framework.*;
public class Test2004-July-24-Saturday-11-40 extends TestCase
{
public Test2004-July-24-Saturday-11-40()
{
}
public void setUp()
{
}
public void tearDown()
{
}
public void testEverything()
{
HashMap h = new HashMap();
h.put("colour", "blue");
Assert.assertEquals( h.get("colour"), "blue" );
Assert.assertEquals( Math.abs(-1), 1 );
}
}
The Elisp code for doing this is:
;; REPL-TO-UNIT, By William Bland <http://www.abstractnonsense.com>
;; This code is released into the public domain, with no warranty.
(defun repl-to-unit ()
(interactive)
(save-excursion
(let ((imports nil))
(goto-char (point-min))
(while (re-search-forward "bsh % \\(import .*;\\)" nil t)
(push (concat (match-string 1) "\n") imports)
(delete-region (match-beginning 0) (match-end 0)))
(goto-char (point-min))
(search-forward "bsh % " nil t)
(delete-region 1 (match-beginning 0))
(goto-char (point-min))
(let ((test-name (format-time-string "%Y-%B-%d-%A-%H-%M"
(current-time))))
(apply 'insert imports)
(insert "import junit.framework.*;\n\n"
"public class Test" test-name " extends TestCase\n{\n"
"public Test" test-name "()\n{\n}\n\n"
"public void setUp()\n{\n}\n\n"
"public void tearDown()\n{\n}\n\n"
"public void testEverything()\n{\n")))
(goto-char (point-min))
(while (re-search-forward "bsh % show();[\n]<.*>" nil t)
(delete-region (match-beginning 0) (match-end 0)))
(goto-char (point-min))
(while (re-search-forward "^Start ClassPath Mapping$" nil t)
(delete-region (match-beginning 0) (match-end 0)))
(goto-char (point-min))
(while (re-search-forward "^Mapping: .*$" nil t)
(delete-region (match-beginning 0) (match-end 0)))
(goto-char (point-min))
(while (re-search-forward "^Error constructing classpath: .*$" nil
t)
(delete-region (match-beginning 0) (match-end 0)))
(goto-char (point-min))
(while (re-search-forward "^Not a classpath component: .*$" nil t)
(delete-region (match-beginning 0) (match-end 0)))
(goto-char (point-min))
(while (re-search-forward "^End ClassPath Mapping$" nil t)
(delete-region (match-beginning 0) (match-end 0)))
(goto-char (point-min))
(while (re-search-forward "bsh % \\(.*\\);[\n]<\\(.*\\)>" nil t)
(let ((result (match-string 2)))
(if (and (not (equal result "true"))
(not (equal result "false"))
(not (equal result "null"))
(not (member (aref result 0)
'(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 ?0 ?+ ?-
?.))))
(setf result (concat "\"" result "\"")))
(replace-match (concat "Assert.assertEquals( "
(match-string 1) ", " result " );"))))
(goto-char (point-min))
(while (search-forward "bsh % " nil t)
(delete-region (match-beginning 0) (match-end 0)))
(goto-char (point-max))
(insert "\n}\n}\n")
(java-mode)
(indent-region (point-min) (point-max))))
I'm sure there will be bugs in the Elisp code - please let me know if
you find any, or if you have problems getting it to work, and I'll be
glad to help.
Cheers,
Bill.
--
Dr. William Bland www.abstractnonsense.com
Computer Programmer awksedgrep (Yahoo IM)
Any sufficiently advanced Emacs user is indistinguishable from magic
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Some Elisp code for working with Beanshell,
William Bland <=