Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions checker/src/main/scala/org/polystat/py2eo/checker/Check.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ object Check {
def apply(inputPath: Path, outputPath: Path, mutations: Iterable[Mutation]): Unit = {
outputPath.createDirectory()

val res = check(inputPath, outputPath, mutations)
if (res isEmpty) {
val testResults = check(inputPath, outputPath, mutations)
if (testResults isEmpty) {
error("Provided tests directory doesn't contain .yaml files")
} else {
Write(outputPath, res, mutations)
val awaitedTestResults = testResults map (testResult => testResult.await)
Write(outputPath, awaitedTestResults, mutations)
WriteConstructions(outputPath, awaitedTestResults)
}
}

Expand All @@ -46,18 +48,19 @@ object Check {

private def check(test: File, outputPath: Path, mutations: Iterable[Mutation]): TestResult = {
val module = test.stripExtension
val category = test.parent.name
println(s"checking $module")
parseYaml(test) match {
case None => TestResult(module, None)
case None => TestResult(module, category, None)
case Some(parsed) =>
Transpile(module, parsed) match {
case None => TestResult(module, None)
case None => TestResult(module, category, None)
case Some(transpiled) =>
val file = File(outputPath / test.changeExtension("eo").name)
file writeAll transpiled

val resultList = mutations map (mutation => (mutation, Future(check(module, parsed, outputPath, mutation))))
TestResult(module, Some(resultList.toMap[Mutation, Future[CompilingResult]]))
TestResult(module, category, Some(resultList.toMap[Mutation, Future[CompilingResult]]))
}
}
}
Expand Down
20 changes: 1 addition & 19 deletions checker/src/main/scala/org/polystat/py2eo/checker/Mutate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ object Mutate {
val operatorMutation: Mutation = Value("Operator-mutation")
val reverseBoolMutation: Mutation = Value("Reverse-bool-literal")
val breakToContinue: Mutation = Value("Break-to-Continue")
val breakSyntax: Mutation = Value("def-to-df")
val literalToIdentifier: Mutation = Value("False-to-false")
val removeBrackets: Mutation = Value("Remove-brackets")
val addExcessParam: Mutation = Value("Add-excess-parameter")
val swapParam: Mutation = Value("Swap-param")
}

Expand All @@ -30,10 +27,7 @@ object Mutate {
case Mutation.operatorMutation => input.replace('+', '-')
case Mutation.reverseBoolMutation => input.replace("true", "false")
case Mutation.breakToContinue => input.replace("break", "continue")
case Mutation.breakSyntax => input.replace("def", "df")
case Mutation.literalToIdentifier => input.replace("False", "false")
case Mutation.removeBrackets => input.replace("()", "")
case Mutation.addExcessParam => PrintPython.print(addExcessParam(parsed, occurrenceNumber))
case Mutation.swapParam => PrintPython.print(swapParam(parsed, occurrenceNumber))
case _ => throw new IllegalArgumentException
}
Expand Down Expand Up @@ -61,21 +55,9 @@ object Mutate {
SimplePass.simpleProcExprInStatementAcc[Int](mutateNamesHelper)(acc, s)._2
}

private def addExcessParam(s: Statement.T, acc: Int): Statement.T = {
def addExcessParamHelper(acc: Int, expr: Expression.T): (Int, Expression.T) = expr match {
case Expression.CallIndex(flag, callee, args, ann) if acc == 0 => (
-1, Expression.CallIndex(flag, callee, args.appended(Some(""), Expression.StringLiteral(List("abc"), ann)), ann)
)
case Expression.CallIndex(_, _, _, _) => (acc - 1, expr)
case _ => (acc, expr)
}

SimplePass.simpleProcExprInStatementAcc[Int](addExcessParamHelper)(acc, s)._2
}

private def swapParam(s: Statement.T, acc: Int): Statement.T = {
def swapParamHelper(acc: Int, expr: Expression.T): (Int, Expression.T) = expr match {
case Expression.CallIndex(flag, callee, args, ann) if acc == 0 => (
case Expression.CallIndex(flag, callee, args, ann) if acc == 0 && args.length > 1 => (
-1, Expression.CallIndex(flag, callee, args.reverse, ann)
)
case Expression.CallIndex(_, _, _, _) => (acc - 1, expr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ object CompilingResult extends Enumeration {
}


case class AwaitedTestResult(name: String, results: Option[Map[Mutation, CompilingResult]])
case class AwaitedTestResult(name: String, category: String, results: Option[Map[Mutation, CompilingResult]])

case class TestResult(name: String, results: Option[Map[Mutation, Future[CompilingResult]]]) {
case class TestResult(name: String, category: String, results: Option[Map[Mutation, Future[CompilingResult]]]) {
def await: AwaitedTestResult = {
results match {
case None => AwaitedTestResult(name, None)
case None => AwaitedTestResult(name, category, None)
case Some(resultMap) =>
val set = resultMap.toSet
val awaited = for {(mutation, futureResult) <- set} yield (mutation, Await.result(futureResult, Duration.Inf))

AwaitedTestResult(name, Some(awaited.toMap))
AwaitedTestResult(name, category, Some(awaited.toMap))
}
}
}
16 changes: 7 additions & 9 deletions checker/src/main/scala/org/polystat/py2eo/checker/Write.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,17 @@ import scala.reflect.io.{File, Path, Streamable}
object Write {

/** Write testing results to index.html in the provided directory */
def apply(outputPath: Path, tests: List[TestResult], mutations: Iterable[Mutation]): Unit = {
val awaited = tests map (test => test.await)

lazy val filtered = mutations filter (mutation => applied(awaited, mutation).nonEmpty)
lazy val sorted = filtered.toList sortWith sorter(awaited)
File(outputPath / "index.html") writeAll html(awaited, sorted)
def apply(outputPath: Path, tests: List[AwaitedTestResult], mutations: Iterable[Mutation]): Unit = {
lazy val filtered = mutations filter (mutation => applied(tests, mutation).nonEmpty)
lazy val sorted = filtered.toList sortWith sorter(tests)
File(outputPath / "index.html") writeAll html(tests, sorted)
}

/** Returns html file contents */
private def html(tests: List[AwaitedTestResult], mutations: List[Mutation]): String = {
lazy val stream = getClass getResourceAsStream "head.html"
lazy val head = Streamable slurp stream
lazy val body = s"<body>\n${table(tests, mutations)}</body>\n"
lazy val body = s"<body>\n${table(tests, mutations)}<a href=\"${WriteConstructions.filename}\">Constructions</a></body>\n"

s"<html lang=\"en-US\">\n$head$body</html>\n"
}
Expand Down Expand Up @@ -58,11 +56,11 @@ object Write {
case None =>
lazy val colspan = mutations size
lazy val str = s"<td colspan=\"$colspan\" class=\"data\">Original test couldn't be transpiled</td>"
s"<tr>\n<th class=\"left\">$name</th>\n$str</tr>\n"
s"<tr>\n<th class=\"left\">$name of ${test.category}</th>\n$str</tr>\n"

case Some(results) =>
lazy val cells = mutations map (mutation => cell(mutation, name, results(mutation)))
s"<tr>\n<th class=\"left\">$name</th>\n${cells mkString}</tr>\n"
s"<tr>\n<th class=\"left\">$name of ${test.category}</th>\n${cells mkString}</tr>\n"
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.polystat.py2eo.checker

import scala.language.postfixOps
import scala.reflect.io.{File, Path, Streamable}

object WriteConstructions {

val filename = "constructions.html"

/** Write testing results to constructions.html in the provided directory */
def apply(outputPath: Path, tests: List[AwaitedTestResult]): Unit = File(outputPath / filename) writeAll html(tests)

/** Returns html file contents */
private def html(tests: List[AwaitedTestResult]): String = {
lazy val stream = getClass getResourceAsStream "head.html"
lazy val head = Streamable slurp stream
lazy val body = s"<body>\n${table(tests)}</body>\n"

s"<html lang=\"en-US\">\n$head$body</html>\n"
}

/** Returns full table */
private def table(tests: List[AwaitedTestResult]): String = {
s"<table id=programs>\n$header${body(tests)}</table>\n"
}

/** Returns table header */
private def header: String = {
s"<tr>\n<th class=\"sorter\">Construction</th>\n<th class=\"sorter data\">Result</th></tr>\n"
}

/** Returns table body */
private def body(tests: List[AwaitedTestResult]): String = {
val constructions = (tests map (test => test.category)).toSet

val body = for (construction <- constructions) yield {
val relevant = tests filter (test => test.category == construction)
val data = relevant.exists(test => {
val sublist = test.results.getOrElse(Map.empty).values.toList
sublist.contains(CompilingResult.failed) || sublist.contains(CompilingResult.nodiff)
})

val cell = if (data)
s"<td class=\"unexpected data\">failed</td>\n"
else
s"<td class=\"expected data\">passed</td>\n"

s"<tr>\n<th class=\"left\">$construction</th>\n$cell</tr>\n"
}

body mkString
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ object Transpile {
}

def applyStyle(pythonCode: String): Option[String] = {
Parse(pythonCode).map(PrintPython.print)
Parse(pythonCode).map(PrintPython.print).flatMap(Parse.apply).map(PrintPython.print)
}

/// [debugPrinter(statement, stageName)]
Expand Down