Skip to content

Commit

Permalink
TestTypeProviders: extracted in a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
GreyCat committed Mar 6, 2024
1 parent 7be4a2b commit 159a40d
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 100 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package io.kaitai.struct.translators

import io.kaitai.struct.datatype.DataType
import io.kaitai.struct.datatype.DataType.{CalcIntType, CalcStrType, CalcUserType, UserTypeInstream}
import io.kaitai.struct.exprlang.Ast
import io.kaitai.struct.format.{ClassSpec, FixedSized, Identifier}

/**
* A few implementations of [[TypeProvider]] for test purposes.
*/
object TestTypeProviders {
/**
* Common type provider implementation for test purposes.
*/
abstract class FakeTypeProvider extends TypeProvider {
val nowClass = ClassSpec.opaquePlaceholder(List("top_class"))

override def resolveEnum(inType: Ast.typeId, enumName: String) =
throw new NotImplementedError

override def resolveType(typeName: Ast.typeId): DataType = {
if (typeName == Ast.typeId(false, List("block"), false)) {
val name = List("top_class", "block")
val r = CalcUserType(name, None, Seq())
val cs = ClassSpec.opaquePlaceholder(name)
cs.seqSize = FixedSized(56)
r.classSpec = Some(cs)
return r
} else {
throw new NotImplementedError
}
}

override def isLazy(attrName: String): Boolean = false

override def isLazy(inClass: ClassSpec, attrName: String): Boolean = false
}

/**
* Type provider that always return one data type when question arises.
* @param t data type to return
*/
case class Always(t: DataType) extends FakeTypeProvider {
override def determineType(name: String): DataType = t

override def determineType(id: Identifier): DataType = t

override def determineType(inClass: ClassSpec, name: String): DataType = t

override def determineType(inClass: ClassSpec, id: Identifier): DataType = t
}

/**
* Emulates the following system of types:
*
* {{{
* meta:
* id: top_class
* types:
* block:
* seq:
* - id: bar
* type: str
* - id: inner
* type: innerblock
* types:
* innerblock:
* instances:
* baz:
* value: 123
* }}}
*/
case object FooBarProvider extends FakeTypeProvider {
override def determineType(name: String): DataType = {
name match {
case "foo" => userOwnedType(List("top_class", "block"))
}
}

override def determineType(id: Identifier): DataType = ???

override def determineType(inClass: ClassSpec, name: String): DataType = {
(inClass.name.last, name) match {
case ("block", "bar") => CalcStrType
case ("block", "inner") => userOwnedType(List("top_class", "block", "innerblock"))
case ("innerblock", "baz") => CalcIntType
}
}

override def determineType(inClass: ClassSpec, id: Identifier): DataType = ???

override def resolveType(typeName: Ast.typeId): DataType = {
typeName.names match {
case Seq("top_class") =>
userOwnedType(List("top_class"))
case Seq("block") |
Seq("top_class", "block") =>
userOwnedType(List("top_class", "block"))
case Seq("innerblock") |
Seq("block", "innerblock") |
Seq("top_class", "block", "innerblock") =>
userOwnedType(List("top_class", "block", "innerblock"))
}
}
}

def userOwnedType(lname: List[String]): UserTypeInstream = {
val cs = ClassSpec.opaquePlaceholder(lname)
val ut = UserTypeInstream(lname, None)
ut.classSpec = Some(cs)
ut
}

def userBorrowedType(lname: List[String]): CalcUserType = {
val cs = ClassSpec.opaquePlaceholder(lname)
val ut = CalcUserType(lname, None)
ut.classSpec = Some(cs)
ut
}
}
101 changes: 1 addition & 100 deletions jvm/src/test/scala/io/kaitai/struct/translators/TranslatorSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package io.kaitai.struct.translators
import io.kaitai.struct.datatype.DataType
import io.kaitai.struct.datatype.DataType._
import io.kaitai.struct.exprlang.{Ast, Expressions}
import io.kaitai.struct.format.{ClassSpec, FixedSized, Identifier}
import io.kaitai.struct.languages._
import io.kaitai.struct.languages.components.{CppImportList, LanguageCompilerStatic}
import io.kaitai.struct.translators.TestTypeProviders._
import io.kaitai.struct.{ImportList, RuntimeConfig, StringLanguageOutputWriter}
import org.scalatest.Tag
import org.scalatest.funsuite.AnyFunSuite
Expand Down Expand Up @@ -751,105 +751,6 @@ class TranslatorSpec extends AnyFunSuite {
type ResultMap = Map[LanguageCompilerStatic, String]
type TestSpec = (String, TypeProvider, DataType, ResultMap)

abstract class FakeTypeProvider extends TypeProvider {
val nowClass = ClassSpec.opaquePlaceholder(List("top_class"))

override def resolveEnum(inType: Ast.typeId, enumName: String) =
throw new NotImplementedError

override def resolveType(typeName: Ast.typeId): DataType = {
if (typeName == Ast.typeId(false, List("block"), false)) {
val name = List("top_class", "block")
val r = CalcUserType(name, None, Seq())
val cs = ClassSpec.opaquePlaceholder(name)
cs.seqSize = FixedSized(56)
r.classSpec = Some(cs)
return r
} else {
throw new NotImplementedError
}
}

override def isLazy(attrName: String): Boolean = false

override def isLazy(inClass: ClassSpec, attrName: String): Boolean = false
}

case class Always(t: DataType) extends FakeTypeProvider {
override def determineType(name: String): DataType = t
override def determineType(id: Identifier): DataType = t
override def determineType(inClass: ClassSpec, name: String): DataType = t
override def determineType(inClass: ClassSpec, id: Identifier): DataType = t
}

/**
* Emulates the following system of types:
*
* <pre>
* meta:
* id: top_class
* types:
* block:
* seq:
* - id: bar
* type: str
* - id: inner
* type: innerblock
* types:
* innerblock:
* instances:
* baz:
* value: 123
* </pre>
*/
case object FooBarProvider extends FakeTypeProvider {
override def determineType(name: String): DataType = {
name match {
case "foo" => userOwnedType(List("top_class", "block"))
}
}

override def determineType(id: Identifier): DataType = ???

override def determineType(inClass: ClassSpec, name: String): DataType = {
(inClass.name.last, name) match {
case ("block", "bar") => CalcStrType
case ("block", "inner") => userOwnedType(List("top_class", "block", "innerblock"))
case ("innerblock", "baz") => CalcIntType
}
}

override def determineType(inClass: ClassSpec, id: Identifier): DataType = ???

override def resolveType(typeName: Ast.typeId): DataType = {
typeName.names match {
case Seq("top_class") =>
userOwnedType(List("top_class"))
case Seq("block") |
Seq("top_class", "block") =>
userOwnedType(List("top_class", "block"))
case Seq("innerblock") |
Seq("block", "innerblock") |
Seq("top_class", "block", "innerblock") =>
userOwnedType(List("top_class", "block", "innerblock"))
}
}
}

def userOwnedType(lname: List[String]) = {
val cs = ClassSpec.opaquePlaceholder(lname)
val ut = UserTypeInstream(lname, None)
ut.classSpec = Some(cs)
ut
}

def userBorrowedType(lname: List[String]) = {
val cs = ClassSpec.opaquePlaceholder(lname)
val ut = CalcUserType(lname, None)
ut.classSpec = Some(cs)
ut
}

lazy val ALL_LANGS = LanguageCompilerStatic.NAME_TO_CLASS.values

def full(src: String, srcType: DataType, expType: DataType, expOut: ResultMap) =
Expand Down

0 comments on commit 159a40d

Please sign in to comment.