Skip to content

Commit

Permalink
add TestIO
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornregnell committed Jun 11, 2024
1 parent b81b16f commit c80904a
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/test/scala/testIO.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package introprog

val tmpDir = "target/tmp"

class TestIO extends munit.FunSuite {
test("TestIO: createDirIfNotExist, saveString, appendString, loadLines, appendLines") {
val existed = IO.createDirIfNotExist(tmpDir)
assert(IO.isExisting(tmpDir), s"dir should exists: $tmpDir")
val s1 = "hello"
val fn = s"$tmpDir/hello.txt"
IO.saveString(s1, fileName = fn)
val s2 = IO.loadString(fileName = fn)
assertEquals(s1, s2, "saved string different from loaded")
IO.appendString("!\n", fileName = fn )
val s3 = IO.loadString(fileName = fn)
assertEquals(s3, s2 + "!\n", "saved string is missing appended '!+newline'")
IO.appendLines(Seq("line2"),fileName = fn)
val s4 = IO.loadLines(fileName = fn)
assertEquals(s4, Vector("hello!", "line2"), s"loadLines not as expected: $s4")
val s5 = IO.loadString(fileName = fn)
assertEquals(s5, "hello!\nline2\n", s"loadLines not as expected: $s5")
IO.appendLines(Seq(),fileName = fn) // nothing should be added, not even newline
assertEquals(s5, IO.loadString(fileName = fn), s"loadLines not as expected: $s5")
}
}

0 comments on commit c80904a

Please sign in to comment.