6502-opcodes/src/test/scala/com/htmlism/mos6502/dsl/DslSpec.scala

156 lines
3.6 KiB
Scala
Raw Normal View History

2020-08-15 18:38:08 +00:00
package com.htmlism.mos6502.dsl
2020-08-15 21:04:16 +00:00
import cats.data.NonEmptyList
2020-08-15 18:38:08 +00:00
import org.scalatest.flatspec._
import org.scalatest.matchers._
class DslSpec extends AnyFlatSpec with should.Matchers {
"the dsl" should "compile" in {
val doc =
asmDoc { implicit ctx =>
2020-08-15 18:44:23 +00:00
group("snake things") { implicit g =>
2020-08-15 21:04:16 +00:00
(define("snakeBodyStart", 0x12.z), define("snakeDirection", 0x02.z), define("snakeLength", 0x03.z))
2020-08-15 18:38:08 +00:00
}
2020-08-15 18:44:23 +00:00
group("ASCII values of keys controlling the snake") { implicit g =>
2020-08-15 21:04:16 +00:00
(define("ASCII_w", 0x77.z), define("ASCII_a", 0x61.z), define("ASCII_s", 0x73.z), define("ASCII_d", 0x64.z))
2020-08-15 18:38:08 +00:00
}
2020-08-15 18:44:23 +00:00
group("System variables") { implicit g =>
2020-08-15 21:04:16 +00:00
(define("sysRandom", 0xfe.z), define("sysLastKey", 0xff.z))
2020-08-15 18:44:23 +00:00
}
2020-08-15 20:23:36 +00:00
2020-08-15 20:29:32 +00:00
group("constants test") { implicit g =>
2020-08-15 21:04:16 +00:00
(constant("margin", 16), constant("secret", 42))
2020-08-15 20:29:32 +00:00
}
2020-08-15 20:23:36 +00:00
()
2020-08-15 18:38:08 +00:00
}
2020-08-15 21:04:16 +00:00
doc shouldEqual AsmDocument(
List(
DefinitionGroup(
"snake things",
List(
Definition("snakeBodyStart", 0x12.z),
Definition("snakeDirection", 0x02.z),
Definition("snakeLength", 0x03.z)
)
),
DefinitionGroup(
"ASCII values of keys controlling the snake",
List(
Definition("ASCII_w", 0x77.z),
Definition("ASCII_a", 0x61.z),
Definition("ASCII_s", 0x73.z),
Definition("ASCII_d", 0x64.z)
)
),
DefinitionGroup(
"System variables",
List(
Definition("sysRandom", 0xfe.z),
Definition("sysLastKey", 0xff.z)
)
),
DefinitionGroup(
"constants test",
List(
Definition("margin", 16),
Definition("secret", 42)
)
)
)
)
2020-08-15 18:38:08 +00:00
}
2020-08-15 21:04:16 +00:00
"enum" should "compile" in {
val doc =
asmDoc { implicit ctx =>
2020-08-15 21:19:36 +00:00
enum[Triforce]
2020-08-15 21:04:16 +00:00
}
doc shouldEqual AsmDocument(
List(
DefinitionGroup(
"foo as enum",
List(
Definition("courage", 0),
Definition("wisdom", 1),
Definition("power", 2)
)
)
)
)
}
"bit field" should "compile" in {
val doc =
asmDoc { implicit ctx =>
2020-08-15 21:19:36 +00:00
bitField[Direction]
2020-08-15 21:04:16 +00:00
}
doc shouldEqual AsmDocument(
List(
DefinitionGroup(
"foo as bit field",
List(
Definition("up", 0x01),
Definition("down", 0x02),
Definition("left", 0x04),
Definition("right", 0x08)
)
)
)
)
}
}
2020-08-15 21:19:36 +00:00
sealed trait Triforce
2020-08-15 21:04:16 +00:00
2020-08-15 21:19:36 +00:00
case object Courage extends Triforce
case object Wisdom extends Triforce
case object Power extends Triforce
object Triforce {
implicit val enumTriforce: EnumAsm[Triforce] =
new EnumAsm[Triforce] {
2020-08-15 21:04:16 +00:00
def comment: String =
"foo as enum"
2020-08-15 21:19:36 +00:00
def all: NonEmptyList[Triforce] =
NonEmptyList.of(Courage, Wisdom, Power)
2020-08-15 21:10:37 +00:00
2020-08-15 21:19:36 +00:00
def label(x: Triforce): String =
x.toString.toLowerCase
2020-08-15 21:10:37 +00:00
2020-08-15 21:19:36 +00:00
def comment(x: Triforce): String =
x.toString
2020-08-15 21:04:16 +00:00
}
2020-08-15 21:19:36 +00:00
}
sealed trait Direction
case object Up extends Direction
case object Down extends Direction
case object Left extends Direction
case object Right extends Direction
2020-08-15 21:04:16 +00:00
2020-08-15 21:19:36 +00:00
object Direction {
implicit val bitFieldDirection: BitField[Direction] =
new BitField[Direction] {
2020-08-15 21:04:16 +00:00
def comment: String =
"foo as bit field"
2020-08-15 21:19:36 +00:00
def all: NonEmptyList[Direction] =
NonEmptyList.of(Up, Down, Left, Right)
2020-08-15 21:04:16 +00:00
2020-08-15 21:19:36 +00:00
def label(x: Direction): String =
x.toString.toLowerCase
2020-08-15 21:10:37 +00:00
2020-08-15 21:19:36 +00:00
def comment(x: Direction): String =
x.toString
2020-08-15 21:04:16 +00:00
}
2020-08-15 18:38:08 +00:00
}