Introducing AS2 classes Test class

AS2 classes - Test for maintaining vars/methods order
This commit is contained in:
Jindra Petřík
2018-01-21 09:07:20 +01:00
parent 2df9318036
commit b11fc959d1
6 changed files with 187 additions and 3 deletions

View File

@@ -349,6 +349,8 @@ public final class SWF implements SWFContainerItem, Timelined {
private static final DecompilerPool decompilerPool = new DecompilerPool();
public static final String AS2_PKG_PREFIX = "__Packages.";
public static List<String> swfSignatures = Arrays.asList(
"FWS", // Uncompressed Flash
"CWS", // ZLib compressed Flash
@@ -2276,10 +2278,9 @@ public final class SWF implements SWFContainerItem, Timelined {
DoInitActionTag dia = (DoInitActionTag) t;
String exportName = getExportName(dia.spriteId);
exportName = exportName != null ? exportName : "_unk_";
final String pkgPrefix = "__Packages.";
String[] classNameParts = null;
if (exportName.startsWith(pkgPrefix)) {
String className = exportName.substring(pkgPrefix.length());
if (exportName.startsWith(AS2_PKG_PREFIX)) {
String className = exportName.substring(AS2_PKG_PREFIX.length());
if (className.contains(".")) {
classNameParts = className.split("\\.");
} else {

View File

@@ -315,6 +315,7 @@ public class ActionScript2Test extends ActionScript2TestBase {
+ "var tg = new com.jpexs.TestSetterGetter();\r\n"
+ "var tcg = new com.jpexs.TestCallSetterGetter();\r\n"
+ "var tvm = new com.jpexs.TestVarsMethods();\r\n"
+ "var tmo = new com.jpexs.TestMaintainOrder();\r\n"
);
}

View File

@@ -0,0 +1,146 @@
/*
* Copyright (C) 2010-2018 JPEXS, All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
*/
package com.jpexs.decompiler.flash;
import com.jpexs.decompiler.flash.action.Action;
import com.jpexs.decompiler.flash.configuration.Configuration;
import com.jpexs.decompiler.flash.helpers.CodeFormatting;
import com.jpexs.decompiler.flash.helpers.HighlightedTextWriter;
import com.jpexs.decompiler.flash.tags.DoInitActionTag;
import com.jpexs.decompiler.flash.tags.Tag;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.fail;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
/**
*
* @author JPEXS
*/
public class ActionScript2TestClasses extends ActionScript2TestBase {
@BeforeClass
public void init() throws IOException, InterruptedException {
//Main.initLogging(false);
Configuration.autoDeobfuscate.set(false);
Configuration.simplifyExpressions.set(false);
Configuration.decompile.set(true);
Configuration.registerNameFormat.set("_loc%d_");
swf = new SWF(new BufferedInputStream(new FileInputStream("testdata/as2/as2.swf")), false);
}
private void compareSrc(String classFullName, String expectedResult) {
DoInitActionTag dia = getClassSource(classFullName);
assertNotNull(dia);
HighlightedTextWriter writer = new HighlightedTextWriter(new CodeFormatting(), false);
try {
Action.actionsToSource(dia, dia.getActions(), "", writer);
} catch (InterruptedException ex) {
fail();
}
String actualResult = cleanPCode(writer.toString());
expectedResult = cleanPCode(expectedResult);
assertEquals(actualResult, expectedResult);
}
private DoInitActionTag getClassSource(String classFullName) {
for (Tag t : swf.getTags()) {
if (t instanceof DoInitActionTag) {
DoInitActionTag dia = (DoInitActionTag) t;
String exportName = swf.getExportName(dia.spriteId);
if (exportName != null && exportName.startsWith(SWF.AS2_PKG_PREFIX)) {
String exportClassName = exportName.substring(SWF.AS2_PKG_PREFIX.length());
if (exportClassName.equals(classFullName)) {
return dia;
}
}
}
}
return null;
}
@Test
public void testVarsMethods() {
compareSrc("com.jpexs.TestVarsMethods", "class com.jpexs.TestVarsMethods\r\n"
+ "{\r\n"
+ "var instVar = 1;\r\n"
+ "static var statVar = 2;\r\n"
+ "function TestVarsMethods()\r\n"
+ "{\r\n"
+ "trace(\"constructor\");\r\n"
+ "}\r\n"
+ "function instMethod()\r\n"
+ "{\r\n"
+ "trace(\"instance method\");\r\n"
+ "}\r\n"
+ "static function statMethod()\r\n"
+ "{\r\n"
+ "trace(\"static method\");\r\n"
+ "}\r\n"
+ "}");
}
@Test
public void testMaintainOrder() {
compareSrc("com.jpexs.TestMaintainOrder", "class com.jpexs.TestMaintainOrder\r\n"
+ "{\r\n"
+ "var a = 1;\r\n"
+ "static var b = 2;\r\n"
+ "var c = 3;\r\n"
+ "static var d = 4;\r\n"
+ "static var e = 5;\r\n"
+ "var f = 6;\r\n"
+ "var g = 7;\r\n"
+ "var _x1 = \"after method m\";\r\n"
+ "function TestMaintainOrder()\r\n"
+ "{\r\n"
+ "}\r\n"
+ "function h()\r\n"
+ "{\r\n"
+ "trace(\"8\");\r\n"
+ "}\r\n"
+ "function i()\r\n"
+ "{\r\n"
+ "trace(\"9\");\r\n"
+ "}\r\n"
+ "static function j()\r\n"
+ "{\r\n"
+ "trace(\"10\");\r\n"
+ "}\r\n"
+ "static function k()\r\n"
+ "{\r\n"
+ "trace(\"11\");\r\n"
+ "}\r\n"
+ "function l()\r\n"
+ "{\r\n"
+ "trace(\"12\");\r\n"
+ "}\r\n"
+ "static function m()\r\n"
+ "{\r\n"
+ "trace(\"13\");\r\n"
+ "}\r\n"
+ "function _x2()\r\n"
+ "{\r\n"
+ "trace(\"after _x1\");\r\n"
+ "}\r\n"
+ "}");
}
}

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,36 @@
class com.jpexs.TestMaintainOrder {
public var a:Number = 1;
public static var b:Number = 2;
public var c:Number = 3;
public static var d:Number = 4;
public static var e:Number = 5;
public var f:Number = 6;
public var g:Number = 7;
public function h(){
trace("8");
}
public function i() {
trace("9");
}
public static function j() {
trace("10");
}
public static function k() {
trace("11");
}
public function l() {
trace("12");
}
public static function m() {
trace("13");
}
public var _x1:String = "after method m";
public function _x2() {
trace("after _x1");
}
}