You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildr.apache.org by bo...@apache.org on 2009/03/18 03:03:11 UTC

svn commit: r755456 - in /buildr/trunk: CHANGELOG doc/languages.textile lib/buildr/java/tests.rb lib/buildr/scala.rb lib/buildr/scala/bdd.rb lib/buildr/scala/tests.rb spec/sandbox.rb spec/scala/bdd_spec.rb spec/scala/tests_spec.rb

Author: boisvert
Date: Wed Mar 18 02:03:11 2009
New Revision: 755456

URL: http://svn.apache.org/viewvc?rev=755456&view=rev
Log:
BUILDR-209 Scala Specs Should Use src/specs/scala/

Added:
    buildr/trunk/lib/buildr/scala/bdd.rb
    buildr/trunk/spec/scala/bdd_spec.rb
Modified:
    buildr/trunk/CHANGELOG
    buildr/trunk/doc/languages.textile
    buildr/trunk/lib/buildr/java/tests.rb
    buildr/trunk/lib/buildr/scala.rb
    buildr/trunk/lib/buildr/scala/tests.rb
    buildr/trunk/spec/sandbox.rb
    buildr/trunk/spec/scala/tests_spec.rb

Modified: buildr/trunk/CHANGELOG
URL: http://svn.apache.org/viewvc/buildr/trunk/CHANGELOG?rev=755456&r1=755455&r2=755456&view=diff
==============================================================================
--- buildr/trunk/CHANGELOG (original)
+++ buildr/trunk/CHANGELOG Wed Mar 18 02:03:11 2009
@@ -25,6 +25,7 @@
 * Change: BUILDR-177 Moved cobertura and emma extensions to lib directory.
 * Change: BUILDR-187 Source code attachment for Eclipse .classpath.
 * Change: BUILDR-188 Source code attachment for IDEA .iml file (Marko Sibakov).
+* Change: BUILDR-209 Scala Specs Should Use src/specs/scala/
 * Change: BUILDR-237 Use MacPorts Scala on OS X.
 * Change: BUILDR-260 Upgrade to Scala 2.7.3 compatible dependencies:  
           ScalaSpecs 1.4.3, ScalaCheck 1.5 and ScalaTest 0.9.5

Modified: buildr/trunk/doc/languages.textile
URL: http://svn.apache.org/viewvc/buildr/trunk/doc/languages.textile?rev=755456&r1=755455&r2=755456&view=diff
==============================================================================
--- buildr/trunk/doc/languages.textile (original)
+++ buildr/trunk/doc/languages.textile Wed Mar 18 02:03:11 2009
@@ -205,9 +205,7 @@
 
 h3. Testing with Scala
 
-Buildr supports three Scala testing frameworks:   "ScalaTest":http://www.artima.com/scalatest,  "ScalaCheck":http://code.google.com/p/scalacheck/ and  "Specs":http://code.google.com/p/specs/.
-
-Scala testing is automatically enabled if you have any @.scala@ source files under @src/test/scala@.  If you are not using this convention, you can explicit set the test framework by doing,
+Buildr supports two main Scala testing frameworks:   "ScalaTest":http://www.artima.com/scalatest and  "Specs":http://code.google.com/p/specs/.  "ScalaCheck":http://code.google.com/p/scalacheck/ is also supported within the confines of either of these two frameworks.  Thus, your Specs may use ScalaCheck properties, as may your ScalaTest suites.
 
 <notextile>
 {% highlight ruby %}
@@ -215,10 +213,10 @@
 {% endhighlight %}
 </notextile>
 
-The @:scalatest@ test framework handles ScalaTest, Specs and ScalaCheck therefore all 3 frameworks may be used within the same project.
-
 h4. ScalaTest
 
+ScalaTest support is activated automatically when there are any @.scala@ source files contained in the @src/test/scala@ directory.  If you are not using this directory convention, you may force the test framework by using the @test.using :scalatest@ directive.
+
 Buildr automatically detects and runs tests that extend the @org.scalatest.Suite@ interface.
 
 A very simplistic test class might look like,
@@ -261,7 +259,9 @@
 
 h4. Specs
 
-The @:scalatest@ framework currently recognizes specifications with class names ending with "Specs", e.g., org.example.StringSpecs.
+Specs is automatically selected whenever there are @.scala@ source files under the @src/spec/scala@ directory.  It is also possible to force selection of the test framework by using the @test.using :specs@ directive.  This can sometimes be useful when Scala sources may be found in *both* @src/test/scala@ and @src/spec/scala@.  Normally in such cases, ScalaTest will have selection precedence, meaning that in case of a conflict between it and Specs, ScalaTest will be chosen.
+
+Any objects which extend the @org.specs.Specification@ superclass will be automatically detected and run.  Note that any *classes* which extend @Specification@ will also be invoked.  As such classes will not have a @main@ method, such an invocation will raise an error.
 
 A simple specification might look like this:
 

Modified: buildr/trunk/lib/buildr/java/tests.rb
URL: http://svn.apache.org/viewvc/buildr/trunk/lib/buildr/java/tests.rb?rev=755456&r1=755455&r2=755456&view=diff
==============================================================================
--- buildr/trunk/lib/buildr/java/tests.rb (original)
+++ buildr/trunk/lib/buildr/java/tests.rb Wed Mar 18 02:03:11 2009
@@ -52,7 +52,7 @@
       target = task.compile.target.to_s
       candidates = Dir["#{target}/**/*.class"].
         map { |file| Util.relative_path(file, target).ext('').gsub(File::SEPARATOR, '.') }.
-        reject { |name| name =~ /\$/ }
+        reject { |name| name =~ /\$./ }
       result = []
       if criteria[:class_names]
         result.concat candidates.select { |name| criteria[:class_names].flatten.any? { |pat| pat === name } }
@@ -74,7 +74,8 @@
         info "#{ex.class}: #{ex.message}"
         raise
       end
-      result.uniq
+      # We strip Scala singleton objects whose .class ends with $
+      result.map { |c| (c =~ /\$$/) ? c[0..(c.size - 2)] : c  }.uniq
     end
     
   end

Modified: buildr/trunk/lib/buildr/scala.rb
URL: http://svn.apache.org/viewvc/buildr/trunk/lib/buildr/scala.rb?rev=755456&r1=755455&r2=755456&view=diff
==============================================================================
--- buildr/trunk/lib/buildr/scala.rb (original)
+++ buildr/trunk/lib/buildr/scala.rb Wed Mar 18 02:03:11 2009
@@ -19,5 +19,6 @@
 
 require 'buildr/scala/compiler'
 require 'buildr/scala/tests'
+require 'buildr/scala/bdd'
 
 Object::Scala = Buildr::Scala

Added: buildr/trunk/lib/buildr/scala/bdd.rb
URL: http://svn.apache.org/viewvc/buildr/trunk/lib/buildr/scala/bdd.rb?rev=755456&view=auto
==============================================================================
--- buildr/trunk/lib/buildr/scala/bdd.rb (added)
+++ buildr/trunk/lib/buildr/scala/bdd.rb Wed Mar 18 02:03:11 2009
@@ -0,0 +1,83 @@
+require 'buildr/core/build'
+require 'buildr/core/compile'
+require 'buildr/java/bdd'
+require 'buildr/scala/tests'
+
+module Buildr::Scala
+  
+  # Specs is a Scala based BDD framework.
+  # To use in your project:
+  #
+  #   test.using :specs
+  # 
+  # This framework will search in your project for:
+  #   src/spec/scala/**/*.scala
+  class Specs < Buildr::TestFramework::JavaBDD
+    @lang = :scala
+    @bdd_dir = :spec
+
+    VERSION = '1.4.3'
+    
+    class << self
+      def version
+        Buildr.settings.build['scala.specs'] || VERSION
+      end
+      
+      def dependencies
+        ["org.specs:specs:jar:#{version}"] + Check.dependencies + JMock.dependencies
+      end
+      
+      def applies_to?(project)  #:nodoc:
+        !Dir[project.path_to(:source, bdd_dir, lang, '**/*.scala')].empty?
+      end
+
+    private
+      def const_missing(const)
+        return super unless const == :REQUIRES # TODO: remove in 1.5
+        Buildr.application.deprecated "Please use Scala::Specs.dependencies/.version instead of ScalaSpecs::REQUIRES/VERSION"
+        dependencies
+      end
+    end
+
+    def initialize(task, options) #:nodoc:
+      super
+      
+      specs = task.project.path_to(:source, :spec, :scala)
+      task.compile.from specs if File.directory?(specs)
+      
+      resources = task.project.path_to(:source, :spec, :resources)
+      task.resources.from resources if File.directory?(resources)
+    end
+    
+    def tests(dependencies)
+      dependencies += [task.compile.target.to_s]
+      filter_classes(dependencies, :interfaces => ['org.specs.Specification'])
+    end
+    
+    def run(specs, dependencies)  #:nodoc:
+      dependencies += [task.compile.target.to_s] + Scalac.dependencies
+      
+      cmd_options = { :properties => options[:properties],
+                      :java_args => options[:java_args],
+                      :classpath => dependencies}
+      
+      specs.inject [] do |passed, spec|
+        begin
+          Java.load
+          Java::Commands.java(spec, cmd_options)
+        rescue => e
+          passed
+        else
+          passed << spec
+        end
+      end
+    end
+  end
+end
+
+# Backwards compatibility stuff.  Remove in 1.5.
+module Buildr
+  ScalaSpecs = Scala::Specs
+end
+
+Buildr::TestFramework << Buildr::Scala::Specs

Modified: buildr/trunk/lib/buildr/scala/tests.rb
URL: http://svn.apache.org/viewvc/buildr/trunk/lib/buildr/scala/tests.rb?rev=755456&r1=755455&r2=755456&view=diff
==============================================================================
--- buildr/trunk/lib/buildr/scala/tests.rb (original)
+++ buildr/trunk/lib/buildr/scala/tests.rb Wed Mar 18 02:03:11 2009
@@ -21,32 +21,8 @@
 
 
 module Buildr::Scala
-
-  # Scala::Specs is available when using Scala::Test
-  module ScalaSpecs
-    VERSION = '1.4.3'
-    
-    class << self
-      def version
-        Buildr.settings.build['scala.specs'] || VERSION
-      end
-      
-      def dependencies
-        ["org.specs:specs:jar:#{version}"]
-      end  
-
-    private
-      def const_missing(const)
-        return super unless const == :REQUIRES # TODO: remove in 1.5
-        Buildr.application.deprecated "Please use Scala::Specs.dependencies/.version instead of ScalaSpecs::REQUIRES/VERSION"
-        dependencies
-      end
-    end
-  end
-
-
-  # Scala::Check is available when using Scala::Test
-  module ScalaCheck
+  # Scala::Check is available when using Scala::Test or Scala::Specs
+  module Check
     VERSION = '1.5'
     
     class << self
@@ -84,12 +60,11 @@
       end
       
       def dependencies
-        ["org.scala-tools.testing:scalatest:jar:#{version}"] + ScalaSpecs.dependencies +
-          ScalaCheck.dependencies + JMock.dependencies
+        ["org.scala-tools.testing:scalatest:jar:#{version}"] + Check.dependencies + JMock.dependencies
       end  
 
       def applies_to?(project) #:nodoc:
-        project.test.compile.language == :scala
+        !Dir[project.path_to(:source, :test, :scala, '**/*.scala')].empty?
       end 
       
     private
@@ -113,36 +88,13 @@
     end
 
     def tests(dependencies) #:nodoc:
-      suites = filter_classes(dependencies, :interfaces => %w{org.scalatest.Suite})
-      # we should really filter using :class => %w{org.specs.Specification} instead of naming convention
-      specs = filter_classes(dependencies, :class_names => [/Specs?$/])
-      [suites, specs].flatten
+      filter_classes(dependencies, :interfaces => %w{org.scalatest.Suite})
     end
 
-    def run(tests, dependencies) #:nodoc:
+    def run(scalatest, dependencies) #:nodoc:
       mkpath task.report_to.to_s
       success = []
-      scalatest = tests.select { |t| t !~ /Specs?$/ }
-      specs = tests.select { |t| t =~ /Specs?$/ }
-
-      # Specs
-      nostacktrace = (options[:nostacktrace]) ? "-ns" : ""
-      cmd_options = { :properties => options[:properties],
-                      :java_args => options[:java_args],
-                      :classpath => dependencies + Scalac.dependencies}
-                      
-      specs.each do |spec|
-        Java.load
-        begin
-          Java::Commands.java(spec, cmd_options)
-        rescue => e
-          print e.message
-        else
-          success << spec
-        end
-      end
 
-      # ScalaTest
       reporter_options = 'TFGBSAR' # testSucceeded, testFailed, testIgnored, suiteAborted, runStopped, runAborted, runCompleted
       scalatest.each do |suite|
         info "ScalaTest #{suite.inspect}"
@@ -196,8 +148,7 @@
 
 # Backwards compatibility stuff.  Remove in 1.5.
 module Buildr
-  ScalaSpecs = Scala::ScalaSpecs
-  ScalaCheck = Scala::ScalaCheck
+  ScalaCheck = Scala::Check
   ScalaTest = Scala::ScalaTest
 end
 

Modified: buildr/trunk/spec/sandbox.rb
URL: http://svn.apache.org/viewvc/buildr/trunk/spec/sandbox.rb?rev=755456&r1=755455&r2=755456&view=diff
==============================================================================
--- buildr/trunk/spec/sandbox.rb (original)
+++ buildr/trunk/spec/sandbox.rb Wed Mar 18 02:03:11 2009
@@ -19,6 +19,7 @@
 # repository and cache these across test cases.
 Buildr.application.instance_eval { @rakefile = File.expand_path('buildfile') }
 repositories.remote << 'http://repo1.maven.org/maven2'
+repositories.remote << 'http://scala-tools.org/repo-releases'
 
 # Add a 'require' here only for optional extensions, not for extensions that should be loaded by default.
 require 'buildr/groovy'

Added: buildr/trunk/spec/scala/bdd_spec.rb
URL: http://svn.apache.org/viewvc/buildr/trunk/spec/scala/bdd_spec.rb?rev=755456&view=auto
==============================================================================
--- buildr/trunk/spec/scala/bdd_spec.rb (added)
+++ buildr/trunk/spec/scala/bdd_spec.rb Wed Mar 18 02:03:11 2009
@@ -0,0 +1,134 @@
+require File.join(File.dirname(__FILE__), '../spec_helpers')
+
+describe Buildr::Scala::Specs do
+  it 'should be the default when tests in src/spec/scala' do
+    write 'src/spec/scala/com/example/MySpecs.scala', <<-SCALA
+      package com.example
+      object MySpecs extends org.specs.Specification {
+        "it" should {
+          "add" in {
+            val sum = 1 + 1
+            sum mustEqual 2
+          }
+        }
+      }
+    SCALA
+    define 'foo'
+    project('foo').test.framework.should eql(:specs)
+  end
+  
+  it 'should include Specs dependencies' do
+    define('foo') { test.using(:specs) }
+    project('foo').test.compile.dependencies.should include(*artifacts(Scala::Specs.dependencies))
+    project('foo').test.dependencies.should include(*artifacts(Scala::Specs.dependencies))
+  end
+  
+  it 'should include ScalaCheck dependencies' do
+    define('foo') { test.using(:specs) }
+    project('foo').test.compile.dependencies.should include(*artifacts(Scala::Check.dependencies))
+    project('foo').test.dependencies.should include(*artifacts(Scala::Check.dependencies))
+  end
+
+  it 'should include JMock dependencies' do
+    define('foo') { test.using(:scalatest) }
+    project('foo').test.compile.dependencies.should include(*artifacts(JMock.dependencies))
+    project('foo').test.dependencies.should include(*artifacts(JMock.dependencies))
+  end
+
+  it 'should include public classes extending org.specs.Specification' do
+    write 'src/spec/scala/com/example/MySpecs.scala', <<-SCALA
+      package com.example
+      object MySpecs extends org.specs.Specification {
+        "it" should {
+          "add" in {
+            val sum = 1 + 1
+            sum mustEqual 2
+          }
+        }
+      }
+    SCALA
+    define('foo').test.invoke
+    project('foo').test.tests.should include('com.example.MySpecs')
+  end
+
+  it 'should pass when spec passes' do
+    write 'src/spec/scala/PassingSpecs.scala', <<-SCALA
+      object PassingSpecs extends org.specs.Specification {
+        "it" should {
+          "add" in {
+            val sum = 1 + 1
+            sum mustEqual 2
+          }
+        }
+      }
+    SCALA
+    lambda { define('foo').test.invoke }.should_not raise_error
+  end
+
+  it 'should fail when ScalaTest test case fails' do
+    write 'src/spec/scala/FailingSpecs.scala', <<-SCALA
+      object FailingSpecs extends org.specs.Specification {
+        "it" should {
+          "add" in {
+            val sum = 1 + 1
+            sum mustEqual 42
+          }
+        }
+      }
+    SCALA
+    lambda { define('foo').test.invoke }.should raise_error(RuntimeError, /Tests failed/) rescue nil
+  end
+
+  it 'should report failed test names' do
+    write 'src/spec/scala/FailingSpecs.scala', <<-SCALA
+      object FailingSpecs extends org.specs.Specification {
+        "it" should {
+          "add" in {
+            val sum = 1 + 1
+            sum mustEqual 42
+          }
+        }
+      }
+    SCALA
+    define('foo').test.invoke rescue
+    project('foo').test.failed_tests.should include('FailingSpecs')
+  end
+  
+  it 'should compile and run specifications with "Specs" suffix' do
+    write 'src/spec/scala/HelloWorldSpecs.scala', <<-SCALA
+      import org.specs._
+  
+      object HelloWorldSpecs extends Specification {
+        "'hello world' has 11 characters" in {
+          "hello world".size must beEqualTo(11)
+        }
+        "'hello world' matches 'h.* w.*'" in {
+          "hello world" must beMatching("h.* w.*")
+        }
+      }
+    SCALA
+    define('foo')
+    project('foo').test.invoke
+    project('foo').test.passed_tests.should include('HelloWorldSpecs')
+  end
+
+
+  it 'should fail if specifications fail' do
+    write 'src/spec/scala/StringSpecs.scala', <<-SCALA
+      import org.specs._
+      import org.specs.runner._
+      
+      object StringSpecs extends Specification {
+        "empty string" should {
+          "have a zero length" in {
+            ("".length) mustEqual(1)
+          }
+        }
+      }
+    SCALA
+    define('foo')
+    project('foo').test.invoke rescue
+    project('foo').test.failed_tests.should include('StringSpecs')
+  end
+
+end

Modified: buildr/trunk/spec/scala/tests_spec.rb
URL: http://svn.apache.org/viewvc/buildr/trunk/spec/scala/tests_spec.rb?rev=755456&r1=755455&r2=755456&view=diff
==============================================================================
--- buildr/trunk/spec/scala/tests_spec.rb (original)
+++ buildr/trunk/spec/scala/tests_spec.rb Wed Mar 18 02:03:11 2009
@@ -40,13 +40,6 @@
     project('foo').test.framework.should eql(:scalatest)
   end
 
-  it 'should be picked if the test language is Scala' do
-    define 'foo' do
-      test.compile.using(:scalac)
-      test.framework.should eql(:scalatest)
-    end
-  end
-
   it 'should include Scalatest dependencies' do
     define('foo') { test.using(:scalatest) }
     project('foo').test.compile.dependencies.should include(*artifacts(Scala::ScalaTest.dependencies))
@@ -58,17 +51,11 @@
     project('foo').test.compile.dependencies.should include(*artifacts(JMock.dependencies))
     project('foo').test.dependencies.should include(*artifacts(JMock.dependencies))
   end
-  
-  it 'should include Specs dependencies' do
-    define('foo') { test.using(:scalatest) }
-    project('foo').test.compile.dependencies.should include(*artifacts(Scala::ScalaSpecs.dependencies))
-    project('foo').test.dependencies.should include(*artifacts(Scala::ScalaSpecs.dependencies))
-  end
 
   it 'should include ScalaCheck dependencies' do
     define('foo') { test.using(:scalatest) }
-    project('foo').test.compile.dependencies.should include(*artifacts(Scala::ScalaCheck.dependencies))
-    project('foo').test.dependencies.should include(*artifacts(Scala::ScalaCheck.dependencies))
+    project('foo').test.compile.dependencies.should include(*artifacts(Scala::Check.dependencies))
+    project('foo').test.dependencies.should include(*artifacts(Scala::Check.dependencies))
   end
 
   it 'should set current directory' do
@@ -208,42 +195,6 @@
     project('foo').test.invoke
   end
 
-  it 'should compile and run specifications with "Specs" suffix' do
-    write 'src/test/scala/HelloWorldSpecs.scala', <<-SCALA
-      import org.specs._
-  
-      object HelloWorldSpecs extends Specification {
-        "'hello world' has 11 characters" in {
-          "hello world".size must beEqualTo(11)
-        }
-        "'hello world' matches 'h.* w.*'" in {
-          "hello world" must beMatching("h.* w.*")
-        }
-      }
-    SCALA
-    define('foo').test.using :scalatest
-    project('foo').test.invoke
-    project('foo').test.passed_tests.should include('HelloWorldSpecs')
-  end
-
-  it 'should fail if specifications fail' do
-    write 'src/test/scala/StringSpecs.scala', <<-SCALA
-      import org.specs._
-      import org.specs.runner._
-      
-      object StringSpecs extends Specification {
-        "empty string" should {
-          "have a zero length" in {
-            ("".length) mustEqual(1)
-          }
-        }
-      }
-    SCALA
-    define('foo')
-    project('foo').test.invoke rescue
-    project('foo').test.failed_tests.should include('StringSpecs')
-  end
-      
   it 'should run with ScalaCheck automatic test case generation' do
     write 'src/test/scala/MySuite.scala', <<-SCALA
       import org.scalatest.prop.PropSuite
@@ -269,7 +220,7 @@
     project('foo').test.invoke
     project('foo').test.passed_tests.should include('MySuite')
   end
-
+  
   it 'should fail if ScalaCheck test case fails' do
     write 'src/test/scala/StringSuite.scala', <<-SCALA
       import org.scalatest.prop.PropSuite
@@ -304,5 +255,6 @@
     project('foo').test.invoke rescue
     project('foo').test.failed_tests.should include('StringSuite')
   end
+
 end