Skip to content

Commit 958c51a

Browse files
dirkfGitHub Actions
authored and
GitHub Actions
committed
[JSInterp] Improve tests
* from yt-dlp/yt-dlp#12313 * also fix d7c2708
1 parent bf59b7e commit 958c51a

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

test/test_jsinterp.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,16 @@ def test_Date(self):
212212
# undefined
213213
self._test(jsi, NaN, args=[JS_Undefined])
214214
# y,m,d, ... - may fail with older dates lacking DST data
215-
jsi = JSInterpreter('function f() { return new Date(%s); }'
216-
% ('2024, 5, 29, 2, 52, 12, 42',))
217-
self._test(jsi, 1719625932042)
215+
jsi = JSInterpreter(
216+
'function f() { return new Date(%s); }'
217+
% ('2024, 5, 29, 2, 52, 12, 42',))
218+
self._test(jsi, (
219+
1719625932042 # UK value
220+
+ (
221+
+ 3600 # back to GMT
222+
+ (time.altzone if time.daylight # host's DST
223+
else time.timezone)
224+
) * 1000))
218225
# no arg
219226
self.assertAlmostEqual(JSInterpreter(
220227
'function f() { return new Date() - 0; }').call_function('f'),
@@ -485,6 +492,14 @@ def test_bitwise_operators_typecast(self):
485492
self._test('function f(){return NaN << 42}', 0)
486493
self._test('function f(){return "21.9" << 1}', 42)
487494
self._test('function f(){return 21 << 4294967297}', 42)
495+
self._test('function f(){return true << "5";}', 32)
496+
self._test('function f(){return true << true;}', 2)
497+
self._test('function f(){return "19" & "21.9";}', 17)
498+
self._test('function f(){return "19" & false;}', 0)
499+
self._test('function f(){return "11.0" >> "2.1";}', 2)
500+
self._test('function f(){return 5 ^ 9;}', 12)
501+
self._test('function f(){return 0.0 << NaN}', 0)
502+
self._test('function f(){return null << undefined}', 0)
488503

489504
def test_negative(self):
490505
self._test('function f(){return 2 * -2.0 ;}', -4)

youtube_dl/jsinterp.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ def _js_to_primitive(v):
154154
)
155155

156156

157+
# more exact: yt-dlp/yt-dlp#12110
157158
def _js_toString(v):
158159
return (
159160
'undefined' if v is JS_Undefined
@@ -162,7 +163,7 @@ def _js_toString(v):
162163
else 'null' if v is None
163164
# bool <= int: do this first
164165
else ('false', 'true')[v] if isinstance(v, bool)
165-
else '{0:.7f}'.format(v).rstrip('.0') if isinstance(v, compat_numeric_types)
166+
else re.sub(r'(?<=\d)\.?0*$', '', '{0:.7f}'.format(v)) if isinstance(v, compat_numeric_types)
166167
else _js_to_primitive(v))
167168

168169

0 commit comments

Comments
 (0)