PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/scala/tests_spec.rb

https://github.com/smw/buildr
Ruby | 260 lines | 231 code | 9 blank | 20 comment | 1 complexity | 03ae2d1ce0bc571eeaeeec9afaa6e10b MD5 | raw file
Possible License(s): Apache-2.0
  1. # Licensed to the Apache Software Foundation (ASF) under one or more
  2. # contributor license agreements. See the NOTICE file distributed with this
  3. # work for additional information regarding copyright ownership. The ASF
  4. # licenses this file to you under the Apache License, Version 2.0 (the
  5. # "License"); 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, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations under
  14. # the License.
  15. require File.join(File.dirname(__FILE__), '../spec_helpers')
  16. # TODO's
  17. # -test passing System props
  18. # -test passing ENV variables
  19. # -test exclude group
  20. # -test include Suite's
  21. # -test exclude Suite's
  22. describe Buildr::Scala::ScalaTest do
  23. it 'should be the default test framework when test cases are in Scala' do
  24. write 'src/test/scala/com/example/MySuite.scala', <<-SCALA
  25. package com.example
  26. import org.scalatest.FunSuite
  27. class MySuite extends FunSuite {
  28. test("addition") {
  29. val sum = 1 + 1
  30. assert(sum === 2)
  31. }
  32. }
  33. SCALA
  34. define 'foo'
  35. project('foo').test.framework.should eql(:scalatest)
  36. end
  37. it 'should include Scalatest dependencies' do
  38. define('foo') { test.using(:scalatest) }
  39. project('foo').test.compile.dependencies.should include(*artifacts(Scala::ScalaTest.dependencies))
  40. project('foo').test.dependencies.should include(*artifacts(Scala::ScalaTest.dependencies))
  41. end
  42. it 'should include JMock dependencies' do
  43. define('foo') { test.using(:scalatest) }
  44. project('foo').test.compile.dependencies.should include(*artifacts(JMock.dependencies))
  45. project('foo').test.dependencies.should include(*artifacts(JMock.dependencies))
  46. end
  47. it 'should include ScalaCheck dependencies' do
  48. define('foo') { test.using(:scalatest) }
  49. project('foo').test.compile.dependencies.should include(*artifacts(Scala::Check.dependencies))
  50. project('foo').test.dependencies.should include(*artifacts(Scala::Check.dependencies))
  51. end
  52. it 'should set current directory' do
  53. mkpath 'baz'
  54. expected = File.expand_path('baz')
  55. expected.gsub!('/', '\\') if expected =~ /^[A-Z]:/ # Java returns back slashed paths for windows
  56. write 'baz/src/test/scala/CurrentDirectoryTestSuite.scala', <<-SCALA
  57. class CurrentDirectoryTestSuite extends org.scalatest.FunSuite {
  58. test("testCurrentDirectory") {
  59. assert("value" === System.getenv("NAME"))
  60. assert(#{expected.inspect} === new java.io.File(".").getCanonicalPath())
  61. }
  62. }
  63. SCALA
  64. define 'bar' do
  65. define 'baz' do
  66. test.include 'CurrentDirectoryTest'
  67. end
  68. end
  69. project('bar:baz').test.invoke
  70. end
  71. it 'should include public classes extending org.scalatest.FunSuite' do
  72. write 'src/test/scala/com/example/MySuite.scala', <<-SCALA
  73. package com.example
  74. import org.scalatest.FunSuite
  75. class MySuite extends FunSuite {
  76. test("addition") {
  77. val sum = 1 + 1
  78. assert(sum === 2)
  79. }
  80. }
  81. SCALA
  82. define('foo').test.invoke
  83. project('foo').test.tests.should include('com.example.MySuite')
  84. end
  85. it 'should ignore classes not extending org.scalatest.FunSuite' do
  86. write 'src/test/scala/com/example/NotASuite.scala', <<-SCALA
  87. package com.example
  88. class Another {
  89. }
  90. SCALA
  91. define('foo').test.invoke
  92. project('foo').test.tests.should be_empty
  93. end
  94. it 'should ignore inner classes' do
  95. write 'src/test/scala/com/example/InnerClassTest.scala', <<-SCALA
  96. package com.example
  97. import org.scalatest.FunSuite
  98. class InnerClassTest extends FunSuite {
  99. test("addition") {
  100. val sum = 1 + 1
  101. assert(sum === 2)
  102. }
  103. class InnerSuite extends FunSuite {
  104. test("addition") {
  105. val sum = 1 + 1
  106. assert(sum === 2)
  107. }
  108. }
  109. }
  110. SCALA
  111. define('foo').test.invoke
  112. project('foo').test.tests.should eql(['com.example.InnerClassTest'])
  113. end
  114. it 'should pass when ScalaTest test case passes' do
  115. write 'src/test/scala/PassingSuite.scala', <<-SCALA
  116. class PassingSuite extends org.scalatest.FunSuite {
  117. test("addition") {
  118. val sum = 1 + 1
  119. assert(sum === 2)
  120. }
  121. }
  122. SCALA
  123. lambda { define('foo').test.invoke }.should_not raise_error
  124. end
  125. it 'should fail when ScalaTest test case fails' do
  126. write 'src/test/scala/FailingSuite.scala', <<-SCALA
  127. class FailingSuite extends org.scalatest.FunSuite {
  128. test("failing") {
  129. assert(false)
  130. }
  131. }
  132. SCALA
  133. lambda { define('foo').test.invoke }.should raise_error(RuntimeError, /Tests failed/) rescue nil
  134. end
  135. it 'should report failed test names' do
  136. write 'src/test/scala/FailingSuite.scala', <<-SCALA
  137. class FailingSuite extends org.scalatest.FunSuite {
  138. test("failing") {
  139. assert(false)
  140. }
  141. }
  142. SCALA
  143. define('foo').test.invoke rescue
  144. project('foo').test.failed_tests.should include('FailingSuite')
  145. end
  146. it 'should report to reports/scalatest/TEST-TestSuiteName.txt' do
  147. write 'src/test/scala/PassingSuite.scala', <<-SCALA
  148. class PassingSuite extends org.scalatest.FunSuite {
  149. test("passing") {
  150. assert(true)
  151. }
  152. }
  153. SCALA
  154. define 'foo' do
  155. test.report_to.should be(file('reports/scalatest'))
  156. end
  157. project('foo').test.invoke
  158. project('foo').file('reports/scalatest/TEST-PassingSuite.txt').should exist
  159. end
  160. it 'should pass properties to Suite' do
  161. write 'src/test/scala/PropertyTestSuite.scala', <<-SCALA
  162. import org.scalatest._
  163. class PropertyTestSuite extends FunSuite {
  164. var properties = Map[String, Any]()
  165. test("testProperty") {
  166. assert(properties("name") === "value")
  167. }
  168. protected override def runTests(testName: Option[String], reporter: Reporter, stopper: Stopper,
  169. includes: Set[String], excludes: Set[String], properties: Map[String, Any]) {
  170. this.properties = properties;
  171. super.runTests(testName, reporter, stopper, includes, excludes, properties)
  172. }
  173. }
  174. SCALA
  175. define('foo').test.using :properties=>{ 'name'=>'value' }
  176. project('foo').test.invoke
  177. end
  178. it 'should run with ScalaCheck automatic test case generation' do
  179. write 'src/test/scala/MySuite.scala', <<-SCALA
  180. import org.scalatest.prop.PropSuite
  181. import org.scalacheck.Arbitrary._
  182. import org.scalacheck.Prop._
  183. class MySuite extends PropSuite {
  184. test("list concatenation") {
  185. val x = List(1, 2, 3)
  186. val y = List(4, 5, 6)
  187. assert(x ::: y === List(1, 2, 3, 4, 5, 6))
  188. check((a: List[Int], b: List[Int]) => a.size + b.size == (a ::: b).size)
  189. }
  190. test(
  191. "list concatenation using a test method",
  192. (a: List[Int], b: List[Int]) => a.size + b.size == (a ::: b).size
  193. )
  194. }
  195. SCALA
  196. define('foo')
  197. project('foo').test.invoke
  198. project('foo').test.passed_tests.should include('MySuite')
  199. end
  200. it 'should fail if ScalaCheck test case fails' do
  201. write 'src/test/scala/StringSuite.scala', <<-SCALA
  202. import org.scalatest.prop.PropSuite
  203. import org.scalacheck.Arbitrary._
  204. import org.scalacheck.Prop._
  205. class StringSuite extends PropSuite {
  206. test("startsWith") {
  207. check( (a: String, b: String) => (a+b).startsWith(a) )
  208. }
  209. test("endsWith") {
  210. check( (a: String, b: String) => (a+b).endsWith(b) )
  211. }
  212. // Is this really always true?
  213. test("concat") {
  214. check( (a: String, b: String) => (a+b).length > a.length && (a+b).length > b.length )
  215. }
  216. test("substring2") {
  217. check( (a: String, b: String) => (a+b).substring(a.length) == b )
  218. }
  219. test("substring3") {
  220. check( (a: String, b: String, c: String) =>
  221. (a+b+c).substring(a.length, a.length+b.length) == b )
  222. }
  223. }
  224. SCALA
  225. define('foo')
  226. project('foo').test.invoke rescue
  227. project('foo').test.failed_tests.should include('StringSuite')
  228. end
  229. end