Skip to content

Commit

Permalink
Fjern løsning
Browse files Browse the repository at this point in the history
  • Loading branch information
hamnis committed Aug 22, 2024
2 parents 8004c7b + 7f7c269 commit e3d8c1f
Show file tree
Hide file tree
Showing 25 changed files with 1,111 additions and 1,031 deletions.
6 changes: 2 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ ThisBuild / scalaVersion := "3.4.2"

lazy val oppgaver = (project in file("oppgaver")).settings(
libraryDependencies ++= Seq(
"org.typelevel" %% "cats-parse" % "1.0.0",
"io.circe" %% "circe-core" % "0.14.9",
"io.circe" %% "circe-parser" % "0.14.9",
"org.scalatest" %% "scalatest" % "3.2.18" % Test
"org.typelevel" %% "cats-parse" % "1.0.0",
"org.scalatest" %% "scalatest" % "3.2.18" % Test
)
)
45 changes: 39 additions & 6 deletions oppgaver/src/test/scala/arktekk/ParserSuite.scala
Original file line number Diff line number Diff line change
@@ -1,31 +1,64 @@
package arktekk

import cats.parse.{Parser, Rfc5234}
import cats.parse.{Parser, Parser0, Rfc5234}
import org.scalatest.funsuite.AnyFunSuite

trait ParserSuite extends AnyFunSuite {

inline def implement_me: Parser[Unit] = ???
inline def implement_me[A]: Parser[A] = ???

inline def assertParses[A](parser: Parser[A], inputs: (String, A)*) = {
inline def assertParses[A](parser: Parser[A], inputs: List[(String, A)]): Unit = {
inputs.foreach { (input, expectedResult) =>
val result = parser.parseAll(input)
if result.isLeft then {
val ll = LazyList.from(0)

val idxs: String = ll.take(input.length).map(i => (i % 10).toString).mkString

println("Feil under parsing av:")
println(idxs)
println(input)
println(s"resultat:\n$result")
}

if result != Right(expectedResult) then {
println("Feil under parsing av:")
println(input)
}

assert(result === Right(expectedResult))
}
}

inline def assertParsesValid[A](parser: Parser[A], inputs: String*) = {
inputs.foreach { input =>
inline def assertParses[A](parser: Parser[A], input: (String, A)): Unit =
assertParses(parser, List(input))

inline def assertParsesValid[A](parser: Parser0[A], inputs: List[String]): Unit = {
inputs.foreach { (input: String) =>
val result = parser.parseAll(input)
if result.isLeft then {
val ll = LazyList.from(0)

val idxs: String = ll.take(input.length).map(i => (i % 10).toString).mkString

println("Feil under parsing av:")
println(idxs)
println(input)
println(s"resultat:\n$result")
}
assert(result.isRight)
}
}

inline def assertParsesInvalid[A](parser: Parser[A], inputs: String*) = {
inline def assertParsesValid[A](parser: Parser0[A], input: String): Unit = assertParsesValid(parser, List(input))

inline def assertParsesInvalid[A](parser: Parser0[A], inputs: List[String]): Unit = {
inputs.foreach { input =>
val result = parser.parseAll(input)
assert(result.isLeft)
}
}

inline def assertParsesInvalid[A](parser: Parser0[A], input: String): Unit = assertParsesInvalid(parser, List(input))
}
2 changes: 2 additions & 0 deletions oppgaver/src/test/scala/arktekk/json/JsonParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ package arktekk.json

import arktekk.ParserSuite

import scala.util.Try

class JsonParser extends ParserSuite {}
145 changes: 145 additions & 0 deletions oppgaver/src/test/scala/arktekk/oppgave1/A_SimpleParsers.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package arktekk.oppgave1

import cats.parse.{Numbers, Parser, Rfc5234}
import arktekk.ParserSuite

class A_SimpleParsers extends ParserSuite {

test("parse \"a\" vha char") {
val input = "a"
val p = implement_me

assertParsesValid(p, input)
}

test("parse \"ab\" vha char og ~") {
val input = "ab"
val p = implement_me

assertParsesValid(p, input)
}

test("parse \"ab\" vha string") {
val input = "ab"
val p = implement_me

assertParsesValid(p, input)
}

test("parse \"aba\" vha char og ~") {
val input = "aba"
val p = implement_me

assertParsesValid(p, input)
}

test("parse \"aba\" vha char og surroundedBy") {
val input = "aba"
val p = implement_me

assertParsesValid(p, input)
}

test("parse \"aba\" vha char og between") {
val input = "aba"
val p = implement_me

assertParsesValid(p, input)
}

test("parse \"aa\" og \"aaa\", men ikke \"a\" eller \"aaaa\" vha rep") {
val validInputs = List("aa", "aaa")
val invalidInputs = List("a", "aaaa")
val p = implement_me

assertParsesValid(p, validInputs)
assertParsesInvalid(p, invalidInputs)
}

test("parse \"(a, a)\" (minst to a'er) vha repSep og between") {
val validInputs = List("(a, a, a)", "(a, a)", "(a, a, a, a)")
val invalidInputs = List("()", "(a)", "(a, aa)", "(a,a)")

val p = implement_me

assertParsesValid(p, validInputs)
assertParsesInvalid(p, invalidInputs)
}

test("parse \"a\" med vilkårlig mange spaces før og/eller etter vha surroundedBy") {
val validInputs = List("a", " a", " a", " a ")

val p = implement_me

assertParsesValid(p, validInputs)
}

test("Batman bruk string, rep og ~") {
val validInput = "nananananananananana Batman"
val invalidInputs = List("nanana Batman", "nananananananananananananananananananana Batman")

val p = implement_me

assertParsesValid(p, validInput)
assertParsesInvalid(p, invalidInputs)
}

test("naturlige tall vha charsWhile") {
val validInputs = List("123", "234", "1", "0")
val invalidInputs = List("-1", "0xff", "a", "12c")

val p = implement_me

assertParsesValid(p, validInputs)
assertParsesInvalid(p, invalidInputs)
}

test("naturlige tall vha charIn") {
val validInputs = List("123", "234", "1", "0")
val invalidInputs = List("-1", "0xff", "a", "12c")

val p = implement_me

assertParsesValid(p, validInputs)
assertParsesInvalid(p, invalidInputs)
}

test("naturlige tall vha Numbers.digit") {
val validInputs = List("123", "234", "1", "0")
val invalidInputs = List("-1", "0xff", "a", "12c")

val p = implement_me

assertParsesValid(p, validInputs)
assertParsesInvalid(p, invalidInputs)
}

test("naturlige tall vha Numbers.digits") {
val validInputs = List("123", "234", "1", "0")
val invalidInputs = List("-1", "0xff", "a", "12c")

val p = implement_me

assertParsesValid(p, validInputs)
assertParsesInvalid(p, invalidInputs)
}

test("DNA vha charIn") {
val validInputs = List("ACGT", "AAA", "TT", "ATGG", "CCAATG")

val p = implement_me

assertParsesValid(p, validInputs)
}

// ABNF
test("parse '1*a b 1*a' vha char, ~ og rep") {
val validInputs = List("aaba", "aba", "aaaabaaa")
val invalidInputs = List("baaa", "aabbaa")

val p = implement_me

assertParsesValid(p, validInputs)
assertParsesInvalid(p, invalidInputs)
}
}
162 changes: 162 additions & 0 deletions oppgaver/src/test/scala/arktekk/oppgave1/B_ABNF.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package arktekk.oppgave1

import arktekk.ParserSuite
import cats.data.NonEmptyList
import cats.parse.{Numbers, Parser, Parser0, Rfc5234}

class B_ABNF extends ParserSuite {

val a = Parser.char('a')
val b = Parser.char('b')
val c = Parser.char('c')
val d = Parser.char('d')

test("a b a") {
val validInputs = List("aba")
val invalidInputs = List("baaa", "aabbaa")

val p = implement_me

assertParsesValid(p, validInputs)
assertParsesInvalid(p, invalidInputs)
}

test("*a b a") {
val validInputs = List("aba", "aaaba", "ba")
val invalidInputs = List("baaa", "aabbaa")

val p = implement_me

assertParsesValid(p, validInputs)
assertParsesInvalid(p, invalidInputs)
}

test("a (b / c) d") {
val validInputs = List("abd", "acd")

val p = implement_me

assertParsesValid(p, validInputs)
}

test("(a b) / (c d)") {
val validInputs = List("ac", "ad", "bc", "bd")

val p = implement_me

assertParsesValid(p, validInputs)
}

// %x30-37
test("%x30-37") {
val validInputs = (0 to 7).toList.map(_.toString)

val p = implement_me

assertParsesValid(p, validInputs)
}

test("*a") {
val validInputs = List("", "a", "aa", "aaa", "aaaa")

val p = implement_me

assertParsesValid(p, validInputs)
}

test("*1a") {
val validInputs = List("", "a")
val invalidInputs = List("aa", "aaa")

val p = implement_me

assertParsesValid(p, validInputs)
assertParsesInvalid(p, invalidInputs)
}

test("1*a") {
val validInputs = List("a", "aa", "aaa")
val invalidInputs = List("")

val p = implement_me

assertParsesValid(p, validInputs)
assertParsesInvalid(p, invalidInputs)
}

test("2*3a") {
val validInputs = List("aa", "aaa")
val invalidInputs = List("", "a", "aaaa")

val p = implement_me

assertParsesValid(p, validInputs)
assertParsesInvalid(p, invalidInputs)
}

test("[a] 2*b") {
val validInputs = List("bb", "abb", "abbb", "bbb")
val invalidInputs = List("aab", "aabb", "aba")

val p = implement_me

assertParsesValid(p, validInputs)
assertParsesInvalid(p, invalidInputs)
}

test("2DIGIT") {
val validInputs = (10 to 99).map(_.toString).toList
val invalidInputs = (0 to 9).map(_.toString).toList

val p = implement_me

assertParsesValid(p, validInputs)
assertParsesInvalid(p, invalidInputs)
}

test("%x41–5A / %x61–7A") {
val validInputs = List(('a' to 'z').toList, ('A' to 'Z').toList).flatten.map(_.toString)
val invalidInputs = List("0", "1", "%")

val p = implement_me

assertParsesValid(p, validInputs)
assertParsesInvalid(p, invalidInputs)
}

test("DIGIT / 'A' / 'B' / 'C' / 'D' / 'E' / 'F'") {
val validInputs = List("0", "1", "6", "D", "E")

val p = implement_me

assertParsesValid(p, validInputs)
}

test("parse '1*a b 1*a'") {
val validInputs = List("aaba", "aba", "aaaabaaa")
val invalidInputs = List("baaa", "aabbaa")

val p = implement_me

assertParsesValid(p, validInputs)
assertParsesInvalid(p, invalidInputs)
}

// zip-part = town-name "," SP state 1*2SP zip-code
// town-name = 1*(ALPHA / SP)
// state = 2ALPHA
// zip-code = 5DIGIT ["-" 4DIGIT]
test("zip") {
val zip_code = implement_me
val state = implement_me
val town_name = implement_me
val zip_part = implement_me

val validInputs = List(
"town, ST 12345",
"city, AB 12345-4000"
)

assertParsesValid(zip_part, validInputs)
}
}
Loading

0 comments on commit e3d8c1f

Please sign in to comment.