import gi from chrysacase import ChrysalideTestCase from gi.repository import GObject from pychrysalide.glibext import StringBuilder class TestStringBuilder(ChrysalideTestCase): """Test cases for pychrysalide.glibext.StringBuilder.""" def ZZZtestStringBuilderCreation(self): """Create objects with StringBuilder interface.""" with self.assertRaisesRegex(NotImplementedError, 'StringBuilder can not be constructed'): sb = StringBuilder() class NewStringBuilderImplem(gi._gi.GObject, StringBuilder): pass nsb = NewStringBuilderImplem() self.assertIsNotNone(nsb) class NewStringBuilderImplem2(GObject.Object, StringBuilder): pass nsb2 = NewStringBuilderImplem() self.assertIsNotNone(nsb2) def ZZZtestStringBuilderMethods(self): """Test the StringBuilder methods.""" class BasicStringBuilderImplem(GObject.Object, StringBuilder): def __init__(self, desc): super().__init__() self._desc = desc def _to_string(self, flags=0): return self._desc desc = 'simple_desc' sb = BasicStringBuilderImplem(desc) self.assertEqual(sb.to_string(), desc) self.assertEqual(str(sb), desc) self.assertEqual(f'{sb}', desc) def testStringBuilderExceptions(self): """Raise exceptions from the StringBuilder interface as expected.""" class BadStringBuilderImplem(GObject.Object, StringBuilder): pass obj = BadStringBuilderImplem() with self.assertRaisesRegex(NotImplementedError, "method implementation is missing for '_to_string'"): s = str(obj) class BadStringBuilderImplem2(GObject.Object, StringBuilder): def _to_string(self, flags=0): return 0xcafe obj2 = BadStringBuilderImplem2() with self.assertRaisesRegex(TypeError, 'object description has to get provided as an UTF-8 string value'): s = str(obj2) class BadStringBuilderImplem3a(GObject.Object, StringBuilder): def _to_string(self, flags=0): return 'desc' class BadStringBuilderImplem3b(BadStringBuilderImplem3a, StringBuilder): def _to_string(self, flags=0): return self.parent_to_string() obj3 = BadStringBuilderImplem3b() with self.assertRaisesRegex(RuntimeError, 'object parent is not a native type'): s = str(obj3) class BadStringBuilderImplem4(GObject.Object, StringBuilder): def _to_string(self, flags=0): raise Exception('error') obj4 = BadStringBuilderImplem4() with self.assertRaisesRegex(Exception, 'error'): s = str(obj4)