def test_mod_adds_mapping_creates(self):
        ts1 = translations.TranslationString("Name is %(name)s")
        ts2 = ts1 % {"name": "MyName"}
        ts3 = ts2 % {"name": "AnotherName"}

        assert ts1.mapping == {}
        assert ts2.mapping == {"name": "MyName"}
        assert ts3.mapping == {"name": "AnotherName"}
    def test_translate_gettext(self):
        ts = translations.TranslationString("Test %(foo)s")
        ts = ts % {"foo": "bar"}

        translation = pretend.stub(
            gettext=pretend.call_recorder(lambda m: "Translated %(foo)s"))

        assert ts.translate(translation) == "Translated bar"
        assert translation.gettext.calls == [pretend.call("Test %(foo)s")]
    def test_stores_values(self):
        message_id = pretend.stub()
        plural = pretend.stub()
        n = pretend.stub()

        ts = translations.TranslationString(message_id, plural, n)

        assert ts.message_id is message_id
        assert ts.plural is plural
        assert ts.n is n
    def test_with_translate_string(self):
        translation = pretend.stub()
        context = {"request": pretend.stub(translation=translation)}
        ts = translations.TranslationString("A Message")
        ts.translate = pretend.call_recorder(lambda t: "translated message")

        translated = translations.translate_value(context, ts)

        assert translated == "translated message"
        assert ts.translate.calls == [pretend.call(translation)]
    def test_translate_ngettext(self):
        ts = translations.TranslationString(
            "Test %(foo)s",
            "Plural %(foos)s",
            1,
        )
        ts = ts % {"foo": "bar"}

        translation = pretend.stub(ngettext=pretend.call_recorder(
            lambda m, p, n: "Translated %(foo)s"), )

        assert ts.translate(translation) == "Translated bar"
        assert translation.ngettext.calls == [
            pretend.call("Test %(foo)s", "Plural %(foos)s", 1),
        ]
    def test_mod_errors_non_mapping(self):
        ts = translations.TranslationString("Name is %(name)s")

        with pytest.raises(TypeError):
            ts % (1, )
 def test_repr(self, args, expected):
     ts = translations.TranslationString(*args)
     assert repr(ts) == expected
    def test_cant_specify_only_n(self):
        message_id = pretend.stub()
        n = pretend.stub()

        with pytest.raises(ValueError):
            translations.TranslationString(message_id, n=n)