Permalink
Browse files

Added ability to run test/unit test cases as part of test suite. Modi…

…fied test_core_arities to follow the model.

git-svn-id: http://svn.codehaus.org/jruby/trunk/jruby@2747 961051c9-f516-0410-bf72-c9f7e237a7b7
  • Loading branch information...
1 parent dc3f0e4 commit 808ccab89dd9c7fc0f59c4709f6c76493180d341 @headius headius committed Jan 15, 2007
View
@@ -0,0 +1,83 @@
+require 'test/unit/ui/testrunnermediator'
+require 'test/unit/ui/testrunnerutilities'
+
+module Test
+ module Unit
+ module UI
+ module JUnit
+ # Runs a Test::Unit::TestSuite on the console.
+ class TestRunner
+ extend TestRunnerUtilities
+
+ attr_accessor :faults
+
+ # Creates a new TestRunner for running the passed
+ # suite. If quiet_mode is true, the output while
+ # running is limited to progress dots, errors and
+ # failures, and the final result. io specifies
+ # where runner output should go to; defaults to
+ # STDOUT.
+ def initialize(suite, output_level=NORMAL, io=STDOUT)
+ if (suite.respond_to?(:suite))
+ @suite = suite.suite
+ else
+ @suite = suite
+ end
+ @output_level = output_level
+ @io = io
+ @already_outputted = false
+ @faults = []
+ end
+
+ # Begins the test run.
+ def start
+ setup_mediator
+ attach_to_mediator
+ return start_mediator
+ end
+
+ private
+ def setup_mediator
+ @mediator = create_mediator(@suite)
+ suite_name = @suite.to_s
+ if ( @suite.kind_of?(Module) )
+ suite_name = @suite.name
+ end
+ end
+
+ def create_mediator(suite)
+ return TestRunnerMediator.new(suite)
+ end
+
+ def attach_to_mediator
+ @mediator.add_listener(TestResult::FAULT, &method(:add_fault))
+ end
+
+ def start_mediator
+ return @mediator.run_suite
+ end
+
+ def add_fault(fault)
+ @faults << fault
+ end
+
+ def started(result)
+ end
+
+ def finished(elapsed_time)
+ end
+
+ def test_started(name)
+ end
+
+ def test_finished(name)
+ end
+ end
+ end
+ end
+ end
+end
+
+if __FILE__ == $0
+ Test::Unit::UI::JUnit::TestRunner.start_command_line_test
+end
@@ -0,0 +1,18 @@
+package org.jruby.test;
+
+import junit.framework.TestCase;
+
+
+
+public class FailingTest extends TestCase {
+ private final String message;
+
+ public FailingTest(String name, String message) {
+ super(name);
+ this.message = message;
+ }
+
+ public void runTest() throws Throwable {
+ fail(message);
+ }
+}
@@ -62,7 +62,6 @@ public static Test suite() throws Throwable {
suite.addTest(new TestSuite(TestKernel.class));
suite.addTest(new TestSuite(TestRubyCollect.class));
suite.addTest(new TestSuite(TestObjectSpace.class));
- suite.addTest(ScriptTestSuite.suite());
suite.addTest(new TestSuite(TestRubySymbol.class));
suite.addTest(JavaSupportTestSuite.suite());
suite.addTest(new TestSuite(TestIdentitySet.class));
@@ -74,6 +73,11 @@ public static Test suite() throws Throwable {
suite.addTest(new TestSuite(TestRubyArray.class));
suite.addTest(new TestSuite(TestVariableCreation.class));
suite.addTest(new TestSuite(YARVMachineTest.class));
+
+ // tests written in Ruby
+ suite.addTest(ScriptTestSuite.suite());
+ suite.addTest(TestUnitTestSuite.suite());
+
return suite;
}
}
@@ -61,7 +61,7 @@ public ScriptTestSuite(String name) {
public static Test suite() throws java.io.IOException {
TestSuite suite = new TestSuite();
- File testIndex = new File("test/test_index");
+ File testIndex = new File(TEST_INDEX);
if (! testIndex.canRead()) {
// Since we don't have any other error reporting mechanism, we
@@ -100,21 +100,6 @@ private static IRuby setupInterpreter() {
return runtime;
}
-
- private static class FailingTest extends TestCase {
- private final String message;
-
- public FailingTest(String name, String message) {
- super(name);
- this.message = message;
- }
-
- public void runTest() throws Throwable {
- fail(message);
- }
- }
-
-
private static class ScriptTest extends TestCase {
private final IRuby runtime;
private final String filename;
@@ -0,0 +1,142 @@
+/*
+ * TestUnitTestSuite.java
+ * JUnit based test
+ *
+ * Created on January 15, 2007, 4:06 PM
+ */
+
+package org.jruby.test;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStreamReader;
+import java.io.PrintStream;
+import java.util.ArrayList;
+import java.util.Iterator;
+import junit.framework.TestCase;
+import junit.framework.*;
+import org.jruby.IRuby;
+import org.jruby.Ruby;
+import org.jruby.RubyArray;
+import org.jruby.RubyString;
+import org.jruby.exceptions.MainExitException;
+import org.jruby.exceptions.RaiseException;
+import org.jruby.javasupport.JavaUtil;
+
+/**
+ *
+ * @author headius
+ */
+public class TestUnitTestSuite extends TestCase {
+ private static final String TEST_DIR = "test";
+ private static final String TEST_INDEX = "test" + File.separator + "test_unit_index";
+
+ public TestUnitTestSuite(String testName) {
+ super(testName);
+ }
+
+ /**
+ * suite method automatically generated by JUnit module
+ */
+ public static Test suite() throws Exception {
+ TestSuite suite = new TestSuite();
+
+ File testIndex = new File(TEST_INDEX);
+
+ if (!testIndex.canRead()) {
+ // Since we don't have any other error reporting mechanism, we
+ // add the error message as an always-failing test to the test suite.
+ suite.addTest(new FailingTest("TestUnitTestSuite",
+ "Couldn't locate " + TEST_INDEX +
+ ". Make sure you run the tests from the base " +
+ "directory of the JRuby sourcecode."));
+ return suite;
+ }
+
+ BufferedReader testFiles =
+ new BufferedReader(new InputStreamReader(new FileInputStream(testIndex)));
+
+ String line;
+ while ((line = testFiles.readLine()) != null) {
+ line = line.trim();
+ if (line.startsWith("#") || line.length() == 0) {
+ continue;
+ }
+
+ suite.addTest(new ScriptTest(line));
+ }
+
+ return suite;
+ }
+
+ private static class ScriptTest extends TestCase {
+ private ByteArrayInputStream in;
+ private ByteArrayOutputStream out;
+ private PrintStream printOut;
+ private ByteArrayOutputStream err;
+ private PrintStream printErr;
+ private IRuby runtime;
+ private final String filename;
+
+ public ScriptTest(String filename) {
+ super(filename);
+ this.filename = filename;
+ }
+
+ protected void setUp() throws Exception {
+ in = new ByteArrayInputStream(new byte[0]);
+ out = new ByteArrayOutputStream();
+ err = new ByteArrayOutputStream();
+ runtime = Ruby.newInstance(in, printOut = new PrintStream(out), printErr = new PrintStream(err));
+ setupInterpreter(runtime);
+ }
+
+ protected void tearDown() throws Exception {
+ in.close();
+ out.close();
+ err.close();
+ printOut.close();
+ printErr.close();
+ }
+
+ private void setupInterpreter(IRuby runtime) {
+ runtime.getLoadService().init(new ArrayList());
+ runtime.defineGlobalConstant("ARGV", runtime.newArray());
+ runtime.defineGlobalConstant("FAILURES", runtime.newArray());
+ }
+
+ private String scriptName() {
+ return new File(TEST_DIR + File.separator + filename).getPath();
+ }
+
+ public void runTest() throws Throwable {
+ StringBuffer script = new StringBuffer();
+
+ try {
+ script.append("require 'test/junit_testrunner.rb'\n");
+ script.append("require '" + scriptName() + "'\n");
+ script.append("runner = Test::Unit::UI::JUnit::TestRunner.new(eval('" + filename + "'.split('_').each {|s| s.capitalize! }.join))\n");
+ script.append("runner.start\n");
+ script.append("runner.faults\n");
+
+ RubyArray faults = (RubyArray)runtime.evalScript(script.toString());
+ StringBuffer faultString = new StringBuffer("Faults encountered in " + scriptName() + ", complete output follows:\n");
+
+ if (!faults.isEmpty()) {
+ for (Iterator iter = faults.iterator(); iter.hasNext();) {
+ String fault = iter.next().toString();
+
+ faultString.append(fault).append("\n");
+ }
+
+ fail(faultString.toString());
+ }
+ } catch (RaiseException re) {
+ fail("Faults encountered in " + scriptName() + ", complete output follows:\n" + re.getException().message + "\n" + re.getException().backtrace());
+ }
+ }
+ }
+}
@@ -2,7 +2,7 @@
# Test that core class methods are correctly raising errors for incorrect call
# arities.
-class TestArities < Test::Unit::TestCase
+class TestCoreArities < Test::Unit::TestCase
def assert_argerr(&b)
assert_raises(ArgumentError, &b)
end
View
@@ -0,0 +1,3 @@
+# This file contains a list of test/unit test case files to execute as part of the build/test cycle.
+
+test_core_arities

0 comments on commit 808ccab

Please sign in to comment.