Author: boisvert
Date: Sun Nov 13 18:57:28 2011
New Revision: 1201473
URL: http://svn.apache.org/viewvc?rev=1201473&view=rev
Log:
Added: Scala Specs2 framework support.
Change: Scala Specs upgraded to 1.6.9 if using Scala 2.9.1
Added:
buildr/trunk/lib/buildr/scala/org/apache/buildr/Specs2Runner.java
Modified:
buildr/trunk/CHANGELOG
buildr/trunk/lib/buildr/scala/bdd.rb
buildr/trunk/spec/sandbox.rb
buildr/trunk/spec/scala/bdd_spec.rb
Modified: buildr/trunk/CHANGELOG
URL:
http://svn.apache.org/viewvc/buildr/trunk/CHANGELOG?rev=1201473&r1=1201472&r2=1201473&view=diff
==============================================================================
--- buildr/trunk/CHANGELOG (original)
+++ buildr/trunk/CHANGELOG Sun Nov 13 18:57:28 2011
@@ -1,4 +1,6 @@
1.4.7 (Pending)
+* Added: Scala Specs2 framework support.
+* Change: Scala Specs upgraded to 1.6.9 if using Scala 2.9.1
* Fixed: ArtifactNamespace fails when using artifacts with classfier.
* Added: Buildr.transitive() now accepts hash with :scopes, :optional and
:scopes_transitive parameters
Modified: buildr/trunk/lib/buildr/scala/bdd.rb
URL:
http://svn.apache.org/viewvc/buildr/trunk/lib/buildr/scala/bdd.rb?rev=1201473&r1=1201472&r2=1201473&view=diff
==============================================================================
--- buildr/trunk/lib/buildr/scala/bdd.rb (original)
+++ buildr/trunk/lib/buildr/scala/bdd.rb Sun Nov 13 18:57:28 2011
@@ -29,13 +29,12 @@ module Buildr::Scala
VERSION = case
when Buildr::Scala.version?("2.8.0")
'1.6.5'
- when Buildr::Scala.version?("2.8.1")
+ when Buildr::Scala.version?("2.8.1"), Buildr::Scala.version?("2.8.2"),
Buildr::Scala.version?("2.9.0")
'1.6.8'
else
- '1.6.8'
+ '1.6.9'
end
-
class << self
def version
custom = Buildr.settings.build['scala.specs']
@@ -55,14 +54,17 @@ module Buildr::Scala
unless @dependencies
super
# Add utility classes (e.g. SpecsSingletonRunner) and other
dependencies
- @dependencies |= [ File.join(File.dirname(__FILE__)) ] + specs +
+ @dependencies |= [ File.join(File.dirname(__FILE__)) ] +
+ specs +
Check.dependencies + JUnit.dependencies +
Scalac.dependencies
end
@dependencies
end
def applies_to?(project) #:nodoc:
- !Dir[project.path_to(:source, bdd_dir, lang, '**/*.scala')].empty?
+ scala_files = Dir[project.path_to(:source, bdd_dir, lang,
'**/*.scala')]
+ return false if scala_files.empty?
+ scala_files.detect { |f| find(f, /\s+(org\.specs\.)/) }
end
private
@@ -71,6 +73,15 @@ module Buildr::Scala
Buildr.application.deprecated "Please use
Scala::Specs.dependencies/.version instead of ScalaSpecs::REQUIRES/VERSION"
dependencies
end
+
+ def find(file, pattern)
+ File.open(file, "r") do |infile|
+ while (line = infile.gets)
+ return true if line.match(pattern)
+ end
+ end
+ false
+ end
end
def initialize(task, options) #:nodoc:
@@ -115,6 +126,117 @@ module Buildr::Scala
end
end
end
+
+ class Specs2 < Buildr::TestFramework::JavaBDD
+ @lang = :scala
+ @bdd_dir = :spec
+
+ VERSION = case
+ when Buildr::Scala.version?("2.8.0"), Buildr::Scala.version?("2.8.1"),
Buildr::Scala.version?("2.8.2")
+ '1.5'
+ else
+ '1.6.1'
+ end
+
+ class << self
+ def version
+ custom = Buildr.settings.build['scala.specs2']
+ (custom =~ /:/) ? Buildr.artifact(custom).version : VERSION
+ end
+
+ def specs
+ custom = Buildr.settings.build['scala.specs2']
+ [ (custom =~ /:/) ? custom : "org.specs2:#{artifact}:jar:#{version}" ]
+ end
+
+ def artifact
+ Buildr.settings.build['scala.specs2.artifact'] ||
"specs2_#{Buildr::Scala.version_without_build}"
+ end
+
+ def scalaz_dependencies
+ if Buildr::Scala.version?("2.8")
+ []
+ else
+ default_version = "6.0.1"
+ custom_version = Buildr.settings.build['scala.specs2-scalaz']
+ version = (custom_version =~ /:/) ?
Buildr.artifact(custom_version).version : default_version
+
+ artifact = Buildr.settings.build['scala.specs2-scalaz.artifact'] ||
"specs2-scalaz-core_#{Buildr::Scala.version_without_build}"
+
+ custom_spec = Buildr.settings.build['scala.specs2-scalaz']
+ spec = [ (custom_spec =~ /:/) ? custom_spec :
"org.specs2:#{artifact}:jar:#{version}" ]
+ Buildr.transitive(spec, :scopes => [nil, "compile", "runtime",
"provided", "optional"], :optional => true)
+ end
+ end
+
+ def dependencies
+ unless @dependencies
+ super
+
+ # Add utility classes (e.g. SpecsSingletonRunner) and other
dependencies
+ options = {
+ :scopes => [nil, "compile", "runtime", "provided", "optional"],
+ :optional => true
+ }
+ @dependencies |= [ File.join(File.dirname(__FILE__)) ] +
Buildr.transitive(specs, options) +
+ scalaz_dependencies + Check.dependencies +
JUnit.dependencies +
+ Scalac.dependencies
+ end
+ @dependencies
+ end
+
+ def applies_to?(project) #:nodoc:
+ scala_files = Dir[project.path_to(:source, bdd_dir, lang,
'**/*.scala')]
+ return false if scala_files.empty?
+ scala_files.detect { |f| find(f, /\s+(org\.specs2\.)/) }
+ end
+
+ private
+
+ def find(file, pattern)
+ File.open(file, "r") do |infile|
+ while (line = infile.gets)
+ return true if line.match(pattern)
+ end
+ end
+ false
+ 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)
+ filter_classes(dependencies, :interfaces => ['org.specs2.Specification',
'org.specs2.mutable.Specification'])
+ end
+
+ def run(specs, dependencies) #:nodoc:
+ properties = { "specs2.outDir" => task.compile.target.to_s }
+
+ cmd_options = { :properties => options[:properties].merge(properties),
+ :java_args => options[:java_args],
+ :classpath => dependencies,
+ :name => false }
+
+ runner = 'org.apache.buildr.Specs2Runner'
+ specs.inject [] do |passed, spec|
+ begin
+ Java::Commands.java(runner, spec, cmd_options)
+ rescue => e
+ passed
+ else
+ passed << spec
+ end
+ end
+ end
+ end
end
# Backwards compatibility stuff. Remove in 1.5.
@@ -123,3 +245,5 @@ module Buildr
end
Buildr::TestFramework << Buildr::Scala::Specs
+Buildr::TestFramework << Buildr::Scala::Specs2
+
Added: buildr/trunk/lib/buildr/scala/org/apache/buildr/Specs2Runner.java
URL:
http://svn.apache.org/viewvc/buildr/trunk/lib/buildr/scala/org/apache/buildr/Specs2Runner.java?rev=1201473&view=auto
==============================================================================
--- buildr/trunk/lib/buildr/scala/org/apache/buildr/Specs2Runner.java (added)
+++ buildr/trunk/lib/buildr/scala/org/apache/buildr/Specs2Runner.java Sun Nov
13 18:57:28 2011
@@ -0,0 +1,38 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.buildr;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+
+public class Specs2Runner {
+
+ public static void main(String[] args) {
+ try {
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ Class clazz = loader.loadClass("org.specs2.runner.ClassRunner");
+ Object instance = clazz.newInstance();
+ Method main = clazz.getMethod("main", String[].class);
+ main.invoke(instance, new Object[] { args });
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+}
+
+
Modified: buildr/trunk/spec/sandbox.rb
URL:
http://svn.apache.org/viewvc/buildr/trunk/spec/sandbox.rb?rev=1201473&r1=1201472&r2=1201473&view=diff
==============================================================================
--- buildr/trunk/spec/sandbox.rb (original)
+++ buildr/trunk/spec/sandbox.rb Sun Nov 13 18:57:28 2011
@@ -42,7 +42,6 @@ artifacts(
Buildr::JaxbXjc.dependencies,
Buildr::Bnd.dependencies,
Buildr::Scala::Scalac.dependencies,
- Buildr::Scala::Specs.dependencies,
Buildr::Shell::BeanShell.artifact,
Buildr::Clojure.dependencies
).each do |path|
Modified: buildr/trunk/spec/scala/bdd_spec.rb
URL:
http://svn.apache.org/viewvc/buildr/trunk/spec/scala/bdd_spec.rb?rev=1201473&r1=1201472&r2=1201473&view=diff
==============================================================================
--- buildr/trunk/spec/scala/bdd_spec.rb (original)
+++ buildr/trunk/spec/scala/bdd_spec.rb Sun Nov 13 18:57:28 2011
@@ -13,13 +13,13 @@
# License for the specific language governing permissions and limitations under
# the License.
-if Java.java.lang.System.getProperty("java.runtime.version") >= "1.6"
-
require File.expand_path(File.join(File.dirname(__FILE__), '..',
'spec_helpers'))
+if Java.java.lang.System.getProperty("java.runtime.version") >= "1.6"
+
describe Buildr::Scala::Specs do
- it 'should be the default when tests in src/spec/scala' do
+ it 'should be the default when tests in src/spec/scala reference
"org.specs"' do
write 'src/spec/scala/com/example/MySpecs.scala', <<-SCALA
package com.example
object MySpecs extends org.specs.Specification {
@@ -47,12 +47,6 @@ describe Buildr::Scala::Specs do
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 objects extending org.specs.Specification' do
write 'src/spec/scala/com/example/MySpecs.scala', <<-SCALA
package com.example
@@ -119,6 +113,110 @@ describe Buildr::Scala::Specs do
end
end
+describe Buildr::Scala::Specs2 do
+
+ it 'should be the default when tests in src/spec/scala reference
"org.specs2"' do
+ write 'src/spec/scala/com/example/MySpecs.scala', <<-SCALA
+ package com.example
+ object MySpecs extends org.specs2.Specification {
+ "it" should {
+ "add" in {
+ val sum = 1 + 1
+ sum mustEqual 2
+ }
+ }
+ }
+ SCALA
+ define 'foo'
+ project('foo').test.framework.should eql(:specs2)
+ end
+
+ it 'should include Specs2 dependencies' do
+ define('foo') { test.using(:specs2) }
+ project('foo').test.compile.dependencies.should
include(*artifacts(Scala::Specs2.dependencies))
+ project('foo').test.dependencies.should
include(*artifacts(Scala::Specs2.dependencies))
+ end
+
+ it 'should include ScalaCheck dependencies' do
+ define('foo') { test.using(:specs2) }
+ 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 public objects extending
org.specs2.mutable.Specification' do
+ write 'src/spec/scala/com/example/MySpecs.scala', <<-SCALA
+ package com.example
+ object MySpecs extends org.specs2.mutable.Specification {
+ "it" should {
+ "add" in {
+ val sum = 1 + 1
+ sum mustEqual 2
+ }
+ }
+ }
+ SCALA
+ define('foo').test.framework.should eql(:specs2)
+ project('foo').test.invoke
+ project('foo').test.tests.should include('com.example.MySpecs$')
+ end
+
+ it 'should include classes extending org.specs2.SpecificationWithJUnit' do
+ write 'src/spec/scala/com/example/MySpecs.scala', <<-SCALA
+ package com.example
+ import org.specs2.mutable._
+ class MySpecs extends org.specs2.SpecificationWithJUnit {
+ def is = {
+ "The 'Hello world' string should" ^
+ "contain 11 characters" ! {
+ "Hello world" must have size(11)
+ }^
+ "start with 'Hello'" ! {
+ "Hello world" must startWith("Hello")
+ }^
+ "end with 'world'" ! {
+ "Hello world" must endWith("world")
+ }
+ }
+ }
+ SCALA
+ define('foo')
+ project('foo').test.framework.should eql(:specs2)
+ project('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.specs2.mutable.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 spec fails' do
+ write 'src/spec/scala/StringSpecs.scala', <<-SCALA
+ import org.specs2.mutable._
+
+ 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
+
elsif Buildr::VERSION >= '1.5'
raise "JVM version guard in #{__FILE__} should be removed since it is
assumed that Java 1.5 is no longer supported."
end