def test_message_invalid_parameter_syntax(self): with self.assertRaises(ICUError): icu_format_html_message("id", "Hey {a b}!", arguments={ "a": "you", "b": "!" })
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">&</span> <a href="/you">you</a> < <a class="red big" href="/there?a=b&c=d">there<</a>!', )
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', )
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", )
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}", )
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", )
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 !", )
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}!", )
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&c=d">Hey</a>', )
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 &&</a>>', )
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 <b>you</b></a><b>there</b>', )
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>!', )
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>', )
def test_parameter_list(self): with self.assertRaises(InvalidArgsError): icu_format_html_message("en", "Hey {0} {1}!", arguments={"0": ["you ", "there"]})
def test_arguments_tuple(self): with self.assertRaises(AttributeError): icu_format_html_message("id", "Hey {0} {1}!", arguments=("you", "there"))
def test_arguments_list(self): with self.assertRaises(AttributeError): icu_format_html_message("en", "Hey {0} {1}!", arguments=["you", "there"])