Debian Codegraph @

tests/test_basic.py

raw symbols SCIP
1 # -*- coding: ascii -*-
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 Basics
24 ========
25
26 test for basics.
27 """
28 __author__ = u"Andr\xe9 Malo"
29
30 import os as _os
31
32 import rjsmin as _rjsmin
33
34 # pylint: disable = protected-access
35 py_jsmin = _rjsmin._make_jsmin(python_only=True)
36 py_jsmin2 = _rjsmin.jsmin_for_posers
37
38 import _rjsmin
39
40 c_jsmin = _rjsmin.jsmin
41
42
43 def load(name):
44 """Load a file"""
45 with open(_os.path.join(_os.path.dirname(__file__), name), "rb") as fp:
46 return fp.read()
47
48
49 def save(name, value):
50 """Load a file"""
51 with open(_os.path.join(_os.path.dirname(__file__), name), "wb") as fp:
52 fp.write(value)
53
54
55 def test_basic():
56 """Test basic.js"""
57 # pylint: disable = unidiomatic-typecheck
58
59 inp = load("js/basic.js")
60 exp = load("js/basic.min.js")
61 # save('js/basic.min.js', py_jsmin(inp))
62 assert type(py_jsmin(inp)) == bytes
63 assert type(py_jsmin2(inp)) == bytes
64 assert type(c_jsmin(inp)) == bytes
65
66 assert py_jsmin(inp) == exp
67 assert py_jsmin2(inp) == exp
68 assert c_jsmin(inp) == exp
69
70 if str is not bytes:
71 assert type(py_jsmin(bytearray(inp))) == bytearray
72 assert type(py_jsmin2(bytearray(inp))) == bytearray
73 assert type(c_jsmin(bytearray(inp))) == bytearray
74 assert py_jsmin(bytearray(inp)) == exp
75 assert py_jsmin2(bytearray(inp)) == exp
76 assert c_jsmin(bytearray(inp)) == exp
77
78 inp = inp.decode("latin-1")
79 exp = exp.decode("latin-1")
80 assert py_jsmin(inp) == exp
81 assert py_jsmin2(inp) == exp
82 assert c_jsmin(inp) == exp
83
84
85 def test_basic_bang():
86 """Test basic.js with bang comments"""
87 inp = load("js/basic.js")
88 exp = load("js/basic.bang.js")
89 # save('js/basic.bang.js', py_jsmin(inp, keep_bang_comments=True))
90 assert py_jsmin(inp, keep_bang_comments=True) == exp
91 assert py_jsmin2(inp, keep_bang_comments=True) == exp
92 assert c_jsmin(inp, keep_bang_comments=True) == exp
93
94 inp = inp.decode("latin-1")
95 exp = exp.decode("latin-1")
96 assert py_jsmin(inp, keep_bang_comments=True) == exp
97 assert py_jsmin2(inp, keep_bang_comments=True) == exp
98 assert c_jsmin(inp, keep_bang_comments=True) == exp