Skip to content
This repository
Browse code

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...
commit 808ccab89dd9c7fc0f59c4709f6c76493180d341 1 parent dc3f0e4
Charles Oliver Nutter headius authored
83 test/junit_testrunner.rb
... ... @@ -0,0 +1,83 @@
  1 +require 'test/unit/ui/testrunnermediator'
  2 +require 'test/unit/ui/testrunnerutilities'
  3 +
  4 +module Test
  5 + module Unit
  6 + module UI
  7 + module JUnit
  8 + # Runs a Test::Unit::TestSuite on the console.
  9 + class TestRunner
  10 + extend TestRunnerUtilities
  11 +
  12 + attr_accessor :faults
  13 +
  14 + # Creates a new TestRunner for running the passed
  15 + # suite. If quiet_mode is true, the output while
  16 + # running is limited to progress dots, errors and
  17 + # failures, and the final result. io specifies
  18 + # where runner output should go to; defaults to
  19 + # STDOUT.
  20 + def initialize(suite, output_level=NORMAL, io=STDOUT)
  21 + if (suite.respond_to?(:suite))
  22 + @suite = suite.suite
  23 + else
  24 + @suite = suite
  25 + end
  26 + @output_level = output_level
  27 + @io = io
  28 + @already_outputted = false
  29 + @faults = []
  30 + end
  31 +
  32 + # Begins the test run.
  33 + def start
  34 + setup_mediator
  35 + attach_to_mediator
  36 + return start_mediator
  37 + end
  38 +
  39 + private
  40 + def setup_mediator
  41 + @mediator = create_mediator(@suite)
  42 + suite_name = @suite.to_s
  43 + if ( @suite.kind_of?(Module) )
  44 + suite_name = @suite.name
  45 + end
  46 + end
  47 +
  48 + def create_mediator(suite)
  49 + return TestRunnerMediator.new(suite)
  50 + end
  51 +
  52 + def attach_to_mediator
  53 + @mediator.add_listener(TestResult::FAULT, &method(:add_fault))
  54 + end
  55 +
  56 + def start_mediator
  57 + return @mediator.run_suite
  58 + end
  59 +
  60 + def add_fault(fault)
  61 + @faults << fault
  62 + end
  63 +
  64 + def started(result)
  65 + end
  66 +
  67 + def finished(elapsed_time)
  68 + end
  69 +
  70 + def test_started(name)
  71 + end
  72 +
  73 + def test_finished(name)
  74 + end
  75 + end
  76 + end
  77 + end
  78 + end
  79 +end
  80 +
  81 +if __FILE__ == $0
  82 + Test::Unit::UI::JUnit::TestRunner.start_command_line_test
  83 +end
18 test/org/jruby/test/FailingTest.java
... ... @@ -0,0 +1,18 @@
  1 +package org.jruby.test;
  2 +
  3 +import junit.framework.TestCase;
  4 +
  5 +
  6 +
  7 +public class FailingTest extends TestCase {
  8 + private final String message;
  9 +
  10 + public FailingTest(String name, String message) {
  11 + super(name);
  12 + this.message = message;
  13 + }
  14 +
  15 + public void runTest() throws Throwable {
  16 + fail(message);
  17 + }
  18 +}
6 test/org/jruby/test/MainTestSuite.java
@@ -62,7 +62,6 @@ public static Test suite() throws Throwable {
62 62 suite.addTest(new TestSuite(TestKernel.class));
63 63 suite.addTest(new TestSuite(TestRubyCollect.class));
64 64 suite.addTest(new TestSuite(TestObjectSpace.class));
65   - suite.addTest(ScriptTestSuite.suite());
66 65 suite.addTest(new TestSuite(TestRubySymbol.class));
67 66 suite.addTest(JavaSupportTestSuite.suite());
68 67 suite.addTest(new TestSuite(TestIdentitySet.class));
@@ -74,6 +73,11 @@ public static Test suite() throws Throwable {
74 73 suite.addTest(new TestSuite(TestRubyArray.class));
75 74 suite.addTest(new TestSuite(TestVariableCreation.class));
76 75 suite.addTest(new TestSuite(YARVMachineTest.class));
  76 +
  77 + // tests written in Ruby
  78 + suite.addTest(ScriptTestSuite.suite());
  79 + suite.addTest(TestUnitTestSuite.suite());
  80 +
77 81 return suite;
78 82 }
79 83 }
17 test/org/jruby/test/ScriptTestSuite.java
@@ -61,7 +61,7 @@ public ScriptTestSuite(String name) {
61 61 public static Test suite() throws java.io.IOException {
62 62 TestSuite suite = new TestSuite();
63 63
64   - File testIndex = new File("test/test_index");
  64 + File testIndex = new File(TEST_INDEX);
65 65
66 66 if (! testIndex.canRead()) {
67 67 // Since we don't have any other error reporting mechanism, we
@@ -100,21 +100,6 @@ private static IRuby setupInterpreter() {
100 100 return runtime;
101 101 }
102 102
103   -
104   - private static class FailingTest extends TestCase {
105   - private final String message;
106   -
107   - public FailingTest(String name, String message) {
108   - super(name);
109   - this.message = message;
110   - }
111   -
112   - public void runTest() throws Throwable {
113   - fail(message);
114   - }
115   - }
116   -
117   -
118 103 private static class ScriptTest extends TestCase {
119 104 private final IRuby runtime;
120 105 private final String filename;
142 test/org/jruby/test/TestUnitTestSuite.java
... ... @@ -0,0 +1,142 @@
  1 +/*
  2 + * TestUnitTestSuite.java
  3 + * JUnit based test
  4 + *
  5 + * Created on January 15, 2007, 4:06 PM
  6 + */
  7 +
  8 +package org.jruby.test;
  9 +
  10 +import java.io.BufferedReader;
  11 +import java.io.ByteArrayInputStream;
  12 +import java.io.ByteArrayOutputStream;
  13 +import java.io.File;
  14 +import java.io.FileInputStream;
  15 +import java.io.InputStreamReader;
  16 +import java.io.PrintStream;
  17 +import java.util.ArrayList;
  18 +import java.util.Iterator;
  19 +import junit.framework.TestCase;
  20 +import junit.framework.*;
  21 +import org.jruby.IRuby;
  22 +import org.jruby.Ruby;
  23 +import org.jruby.RubyArray;
  24 +import org.jruby.RubyString;
  25 +import org.jruby.exceptions.MainExitException;
  26 +import org.jruby.exceptions.RaiseException;
  27 +import org.jruby.javasupport.JavaUtil;
  28 +
  29 +/**
  30 + *
  31 + * @author headius
  32 + */
  33 +public class TestUnitTestSuite extends TestCase {
  34 + private static final String TEST_DIR = "test";
  35 + private static final String TEST_INDEX = "test" + File.separator + "test_unit_index";
  36 +
  37 + public TestUnitTestSuite(String testName) {
  38 + super(testName);
  39 + }
  40 +
  41 + /**
  42 + * suite method automatically generated by JUnit module
  43 + */
  44 + public static Test suite() throws Exception {
  45 + TestSuite suite = new TestSuite();
  46 +
  47 + File testIndex = new File(TEST_INDEX);
  48 +
  49 + if (!testIndex.canRead()) {
  50 + // Since we don't have any other error reporting mechanism, we
  51 + // add the error message as an always-failing test to the test suite.
  52 + suite.addTest(new FailingTest("TestUnitTestSuite",
  53 + "Couldn't locate " + TEST_INDEX +
  54 + ". Make sure you run the tests from the base " +
  55 + "directory of the JRuby sourcecode."));
  56 + return suite;
  57 + }
  58 +
  59 + BufferedReader testFiles =
  60 + new BufferedReader(new InputStreamReader(new FileInputStream(testIndex)));
  61 +
  62 + String line;
  63 + while ((line = testFiles.readLine()) != null) {
  64 + line = line.trim();
  65 + if (line.startsWith("#") || line.length() == 0) {
  66 + continue;
  67 + }
  68 +
  69 + suite.addTest(new ScriptTest(line));
  70 + }
  71 +
  72 + return suite;
  73 + }
  74 +
  75 + private static class ScriptTest extends TestCase {
  76 + private ByteArrayInputStream in;
  77 + private ByteArrayOutputStream out;
  78 + private PrintStream printOut;
  79 + private ByteArrayOutputStream err;
  80 + private PrintStream printErr;
  81 + private IRuby runtime;
  82 + private final String filename;
  83 +
  84 + public ScriptTest(String filename) {
  85 + super(filename);
  86 + this.filename = filename;
  87 + }
  88 +
  89 + protected void setUp() throws Exception {
  90 + in = new ByteArrayInputStream(new byte[0]);
  91 + out = new ByteArrayOutputStream();
  92 + err = new ByteArrayOutputStream();
  93 + runtime = Ruby.newInstance(in, printOut = new PrintStream(out), printErr = new PrintStream(err));
  94 + setupInterpreter(runtime);
  95 + }
  96 +
  97 + protected void tearDown() throws Exception {
  98 + in.close();
  99 + out.close();
  100 + err.close();
  101 + printOut.close();
  102 + printErr.close();
  103 + }
  104 +
  105 + private void setupInterpreter(IRuby runtime) {
  106 + runtime.getLoadService().init(new ArrayList());
  107 + runtime.defineGlobalConstant("ARGV", runtime.newArray());
  108 + runtime.defineGlobalConstant("FAILURES", runtime.newArray());
  109 + }
  110 +
  111 + private String scriptName() {
  112 + return new File(TEST_DIR + File.separator + filename).getPath();
  113 + }
  114 +
  115 + public void runTest() throws Throwable {
  116 + StringBuffer script = new StringBuffer();
  117 +
  118 + try {
  119 + script.append("require 'test/junit_testrunner.rb'\n");
  120 + script.append("require '" + scriptName() + "'\n");
  121 + script.append("runner = Test::Unit::UI::JUnit::TestRunner.new(eval('" + filename + "'.split('_').each {|s| s.capitalize! }.join))\n");
  122 + script.append("runner.start\n");
  123 + script.append("runner.faults\n");
  124 +
  125 + RubyArray faults = (RubyArray)runtime.evalScript(script.toString());
  126 + StringBuffer faultString = new StringBuffer("Faults encountered in " + scriptName() + ", complete output follows:\n");
  127 +
  128 + if (!faults.isEmpty()) {
  129 + for (Iterator iter = faults.iterator(); iter.hasNext();) {
  130 + String fault = iter.next().toString();
  131 +
  132 + faultString.append(fault).append("\n");
  133 + }
  134 +
  135 + fail(faultString.toString());
  136 + }
  137 + } catch (RaiseException re) {
  138 + fail("Faults encountered in " + scriptName() + ", complete output follows:\n" + re.getException().message + "\n" + re.getException().backtrace());
  139 + }
  140 + }
  141 + }
  142 +}
2  test/test_core_arities.rb
@@ -2,7 +2,7 @@
2 2
3 3 # Test that core class methods are correctly raising errors for incorrect call
4 4 # arities.
5   -class TestArities < Test::Unit::TestCase
  5 +class TestCoreArities < Test::Unit::TestCase
6 6 def assert_argerr(&b)
7 7 assert_raises(ArgumentError, &b)
8 8 end
3  test/test_unit_index
... ... @@ -0,0 +1,3 @@
  1 +# This file contains a list of test/unit test case files to execute as part of the build/test cycle.
  2 +
  3 +test_core_arities

0 comments on commit 808ccab

Please sign in to comment.
Something went wrong with that request. Please try again.