Starting to write tests for character reader.

This commit is contained in:
Pat Garrity 2024-02-18 10:27:00 -06:00
parent 7ae19980d9
commit fdb150b12d
Signed by: pfm
GPG key ID: 5CA5D21BAB7F3A76
2 changed files with 50 additions and 0 deletions

View file

@ -0,0 +1,11 @@
namespace tests
given A
class Semigroup
defn combine: A -> A -> A
end class
given A :: Semigroup
class Monoid
defn empty: A
end class

View file

@ -0,0 +1,39 @@
package ava.parser
import cats.effect.IO
import cats.effect.unsafe.IORuntime
import java.io.InputStream
import scala.io.Source
class CharacterReaderTests extends munit.FunSuite:
import CharacterReaderTests.*
implicit val runtime: IORuntime = cats.effect.unsafe.IORuntime.global
test("should read a file end to end") {
val file = Files.TestSource1
val expected = loadFileToString(file)
val reader = CharacterReader.forInputStream(loadFileToStream(file))
val output = fs2.Stream
.repeatEval(IO(reader.consume()))
.takeWhile(_.isDefined)
.compile
.toList
.map(_.flatten)
.map(_.mkString)
.unsafeRunSync()
assertEquals(output, expected)
}
private def loadFileToString(name: String): String =
Source.fromResource(name).mkString
private def loadFileToStream(name: String): InputStream =
getClass().getClassLoader().getResourceAsStream(name)
object CharacterReaderTests:
object Files:
val TestSource1: String = "test-source-1.ava"