| 1 |
import pytest |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
@pytest.mark.skip(reason="TODO: This test is moved from the module, properly implement it.") |
| 6 |
def test_run(): |
| 7 |
p = JavaScriptPacker() |
| 8 |
with open(sys.argv[1]) as f: |
| 9 |
script = f.read() |
| 10 |
result = p.pack(script, encoding=62, fastDecode=True, compaction=True) |
| 11 |
with open(f"{sys.argv[1]}pack", "w") as f: |
| 12 |
f.write(result) |
| 13 |
|
| 14 |
|
| 15 |
@pytest.mark.skip(reason="TODO: This test is moved from the module, properly implement it.") |
| 16 |
def test_run1(): |
| 17 |
test_scripts = [] |
| 18 |
|
| 19 |
test_scripts.append( |
| 20 |
( |
| 21 |
"""// ---------------------------------------------------------------------- |
| 22 |
// public interface |
| 23 |
// ----------------------------------------------------------------------- |
| 24 |
|
| 25 |
cssQuery.toString = function() { |
| 26 |
return "function cssQuery() {\n [version " + version + "]\n}"; |
| 27 |
};""", |
| 28 |
0, |
| 29 |
False, |
| 30 |
False, |
| 31 |
"""cssQuery.toString=function(){return"function cssQuery() {\n """ |
| 32 |
"""[version "+version+"]\n}"};""", |
| 33 |
) |
| 34 |
) |
| 35 |
|
| 36 |
test_scripts.append( |
| 37 |
( |
| 38 |
"""function test(_localvar) { |
| 39 |
var $name = 'foo'; |
| 40 |
var $$dummy = 2; |
| 41 |
|
| 42 |
return $name + $$dummy; |
| 43 |
}""", |
| 44 |
0, |
| 45 |
False, |
| 46 |
True, |
| 47 |
"""function test(_0){var n='foo';var du=2;return n+du}""", |
| 48 |
) |
| 49 |
) |
| 50 |
|
| 51 |
test_scripts.append( |
| 52 |
( |
| 53 |
"""function _test($localvar) { |
| 54 |
var $name = 1; |
| 55 |
var _dummy = 2; |
| 56 |
var __foo = 3; |
| 57 |
|
| 58 |
return $name + _dummy + $localvar + __foo; |
| 59 |
}""", |
| 60 |
0, |
| 61 |
False, |
| 62 |
True, |
| 63 |
"""function _1(l){var n=1;var _0=2;var __foo=3;return n+_0+l+__foo}""", |
| 64 |
) |
| 65 |
) |
| 66 |
|
| 67 |
test_scripts.append( |
| 68 |
( |
| 69 |
"""function _test($localvar) { |
| 70 |
var $name = 1; |
| 71 |
var _dummy = 2; |
| 72 |
var __foo = 3; |
| 73 |
|
| 74 |
return $name + _dummy + $localvar + __foo; |
| 75 |
} |
| 76 |
|
| 77 |
function _bar(_ocalvar) { |
| 78 |
var $name = 1; |
| 79 |
var _dummy = 2; |
| 80 |
var __foo = 3; |
| 81 |
|
| 82 |
return $name + _dummy + $localvar + __foo; |
| 83 |
}""", |
| 84 |
0, |
| 85 |
False, |
| 86 |
True, |
| 87 |
"""function _3(l){var n=1;var _0=2;var __foo=3;return n+_0+l+__foo}""" |
| 88 |
"""function _2(_1){var n=1;var _0=2;var __foo=3;return n+_0+l+__foo}""", |
| 89 |
) |
| 90 |
) |
| 91 |
|
| 92 |
test_scripts.append(("cssQuery1.js", 0, False, False, "cssQuery1-p1.js")) |
| 93 |
test_scripts.append(("cssQuery.js", 0, False, False, "cssQuery-p1.js")) |
| 94 |
test_scripts.append(("pack.js", 0, False, False, "pack-p1.js")) |
| 95 |
test_scripts.append(("cssQuery.js", 0, False, True, "cssQuery-p2.js")) |
| 96 |
# the following ones are different, because javascript might use an |
| 97 |
# unstable sort algorithm while python uses an stable sort algorithm |
| 98 |
test_scripts.append(("pack.js", 0, False, True, "pack-p2.js")) |
| 99 |
test_scripts.append( |
| 100 |
( |
| 101 |
"test.js", |
| 102 |
0, |
| 103 |
False, |
| 104 |
True, |
| 105 |
"""function _4(l){var n=1;var _0=2;var __foo=3;return n+_0+l+__foo}""" |
| 106 |
"""function _3(_1){var n=1;var _2=2;var __foo=3;return n+_2+l+__foo}""", |
| 107 |
) |
| 108 |
) |
| 109 |
test_scripts.append( |
| 110 |
( |
| 111 |
"test.js", |
| 112 |
10, |
| 113 |
False, |
| 114 |
False, |
| 115 |
"""eval(function(p,a,c,k,e,d){while(c--){if(k[c]){p=p.replace(new RegExp""" |
| 116 |
"""("\\b"+e(c)+"\\b","g"),k[c])}}return p}('8 13($6){0 $4=1;0 7=2;0 5=3;""" |
| 117 |
"""9 $4+7+$6+5}8 11(12){0 $4=1;0 10=2;0 5=3;9 $4+10+$6+5}',10,14,'var||||""" |
| 118 |
"""name|__foo|localvar|_dummy|function|return|_2|_bar|_ocalvar|_test'""" |
| 119 |
""".split('|')))""", |
| 120 |
) |
| 121 |
) |
| 122 |
test_scripts.append( |
| 123 |
( |
| 124 |
"test.js", |
| 125 |
62, |
| 126 |
False, |
| 127 |
False, |
| 128 |
"""eval(function(p,a,c,k,e,d){while(c--){if(k[c]){p=p.replace(new RegExp""" |
| 129 |
"""("\\b"+e(c)+"\\b","g"),k[c])}}return p}('8 d($6){0 $4=1;0 7=2;0 5=3;9 """ |
| 130 |
"""$4+7+$6+5}8 b(c){0 $4=1;0 a=2;0 5=3;9 $4+a+$6+5}',14,14,'var||||name|""" |
| 131 |
"""__foo|localvar|_dummy|function|return|_2|_bar|_ocalvar|_test'.split('|""" |
| 132 |
"""')))""", |
| 133 |
) |
| 134 |
) |
| 135 |
test_scripts.append(("test.js", 95, False, False, "test-p4.js")) |
| 136 |
test_scripts.append(("cssQuery.js", 0, False, True, "cssQuery-p3.js")) |
| 137 |
test_scripts.append(("cssQuery.js", 62, False, True, "cssQuery-p4.js")) |
| 138 |
|
| 139 |
p = JavaScriptPacker() |
| 140 |
for script, encoding, fastDecode, specialChars, expected in test_scripts: |
| 141 |
if os.path.exists(script): |
| 142 |
with open(script) as f: |
| 143 |
_script = f.read() |
| 144 |
else: |
| 145 |
_script = script |
| 146 |
if os.path.exists(expected): |
| 147 |
with open(expected) as f: |
| 148 |
_expected = f.read() |
| 149 |
else: |
| 150 |
_expected = expected |
| 151 |
print(script[:20], encoding, fastDecode, specialChars, expected[:20]) |
| 152 |
print("=" * 40) |
| 153 |
result = p.pack(_script, encoding, fastDecode, specialChars) |
| 154 |
print(len(result), len(_script)) |
| 155 |
if result != _expected: |
| 156 |
print("ERROR!!!!!!!!!!!!!!!!") |
| 157 |
print(_expected) |
| 158 |
print(result) |
| 159 |
# print list(difflib.unified_diff(result, _expected)) |