Giter Site home page Giter Site logo

parser-test's Introduction

Parser Test Build Status

Scalatest extension to test parsers made with Scala Standard Parser Combinator Library.

Setup

To include this module in your SBT project, just add the following line to your .sbt project definition:

libraryDependencies += "org.uqbar" %% "parser-test" % "latest.integration" % "test"

Or, if you are using Uqbar's SBT Flexible Dependencies Plugin:

lazy val project = FDProject(
	"org.uqbar" %% "parser-test" % "latest.integration" % "test"
)

Usage

Creating a Test

To create a parser test just create a test class extending your favorite Scalatest style and mix in org.uqbar.testing.ParserTest[T] being T the type of your parser definition.

import scala.util.parsing.combinator.RegexParsers
import org.uqbar.testing.ParserTest

class MyAwesomeParser extends RegexParsers {
	...
}

class MyParserTest extends FreeSpec with ParserTest[MyAwesomeParser] {
	...
}

Now write your tests the way you always do!

Matchers Description

This extension provides the following matchers:

beParsedTo

beParsedTo[T](expected: T)(implicit parser: Parser[T])

Use this matcher to test whether a string gets parsed to the expected result or not.

"42" should beParsedTo (Number(42))(numberParser)

beParsed

beParsed[T](implicit parser: Parser[T])

Use this matcher to test whether a string gets parsed by a parser or not, disregarding what exactly it gets parsed to.

"42" should beParsed()(numberParser)

Tips & Tricks

For all the matchers, the tested parser can also be declared as an implicit value in the test context:

implicit val parser: Parser[Number] = numberParser

"42" should beParsedTo (Number(42))

"42" should not (beParsedTo (Number(42)))

"00" should beParsed[Number]

Optionally, many tests can be simplified by shortening the expected result construction by using implicit conversions and picking the implicit parser with type arguments:

implicit val parser: Parser[Client] = clientParser
implicit def String_to_Product(name: String) = new Product(new Identifier(name))

"Prod >> cheese" should beParsedTo [Product] ("cheese")

If you have intermediate parsers definitions that you would rather not make public but still would like to test, make your parser definition a trait and mix it with the test class to test any protected parser:

import scala.util.parsing.combinator.RegexParsers
import org.uqbar.testing.ParserTest

object MyAwesomeParser extends MyAwesomeParser
trait MyAwesomeParser extends RegexParsers {
	lazy val foo: Parser[Foo] = fooPart ~ otherFooPart
	protected lazy val fooPart = ...
	protected lazy val otherFooPart = ...
}

class MyParserTest extends FreeSpec with ParserTest[MyAwesomeParser] with MyAwesomeParser {
	// Here you can test the foo parser, but also fooPart and otherFooPart as well!
}

Contributions

Yes, please! Pull requests are always welcome, just try to keep it small and clean.

License

This code is open source software licensed under the LGPL v3 License by The Uqbar Foundation. Feel free to use it accordingly.

parser-test's People

Watchers

Franco Leonardo Bulgarelli avatar Gaston Prieto avatar James Cloos avatar Federico Scarpa avatar Ernesto Bossi avatar Martín Dias avatar  avatar Fernando Dodino avatar  avatar  avatar

parser-test's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.