unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: William Bland <wjb@abstractnonsense.com>
Subject: Some Elisp code for working with Beanshell
Date: Tue, 27 Jul 2004 10:30:14 -0700	[thread overview]
Message-ID: <1090949414.10791.43.camel@localhost.localdomain> (raw)

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();
<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

                 reply	other threads:[~2004-07-27 17:30 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1090949414.10791.43.camel@localhost.localdomain \
    --to=wjb@abstractnonsense.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).