eerohele / expek

Unit test XSLT with Scala

Version Matrix

Expek Build Status

Write unit tests for XSLT in Scala. Why not?

Using Expek with IntelliJ IDEA

NOTE: This library is still in its infancy, so I can't guarantee that the API is stable yet. Any feedback on how to improve and extend it is warmly welcomed.

Example

Here's a simple Expek specification:

import org.specs2.mutable
import com.github.eerohele.expek.XsltSpecification

class ExampleSpecification extends mutable.Specification with XsltSpecification {
    val stylesheet = XSLT.file("/path/to/stylesheet.xsl")

    // Test whether your XSLT stylesheet actually turns <foo> into <bar>
    "<foo> becomes <bar>" >> {
        applying { <foo a="b">x</foo> } must produce { <bar c="d">y</bar> }
    }
}

For more examples on how you can use Expek see the example specifications and example.xsl.

To run the example specifications, clone this repo and run sbt examples/run (you must have SBT installed).

There's also an experimental set of tests I wrote to test DITA-OT HTML5 XSLT stylesheets.

Documentation

You might also find the documentation for specs2 helpful.

Use

SBT

In your build.sbt, add:

resolvers += Resolver.jcenterRepo

libraryDependencies ++= Seq("com.github.eerohele" % "expek_2.11" % "0.1.0")

Stick tests under src/test/scala, run sbt test.

Gradle

In your build.gradle, add:

apply plugin: 'scala'

repositories {
     jcenter()

     maven {
        url "https://oss.sonatype.org/content/repositories/releases"
     }
}

dependencies {
    testCompile 'org.specs2:specs2-junit_2.11:3.8.3'
    testCompile 'com.github.eerohele:expek_2.11:0.1.0'
}

Stick tests under src/test/scala.

In your Expek specification Scala file, add the @RunWith annotation:

import org.junit.runner.RunWith
import org.specs2.runner.JUnitRunner

@RunWith(classOf[JUnitRunner])
class MySpecification ... { ... }

Run gradle test.

Components

  • Use Saxon's Xslt30Transformer to apply or call templates and functions in the stylesheet.
  • Compare expected and actual XML with XMLUnit 2.
  • Run tests with specs2.