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
17
18import org.scalatest.matchers.ShouldMatchers._
19
20class TestFailedExceptionWithImportSpec extends Spec {
21
22  val baseLineNumber = 22
23
24  describe("The TestFailedException") {
25
26    it("should give the proper line on fail()") {
27      try {
28        fail()
29      }
30      catch {
31        case e: TestFailedException =>
32          e.failedCodeFileNameAndLineNumberString match {
33            case Some(s) => s should equal ("TestFailedExceptionWithImportSpec.scala:" + (baseLineNumber + 6))
34            case None => fail("fail() didn't produce a file name and line number string: " + e.failedCodeFileNameAndLineNumberString, e)
35          }
36        case e =>
37          fail("fail() didn't produce a TestFailedException", e)
38      }
39    }
40
41    it("should give the proper line on fail(\"message\")") {
42      try {
43        fail("some message")
44      }
45      catch {
46        case e: TestFailedException =>
47          e.failedCodeFileNameAndLineNumberString match {
48            case Some(s) => s should equal ("TestFailedExceptionWithImportSpec.scala:" + (baseLineNumber + 21))
49            case None => fail("fail(\"some message\") didn't produce a file name and line number string", e)
50          }
51        case e =>
52          fail("fail(\"some message\") didn't produce a TestFailedException", e)
53      }
54    }
55
56    it("should give the proper line on fail(throwable)") {
57      try {
58        fail(new RuntimeException)
59      }
60      catch {
61        case e: TestFailedException =>
62          e.failedCodeFileNameAndLineNumberString match {
63            case Some(s) => s should equal ("TestFailedExceptionWithImportSpec.scala:" + (baseLineNumber + 36))
64            case None => fail("fail(throwable) didn't produce a file name and line number string", e)
65          }
66        case e =>
67          fail("fail(throwable) didn't produce a TestFailedException", e)
68      }
69    }
70
71    it("should give the proper line on fail(\"some message\", throwable)") {
72      try {
73        fail("some message", new RuntimeException)
74      }
75      catch {
76        case e: TestFailedException =>
77          e.failedCodeFileNameAndLineNumberString match {
78            case Some(s) => s should equal ("TestFailedExceptionWithImportSpec.scala:" + (baseLineNumber + 51))
79            case None => fail("fail(\"some message\", throwable) didn't produce a file name and line number string", e)
80          }
81        case e =>
82          fail("fail(\"some message\", throwable) didn't produce a TestFailedException", e)
83      }
84    }
85
86    it("should give the proper line on assert(false)") {
87      try {
88        assert(false)
89      }
90      catch {
91        case e: TestFailedException =>
92          e.failedCodeFileNameAndLineNumberString match {
93            case Some(s) => s should equal ("TestFailedExceptionWithImportSpec.scala:" + (baseLineNumber + 66))
94            case None => fail("assert(false) didn't produce a file name and line number string", e)
95          }
96        case e =>
97          fail("assert(false) didn't produce a TestFailedException", e)
98      }
99    }
100
101    it("should give the proper line on assert(false, \"some message\")") {
102      try {
103        assert(false, "some message")
104      }
105      catch {
106        case e: TestFailedException =>
107          e.failedCodeFileNameAndLineNumberString match {
108            case Some(s) => s should equal ("TestFailedExceptionWithImportSpec.scala:" + (baseLineNumber + 81))
109            case None => fail("assert(false, \"some message\") didn't produce a file name and line number string", e)
110          }
111        case e =>
112          fail("assert(false, \"some message\") didn't produce a TestFailedException", e)
113      }
114    }
115
116    it("should give the proper line on assert(1 === 2)") {
117      try {
118        assert(1 === 2)
119      }
120      catch {
121        case e: TestFailedException =>
122          e.failedCodeFileNameAndLineNumberString match {
123            case Some(s) => s should equal ("TestFailedExceptionWithImportSpec.scala:" + (baseLineNumber + 96))
124            case None => fail("assert(1 === 2) didn't produce a file name and line number string", e)
125          }
126        case e =>
127          fail("assert(1 === 2) didn't produce a TestFailedException", e)
128      }
129    }
130
131    it("should give the proper line on assert(1 === 2, \"some message\")") {
132      try {
133        assert(1 === 2, "some message")
134      }
135      catch {
136        case e: TestFailedException =>
137          e.failedCodeFileNameAndLineNumberString match {
138            case Some(s) => s should equal ("TestFailedExceptionWithImportSpec.scala:" + (baseLineNumber + 111))
139            case None => fail("assert(1 === 2, \"some message\") didn't produce a file name and line number string", e)
140          }
141        case e =>
142          fail("assert(1 === 2, \"some message\") didn't produce a TestFailedException", e)
143      }
144    }
145
146    it("should give the proper line on expect(1) { 2 }") {
147      try {
148        expect(1) { 2 }
149      }
150      catch {
151        case e: TestFailedException =>
152          e.failedCodeFileNameAndLineNumberString match {
153            case Some(s) => s should equal ("TestFailedExceptionWithImportSpec.scala:" + (baseLineNumber + 126))
154            case None => fail("expect(1) { 2 } didn't produce a file name and line number string", e)
155          }
156        case e =>
157          fail("expect(1) { 2 } didn't produce a TestFailedException", e)
158      }
159    }
160
161    it("should give the proper line on expect(1, \"some message\") { 2 }") {
162      try {
163        expect(1, "some message") { 2 }
164      }
165      catch {
166        case e: TestFailedException =>
167          e.failedCodeFileNameAndLineNumberString match {
168            case Some(s) => s should equal ("TestFailedExceptionWithImportSpec.scala:" + (baseLineNumber + 141))
169            case None => fail("expect(1, \"some message\") { 2 } didn't produce a file name and line number string", e)
170          }
171        case e =>
172          fail("expect(1, \"some message\") { 2 } didn't produce a TestFailedException", e)
173      }
174    }
175
176    it("should give the proper line on intercept[IllegalArgumentException] {}") {
177      try {
178        intercept[IllegalArgumentException] {}
179      }
180      catch {
181        case e: TestFailedException =>
182          e.failedCodeFileNameAndLineNumberString match {
183            case Some(s) => s should equal ("TestFailedExceptionWithImportSpec.scala:" + (baseLineNumber + 156))
184            case None => fail("intercept[IllegalArgumentException] {} didn't produce a file name and line number string", e)
185          }
186        case e =>
187          fail("intercept[IllegalArgumentException] {} didn't produce a TestFailedException", e)
188      }
189    }
190
191    it("should give the proper line on intercept[IllegalArgumentException] { throw new RuntimeException }") {
192      try {
193        intercept[IllegalArgumentException] { if (false) 1 else throw new RuntimeException }
194      }
195      catch {
196        case e: TestFailedException =>
197          e.failedCodeFileNameAndLineNumberString match {
198            case Some(s) => s should equal ("TestFailedExceptionWithImportSpec.scala:" + (baseLineNumber + 171))
199            case None => fail("intercept[IllegalArgumentException] { throw new RuntimeException } didn't produce a file name and line number string", e)
200          }
201        case e =>
202          fail("intercept[IllegalArgumentException] { throw new RuntimeException } didn't produce a TestFailedException", e)
203      }
204    }
205
206    it("should give the proper line on 1 should be === 2") {
207      try {
208        1 should be === 2
209      }
210      catch {
211        case e: TestFailedException =>
212          e.failedCodeFileNameAndLineNumberString match {
213            case Some(s) =>
214              if (s != ("TestFailedExceptionWithImportSpec.scala:" + (baseLineNumber + 186))) {
215                fail("s was: " + s, e)
216              }
217            case None => fail("assert(1 === 2) didn't produce a file name and line number string", e)
218          }
219        case e =>
220          fail("assert(1 === 2) didn't produce a TestFailedException", e)
221      }
222    }
223
224    it("should give the proper line on evaluating {} should produce [IllegalArgumentException] {}") {
225      try {
226        evaluating {} should produce [IllegalArgumentException]
227      }
228      catch {
229        case e: TestFailedException =>
230          e.failedCodeFileNameAndLineNumberString match {
231            case Some(s) =>
232            if (s != ("TestFailedExceptionWithImportSpec.scala:" + (baseLineNumber + 204))) {
233                fail("s was: " + s, e)
234              }
235            case None => fail("evaluating {} should produce [IllegalArgumentException] didn't produce a file name and line number string", e)
236          }
237        case e =>
238          fail("evaluating {} should produce [IllegalArgumentException] didn't produce a TestFailedException", e)
239      }
240    }
241
242    it("should give the proper line on evaluating { throw new RuntimeException } should produce [IllegalArgumentException]") {
243      try {
244        evaluating { if (false) 1 else throw new RuntimeException } should produce [IllegalArgumentException]
245      }
246      catch {
247        case e: TestFailedException =>
248          e.failedCodeFileNameAndLineNumberString match {
249            case Some(s) => s should equal ("TestFailedExceptionWithImportSpec.scala:" + (baseLineNumber + 222))
250            case None => fail("evaluating { throw new RuntimeException } should produce [IllegalArgumentException] didn't produce a file name and line number string", e)
251          }
252        case e =>
253          fail("evaluating { throw new RuntimeException } should produce [IllegalArgumentException] didn't produce a TestFailedException", e)
254      }
255    }
256  }
257}
258
259