| 1 |
|
| 2 |
u""" |
| 3 |
:Copyright: |
| 4 |
|
| 5 |
Copyright 2019 - 2025 |
| 6 |
Andr\xe9 Malo or his licensors, as applicable |
| 7 |
|
| 8 |
:License: |
| 9 |
|
| 10 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 11 |
you may not use this file except in compliance with the License. |
| 12 |
You may obtain a copy of the License at |
| 13 |
|
| 14 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 15 |
|
| 16 |
Unless required by applicable law or agreed to in writing, software |
| 17 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 18 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 |
See the License for the specific language governing permissions and |
| 20 |
limitations under the License. |
| 21 |
|
| 22 |
========== |
| 23 |
Issue 17 |
| 24 |
========== |
| 25 |
|
| 26 |
https://github.com/ndparker/rjsmin/issues/17 |
| 27 |
""" |
| 28 |
__author__ = u"Andr\xe9 Malo" |
| 29 |
|
| 30 |
import rjsmin as _rjsmin |
| 31 |
|
| 32 |
|
| 33 |
py_jsmin = _rjsmin._make_jsmin(python_only=True) |
| 34 |
py_jsmin2 = _rjsmin.jsmin_for_posers |
| 35 |
|
| 36 |
import _rjsmin |
| 37 |
|
| 38 |
c_jsmin = _rjsmin.jsmin |
| 39 |
|
| 40 |
|
| 41 |
def test_non_issue(): |
| 42 |
"""Test issue""" |
| 43 |
inp = b"console.write((f++)/ 4 + 3 / 2)" |
| 44 |
exp = b"console.write((f++)/4+3/2)" |
| 45 |
|
| 46 |
assert py_jsmin(inp) == exp |
| 47 |
assert py_jsmin2(inp) == exp |
| 48 |
assert c_jsmin(inp) == exp |
| 49 |
|
| 50 |
inp = inp.decode("latin-1") |
| 51 |
exp = exp.decode("latin-1") |
| 52 |
assert py_jsmin(inp) == exp |
| 53 |
assert py_jsmin2(inp) == exp |
| 54 |
assert c_jsmin(inp) == exp |
| 55 |
|
| 56 |
|
| 57 |
def test_non_issue_bang(): |
| 58 |
"""Test issue with bang comments""" |
| 59 |
inp = b"console.write((f++)/ 4 + 3 / 2)" |
| 60 |
exp = b"console.write((f++)/4+3/2)" |
| 61 |
|
| 62 |
assert py_jsmin(inp, keep_bang_comments=True) == exp |
| 63 |
assert py_jsmin2(inp, keep_bang_comments=True) == exp |
| 64 |
assert c_jsmin(inp, keep_bang_comments=True) == exp |
| 65 |
|
| 66 |
inp = inp.decode("latin-1") |
| 67 |
exp = exp.decode("latin-1") |
| 68 |
assert py_jsmin(inp, keep_bang_comments=True) == exp |
| 69 |
assert py_jsmin2(inp, keep_bang_comments=True) == exp |
| 70 |
assert c_jsmin(inp, keep_bang_comments=True) == exp |
| 71 |
|
| 72 |
|
| 73 |
def test_non_issue2(): |
| 74 |
"""Test issue""" |
| 75 |
inp = ( |
| 76 |
b"if (Y) { ba=Math.max(ba, Math.round(W.minHeight/Y))}; " |
| 77 |
b"bo.setKnobFactor((bp.width===0)? 0: br.width /bp.width);" |
| 78 |
) |
| 79 |
exp = ( |
| 80 |
b"if(Y){ba=Math.max(ba,Math.round(W.minHeight/Y))};" |
| 81 |
b"bo.setKnobFactor((bp.width===0)?0:br.width/bp.width);" |
| 82 |
) |
| 83 |
|
| 84 |
assert py_jsmin(inp) == exp |
| 85 |
assert py_jsmin2(inp) == exp |
| 86 |
assert c_jsmin(inp) == exp |
| 87 |
|
| 88 |
inp = inp.decode("latin-1") |
| 89 |
exp = exp.decode("latin-1") |
| 90 |
assert py_jsmin(inp) == exp |
| 91 |
assert py_jsmin2(inp) == exp |
| 92 |
assert c_jsmin(inp) == exp |
| 93 |
|
| 94 |
|
| 95 |
def test_non_issue2_bang(): |
| 96 |
"""Test issue""" |
| 97 |
inp = ( |
| 98 |
b"if (Y) { ba=Math.max(ba, Math.round(W.minHeight/Y))}; " |
| 99 |
b"bo.setKnobFactor((bp.width===0)? 0: br.width /bp.width);" |
| 100 |
) |
| 101 |
exp = ( |
| 102 |
b"if(Y){ba=Math.max(ba,Math.round(W.minHeight/Y))};" |
| 103 |
b"bo.setKnobFactor((bp.width===0)?0:br.width/bp.width);" |
| 104 |
) |
| 105 |
|
| 106 |
assert py_jsmin(inp, keep_bang_comments=True) == exp |
| 107 |
assert py_jsmin2(inp, keep_bang_comments=True) == exp |
| 108 |
assert c_jsmin(inp, keep_bang_comments=True) == exp |
| 109 |
|
| 110 |
inp = inp.decode("latin-1") |
| 111 |
exp = exp.decode("latin-1") |
| 112 |
assert py_jsmin(inp, keep_bang_comments=True) == exp |
| 113 |
assert py_jsmin2(inp, keep_bang_comments=True) == exp |
| 114 |
assert c_jsmin(inp, keep_bang_comments=True) == exp |
| 115 |
|
| 116 |
|
| 117 |
def test_non_issue_complex(): |
| 118 |
"""Test issue""" |
| 119 |
inp = ( |
| 120 |
b"console.write((f++)/*!dude*// 4 + 3 /a /* lalala */ " |
| 121 |
b"/*!lololo*// 2)" |
| 122 |
) |
| 123 |
exp = b"console.write((f++)/4+3/a/2)" |
| 124 |
|
| 125 |
assert py_jsmin(inp) == exp |
| 126 |
assert py_jsmin2(inp) == exp |
| 127 |
assert c_jsmin(inp) == exp |
| 128 |
|
| 129 |
inp = inp.decode("latin-1") |
| 130 |
exp = exp.decode("latin-1") |
| 131 |
assert py_jsmin(inp) == exp |
| 132 |
assert py_jsmin2(inp) == exp |
| 133 |
assert c_jsmin(inp) == exp |
| 134 |
|
| 135 |
|
| 136 |
def test_non_issue_complex_bang(): |
| 137 |
"""Test issue with bang comments""" |
| 138 |
inp = ( |
| 139 |
b"console.write((f++)/*!dude*// 4 + 3 /a /* lalala */ " |
| 140 |
b"/*!lololo*// 2)" |
| 141 |
) |
| 142 |
exp = b"console.write((f++)/*!dude*//4+3/a/*!lololo*//2)" |
| 143 |
|
| 144 |
assert py_jsmin(inp, keep_bang_comments=True) == exp |
| 145 |
assert py_jsmin2(inp, keep_bang_comments=True) == exp |
| 146 |
assert c_jsmin(inp, keep_bang_comments=True) == exp |
| 147 |
|
| 148 |
inp = inp.decode("latin-1") |
| 149 |
exp = exp.decode("latin-1") |
| 150 |
assert py_jsmin(inp, keep_bang_comments=True) == exp |
| 151 |
assert py_jsmin2(inp, keep_bang_comments=True) == exp |
| 152 |
assert c_jsmin(inp, keep_bang_comments=True) == exp |
| 153 |
|
| 154 |
|
| 155 |
def test_issue(): |
| 156 |
"""Test issue""" |
| 157 |
inp = b"for(f=0;f<z;f++)/^ *-+: *$/.test(x)" |
| 158 |
exp = b"for(f=0;f<z;f++)/^ *-+: *$/.test(x)" |
| 159 |
|
| 160 |
assert py_jsmin(inp) == exp |
| 161 |
assert py_jsmin2(inp) == exp |
| 162 |
assert c_jsmin(inp) == exp |
| 163 |
|
| 164 |
inp = inp.decode("latin-1") |
| 165 |
exp = exp.decode("latin-1") |
| 166 |
assert py_jsmin(inp) == exp |
| 167 |
assert py_jsmin2(inp) == exp |
| 168 |
assert c_jsmin(inp) == exp |
| 169 |
|
| 170 |
|
| 171 |
def test_issue_bang(): |
| 172 |
"""Test issue with bang comments""" |
| 173 |
inp = b"for(f=0;f<z;f++)/^ *-+: *$/.test(x)" |
| 174 |
exp = b"for(f=0;f<z;f++)/^ *-+: *$/.test(x)" |
| 175 |
|
| 176 |
assert py_jsmin(inp, keep_bang_comments=True) == exp |
| 177 |
assert py_jsmin2(inp, keep_bang_comments=True) == exp |
| 178 |
assert c_jsmin(inp, keep_bang_comments=True) == exp |
| 179 |
|
| 180 |
inp = inp.decode("latin-1") |
| 181 |
exp = exp.decode("latin-1") |
| 182 |
assert py_jsmin(inp, keep_bang_comments=True) == exp |
| 183 |
assert py_jsmin2(inp, keep_bang_comments=True) == exp |
| 184 |
assert c_jsmin(inp, keep_bang_comments=True) == exp |
| 185 |
|
| 186 |
|
| 187 |
def test_issue_complex(): |
| 188 |
"""Test issue""" |
| 189 |
inp = ( |
| 190 |
b"for(f=0;f<z;f++)/*!dude*//^ *-+: *$/i/*haha*//*!hoho*/\n" |
| 191 |
b"./*!hihi*/\n/*huhu*/test(x)" |
| 192 |
) |
| 193 |
exp = b"for(f=0;f<z;f++)/^ *-+: *$/i.test(x)" |
| 194 |
|
| 195 |
assert py_jsmin(inp) == exp |
| 196 |
assert py_jsmin2(inp) == exp |
| 197 |
assert c_jsmin(inp) == exp |
| 198 |
|
| 199 |
inp = inp.decode("latin-1") |
| 200 |
exp = exp.decode("latin-1") |
| 201 |
assert py_jsmin(inp) == exp |
| 202 |
assert py_jsmin2(inp) == exp |
| 203 |
assert c_jsmin(inp) == exp |
| 204 |
|
| 205 |
|
| 206 |
def test_issue_complex_bang(): |
| 207 |
"""Test issue with bang comments""" |
| 208 |
inp = ( |
| 209 |
b"for(f=0;f<z;f++)/*!dude*//^ *-+: *$/i/*haha*//*!hoho*/\n" |
| 210 |
b"./*!hihi*/\n/*huhu*/test(x)" |
| 211 |
) |
| 212 |
exp = b"for(f=0;f<z;f++)/*!dude*//^ *-+: *$/i/*!hoho*/./*!hihi*/test(x)" |
| 213 |
|
| 214 |
assert py_jsmin(inp, keep_bang_comments=True) == exp |
| 215 |
assert py_jsmin2(inp, keep_bang_comments=True) == exp |
| 216 |
assert c_jsmin(inp, keep_bang_comments=True) == exp |
| 217 |
|
| 218 |
inp = inp.decode("latin-1") |
| 219 |
exp = exp.decode("latin-1") |
| 220 |
assert py_jsmin(inp, keep_bang_comments=True) == exp |
| 221 |
assert py_jsmin2(inp, keep_bang_comments=True) == exp |
| 222 |
assert c_jsmin(inp, keep_bang_comments=True) == exp |
| 223 |
|
| 224 |
|
| 225 |
def test_issue_error1(): |
| 226 |
"""Test issue""" |
| 227 |
inp = b"for(f=0;f<z;f++)/^ *-+: *$//*" |
| 228 |
exp = b"for(f=0;f<z;f++)/^*-+:*$" |
| 229 |
|
| 230 |
assert py_jsmin(inp) == exp |
| 231 |
assert py_jsmin2(inp) == exp |
| 232 |
assert c_jsmin(inp) == exp |
| 233 |
|
| 234 |
inp = inp.decode("latin-1") |
| 235 |
exp = exp.decode("latin-1") |
| 236 |
assert py_jsmin(inp) == exp |
| 237 |
assert py_jsmin2(inp) == exp |
| 238 |
assert c_jsmin(inp) == exp |
| 239 |
|
| 240 |
|
| 241 |
def test_issue_error2(): |
| 242 |
"""Test issue""" |
| 243 |
inp = b"for(f=0;f<z;f++)/" |
| 244 |
exp = b"for(f=0;f<z;f++)/" |
| 245 |
|
| 246 |
assert py_jsmin(inp) == exp |
| 247 |
assert py_jsmin2(inp) == exp |
| 248 |
assert c_jsmin(inp) == exp |
| 249 |
|
| 250 |
inp = inp.decode("latin-1") |
| 251 |
exp = exp.decode("latin-1") |
| 252 |
assert py_jsmin(inp) == exp |
| 253 |
assert py_jsmin2(inp) == exp |
| 254 |
assert c_jsmin(inp) == exp |
| 255 |
|
| 256 |
|
| 257 |
def test_issue_error3(): |
| 258 |
"""Test issue""" |
| 259 |
inp = b"for(f=0;f<z;f++)/^ *-+: *$/./" |
| 260 |
exp = b"for(f=0;f<z;f++)/^*-+:*$/./" |
| 261 |
|
| 262 |
assert py_jsmin(inp) == exp |
| 263 |
assert py_jsmin2(inp) == exp |
| 264 |
assert c_jsmin(inp) == exp |
| 265 |
|
| 266 |
inp = inp.decode("latin-1") |
| 267 |
exp = exp.decode("latin-1") |
| 268 |
assert py_jsmin(inp) == exp |
| 269 |
assert py_jsmin2(inp) == exp |
| 270 |
assert c_jsmin(inp) == exp |
| 271 |
|
| 272 |
|
| 273 |
def test_issue_error4(): |
| 274 |
"""Test issue""" |
| 275 |
inp = b'for(f=0;f<z;f++)/^ *-+: *$/."lalala"' |
| 276 |
exp = b'for(f=0;f<z;f++)/^*-+:*$/."lalala"' |
| 277 |
|
| 278 |
assert py_jsmin(inp) == exp |
| 279 |
assert py_jsmin2(inp) == exp |
| 280 |
assert c_jsmin(inp) == exp |
| 281 |
|
| 282 |
inp = inp.decode("latin-1") |
| 283 |
exp = exp.decode("latin-1") |
| 284 |
assert py_jsmin(inp) == exp |
| 285 |
assert py_jsmin2(inp) == exp |
| 286 |
assert c_jsmin(inp) == exp |