Skip to content

Commit 4ef3475

Browse files
committed
NIT Refactor sclicheck
1 parent df1ecfb commit 4ef3475

File tree

2 files changed

+25
-30
lines changed

2 files changed

+25
-30
lines changed

modules/docs-tests/src/main/scala/sclicheck/sclicheck.scala

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import fansi.Color.{Blue, Green, Red}
55
import java.io.File
66
import java.security.SecureRandom
77

8+
import scala.annotation.tailrec
89
import scala.io.StdIn.readLine
910
import scala.util.Random
1011
import scala.util.matching.Regex
@@ -28,8 +29,8 @@ case class Options(
2829

2930
enum Commands:
3031
def context: Context
31-
def name = toString.takeWhile(_ != '(')
32-
def log = this match {
32+
def name: String = toString.takeWhile(_ != '(')
33+
def log: Any = this match {
3334
case _: Clear => ""
3435
case Check(patterns, regex, _) =>
3536
val kind = if regex then "regexes" else "patterns"
@@ -51,13 +52,13 @@ enum Commands:
5152
case Clear(context: Context)
5253

5354
case class Context(file: os.RelPath, line: Int):
54-
def proceed(linesToSkip: Int = 1) = copy(line = line + linesToSkip)
55-
override def toString = s"$file:$line"
55+
def proceed(linesToSkip: Int = 1): Context = copy(line = line + linesToSkip)
56+
override def toString = s"$file:$line"
5657

5758
case class FailedCheck(line: Int, file: os.RelPath, txt: String)
5859
extends RuntimeException(s"[$file:$line] $txt")
5960

60-
def check(cond: Boolean, msg: => String)(using c: Context) =
61+
def check(cond: Boolean, msg: => String)(using c: Context): Unit =
6162
if !cond then throw FailedCheck(c.line, c.file, msg)
6263

6364
@annotation.tailrec
@@ -197,10 +198,10 @@ def checkFile(file: os.Path, options: Options): Unit =
197198
var lastOutput: String = null
198199
val allSources = Set.newBuilder[os.Path]
199200

200-
def runCommand(cmd: Commands, log: String => Unit) =
201+
def runCommand(cmd: Commands, log: String => Unit): Unit =
201202
given Context = cmd.context
202203

203-
def writeFile(file: os.Path, code: Seq[String], c: Context) =
204+
def writeFile(file: os.Path, code: Seq[String], c: Context): Unit =
204205
val (prefixLines, codeLines) =
205206
code match
206207
case shbang :: tail if shbang.startsWith("#!") =>
@@ -254,7 +255,7 @@ def checkFile(file: os.Path, options: Options): Unit =
254255
else
255256
check(exitCode == 0, s"Compilation failed.")
256257

257-
case Commands.Check(patterns, regex, line) =>
258+
case Commands.Check(patterns, regex, _) =>
258259
check(lastOutput != null, "No output stored from previous commands")
259260
val lines = lastOutput.linesIterator.toList
260261

@@ -282,7 +283,7 @@ def checkFile(file: os.Path, options: Options): Unit =
282283
commands.foreach { cmd =>
283284
val logs = List.newBuilder[String]
284285

285-
def printResult(success: Boolean, startTime: Long) =
286+
def printResult(success: Boolean, startTime: Long): Unit =
286287
val duration = System.currentTimeMillis - startTime
287288
val commandName = s"[${cmd.name} in $duration ms]"
288289
val cmdLog =
@@ -341,8 +342,8 @@ def checkFile(file: os.Path, options: Options): Unit =
341342

342343
os.list(out).filter(_.last.endsWith(".scala")).foreach(p => os.copy.into(p, exampleDir))
343344

344-
@main def check(args: String*) =
345-
def processFiles(options: Options) =
345+
@main def check(args: String*): Unit =
346+
def processFiles(options: Options): Unit =
346347
val paths = options.files.map { str =>
347348
val path = os.Path(str, os.pwd)
348349
assert(os.exists(path), s"Provided path $str does not exists in ${os.pwd}")
@@ -382,6 +383,7 @@ def checkFile(file: os.Path, options: Options): Unit =
382383
val Dest = PathParameter("--dest")
383384
val StatusFile = PathParameter("--status-file")
384385

386+
@tailrec
385387
def parseArgs(args: Seq[String], options: Options): Options = args match
386388
case Nil => options
387389
case "--step" :: rest =>

modules/docs-tests/src/test/scala/sclicheck/SclicheckTests.scala

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,26 @@ package sclicheck
22

33
class SclicheckTests extends munit.FunSuite:
44
test("Run regex") {
5-
assert(None == clue(CompileBlock.unapplySeq("``scala compile")))
5+
assert(clue(CompileBlock.unapplySeq("``scala compile")).isEmpty)
66
assert(
7-
Some(Seq("```", "scala", "compile", null)) == clue(
8-
CompileBlock.unapplySeq("```scala compile")
9-
)
7+
clue(CompileBlock.unapplySeq("```scala compile"))
8+
.contains(Seq("```", "scala", "compile", null))
109
)
1110
assert(
12-
Some(Seq("```", "scala", "fail", null)) == clue(CompileBlock.unapplySeq("```scala fail"))
11+
clue(CompileBlock.unapplySeq("```scala fail"))
12+
.contains(Seq("```", "scala", "fail", null))
1313
)
1414
assert(
15-
Some(Seq("````", "markdown", "compile", null)) == clue(
16-
CompileBlock.unapplySeq("````markdown compile")
17-
)
15+
clue(CompileBlock.unapplySeq("````markdown compile"))
16+
.contains(Seq("````", "markdown", "compile", null))
1817
)
1918
assert(
20-
Some(Seq("````", "markdown", "fail", "a.md")) == clue(
21-
CompileBlock.unapplySeq("````markdown fail title=a.md")
22-
)
19+
clue(CompileBlock.unapplySeq("````markdown fail title=a.md"))
20+
.contains(Seq("````", "markdown", "fail", "a.md"))
2321
)
22+
assert(clue(CompileBlock.unapplySeq("``scala fail title=a.sc")).isEmpty)
2423
assert(
25-
None == clue(
26-
CompileBlock.unapplySeq("``scala fail title=a.sc")
27-
)
28-
)
29-
assert(
30-
Some(Seq("```", "scala", "fail", "a.sc")) == clue(
31-
CompileBlock.unapplySeq("```scala fail title=a.sc")
32-
)
24+
clue(CompileBlock.unapplySeq("```scala fail title=a.sc"))
25+
.contains(Seq("```", "scala", "fail", "a.sc"))
3326
)
3427
}

0 commit comments

Comments
 (0)