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 9fae8083b..9eaee3f4b 100644 --- a/jvm/src/test/scala/io/kaitai/struct/translators/TranslatorSpec.scala +++ b/jvm/src/test/scala/io/kaitai/struct/translators/TranslatorSpec.scala @@ -11,6 +11,7 @@ import org.scalactic.source.Position import org.scalatest.Tag import org.scalatest.funspec.AnyFunSpec import org.scalatest.matchers.should.Matchers._ +import scala.collection.immutable.ListMap class TranslatorSpec extends AnyFunSpec { describe("integer literals + unary minus") { @@ -21,7 +22,7 @@ class TranslatorSpec extends AnyFunSpec { everybody("0x1234", "4660") // less and more than 32 Bit signed int everybody("1000000000", "1000000000") - everybodyExcept("100000000000", "100000000000", Map[LanguageCompilerStatic, String]( + everybodyExcept("100000000000", "100000000000", ResultMap( CppCompiler -> "100000000000LL", GoCompiler -> "int64(100000000000)", JavaCompiler -> "100000000000L" @@ -30,19 +31,19 @@ class TranslatorSpec extends AnyFunSpec { // 0x7fff_ffff everybody("2147483647", "2147483647") // 0x8000_0000 - everybodyExcept("2147483648", "2147483648", Map[LanguageCompilerStatic, String]( + everybodyExcept("2147483648", "2147483648", ResultMap( CppCompiler -> "2147483648UL", GoCompiler -> "uint32(2147483648)", JavaCompiler -> "2147483648L", )) // 0xffff_ffff - everybodyExcept("4294967295", "4294967295", Map[LanguageCompilerStatic, String]( + everybodyExcept("4294967295", "4294967295", ResultMap( CppCompiler -> "4294967295UL", GoCompiler -> "uint32(4294967295)", JavaCompiler -> "4294967295L", )) // 0x1_0000_0000 - everybodyExcept("4294967296", "4294967296", Map[LanguageCompilerStatic, String]( + everybodyExcept("4294967296", "4294967296", ResultMap( CppCompiler -> "4294967296LL", GoCompiler -> "int64(4294967296)", JavaCompiler -> "4294967296L", @@ -51,26 +52,26 @@ class TranslatorSpec extends AnyFunSpec { // -0x7fff_ffff everybody("-2147483647", "-2147483647") // -0x8000_0000 - everybodyExcept("-2147483648", "-2147483648", Map[LanguageCompilerStatic, String]( + everybodyExcept("-2147483648", "-2147483648", ResultMap( CppCompiler -> "(-2147483647 - 1)", LuaCompiler -> "(-2147483647 - 1)", PHPCompiler -> "(-2147483647 - 1)", )) // -0x8000_0001 - everybodyExcept("-2147483649", "-2147483649", Map[LanguageCompilerStatic, String]( + everybodyExcept("-2147483649", "-2147483649", ResultMap( CppCompiler -> "-2147483649LL", GoCompiler -> "int64(-2147483649)", JavaCompiler -> "-2147483649L", )) // 0x7fff_ffff_ffff_ffff - everybodyExcept("9223372036854775807", "9223372036854775807", Map[LanguageCompilerStatic, String]( + everybodyExcept("9223372036854775807", "9223372036854775807", ResultMap( CppCompiler -> "9223372036854775807LL", GoCompiler -> "int64(9223372036854775807)", JavaCompiler -> "9223372036854775807L", )) // 0x8000_0000_0000_0000 - everybodyExcept("9223372036854775808", "9223372036854775808", Map[LanguageCompilerStatic, String]( + everybodyExcept("9223372036854775808", "9223372036854775808", ResultMap( CppCompiler -> "9223372036854775808ULL", GoCompiler -> "uint64(9223372036854775808)", JavaCompiler -> "0x8000000000000000L", @@ -78,7 +79,7 @@ class TranslatorSpec extends AnyFunSpec { PHPCompiler -> "(-9223372036854775807 - 1)", )) // 0xffff_ffff_ffff_ffff - everybodyExcept("18446744073709551615", "18446744073709551615", Map[LanguageCompilerStatic, String]( + everybodyExcept("18446744073709551615", "18446744073709551615", ResultMap( CppCompiler -> "18446744073709551615ULL", GoCompiler -> "uint64(18446744073709551615)", JavaCompiler -> "0xffffffffffffffffL", @@ -86,13 +87,13 @@ class TranslatorSpec extends AnyFunSpec { PHPCompiler -> "-1", )) // -0x7fff_ffff_ffff_ffff - everybodyExcept("-9223372036854775807", "-9223372036854775807", Map[LanguageCompilerStatic, String]( + everybodyExcept("-9223372036854775807", "-9223372036854775807", ResultMap( CppCompiler -> "-9223372036854775807LL", GoCompiler -> "int64(-9223372036854775807)", JavaCompiler -> "-9223372036854775807L", )) // -0x8000_0000_0000_0000 - everybodyExcept("-9223372036854775808", "-9223372036854775808", Map[LanguageCompilerStatic, String]( + everybodyExcept("-9223372036854775808", "-9223372036854775808", ResultMap( CppCompiler -> "(-9223372036854775807LL - 1)", GoCompiler -> "int64(-9223372036854775808)", JavaCompiler -> "-9223372036854775808L", @@ -110,7 +111,7 @@ class TranslatorSpec extends AnyFunSpec { describe("simple integer operations") { everybody("1 + 2", "(1 + 2)") - everybodyExcept("3 / 2", "(3 / 2)", Map( + everybodyExcept("3 / 2", "(3 / 2)", ResultMap( JavaScriptCompiler -> "Math.floor(3 / 2)", LuaCompiler -> "math.floor(3 / 2)", PerlCompiler -> "int(3 / 2)", @@ -122,7 +123,7 @@ class TranslatorSpec extends AnyFunSpec { everybody("1 + 2 * 5", "(1 + (2 * 5))") - everybodyExcept("(1 + 2) / (7 * 8)", "((1 + 2) / (7 * 8))", Map( + everybodyExcept("(1 + 2) / (7 * 8)", "((1 + 2) / (7 * 8))", ResultMap( JavaScriptCompiler -> "Math.floor((1 + 2) / (7 * 8))", LuaCompiler -> "math.floor((1 + 2) / (7 * 8))", PerlCompiler -> "int((1 + 2) / (7 * 8))", @@ -134,7 +135,7 @@ class TranslatorSpec extends AnyFunSpec { everybody("1 == 2", "1 == 2", CalcBooleanType) - full("2 < 3 ? \"foo\" : \"bar\"", CalcIntType, CalcStrType, Map[LanguageCompilerStatic, String]( + full("2 < 3 ? \"foo\" : \"bar\"", CalcIntType, CalcStrType, ResultMap( CppCompiler -> "((2 < 3) ? (std::string(\"foo\")) : (std::string(\"bar\")))", CSharpCompiler -> "(2 < 3 ? \"foo\" : \"bar\")", GoCompiler -> @@ -154,10 +155,10 @@ class TranslatorSpec extends AnyFunSpec { RubyCompiler -> "(2 < 3 ? \"foo\" : \"bar\")" )) - everybodyExcept("~777", "~777", Map[LanguageCompilerStatic, String]( + everybodyExcept("~777", "~777", ResultMap( GoCompiler -> "^777" )) - everybodyExcept("~(7+3)", "~((7 + 3))", Map[LanguageCompilerStatic, String]( + everybodyExcept("~(7+3)", "~((7 + 3))", ResultMap( GoCompiler -> "^((7 + 3))" )) } @@ -175,7 +176,7 @@ class TranslatorSpec extends AnyFunSpec { } describe("boolean literals") { - full("true", CalcBooleanType, CalcBooleanType, Map[LanguageCompilerStatic, String]( + full("true", CalcBooleanType, CalcBooleanType, ResultMap( CppCompiler -> "true", CSharpCompiler -> "true", GoCompiler -> "true", @@ -188,7 +189,7 @@ class TranslatorSpec extends AnyFunSpec { RubyCompiler -> "true" )) - full("false", CalcBooleanType, CalcBooleanType, Map[LanguageCompilerStatic, String]( + full("false", CalcBooleanType, CalcBooleanType, ResultMap( CppCompiler -> "false", CSharpCompiler -> "false", GoCompiler -> "false", @@ -202,7 +203,7 @@ class TranslatorSpec extends AnyFunSpec { )) } - full("some_bool.to_i", CalcBooleanType, CalcIntType, Map[LanguageCompilerStatic, String]( + full("some_bool.to_i", CalcBooleanType, CalcIntType, ResultMap( CppCompiler -> "((some_bool()) ? 1 : 0)", CSharpCompiler -> "(SomeBool ? 1 : 0)", GoCompiler -> """tmp1 := 0 @@ -220,7 +221,7 @@ class TranslatorSpec extends AnyFunSpec { )) describe("member access") { - full("foo_str", CalcStrType, CalcStrType, Map[LanguageCompilerStatic, String]( + full("foo_str", CalcStrType, CalcStrType, ResultMap( CppCompiler -> "foo_str()", CSharpCompiler -> "FooStr", GoCompiler -> "this.FooStr", @@ -233,7 +234,7 @@ class TranslatorSpec extends AnyFunSpec { RubyCompiler -> "foo_str" )) - full("foo_block", userOwnedType(List("block")), userBorrowedType(List("block")), Map[LanguageCompilerStatic, String]( + full("foo_block", userOwnedType(List("block")), userBorrowedType(List("block")), ResultMap( CppCompiler -> "foo_block()", CSharpCompiler -> "FooBlock", GoCompiler -> "this.FooBlock", @@ -246,7 +247,7 @@ class TranslatorSpec extends AnyFunSpec { RubyCompiler -> "foo_block" )) - full("foo.bar", FooBarProvider, CalcStrType, Map[LanguageCompilerStatic, String]( + full("foo.bar", FooBarProvider, CalcStrType, ResultMap( CppCompiler -> "foo()->bar()", CSharpCompiler -> "Foo.Bar", GoCompiler -> "this.Foo.Bar", @@ -259,7 +260,7 @@ class TranslatorSpec extends AnyFunSpec { RubyCompiler -> "foo.bar" )) - full("foo.inner.baz", FooBarProvider, CalcIntType, Map[LanguageCompilerStatic, String]( + full("foo.inner.baz", FooBarProvider, CalcIntType, ResultMap( CppCompiler -> "foo()->inner()->baz()", CSharpCompiler -> "Foo.Inner.Baz", GoCompiler -> "this.Foo.Inner.Baz", @@ -272,7 +273,7 @@ class TranslatorSpec extends AnyFunSpec { RubyCompiler -> "foo.inner.baz" )) - full("_root.foo", userOwnedType(List("top_class", "block")), userBorrowedType(List("top_class", "block")), Map[LanguageCompilerStatic, String]( + full("_root.foo", userOwnedType(List("top_class", "block")), userBorrowedType(List("top_class", "block")), ResultMap( CppCompiler -> "_root()->foo()", CSharpCompiler -> "M_Root.Foo", GoCompiler -> "this._root.Foo", @@ -285,7 +286,7 @@ class TranslatorSpec extends AnyFunSpec { RubyCompiler -> "_root.foo" )) - full("a != 2 and a != 5", CalcIntType, CalcBooleanType, Map[LanguageCompilerStatic, String]( + full("a != 2 and a != 5", CalcIntType, CalcBooleanType, ResultMap( CppCompiler -> " ((a() != 2) && (a() != 5)) ", CSharpCompiler -> " ((A != 2) && (A != 5)) ", GoCompiler -> " ((this.A != 2) && (this.A != 5)) ", @@ -301,7 +302,7 @@ class TranslatorSpec extends AnyFunSpec { describe("arrays") { describe("literals") { - full("[0, 1, 100500]", CalcIntType, ArrayTypeInStream(CalcIntType), Map[LanguageCompilerStatic, String]( + full("[0, 1, 100500]", CalcIntType, ArrayTypeInStream(CalcIntType), ResultMap( CSharpCompiler -> "new List { 0, 1, 100500 }", GoCompiler -> "[]int{0, 1, 100500}", JavaCompiler -> "new ArrayList(Arrays.asList(0, 1, 100500))", @@ -313,7 +314,7 @@ class TranslatorSpec extends AnyFunSpec { RubyCompiler -> "[0, 1, 100500]" )) - full("[34, 0, 10, 64, 65, 66, 92]", CalcIntType, CalcBytesType, Map[LanguageCompilerStatic, String]( + full("[34, 0, 10, 64, 65, 66, 92]", CalcIntType, CalcBytesType, ResultMap( CppCompiler -> "std::string(\"\\x22\\x00\\x0A\\x40\\x41\\x42\\x5C\", 7)", CSharpCompiler -> "new byte[] { 34, 0, 10, 64, 65, 66, 92 }", GoCompiler -> "[]uint8{34, 0, 10, 64, 65, 66, 92}", @@ -326,7 +327,7 @@ class TranslatorSpec extends AnyFunSpec { RubyCompiler -> "[34, 0, 10, 64, 65, 66, 92].pack('C*')" )) - full("[255, 0, 255]", CalcIntType, CalcBytesType, Map[LanguageCompilerStatic, String]( + full("[255, 0, 255]", CalcIntType, CalcBytesType, ResultMap( CppCompiler -> "std::string(\"\\xFF\\x00\\xFF\", 3)", CSharpCompiler -> "new byte[] { 255, 0, 255 }", GoCompiler -> "[]uint8{255, 0, 255}", @@ -341,7 +342,7 @@ class TranslatorSpec extends AnyFunSpec { } describe("operations") { - full("[0, 1, 2].length", CalcIntType, CalcIntType, Map[LanguageCompilerStatic, String]( + full("[0, 1, 2].length", CalcIntType, CalcIntType, ResultMap( CppCompiler -> "std::string(\"\\x00\\x01\\x02\", 3).length()", CSharpCompiler -> "new byte[] { 0, 1, 2 }.Length", GoCompiler -> "len([]uint8{0, 1, 2})", @@ -354,7 +355,7 @@ class TranslatorSpec extends AnyFunSpec { RubyCompiler -> "[0, 1, 2].pack('C*').size" )) - full("a[42]", ArrayTypeInStream(CalcStrType), CalcStrType, Map[LanguageCompilerStatic, String]( + full("a[42]", ArrayTypeInStream(CalcStrType), CalcStrType, ResultMap( CppCompiler -> "a()->at(42)", CSharpCompiler -> "A[42]", GoCompiler -> "this.A[42]", @@ -367,7 +368,7 @@ class TranslatorSpec extends AnyFunSpec { RubyCompiler -> "a[42]" )) - full("a[42 - 2]", ArrayTypeInStream(CalcStrType), CalcStrType, Map[LanguageCompilerStatic, String]( + full("a[42 - 2]", ArrayTypeInStream(CalcStrType), CalcStrType, ResultMap( CppCompiler -> "a()->at((42 - 2))", CSharpCompiler -> "A[(42 - 2)]", GoCompiler -> "this.A[(42 - 2)]", @@ -380,7 +381,7 @@ class TranslatorSpec extends AnyFunSpec { RubyCompiler -> "a[(42 - 2)]" )) - full("a.first", ArrayTypeInStream(CalcIntType), CalcIntType, Map[LanguageCompilerStatic, String]( + full("a.first", ArrayTypeInStream(CalcIntType), CalcIntType, ResultMap( CppCompiler -> "a()->front()", CSharpCompiler -> "A[0]", GoCompiler -> "this.A[0]", @@ -393,7 +394,7 @@ class TranslatorSpec extends AnyFunSpec { RubyCompiler -> "a.first" )) - full("a.last", ArrayTypeInStream(CalcIntType), CalcIntType, Map[LanguageCompilerStatic, String]( + full("a.last", ArrayTypeInStream(CalcIntType), CalcIntType, ResultMap( CppCompiler -> "a()->back()", CSharpCompiler -> "A[A.Count - 1]", GoCompiler -> @@ -408,7 +409,7 @@ class TranslatorSpec extends AnyFunSpec { RubyCompiler -> "a.last" )) - full("a.size", ArrayTypeInStream(CalcIntType), CalcIntType, Map[LanguageCompilerStatic, String]( + full("a.size", ArrayTypeInStream(CalcIntType), CalcIntType, ResultMap( CppCompiler -> "a()->size()", CSharpCompiler -> "A.Count", GoCompiler -> "len(this.A)", @@ -425,7 +426,7 @@ class TranslatorSpec extends AnyFunSpec { describe("strings") { describe("literals") { - full("\"str\"", CalcIntType, CalcStrType, Map[LanguageCompilerStatic, String]( + full("\"str\"", CalcIntType, CalcStrType, ResultMap( CppCompiler -> "std::string(\"str\")", CSharpCompiler -> "\"str\"", GoCompiler -> "\"str\"", @@ -438,7 +439,7 @@ class TranslatorSpec extends AnyFunSpec { RubyCompiler -> "\"str\"" )) - full("\"str\\nnext\"", CalcIntType, CalcStrType, Map[LanguageCompilerStatic, String]( + full("\"str\\nnext\"", CalcIntType, CalcStrType, ResultMap( CppCompiler -> "std::string(\"str\\nnext\")", CSharpCompiler -> "\"str\\nnext\"", GoCompiler -> "\"str\\nnext\"", @@ -451,7 +452,7 @@ class TranslatorSpec extends AnyFunSpec { RubyCompiler -> "\"str\\nnext\"" )) - full("\"str\\u000anext\"", CalcIntType, CalcStrType, Map[LanguageCompilerStatic, String]( + full("\"str\\u000anext\"", CalcIntType, CalcStrType, ResultMap( CppCompiler -> "std::string(\"str\\nnext\")", CSharpCompiler -> "\"str\\nnext\"", GoCompiler -> "\"str\\nnext\"", @@ -464,7 +465,7 @@ class TranslatorSpec extends AnyFunSpec { RubyCompiler -> "\"str\\nnext\"" )) - full("\"str\\0next\"", CalcIntType, CalcStrType, Map[LanguageCompilerStatic, String]( + full("\"str\\0next\"", CalcIntType, CalcStrType, ResultMap( CppCompiler -> "std::string(\"str\\000next\", 8)", CSharpCompiler -> "\"str\\0next\"", GoCompiler -> "\"str\\000next\"", @@ -479,7 +480,7 @@ class TranslatorSpec extends AnyFunSpec { } describe("operators") { - everybodyExcept("\"str1\" + \"str2\"", "\"str1\" + \"str2\"", Map[LanguageCompilerStatic, String]( + everybodyExcept("\"str1\" + \"str2\"", "\"str1\" + \"str2\"", ResultMap( CppCompiler -> "std::string(\"str1\") + std::string(\"str2\")", LuaCompiler -> "\"str1\" .. \"str2\"", PerlCompiler -> "\"str1\" . \"str2\"", @@ -487,7 +488,7 @@ class TranslatorSpec extends AnyFunSpec { PythonCompiler -> "u\"str1\" + u\"str2\"" ), CalcStrType) - everybodyExcept("\"str1\" == \"str2\"", "\"str1\" == \"str2\"", Map[LanguageCompilerStatic, String]( + everybodyExcept("\"str1\" == \"str2\"", "\"str1\" == \"str2\"", ResultMap( CppCompiler -> "std::string(\"str1\") == (std::string(\"str2\"))", JavaCompiler -> "\"str1\".equals(\"str2\")", LuaCompiler -> "\"str1\" == \"str2\"", @@ -495,7 +496,7 @@ class TranslatorSpec extends AnyFunSpec { PythonCompiler -> "u\"str1\" == u\"str2\"" ), CalcBooleanType) - everybodyExcept("\"str1\" != \"str2\"", "\"str1\" != \"str2\"", Map[LanguageCompilerStatic, String]( + everybodyExcept("\"str1\" != \"str2\"", "\"str1\" != \"str2\"", ResultMap( CppCompiler -> "std::string(\"str1\") != std::string(\"str2\")", JavaCompiler -> "!(\"str1\").equals(\"str2\")", LuaCompiler -> "\"str1\" ~= \"str2\"", @@ -503,7 +504,7 @@ class TranslatorSpec extends AnyFunSpec { PythonCompiler -> "u\"str1\" != u\"str2\"" ), CalcBooleanType) - everybodyExcept("\"str1\" < \"str2\"", "\"str1\" < \"str2\"", Map[LanguageCompilerStatic, String]( + everybodyExcept("\"str1\" < \"str2\"", "\"str1\" < \"str2\"", ResultMap( CppCompiler -> "(std::string(\"str1\").compare(std::string(\"str2\")) < 0)", CSharpCompiler -> "(\"str1\".CompareTo(\"str2\") < 0)", JavaCompiler -> "(\"str1\".compareTo(\"str2\") < 0)", @@ -514,7 +515,7 @@ class TranslatorSpec extends AnyFunSpec { } describe("methods") { - full("\"str\".length", CalcIntType, CalcIntType, Map[LanguageCompilerStatic, String]( + full("\"str\".length", CalcIntType, CalcIntType, ResultMap( CppCompiler -> "std::string(\"str\").length()", CSharpCompiler -> "\"str\".Length", GoCompiler -> "utf8.RuneCountInString(\"str\")", @@ -527,7 +528,7 @@ class TranslatorSpec extends AnyFunSpec { RubyCompiler -> "\"str\".size" )) - full("\"str\".reverse", CalcIntType, CalcStrType, Map[LanguageCompilerStatic, String]( + full("\"str\".reverse", CalcIntType, CalcStrType, ResultMap( CppCompiler -> "kaitai::kstream::reverse(std::string(\"str\"))", CSharpCompiler -> "KaitaiStream.StringReverse(\"str\")", GoCompiler -> "kaitai.StringReverse(\"str\")", @@ -540,7 +541,7 @@ class TranslatorSpec extends AnyFunSpec { RubyCompiler -> "\"str\".reverse" )) - full("\"12345\".to_i", CalcIntType, CalcIntType, Map[LanguageCompilerStatic, String]( + full("\"12345\".to_i", CalcIntType, CalcIntType, ResultMap( CppCompiler -> "kaitai::kstream::string_to_int(std::string(\"12345\"))", CSharpCompiler -> "Convert.ToInt64(\"12345\", 10)", GoCompiler -> @@ -558,7 +559,7 @@ class TranslatorSpec extends AnyFunSpec { RubyCompiler -> "\"12345\".to_i" )) - full("\"1234fe\".to_i(16)", CalcIntType, CalcIntType, Map[LanguageCompilerStatic, String]( + full("\"1234fe\".to_i(16)", CalcIntType, CalcIntType, ResultMap( CppCompiler -> "kaitai::kstream::string_to_int(std::string(\"1234fe\"), 16)", CSharpCompiler -> "Convert.ToInt64(\"1234fe\", 16)", GoCompiler -> @@ -580,7 +581,7 @@ class TranslatorSpec extends AnyFunSpec { describe("casting") { describe("for user types") { - full("other.as.bar", FooBarProvider, CalcStrType, Map[LanguageCompilerStatic, String]( + full("other.as.bar", FooBarProvider, CalcStrType, ResultMap( CppCompiler -> "static_cast(other())->bar()", CSharpCompiler -> "((TopClass.Block) (Other)).Bar", GoCompiler -> "this.Other.(TopClass.Block).Bar", @@ -593,7 +594,7 @@ class TranslatorSpec extends AnyFunSpec { RubyCompiler -> "other.bar" )) - full("other.as.baz", FooBarProvider, CalcIntType, Map[LanguageCompilerStatic, String]( + full("other.as.baz", FooBarProvider, CalcIntType, ResultMap( CppCompiler -> "static_cast(other())->baz()", CSharpCompiler -> "((TopClass.Block.Innerblock) (Other)).Baz", GoCompiler -> "this.Other.(TopClass.Block.Innerblock).Baz", @@ -608,7 +609,7 @@ class TranslatorSpec extends AnyFunSpec { } describe("for primitive pure types") { - full("(1 + 2).as", CalcIntType, IntMultiType(true, Width2, None), Map[LanguageCompilerStatic, String]( + full("(1 + 2).as", CalcIntType, IntMultiType(true, Width2, None), ResultMap( CppCompiler -> "static_cast((1 + 2))", CSharpCompiler -> "((short) ((1 + 2)))", GoCompiler -> "int16((1 + 2))", @@ -623,7 +624,7 @@ class TranslatorSpec extends AnyFunSpec { } describe("for empty arrays") { - full("[].as", CalcIntType, CalcBytesType, Map[LanguageCompilerStatic, String]( + full("[].as", CalcIntType, CalcBytesType, ResultMap( CppCompiler -> "std::string(\"\", 0)", CSharpCompiler -> "new byte[] { }", GoCompiler -> "[]uint8{}", @@ -636,7 +637,7 @@ class TranslatorSpec extends AnyFunSpec { RubyCompiler -> "[].pack('C*')" )) - full("[].as", CalcIntType, ArrayTypeInStream(Int1Type(false)), Map[LanguageCompilerStatic, String]( + full("[].as", CalcIntType, ArrayTypeInStream(Int1Type(false)), ResultMap( CppCompiler -> "std::string(\"\")", CSharpCompiler -> "new List { }", GoCompiler -> "[]uint8{}", @@ -649,7 +650,7 @@ class TranslatorSpec extends AnyFunSpec { RubyCompiler -> "[]" )) - full("[].as", CalcIntType, ArrayTypeInStream(FloatMultiType(Width8, None)), Map[LanguageCompilerStatic, String]( + full("[].as", CalcIntType, ArrayTypeInStream(FloatMultiType(Width8, None)), ResultMap( CppCompiler -> "std::string(\"\", 0)", CSharpCompiler -> "new List { }", GoCompiler -> "[]float64{}", @@ -665,7 +666,7 @@ class TranslatorSpec extends AnyFunSpec { describe("to do type enforcement") { // type enforcement: casting to non-literal byte array - full("[0 + 1, 5].as", CalcIntType, CalcBytesType, Map[LanguageCompilerStatic, String]( + full("[0 + 1, 5].as", CalcIntType, CalcBytesType, ResultMap( CppCompiler -> "???", CSharpCompiler -> "new byte[] { (0 + 1), 5 }", GoCompiler -> "[]uint8{(0 + 1), 5}", @@ -679,7 +680,7 @@ class TranslatorSpec extends AnyFunSpec { )) // type enforcement: casting to array of integers - full("[0, 1, 2].as", CalcIntType, ArrayTypeInStream(Int1Type(false)), Map[LanguageCompilerStatic, String]( + full("[0, 1, 2].as", CalcIntType, ArrayTypeInStream(Int1Type(false)), ResultMap( CSharpCompiler -> "new List { 0, 1, 2 }", GoCompiler -> "[]uint8{0, 1, 2}", JavaCompiler -> "new ArrayList(Arrays.asList(0, 1, 2))", @@ -728,12 +729,12 @@ class TranslatorSpec extends AnyFunSpec { } describe("f-strings") { - everybodyExcept("f\"abc\"", "\"abc\"", Map( + everybodyExcept("f\"abc\"", "\"abc\"", ResultMap( CppCompiler -> "std::string(\"abc\")", PythonCompiler -> "u\"abc\"" ), CalcStrType) - full("f\"abc{1}%def\"", CalcIntType, CalcStrType, Map[LanguageCompilerStatic, String]( + full("f\"abc{1}%def\"", CalcIntType, CalcStrType, ResultMap( CppCompiler -> "std::string(\"abc\") + kaitai::kstream::to_string(1) + std::string(\"%def\")", CSharpCompiler -> "\"abc\" + (1).ToString() + \"%def\"", GoCompiler -> "fmt.Sprintf(\"abc%v%%def\", 1)", @@ -764,7 +765,7 @@ class TranslatorSpec extends AnyFunSpec { val goOutput = new StringLanguageOutputWriter(" ") - val langs = Map[LanguageCompilerStatic, AbstractTranslator with TypeDetector]( + val langs = ListMap[LanguageCompilerStatic, AbstractTranslator with TypeDetector]( CppCompiler -> new CppTranslator(tp, new CppImportList(), new CppImportList(), RuntimeConfig()), CSharpCompiler -> new CSharpTranslator(tp, new ImportList()), GoCompiler -> new GoTranslator(goOutput, tp, new ImportList()), @@ -802,7 +803,11 @@ class TranslatorSpec extends AnyFunSpec { } } - type ResultMap = Map[LanguageCompilerStatic, String] + // We use `ListMap` so the tests run in the same order every time. This is to + // give us logs that can be compared without manual sorting. + type ResultMap = ListMap[LanguageCompilerStatic, String] + def ResultMap(pairs: (LanguageCompilerStatic, String)*): ResultMap = ListMap(pairs: _*) + type TestSpec = (String, TypeProvider, DataType, ResultMap) lazy val ALL_LANGS = LanguageCompilerStatic.NAME_TO_CLASS.values @@ -814,10 +819,10 @@ class TranslatorSpec extends AnyFunSpec { runTest(src, tp, expType, expOut) def everybody(src: String, expOut: String, expType: DataType = CalcIntType)(implicit pos: Position) = - runTest(src, Always(CalcIntType), expType, ALL_LANGS.map((langObj) => langObj -> expOut).toMap) + runTest(src, Always(CalcIntType), expType, ResultMap(ALL_LANGS.map((langObj) => langObj -> expOut).toSeq:_*)) def everybodyExcept(src: String, commonExpOut: String, rm: ResultMap, expType: DataType = CalcIntType)(implicit pos: Position) = - runTest(src, Always(CalcIntType), expType, ALL_LANGS.map((langObj) => + runTest(src, Always(CalcIntType), expType, ResultMap(ALL_LANGS.map((langObj) => langObj -> rm.getOrElse(langObj, commonExpOut) - ).toMap) + ).toSeq:_*)) }