From 27bfa6bb6a3971e70c1d5878300b70da3a69a85b Mon Sep 17 00:00:00 2001 From: bugdea1er Date: Sun, 29 May 2022 20:11:27 +0300 Subject: [PATCH 1/3] Added constructions summary --- .../org/polystat/py2eo/checker/Check.scala | 15 +++--- .../org/polystat/py2eo/checker/Mutate.scala | 20 +------ .../polystat/py2eo/checker/TestResult.scala | 8 +-- .../org/polystat/py2eo/checker/Write.scala | 16 +++--- .../py2eo/checker/WriteConstructions.scala | 53 +++++++++++++++++++ 5 files changed, 74 insertions(+), 38 deletions(-) create mode 100644 checker/src/main/scala/org/polystat/py2eo/checker/WriteConstructions.scala diff --git a/checker/src/main/scala/org/polystat/py2eo/checker/Check.scala b/checker/src/main/scala/org/polystat/py2eo/checker/Check.scala index d74167eec..088f524f8 100644 --- a/checker/src/main/scala/org/polystat/py2eo/checker/Check.scala +++ b/checker/src/main/scala/org/polystat/py2eo/checker/Check.scala @@ -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) } } @@ -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]])) } } } diff --git a/checker/src/main/scala/org/polystat/py2eo/checker/Mutate.scala b/checker/src/main/scala/org/polystat/py2eo/checker/Mutate.scala index 144274574..344b1c289 100644 --- a/checker/src/main/scala/org/polystat/py2eo/checker/Mutate.scala +++ b/checker/src/main/scala/org/polystat/py2eo/checker/Mutate.scala @@ -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") } @@ -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 } @@ -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) diff --git a/checker/src/main/scala/org/polystat/py2eo/checker/TestResult.scala b/checker/src/main/scala/org/polystat/py2eo/checker/TestResult.scala index 1a88b7f92..5dce003c1 100644 --- a/checker/src/main/scala/org/polystat/py2eo/checker/TestResult.scala +++ b/checker/src/main/scala/org/polystat/py2eo/checker/TestResult.scala @@ -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)) } } } diff --git a/checker/src/main/scala/org/polystat/py2eo/checker/Write.scala b/checker/src/main/scala/org/polystat/py2eo/checker/Write.scala index d63b43ab6..bd4d76820 100644 --- a/checker/src/main/scala/org/polystat/py2eo/checker/Write.scala +++ b/checker/src/main/scala/org/polystat/py2eo/checker/Write.scala @@ -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"\n${table(tests, mutations)}\n" + lazy val body = s"\n${table(tests, mutations)}Constructions\n" s"\n$head$body\n" } @@ -58,11 +56,11 @@ object Write { case None => lazy val colspan = mutations size lazy val str = s"Original test couldn't be transpiled" - s"\n$name\n$str\n" + s"\n$name of ${test.category}\n$str\n" case Some(results) => lazy val cells = mutations map (mutation => cell(mutation, name, results(mutation))) - s"\n$name\n${cells mkString}\n" + s"\n$name of ${test.category}\n${cells mkString}\n" } } diff --git a/checker/src/main/scala/org/polystat/py2eo/checker/WriteConstructions.scala b/checker/src/main/scala/org/polystat/py2eo/checker/WriteConstructions.scala new file mode 100644 index 000000000..d92c65f69 --- /dev/null +++ b/checker/src/main/scala/org/polystat/py2eo/checker/WriteConstructions.scala @@ -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"\n${table(tests)}\n" + + s"\n$head$body\n" + } + + /** Returns full table */ + private def table(tests: List[AwaitedTestResult]): String = { + s"\n$header${body(tests)}
\n" + } + + /** Returns table header */ + private def header: String = { + s"\nConstruction\nResult\n" + } + + /** Returns table body */ + private def body(tests: List[AwaitedTestResult]): String = { + val constructions = (tests map (test => test.category)).toSet + val constructionsMap = (for (c <- constructions) yield (c, tests filter (test => test.category == c))).toMap + + val body = for ((construction, list) <- constructionsMap) yield { + val data = list.exists(test => { + val sublist = test.results.getOrElse(Map.empty).values.toList + sublist.contains(CompilingResult.failed) || sublist.contains(CompilingResult.nodiff) + }) + + val cell = if (data) + s"failed\n" + else + s"passed\n" + + s"\n$construction\n$cell\n" + } + + body mkString + } +} From 51d9fc73063a52c93b78086eaed904019d9999c2 Mon Sep 17 00:00:00 2001 From: bugdea1er Date: Sun, 29 May 2022 20:18:27 +0300 Subject: [PATCH 2/3] Update WriteConstructions.scala --- .../org/polystat/py2eo/checker/WriteConstructions.scala | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/checker/src/main/scala/org/polystat/py2eo/checker/WriteConstructions.scala b/checker/src/main/scala/org/polystat/py2eo/checker/WriteConstructions.scala index d92c65f69..c04ae3814 100644 --- a/checker/src/main/scala/org/polystat/py2eo/checker/WriteConstructions.scala +++ b/checker/src/main/scala/org/polystat/py2eo/checker/WriteConstructions.scala @@ -32,10 +32,10 @@ object WriteConstructions { /** Returns table body */ private def body(tests: List[AwaitedTestResult]): String = { val constructions = (tests map (test => test.category)).toSet - val constructionsMap = (for (c <- constructions) yield (c, tests filter (test => test.category == c))).toMap - val body = for ((construction, list) <- constructionsMap) yield { - val data = list.exists(test => { + 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) }) From 9d52027556c8a47996db587a5b573331caaf911d Mon Sep 17 00:00:00 2001 From: bugdea1er Date: Sun, 29 May 2022 22:09:15 +0300 Subject: [PATCH 3/3] Updated styler --- .../main/scala/org/polystat/py2eo/transpiler/Transpile.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transpiler/src/main/scala/org/polystat/py2eo/transpiler/Transpile.scala b/transpiler/src/main/scala/org/polystat/py2eo/transpiler/Transpile.scala index f6fd3ef04..84f38ab0d 100644 --- a/transpiler/src/main/scala/org/polystat/py2eo/transpiler/Transpile.scala +++ b/transpiler/src/main/scala/org/polystat/py2eo/transpiler/Transpile.scala @@ -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)]