From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: William Bland Newsgroups: gmane.emacs.devel Subject: Some Elisp code for working with Beanshell Date: Tue, 27 Jul 2004 10:30:14 -0700 Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Message-ID: <1090949414.10791.43.camel@localhost.localdomain> NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 7bit X-Trace: sea.gmane.org 1090949466 22974 80.91.224.253 (27 Jul 2004 17:31:06 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Tue, 27 Jul 2004 17:31:06 +0000 (UTC) Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Tue Jul 27 19:30:55 2004 Return-path: Original-Received: from lists.gnu.org ([199.232.76.165]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1BpVmx-0005yT-00 for ; Tue, 27 Jul 2004 19:30:55 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.33) id 1BpVq3-0002TO-1g for ged-emacs-devel@m.gmane.org; Tue, 27 Jul 2004 13:34:07 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.33) id 1BpVpv-0002Su-Tb for emacs-devel@gnu.org; Tue, 27 Jul 2004 13:34:00 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.33) id 1BpVpv-0002Sb-6j for Emacs-devel@gnu.org; Tue, 27 Jul 2004 13:33:59 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.33) id 1BpVpv-0002SR-3y for Emacs-devel@gnu.org; Tue, 27 Jul 2004 13:33:59 -0400 Original-Received: from [209.11.141.66] (helo=localhost.localdomain) by monty-python.gnu.org with esmtp (TLSv1:DES-CBC3-SHA:168) (Exim 4.34) id 1BpVmJ-0001M3-Hi for Emacs-devel@gnu.org; Tue, 27 Jul 2004 13:30:15 -0400 Original-Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by localhost.localdomain (8.12.8/8.12.8) with ESMTP id i6RHUEWl010989 for ; Tue, 27 Jul 2004 10:30:14 -0700 Original-Received: (from wjb@localhost) by localhost.localdomain (8.12.8/8.12.8/Submit) id i6RHUEa3010987 for Emacs-devel@gnu.org; Tue, 27 Jul 2004 10:30:14 -0700 X-Authentication-Warning: localhost.localdomain: wjb set sender to wjb@abstractnonsense.com using -f Original-To: Emacs-devel@gnu.org X-Mailer: Ximian Evolution 1.4.6 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: main.gmane.org gmane.emacs.devel:26021 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:26021 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 (pat@pat.net) bsh % import java.util.*; bsh % HashMap h = new HashMap(); bsh % h.put("colour", "blue"); bsh % show(); bsh % h.get("colour"); 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 ;; 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