Quick binary expression handling for “test_token_buffering“
[vadmium-streams.git] / test_javascript.py
blob9d0cf8ec0e57ab77c56334d99543e6fb6f5f81c3
1 from unittest import TestCase
2 import javascript
3 from io import StringIO
4 from unittest.mock import patch
5 from math import inf, ldexp, copysign
7 class Test(TestCase):
8 def test_identifier_escaped(self):
9 for [code, name] in (
10 (r'ab\u0063de', 'abcde'),
11 (r'\u005Cu123\u0034', '\\u1234'),
13 with self.subTest(code):
14 [code] = javascript.parse(StringIO(code))
15 self.assertEqual(code, ('identifier', name))
16 for code in ('ab\\y0033z', 'ab\\', '\\u123\\u0034'):
17 with self.subTest(code):
18 parser = javascript.parse(StringIO(code))
19 self.assertRaises(Exception, next, parser, None)
21 def test_token_buffering(self):
22 with patch.object(javascript.Parser, 'BUFSIZE', 10):
23 [code] = javascript.parse(StringIO('abcdefg >>>= x'))
24 self.assertEqual(code,
25 ( '>>>=', ('identifier', 'abcdefg'), ('identifier', 'x') ))
27 def test_rounding(self):
28 for [code, rounded] in (
29 ('0.24703''28229''20623''27208e-323', 0),
30 ('0.24703''28229''20623''27209e-323', ldexp(2**-52, -1022)),
31 ('24703''28229''20623''27208e-343', 0),
32 ('24703''28229''20623''27209e-343', ldexp(2**-52, -1022)),
33 ('0.17976''93134''86231''58079e+309', ldexp(1 - 2**-53, +1024)),
34 ('0.17976''93134''86231''58080e+309', inf),
35 ('17976''93134''86231''58079e+289', ldexp(1 - 2**-53, +1024)),
36 ('17976''93134''86231''58080e+289', inf),
37 (f'{2**52 + 0}.5', 2**52),
38 (f'{2**52 + 1}.5', 2**52 + 2),
39 (hex( 2**1024 - 2**(1024 - 53) + (0b10 << (1024 - 55)) ), inf),
40 (hex( 2**1024 - 2**(1024 - 53) + (0b01 << (1024 - 55)) ),
41 ldexp(1 - 2**-53, +1024)),
42 (hex(2**53 + 0b01), 2**53),
43 (hex(2**53 + 0b11), 2**53 + 0b100),
45 with self.subTest(code):
46 [code] = javascript.parse(StringIO(code))
47 self.assertEqual(code, rounded)
48 self.assertEqual(copysign(1, code), copysign(1, rounded))