Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
earldouglas committed Jun 10, 2013
0 parents commit 018cba1
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 0 deletions.
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2012, Versal Group, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT , INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
81 changes: 81 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# FireOtter

FireOtter is a simple CSV parsing library for building tests on human-readable, spreadsheet-based specifications.

## Usage

Add FireOtter to the list of dependencies in build.sbt:

```
resolvers += "sonatype-snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"
libraryDependencies += "com.versal" %% "fireotter" % "0.1.0-SNAPSHOT" % "test"
```

## How it works

FireOtter isn't much more than a CSV parser, but it provides a handy tool for test-driven development based on *code-free* test specifications written as human-readable spreadsheets.

First, establish a convention for how to express test specifications as spreadsheet rows. Then write the code necessary to bind them, given as a `Traversable[Seq[String]]`, to your favorite test framework

Now you can run through the usual TDD loop:

1. Describe a desired feature in terms of your expression convention
2. Write the code needed to make the tests pass
3. Repeat

## Example

*CSV test specifications:*

<table>
<tr><th>test</th><th>function</th><th>input</th><th>expected output</th></tr>
<tr><td>1 + 1 = 2</td><td>add</td><td>"1</td><td>1"</td><td>2</td></tr>
<tr><td>2 - 1 = 1</td><td>subtract</td><td>"2</td><td>1"</td><td>1</td></tr>
<tr><td>2 * 3 = 6</td><td>multiply</td><td>"2</td><td>3"</td><td>6</td></tr>
<tr><td>6 / 3 = 2</td><td>divide</td><td>"6</td><td>3"</td><td>2</td></tr>
</table>

*Code under test:*

```scala
object Arithmetic {
def add(x: Int, y: Int): Int = x + y
def subtract(x: Int, y: Int): Int = x - y
def multiply(x: Int, y: Int): Int = x * y
def divide(x: Int, y: Int): Int = x / y
}
```

*CSV specification parser and tester:*

```scala
import com.versal.fireotter._

val specs: Traversable[Seq[String]] = csv(resource("arithmetic.csv"))

specs foreach { spec =>
val inputs: Seq[Int] = spec(2).split(",").map(_.toInt)
val output = spec(1) match {
case "add" => Arithmetic.add(inputs(0), inputs(1))
case "subtract" => Arithmetic.subtract(inputs(0), inputs(1))
case "multiply" => Arithmetic.multiply(inputs(0), inputs(1))
case "divide" => Arithmetic.divide(inputs(0), inputs(1))
}
val expectedOutput: Int = spec(3).toInt
test(spec(0)) { assert(output === expectedOutput) }
}
```

*Test output under ScalaTest:*

```
> test
[info] ArithmeticTest:
[info] - 1 + 1 = 2
[info] - 2 - 1 = 1
[info] - 2 * 3 = 6
[info] - 6 / 3 = 2
[info] Passed: : Total 4, Failed 0, Errors 0, Passed 4, Skipped 0
[success] Total time: 0 s, completed Jun 5, 2013 1:01:05 PM
```
15 changes: 15 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// BASIC METADATA //

organization := "com.versal"

name := "fireotter"

version := "0.1.0-SNAPSHOT"

scalaVersion := "2.10.1"

crossScalaVersions := Seq("2.9.0", "2.9.1", "2.9.2", "2.9.3", "2.10.0", "2.10.1")

// SCALATEST //

libraryDependencies += "org.scalatest" %% "scalatest" % "1.9.1" % "test"
1 change: 1 addition & 0 deletions project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=0.12.3
32 changes: 32 additions & 0 deletions publish.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
publishMavenStyle := true

publishTo <<= version { (v: String) =>
val nexus = "https://oss.sonatype.org/"
if (v.trim.endsWith("SNAPSHOT"))
Some("snapshots" at nexus + "content/repositories/snapshots")
else
Some("releases" at nexus + "service/local/staging/deploy/maven2")
}

pomIncludeRepository := { _ => false }

pomExtra := (
<url>https://github.com/Versal/fireotter</url>
<licenses>
<license>
<name>BSD-style</name>
<url>http://www.opensource.org/licenses/bsd-license.php</url>
<distribution>repo</distribution>
</license>
</licenses>
<scm>
<url>git@github.com:Versal/fireotter.git</url>
<connection>scm:git:git@github.com:Versal/fireotter.git</connection>
</scm>
<developers>
<developer>
<id>JamesEarlDouglas</id>
<name>James Earl Douglas</name>
<url>https://github.com/JamesEarlDouglas</url>
</developer>
</developers>)
60 changes: 60 additions & 0 deletions src/main/scala/fireotter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.versal.fireotter

object `package` {

def resource(name: String): String = getClass.getClassLoader.getResource(name).getPath

def csv(path: String): Traversable[Seq[String]] = new Traversable[Seq[String]] {

import java.io.{BufferedReader, InputStreamReader, FileInputStream}

override def foreach[U](f: Seq[String] => U): Unit = {
val reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"))
try {
reader.readLine()
var next = true
while (next) {
val line = reader.readLine()
if (line != null) f(parse(line))
else next = false
}
} finally {
reader.close()
}
}

def toMap[T, U](toPair: Seq[String] => (T, U)): Map[T, U] = {
val mapBuilder = Map.newBuilder[T, U]
for (row <- this) mapBuilder += toPair(row)
mapBuilder.result
}

private def parse(line: String): Seq[String] = {
var quoted = false
var prevWasQuote = false
var escape = false
val cell = new StringBuilder
var cells: Seq[String] = Seq.empty
for (char <- line) {
if (escape) {
cell += char
escape = false
} else if (char == '\\') {
escape = true
} else if (char == '"') {
if (prevWasQuote && !quoted) cell += char
quoted = !quoted
} else if (char == ',' && !quoted) {
cells = cells ++ Seq(cell.result)
cell.clear
} else {
cell += char
}
prevWasQuote = (char == '"')
}
cells ++ Seq(cell.result)
}

}

}
5 changes: 5 additions & 0 deletions src/test/resources/arithmetic.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test,function,input,expected output
1 + 1 = 2,add,"1,1",2
2 - 1 = 1,subtract,"2,1",1
2 * 3 = 6,multiply,"2,3",6
6 / 3 = 2,divide,"6,3",2
32 changes: 32 additions & 0 deletions src/test/scala/tests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.versal.fireotter.tests

object Arithmetic {
def add(x: Int, y: Int): Int = x + y
def subtract(x: Int, y: Int): Int = x - y
def multiply(x: Int, y: Int): Int = x * y
def divide(x: Int, y: Int): Int = x / y
}

class ArithmeticTest extends org.scalatest.FunSuite {

import com.versal.fireotter._

val specs: Traversable[Seq[String]] = csv(resource("arithmetic.csv"))

specs foreach { spec =>

val inputs: Seq[Int] = spec(2).split(",").map(_.toInt)

val output = spec(1) match {
case "add" => Arithmetic.add(inputs(0), inputs(1))
case "subtract" => Arithmetic.subtract(inputs(0), inputs(1))
case "multiply" => Arithmetic.multiply(inputs(0), inputs(1))
case "divide" => Arithmetic.divide(inputs(0), inputs(1))
}

val expectedOutput: Int = spec(3).toInt

test(spec(0)) { assert(output === expectedOutput) }
}

}

0 comments on commit 018cba1

Please sign in to comment.