Created
June 23, 2016 15:53
-
-
Save themodernlife/28cc4842c293d554f670abffad0aa8fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AddingParser(val input: ParserInput) extends Parser { | |
def num = rule { | |
capture(oneOrMore(CharPredicate.Digit)) ~> (_.toInt) | |
} | |
def root = rule { | |
push(0) ~ zeroOrMore(num ~> ((_: Int) + _)).separatedBy(" ") ~ EOI | |
} | |
} | |
class XyzTest extends FlatSpec with Matchers { | |
"A parser" should "foldLeft" in { | |
val ret1 = new AddingParser("").root.run() | |
val ret2 = new AddingParser("1 2 3").root.run() | |
val ret3 = new AddingParser("x").root.run() | |
ret1 should be(Success(0)) | |
ret2 should be(Success(6)) | |
ret3 should be('failure) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment