From d07132396a0964a8ca74345366f79bd191c7242e Mon Sep 17 00:00:00 2001 From: Mingun Date: Sat, 9 Mar 2024 00:42:16 +0500 Subject: [PATCH] Fix `TranslatorSpec` tests (#273) This PR fixes TranslatorSpec tests by changing expected values to the actually used. Most commits fixes errors that was introduced during refactorings which did not update tests. This is understandable -- if the tests are already constantly falling, then few people think that they need to see if their changes break some of them. Many tests related to that that each expression was wrapped in parenthesis even when this is not necessary. I know, the resulting code because of this can look ugly, and probably you want to remove those extra parenthesis instead of baking them into tests... but implementing such translation is a long story and I think, it is better to fix tests now just so you can use normal workflow where other PRs will be accepted based on passed tests instead of blindly trust to the PR author and maintainers' reviews. I think everyone will agree that this has already led to a bunch of errors that have been hidden for years. If you want to remove those extra parenthesis, then open a separate issue / PR, where the changes in the code and in the tests will be made simultaneously. As a result, after this PR is only 21 failed tests (61 before) and only 9 in TranslatorSpec due to missing implementations of various features. I recommend disable those tests until features would be implemented, but I don't know how to do that. Anyway, this is better to do in another PR. --- .../struct/translators/TranslatorSpec.scala | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) 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 8fa3be6fc..9fae8083b 100644 --- a/jvm/src/test/scala/io/kaitai/struct/translators/TranslatorSpec.scala +++ b/jvm/src/test/scala/io/kaitai/struct/translators/TranslatorSpec.scala @@ -135,8 +135,8 @@ class TranslatorSpec extends AnyFunSpec { everybody("1 == 2", "1 == 2", CalcBooleanType) full("2 < 3 ? \"foo\" : \"bar\"", CalcIntType, CalcStrType, Map[LanguageCompilerStatic, String]( - CppCompiler -> "(2 < 3) ? (std::string(\"foo\")) : (std::string(\"bar\"))", - CSharpCompiler -> "2 < 3 ? \"foo\" : \"bar\"", + CppCompiler -> "((2 < 3) ? (std::string(\"foo\")) : (std::string(\"bar\")))", + CSharpCompiler -> "(2 < 3 ? \"foo\" : \"bar\")", GoCompiler -> """var tmp1 string; |if (2 < 3) { @@ -145,13 +145,13 @@ class TranslatorSpec extends AnyFunSpec { | tmp1 = "bar" |} |tmp1""".stripMargin, - JavaCompiler -> "2 < 3 ? \"foo\" : \"bar\"", - JavaScriptCompiler -> "2 < 3 ? \"foo\" : \"bar\"", - LuaCompiler -> "utils.box_unwrap(2 < 3 and utils.box_wrap(\"foo\") or \"bar\")", - PerlCompiler -> "2 < 3 ? \"foo\" : \"bar\"", - PHPCompiler -> "2 < 3 ? \"foo\" : \"bar\"", - PythonCompiler -> "u\"foo\" if 2 < 3 else u\"bar\"", - RubyCompiler -> "2 < 3 ? \"foo\" : \"bar\"" + JavaCompiler -> "(2 < 3 ? \"foo\" : \"bar\")", + JavaScriptCompiler -> "(2 < 3 ? \"foo\" : \"bar\")", + LuaCompiler -> "utils.box_unwrap((2 < 3) and utils.box_wrap(\"foo\") or (\"bar\"))", + PerlCompiler -> "(2 < 3 ? \"foo\" : \"bar\")", + PHPCompiler -> "(2 < 3 ? \"foo\" : \"bar\")", + PythonCompiler -> "(u\"foo\" if 2 < 3 else u\"bar\")", + RubyCompiler -> "(2 < 3 ? \"foo\" : \"bar\")" )) everybodyExcept("~777", "~777", Map[LanguageCompilerStatic, String]( @@ -203,7 +203,7 @@ class TranslatorSpec extends AnyFunSpec { } full("some_bool.to_i", CalcBooleanType, CalcIntType, Map[LanguageCompilerStatic, String]( - CppCompiler -> "some_bool()", + CppCompiler -> "((some_bool()) ? 1 : 0)", CSharpCompiler -> "(SomeBool ? 1 : 0)", GoCompiler -> """tmp1 := 0 |if this.SomeBool { @@ -212,7 +212,7 @@ class TranslatorSpec extends AnyFunSpec { |tmp1""".stripMargin, JavaCompiler -> "(someBool() ? 1 : 0)", JavaScriptCompiler -> "(this.someBool | 0)", - LuaCompiler -> "self.some_bool and 1 or 0", + LuaCompiler -> "(self.some_bool and 1 or 0)", PerlCompiler -> "$self->some_bool()", PHPCompiler -> "intval($this->someBool())", PythonCompiler -> "int(self.some_bool)", @@ -286,16 +286,16 @@ class TranslatorSpec extends AnyFunSpec { )) full("a != 2 and a != 5", CalcIntType, CalcBooleanType, Map[LanguageCompilerStatic, String]( - CppCompiler -> "a() != 2 && a() != 5", - CSharpCompiler -> "A != 2 && A != 5", - GoCompiler -> "a != 2 && a != 5", - JavaCompiler -> "a() != 2 && a() != 5", - JavaScriptCompiler -> "this.a != 2 && this.a != 5", - LuaCompiler -> "self.a ~= 2 and self.a ~= 5", - PerlCompiler -> "$self->a() != 2 && $self->a() != 5", - PHPCompiler -> "$this->a() != 2 && $this->a() != 5", - PythonCompiler -> "self.a != 2 and self.a != 5", - RubyCompiler -> "a != 2 && a != 5" + CppCompiler -> " ((a() != 2) && (a() != 5)) ", + CSharpCompiler -> " ((A != 2) && (A != 5)) ", + GoCompiler -> " ((this.A != 2) && (this.A != 5)) ", + JavaCompiler -> " ((a() != 2) && (a() != 5)) ", + JavaScriptCompiler -> " ((this.a != 2) && (this.a != 5)) ", + LuaCompiler -> " ((self.a ~= 2) and (self.a ~= 5)) ", + PerlCompiler -> " (($self->a() != 2) && ($self->a() != 5)) ", + PHPCompiler -> " (($this->a() != 2) && ($this->a() != 5)) ", + PythonCompiler -> " ((self.a != 2) and (self.a != 5)) ", + RubyCompiler -> " ((a != 2) && (a != 5)) " )) } @@ -536,7 +536,7 @@ class TranslatorSpec extends AnyFunSpec { LuaCompiler -> "string.reverse(\"str\")", PerlCompiler -> "scalar(reverse(\"str\"))", PHPCompiler -> "strrev(\"str\")", - PythonCompiler -> "u\"str\"[::-1]", + PythonCompiler -> "(u\"str\")[::-1]", RubyCompiler -> "\"str\".reverse" ))