Debian Codegraph @

tests/messages/test_jslexer.py

raw
1 from babel.messages import jslexer
2
3
4 def test_unquote():
5 assert jslexer.unquote_string('""') == ''
6 assert jslexer.unquote_string(r'"h\u00ebllo"') == "hëllo"
7 assert jslexer.unquote_string(r'"h\xebllo"') == "hëllo"
8 assert jslexer.unquote_string(r'"\xebb"') == "ëb"
9
10
11 def test_dollar_in_identifier():
12 assert list(jslexer.tokenize('dollar$dollar')) == [('name', 'dollar$dollar', 1)]
13
14
15 def test_dotted_name():
16 assert list(jslexer.tokenize("foo.bar(quux)", dotted=True)) == [
17 ('name', 'foo.bar', 1),
18 ('operator', '(', 1),
19 ('name', 'quux', 1),
20 ('operator', ')', 1),
21 ]
22
23
24 def test_dotted_name_end():
25 assert list(jslexer.tokenize("foo.bar", dotted=True)) == [
26 ('name', 'foo.bar', 1),
27 ]
28
29
30 def test_template_string():
31 assert list(jslexer.tokenize("gettext `foo\"bar\"p`", template_string=True)) == [
32 ('name', 'gettext', 1),
33 ('template_string', '`foo"bar"p`', 1),
34 ]
35
36
37 def test_jsx():
38 assert list(jslexer.tokenize("""
39 <option value="val1">{ i18n._('String1') }</option>
40 <option value="val2">{ i18n._('String 2') }</option>
41 <option value="val3">{ i18n._('String 3') }</option>
42 <component value={i18n._('String 4')} />
43 <comp2 prop={<comp3 />} data={{active: true}}>
44 <btn text={ i18n._('String 5') } />
45 </comp2>
46 """, jsx=True)) == [
47 ('jsx_tag', '<option', 2),
48 ('name', 'value', 2),
49 ('operator', '=', 2),
50 ('string', '"val1"', 2),
51 ('operator', '>', 2),
52 ('operator', '{', 2),
53 ('name', 'i18n._', 2),
54 ('operator', '(', 2),
55 ('string', "'String1'", 2),
56 ('operator', ')', 2),
57 ('operator', '}', 2),
58 ('jsx_tag', '</option', 2),
59 ('operator', '>', 2),
60 ('jsx_tag', '<option', 3),
61 ('name', 'value', 3),
62 ('operator', '=', 3),
63 ('string', '"val2"', 3),
64 ('operator', '>', 3),
65 ('operator', '{', 3),
66 ('name', 'i18n._', 3),
67 ('operator', '(', 3),
68 ('string', "'String 2'", 3),
69 ('operator', ')', 3),
70 ('operator', '}', 3),
71 ('jsx_tag', '</option', 3),
72 ('operator', '>', 3),
73 ('jsx_tag', '<option', 4),
74 ('name', 'value', 4),
75 ('operator', '=', 4),
76 ('string', '"val3"', 4),
77 ('operator', '>', 4),
78 ('operator', '{', 4),
79 ('name', 'i18n._', 4),
80 ('operator', '(', 4),
81 ('string', "'String 3'", 4),
82 ('operator', ')', 4),
83 ('operator', '}', 4),
84 ('jsx_tag', '</option', 4),
85 ('operator', '>', 4),
86 ('jsx_tag', '<component', 5),
87 ('name', 'value', 5),
88 ('operator', '=', 5),
89 ('operator', '{', 5),
90 ('name', 'i18n._', 5),
91 ('operator', '(', 5),
92 ('string', "'String 4'", 5),
93 ('operator', ')', 5),
94 ('operator', '}', 5),
95 ('jsx_tag', '/>', 5),
96 ('jsx_tag', '<comp2', 6),
97 ('name', 'prop', 6),
98 ('operator', '=', 6),
99 ('operator', '{', 6),
100 ('jsx_tag', '<comp3', 6),
101 ('jsx_tag', '/>', 6),
102 ('operator', '}', 6),
103 ('name', 'data', 6),
104 ('operator', '=', 6),
105 ('operator', '{', 6),
106 ('operator', '{', 6),
107 ('name', 'active', 6),
108 ('operator', ':', 6),
109 ('name', 'true', 6),
110 ('operator', '}', 6),
111 ('operator', '}', 6),
112 ('operator', '>', 6),
113 ('jsx_tag', '<btn', 7),
114 ('name', 'text', 7),
115 ('operator', '=', 7),
116 ('operator', '{', 7),
117 ('name', 'i18n._', 7),
118 ('operator', '(', 7),
119 ('string', "'String 5'", 7),
120 ('operator', ')', 7),
121 ('operator', '}', 7),
122 ('jsx_tag', '/>', 7),
123 ('jsx_tag', '</comp2', 8),
124 ('operator', '>', 8),
125 ]