Esempio n. 1
0
 def test_message_invalid_parameter_syntax(self):
     with self.assertRaises(ICUError):
         icu_format_html_message("id",
                                 "Hey {a b}!",
                                 arguments={
                                     "a": "you",
                                     "b": "!"
                                 })
Esempio n. 2
0
 def test_tag_placeholders(self):
     self.assertEqual(
         icu_format_html_message(
             "en",
             '<span0 class="nope">Hey {first}</span0><span1></span1> <span0>{second}</span0> <a0>{a}<b></b></a0> < <a1>there<</a1>!<br /><script type="text/javascript" src="mybadscript.js"></script>',
             arguments={
                 "a": "you",
                 "first": "hello",
                 "second": "&"
             },
             tags={
                 "a0": {
                     "tag": "a",
                     "attrs": {
                         "href": "/you"
                     }
                 },
                 "a1": {
                     "tag": "a",
                     "attrs": {
                         "href": "/there?a=b&c=d",
                         "class": "red big"
                     },
                 },
                 "span0": {
                     "tag": "span",
                     "attrs": {
                         "id": "hi"
                     }
                 },
             },
         ),
         '<span id="hi">Hey hello</span> <span id="hi">&amp;</span> <a href="/you">you</a> &lt; <a class="red big" href="/there?a=b&amp;c=d">there&lt;</a>!',
     )
Esempio n. 3
0
 def test_tags(self):
     self.assertEqual(
         icu_format_html_message(
             "en",
             '<a0 id="nope">Hello</a0><b0>you</b0><div>there</div>',
             arguments={"a": "you"},
             tags={
                 "a0": {
                     "tag": "a",
                     "attrs": {
                         "href": "/you",
                         "class": "the test",
                         "data-target": "someid",
                     },
                 },
                 "span0": {
                     "tag": "span",
                     "attrs": {
                         "id": "ignore"
                     }
                 },
             },
         ),
         '<a class="the test" data-target="someid" href="/you">Hello</a>youthere',
     )
Esempio n. 4
0
 def test_date_support(self):
     self.assertEqual(
         icu_format_html_message(
             "en",
             "Hello, it is {now, date, medium} at {now, time,kk:mm z}",
             arguments={"now": datetime(2020, 2, 11, 16, 51)},
         ),
         "Hello, it is Feb 11, 2020 at 16:51 UTC",
     )
Esempio n. 5
0
 def test_message_different_arguments(self):
     self.assertEqual(
         icu_format_html_message("id",
                                 "Hey {a} {0} {c}",
                                 arguments={
                                     "a": "you",
                                     "b": "!"
                                 }),
         "Hey you {0} {c}",
     )
Esempio n. 6
0
 def test_icu_support(self):
     message = ("Hello {a}, you have {g, select,"
                "   male {{n, plural,"
                "       =0 {no boys} one {# boy} other {# boys}"
                "   }}"
                "   female {{n, plural,"
                "       =0 {no girls} one {# girl} other {# girls}"
                "   }}"
                "   other {{n, plural,"
                "       =0 {no children} one {# child} other {# children}"
                "   }}"
                "}")
     self.assertEqual(
         icu_format_html_message("en",
                                 message,
                                 arguments={
                                     "a": "there",
                                     "g": "male",
                                     "n": 17
                                 }),
         "Hello there, you have 17 boys",
     )
     self.assertEqual(
         icu_format_html_message("en",
                                 message,
                                 arguments={
                                     "a": "there",
                                     "g": "female",
                                     "n": 18
                                 }),
         "Hello there, you have 18 girls",
     )
     self.assertEqual(
         icu_format_html_message("en",
                                 message,
                                 arguments={
                                     "a": "there",
                                     "g": "other",
                                     "n": 0
                                 }),
         "Hello there, you have no children",
     )
Esempio n. 7
0
 def test_message_numeric_parameter(self):
     self.assertEqual(
         icu_format_html_message("en",
                                 "Hey {a} {0} {b}",
                                 arguments={
                                     "a": "you",
                                     "b": "!",
                                     "0": "there"
                                 }),
         "Hey you there !",
     )
Esempio n. 8
0
 def test_trans_params(self):
     self.assertEqual(
         icu_format_html_message(
             "en",
             "Hey {a} {param_b} {c}!",
             arguments={
                 "param_b": "there",
                 "a": "you",
                 "d": "tester"
             },
         ),
         "Hey you there {c}!",
     )
Esempio n. 9
0
 def test_escapes_tag_attrs(self):
     self.assertEqual(
         icu_format_html_message(
             "en",
             "<a0>Hey</a0>",
             tags={"a0": {
                 "tag": "a",
                 "attrs": {
                     "href": "/you?a=b&c=d"
                 }
             }},
         ),
         '<a href="/you?a=b&amp;c=d">Hey</a>',
     )
Esempio n. 10
0
 def test_escapes_text(self):
     self.assertEqual(
         icu_format_html_message(
             "en",
             "<a0>Hello &<b>&</b></a0>>",
             tags={"a0": {
                 "tag": "a",
                 "attrs": {
                     "href": "/you"
                 }
             }},
         ),
         '<a href="/you">Hello &amp;&amp;</a>&gt;',
     )
Esempio n. 11
0
 def test_escapes_params(self):
     self.assertEqual(
         icu_format_html_message(
             "en",
             "<a0>Hey {name}</a0>{test}",
             arguments={
                 "name": "<b>you</b>",
                 "test": "<b>there</b>"
             },
             tags={"a0": {
                 "tag": "a",
                 "attrs": {
                     "href": "/you"
                 }
             }},
         ),
         '<a href="/you">Hey &lt;b&gt;you&lt;/b&gt;</a>&lt;b&gt;there&lt;/b&gt;',
     )
Esempio n. 12
0
 def test_params_in_tags(self):
     self.assertEqual(
         icu_format_html_message(
             "en",
             "<a0>Hello {name}</a0>{test}",
             arguments={
                 "name": "you",
                 "test": "!"
             },
             tags={"a0": {
                 "tag": "a",
                 "attrs": {
                     "href": "/you"
                 }
             }},
         ),
         '<a href="/you">Hello you</a>!',
     )
Esempio n. 13
0
 def test_nested_tags(self):
     self.assertEqual(
         icu_format_html_message(
             "en",
             "<a0>Hello<b0>you</b0><div>there</div></a0>",
             arguments={"a": "you"},
             tags={
                 "a0": {
                     "tag": "a",
                     "attrs": {
                         "href": "/you"
                     }
                 },
                 "b0": {
                     "tag": "b",
                     "attrs": {
                         "id": "hi"
                     }
                 },
             },
         ),
         '<a href="/you">Helloyouthere</a>',
     )
Esempio n. 14
0
 def test_parameter_list(self):
     with self.assertRaises(InvalidArgsError):
         icu_format_html_message("en",
                                 "Hey {0} {1}!",
                                 arguments={"0": ["you ", "there"]})
Esempio n. 15
0
 def test_arguments_tuple(self):
     with self.assertRaises(AttributeError):
         icu_format_html_message("id",
                                 "Hey {0} {1}!",
                                 arguments=("you", "there"))
Esempio n. 16
0
 def test_arguments_list(self):
     with self.assertRaises(AttributeError):
         icu_format_html_message("en",
                                 "Hey {0} {1}!",
                                 arguments=["you", "there"])