From 159a40d1a19ae9d67eb5b42547009d711fad136c Mon Sep 17 00:00:00 2001 From: Mikhail Yakshin Date: Wed, 6 Mar 2024 09:57:58 +0000 Subject: [PATCH] TestTypeProviders: extracted in a separate file --- .../translators/TestTypeProviders.scala | 120 ++++++++++++++++++ .../struct/translators/TranslatorSpec.scala | 101 +-------------- 2 files changed, 121 insertions(+), 100 deletions(-) create mode 100644 jvm/src/test/scala/io/kaitai/struct/translators/TestTypeProviders.scala diff --git a/jvm/src/test/scala/io/kaitai/struct/translators/TestTypeProviders.scala b/jvm/src/test/scala/io/kaitai/struct/translators/TestTypeProviders.scala new file mode 100644 index 000000000..051ee216d --- /dev/null +++ b/jvm/src/test/scala/io/kaitai/struct/translators/TestTypeProviders.scala @@ -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 + } +} diff --git a/jvm/src/test/scala/io/kaitai/struct/translators/TranslatorSpec.scala b/jvm/src/test/scala/io/kaitai/struct/translators/TranslatorSpec.scala index e54aa8d8a..4546c0dc8 100644 --- a/jvm/src/test/scala/io/kaitai/struct/translators/TranslatorSpec.scala +++ b/jvm/src/test/scala/io/kaitai/struct/translators/TranslatorSpec.scala @@ -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 @@ -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: - * - *
-    *   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]) = { - 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) =