We need to replace some JavaScript code in Tobago, which has basically the same functionality like the new date.js stuff. I started to work on this, too. Perhaps we should join forces. But I definitely don't have as much time as Martin -- working on this the whole night...

I pulled together some ends from a nearly abandoned web test tool of ours and started to write a JavaScriptTestCase. Now seeing that Martin's date parsing and formatting code is far more advanced than my stuff I came up with yesterday, I thought my unit testing stuff could be somehow useful for you, too. It just some very basic code using Mozilla's Rhino. But this way it's easier to test the JavaScript code from your Java IDE and at least for Tobago I need to be pretty close to the Java implementation of SimpleDateFormat. This way the two implementations can be compared pretty easily.

I spotted some problems regarding formats including "MMM" and "MMMM". But I didn't have the time to look into the JavaScript implementation up to now. I hope I can do this, when I got home and something to eat.

I will need a more sophisticated approach for localization, too ;-) I will provide some suggestions to this later...

Regards,
Arvid

Martin Marinschek wrote:

Hi *,

I have added the first draft of a javascript date parser - it would be
great if you could check it out, add functionality as needed (should
be pretty straightforward) and (of course) use it!

regards,

Martin
import org.mozilla.javascript.JavaScriptException;

import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateConverterTest extends JavaScriptTestCase {

  public void testCalendar() throws IOException, JavaScriptException {
    loadScriptFile("js/dateConverter.js");

    String format = "yyyyMMdd";
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
    assertEquals(simpleDateFormat.format(new Date()), eval("formatDate(new 
Date(), \"" + format + "\")"));
  }

}
import junit.framework.TestCase;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.JavaScriptException;

import java.io.IOException;
import java.io.FileReader;

public class JavaScriptTestCase extends TestCase {

  protected Context cx;
  protected Scriptable scope;

  protected void setUp() throws Exception {
    super.setUp();
    cx = Context.enter();
    scope = cx.initStandardObjects(null);
  }

  protected void tearDown() throws Exception {
    Context.exit();
  }

  protected Object eval(String script) throws JavaScriptException {
    return cx.evaluateString(scope, script, "test", 1, null);
  }

  protected int evalInt(String script) throws JavaScriptException {
    Object o = eval(script);
    if (o instanceof Number) {
      return ((Number) o).intValue();
    }
    throw new JavaScriptException(null, "invalid return type "
        + o.getClass().getName() + " with value " + o, 0);
  }

  protected boolean evalBoolean(String script) throws JavaScriptException {
    Object o = eval(script);
    if (o instanceof Boolean) {
      return ((Boolean) o).booleanValue();
    }
    throw new JavaScriptException(null, "invalid return type "
        + o.getClass().getName() + " with value " + o, 0);
  }

  // XXX directory handling
  protected void loadScriptFile(String jsFile)
      throws IOException, JavaScriptException {
    cx.evaluateReader(scope, new FileReader(jsFile), jsFile, 0, null);
  }
}
var Class = {
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
  }
}

Reply via email to