package scalaparser import org.parboiled2.ParseError import utest._ import utest.framework.Test import utest.util.Tree import scala.util.{Failure, Success} object SyntaxTest extends TestSuite{ def check[T](input: String) = { new ScalaSyntax(input).CompilationUnit.run() match{ case Failure(f: ParseError) => println(f.position) println(f.formatExpectedAsString) println(f.formatTraces) throw new Exception(f.position + "\t" + f.formatTraces) case Success(parsed) => assert(parsed == input) } } def tests = TestSuite{ 'unit { * - check( "package torimatomeru" ) * - check( """ |package torimatomeru | |import org.parboiled2.ParseError |import utest._ |import utest.framework.Test """.stripMargin ) * - check( """ |package torimatomeru | |import org.parboiled2.ParseError |import utest._ |import utest.framework.Test |import utest.util.Tree | |import scala.util.{Failure, Success} | |object SyntaxTest extends TestSuite """.stripMargin ) * - check( """ |object SyntaxTest extends TestSuite{ | def check[T](input: String) = { | | } |} """.stripMargin ) * - check( """ |object SyntaxTest{ | a() | throw 1 |} """.stripMargin ) * - check( """ |object SyntaxTest extends TestSuite{ | def check[T](input: String) = { | new ScalaSyntax(input).CompilationUnit.run() match{ | case Failure(f: ParseError) => | println(f.position) | println(f.formatExpectedAsString) | println(f.formatTraces) | throw new Exception(f.position + "\t" + f.formatTraces) | case Success(parsed) => | assert(parsed == input) | } | } |} """.stripMargin ) * - check( """package scalatex | | |import org.parboiled2._ |import torimatomeru.ScalaSyntax | |import scalatex.stages.{Trim, Parser, Ast} |import scalatex.stages.Ast.Block.{IfElse, For, Text} |import Ast.Chain.Args | |object ParserTests extends utest.TestSuite{ | import Ast._ | import utest._ | def check[T](input: String, parse: Parser => scala.util.Try[T], expected: T) = { | val parsed = parse(new Parser(input)).get | assert(parsed == expected) | } | def tests = TestSuite{} |} """.stripMargin ) * - check( """ |object Moo{ | a | .b | | c |} """.stripMargin ) * - check( """ |object Moo{ | filename | .asInstanceOf[Literal] |10 |} """.stripMargin ) * - check( """ |object Cow{ | ().mkString | | 1 |} """.stripMargin ) * - check( """ |object O{ | private[this] val applyMacroFull = 1 |} """.stripMargin ) * - check( """ |object O{ | private[this] def applyMacroFull(c: Context) | (expr: c.Expr[String], | runtimeErrors: Boolean, | debug: Boolean) | : c.Expr[Frag] = { | } |} """.stripMargin ) * - check( """ |object O{ | class DebugFailure extends Exception | | 1 |} """.stripMargin ) * - check( """ |package torimatomeru | |package syntax | |import org.parboiled2._ | """.stripMargin ) * - check( """ |object Foo{ | 0 match { | case A | B => 0 | } |} """.stripMargin ) // * - check(io.Source.fromFile("scalatexApi/src/main/scala/scalatex/stages/Trim.scala").mkString) // * - check( // """ // |package scalatex.stages // |import acyclic.file // | // |/** // | * Preprocesses the input string to normalize things related to whitespace // | * // | * Find the "first" non-whitespace-line of the text and remove the front // | * of every line to align that first line with the left margin. // | * // | * Remove all trailing whitespace from each line. // | */ // |object Trim extends (String => (String, Int)){ // | def apply(str: String) = { // | val lines = str.split("\n", -1) // | val offset = lines.iterator // | .filter(_.length > 0) // | .next() // | .takeWhile(_ == ' ') // | .length // | val res = lines.iterator // | .map(_.replaceFirst("\\s+$", "")) // | .mkString("\n") // | (res, offset) // | } // | def old(str: String) = { // | val (res, offset) = this.apply(str) // | res.split("\n", -1).map(_.drop(offset)).mkString("\n") // | } // |} // | // """.stripMargin // ) } 'file{ def checkFile(path: String) = check(io.Source.fromFile(path).mkString) * - checkFile("scalatexApi/src/test/scala/scalaparser/SyntaxTest.scala") * - checkFile("scalatexApi/src/main/scala/scalaparser/syntax/Basic.scala") * - checkFile("scalatexApi/src/main/scala/scalaparser/syntax/Identifiers.scala") * - checkFile("scalatexApi/src/main/scala/scalaparser/syntax/Literals.scala") // All the commented files seem to make the parser run forever. There's probably // some exponential performance somewhere in there, but I can't see it =/ // * - check(io.Source.fromFile("scalatexApi/src/main/scala/scalaparser/ScalaSyntax.scala").mkString) * - checkFile("scalatexApi/src/test/scala/scalatex/TestUtil.scala") // * - check(io.Source.fromFile("scalatexApi/src/main/scala/scalatex/stages/Compiler.scala").mkString) * - checkFile("scalatexApi/src/main/scala/scalatex/stages/Parser.scala") // * - check(io.Source.fromFile("scalatexApi/src/test/scala/scalatex/ParserTests.scala").mkString) * - checkFile("scalatexApi/src/main/scala/scalatex/stages/Trim.scala") // * - check(io.Source.fromFile("scalatexApi/src/main/scala/scalatex/package.scala").mkString) } } }