def test_custom_globals_with_extra_globals(self): context = Context.from_string( self.source, globals=dict(hour=MyGlobal('-7')), extra_globals=dict(my=MyGlobal('someval')), ) self.assertTrue(context('hour'), '-7') self.assertTrue(context('my'), 'someval')
def test_default_globals_are_accessible(self): context = Context.from_string(self.source) hour_value = context('hour') self.assertTrue(hour_value.isdigit(), hour_value) os_value = context('os') self.assertTrue(os_value in ('linux', 'mac', 'win', 'unknown'), os_value)
def test_extra_globals(self): context = Context.from_string(self.source, extra_globals=dict( my=MyGlobal('someval'), os=MyGlobal('bestosever'), )) hour_value = context('hour') self.assertTrue(hour_value.isdigit(), hour_value) self.assertEqual(context('os'), 'bestosever') self.assertEqual(context('my'), 'someval')
def setUp(self): self.context = Context.from_string(""" <one 'eins'> <types { str: "string", num: "number", bool: "boolean", *other: "other" }> <typesIndexed[$type] { str: "string", num: "number", bool: "boolean", *other: "other" }> <type "{{ types[$type] }}"> <withAttribs 'Me have attributes!' color:'red' > <withBadAttribs error:'{{ $noSuchVar }}' indexError[$noSuchVar]:{k1: "v1", k2: "v2"} > <withoutContent dummy:"dummy"> <accessObjKeyProp "{{ $obj.key }}"> <luckyNum "Your lucky number is {{ $num }}"> """)
def test_custom_globals(self): context = Context.from_string(self.source, globals={}) self.assertEqual(context('hour'), '{{ @hour }}')
def test_error_hook_on_missing_variable(self): tr = Context.from_file(StringIO('<a "{{ $missing }}">'), error_hook=self.store_error) self.assertTrue(tr('a'), '{{ $missing }}') self.assertTrue(isinstance(self.error, NameError))
def test_raise_error_hook(self): tr = Context.from_file(os.path.join(DIR, 'samples', 'numbers.l20n'), error_hook=self.raise_error) self.assertRaises(NameError, tr, 'noSuchEntity')
def test_error_hook_on_missing_entity(self): tr = Context.from_string('', error_hook=self.store_error) tr('noSuchEntity') self.assertTrue(isinstance(self.error, NameError))
def test_unicode_load_by_path(self): tr = Context.from_file(os.path.join(DIR, 'samples', 'unicode-de.l20n')) self.assertEqual(tr('key'), u'Schl\xfcssel') self.assertEqual(tr('keyEscaped'), u'Schl\xfcssel')
def test_load_by_file_like(self): f = StringIO('<entity "some value">') tr = Context.from_file(f) self.assertEqual(tr('entity'), "some value")
def test_load_by_open_file(self): with open(os.path.join(DIR, 'samples', 'numbers.l20n')) as f: tr = Context.from_file(f) self.assertEqual(tr('pi'), '3.14')
def test_load_by_path(self): tr = Context.from_file(os.path.join(DIR, 'samples', 'numbers.l20n')) self.assertEqual(tr('thousand'), '1000')