1/*
2 * Copyright 2001-2008 Artima, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.scalatest.tools {
17
18import org.scalatest.FunSuite
19import org.scalatools.testing.{Event, EventHandler, Result, Logger, Runner => TestingRunner}
20
21  // testing runner.run:
22  // def run(testClassName: String, fingerprint: TestFingerprint, args: Array[String]): Array[Event]
23  class ScalaTestRunnerSuite extends FunSuite {
24    test("call with simple class") {
25      val results = run("org.scalatest.tools.test.SimpleTest")
26      assert(results(0).testName === "hello, world")
27      assert(results(0).result === Result.Success)
28    }
29
30    test("three different results") {
31      val results = run("org.scalatest.tools.test.ThreeTestsTest")
32
33      assert(results(0).testName === "hello, world")
34      assert(results(0).result === Result.Success)
35
36      assert(results(1).testName === "throw")
37      assert(results(1).result === Result.Failure)
38      assert(results(1).error.getMessage === "baah")
39
40      assert(results(2).testName === "assert bad")
41      assert(results(2).result === Result.Failure)
42      assert(results(2).error.getMessage === "1 did not equal 3")
43
44      assert(results.size === 3)
45    }
46
47    test("one tag included") {
48      val results = run("org.scalatest.tools.test.TagsTest", "-n hello")
49
50      assert(results(0).testName === "hello, world")
51      assert(results(0).result === Result.Success)
52
53      assert(results.size === 1)
54    }
55
56    test("two tags included") {
57      val results = run("org.scalatest.tools.test.TagsTest", Array("-n", "hello helloAgain"))
58
59      assert(results(0).testName === "hello, world")
60      assert(results(0).result === Result.Success)
61
62      assert(results(1).testName === "hello, world again")
63      assert(results(1).result === Result.Success)
64
65      assert(results.size === 2)
66    }
67
68    test("one tag excluded") {
69      val results = run("org.scalatest.tools.test.TagsTest", Array("-l", "hello"))
70
71      assert(results(0).testName === "hello, world again")
72      assert(results(0).result === Result.Success)
73
74      assert(results(1).testName === "tag3")
75      assert(results(1).result === Result.Success)
76
77      assert(results(2).testName === "throw")
78      assert(results(2).result === Result.Failure)
79      assert(results(2).error.getMessage === "baah")
80
81      assert(results(3).testName === "assert bad")
82      assert(results(3).result === Result.Failure)
83      assert(results(3).error.getMessage === "1 did not equal 3")
84
85      assert(results.size === 4)
86    }
87
88    test("configs") {
89      val results = run("org.scalatest.tools.test.TestWithConfigMap", "-Djosh=cool")
90      assert(results(0).testName === "get config")
91      assert(results(0).result === Result.Success)
92
93      val resultsF = run("org.scalatest.tools.test.TestWithConfigMap", "-Djosh=bad")
94      assert(resultsF(0).testName === "get config")
95      assert(resultsF(0).result === Result.Failure)
96      assert(resultsF(0).error.getMessage === "\"[bad]\" did not equal \"[cool]\"")
97    }
98
99    test("configs 2"){
100      val results = run("org.scalatest.tools.test.TestWithConfigMap2", "-Da=z -Db=y -Dc=x")
101      assert(results(0).testName === "get config")
102      assert(results(0).result === Result.Success)
103    }
104
105    test("illegal arg on private constructor"){
106      intercept[IllegalArgumentException] {
107        run("org.scalatest.tools.test.PrivateConstructor")
108      }
109    }
110
111    test("skipped test results in Result.Skipped") {
112      val results = run("org.scalatest.tools.test.SuiteWithSkippedTest")
113      assert(results.size === 2)
114
115      assert(results(0).testName === "dependeeThatFails")
116      assert(results(0).result === Result.Failure)
117      assert(results(0).error.getMessage === "fail")
118
119      assert(results(1).testName === "depender")
120      assert(results(1).result === Result.Skipped)
121    }
122
123
124    test("pending test results in Result.Skipped") {
125      val results = run("org.scalatest.tools.test.PendingTest")
126      assert(results.size === 1)
127
128      assert(results(0).testName === "i am pending")
129      assert(results(0).result === Result.Skipped)
130    }
131
132    val framework = new ScalaTestFramework
133
134    val runner: TestingRunner = {
135      framework.testRunner(currentThread.getContextClassLoader, Array(new TestLogger))
136    }
137
138    val fingerprint = {
139      val fingerprints = framework.tests
140      fingerprints(0).
141                    asInstanceOf[org.scalatools.testing.TestFingerprint]
142    }
143
144    def run(classname: String): Array[Event] = run(classname, Array[String]())
145    def run(classname: String, args:String): Array[Event] = run(classname, args.split(" "))
146    def run(classname: String, args:Array[String]): Array[Event] = {
147      // val buf = scala.collection.mutable.ArrayBuffer[Event]() // Only worked under 2.8
148      val buf = new scala.collection.mutable.ArrayBuffer[Event]
149      val listener = new EventHandler {
150        def handle(event: Event) {
151          buf += event
152        }
153      }
154      runner.run(classname, fingerprint, listener, args)
155      buf.toArray
156    }
157
158    class TestLogger extends Logger {
159      def trace(t:Throwable) {}
160      def error(msg: String) {}
161      def warn(msg: String) {}
162      def info(msg: String) {}
163      def debug(msg: String) {}
164      def ansiCodesSupported = false
165    }
166  }
167
168  package test{
169
170    private class SimpleTest extends FunSuite {
171      test("hello, world") {"hello, world"}
172    }
173
174    private class ThreeTestsTest extends FunSuite {
175      test("hello, world") {"hello, world"}
176      test("throw") {throw new Exception("baah")}
177      test("assert bad") {assert(1 === 3)}
178    }
179
180    import org.scalatest.fixture.FixtureFunSuite
181    private class TestWithConfigMap extends FixtureFunSuite {
182      type FixtureParam = String
183      override def withFixture(test: OneArgTest) {
184        test(test.configMap("josh").toString)
185      }
186      test("get config"){ conf => assert(conf === "cool") }
187    }
188
189
190    private class TestWithConfigMap2 extends FixtureFunSuite {
191      type FixtureParam = Map[String,Any]
192      override def withFixture(test: OneArgTest) {
193        test(test.configMap)
194      }
195      test("get config"){ conf => assert(conf === Map("a" -> "z", "b" -> "y", "c" -> "x")) }
196    }
197
198    private class TagsTest extends FunSuite {
199      test("hello, world", org.scalatest.Tag("hello")) {"hello, world"}
200      test("hello, world again", org.scalatest.Tag("helloAgain")) {"hello, world again"}
201      test("tag3", org.scalatest.Tag("tag3")) {"tag3"}
202      test("throw") {throw new Exception("baah")}
203      test("assert bad") {assert(1 === 3)}
204    }
205
206    private class PrivateConstructor private() extends FunSuite
207
208    private class PendingTest extends FunSuite {
209      test("i am pending")(pending)
210    }
211
212    import org.scalatest.testng.TestNGSuite
213    private class SuiteWithSkippedTest extends TestNGSuite {
214      import org.testng.annotations.Test
215      @Test(groups = Array("run")) def dependeeThatFails() { throw new Exception("fail") }
216      @Test(dependsOnGroups = Array("run")) def depender() {}
217    }
218  }
219}
220