def do_pprint(self, val): if val is None: self.info("Usage: pp <arg>") try: self.message(objprint.objstr(self._get_val(val))) except Exception as e: self.info("".join(traceback.format_exception_only(type(e), e)), end="")
def test_dict(self): lsts = ( ({}, "{}"), ({"Age": "19"}, "{'Age': '19'}"), ({"Number": 1, "Letter": 'a'}, "{'Letter': 'a', 'Number': 1}") ) for obj, s in lsts: self.assertEqual(objstr(obj), s)
def test_tuple(self): lsts = ( ((), "()"), ((1, 2), "(1, 2)"), ((1, 'a'), "(1, 'a')") ) for obj, s in lsts: self.assertEqual(objstr(obj), s)
def do_print(obj, stream): if self.custom_printer is not None: self.custom_printer(obj) else: if type(obj) is str: print(obj, file=stream) else: print(objstr(obj), file=stream)
def test_list(self): lsts = ( ([], "[]"), ([1, 2], "[1, 2]"), ([1, 'a'], "[1, 'a']"), ([123, 'a' * 100], f"[\n 123,\n '{'a' * 100}'\n]") ) for obj, s in lsts: self.assertEqual(objstr(obj), s)
def test_get_ellipsis(self): self.assertEqual("<C ... >", objstr(C(), 5))
def test_one_line_object(self): s = io.StringIO() self.assertNotIn("\n", objstr(s))
def test_full_match(self): f = ObjTest({"give": "strGive", "curve": "strCurve", "head": "strHead"}) actual = objstr(f, include=['.*e']) self.assertIn("give", actual) self.assertIn("curve", actual) self.assertNotIn("head", actual)
def test_multiline(self): n = ObjTest({"name": "Apple", "age": 10}) m = ObjTest({"lst": [1, 2, n]}) actual = objstr(m) self.assertEqual(actual.count("\n"), 9)
def do_print(obj, stream): if type(obj) is str: print(obj, file=stream) else: print(objstr(obj), file=stream)
def test_wo_attr(self): e = random._random.Random() actual = objstr(e) self.assertTrue(len(actual) > 0)
def test_exclude(self): t = InExclude() expected = "<InExclude\n .a = 'in'\n>" self.assertEqual(objstr(t, exclude=['b', 'c']), expected)
def test_get_ellipsis(self): obj = ObjTest({}) self.assertEqual("<ObjTest ... >", objstr(obj, 5))
def test_add_match(self): t = ObjTest({"xyz": "xyzVal", "xyzz": "xyzzVal", "xyzxz": "xyzxzVal"}) actual = objstr(t, include=['xyz+']) self.assertIn("xyz", actual) self.assertIn("xyzz", actual) self.assertNotIn("xyzxz", actual)
def test_None(self): self.assertEqual('None', objstr(None))
def test_function_type(self): self.assertEqual("<function run>", objstr(Car.run))
def test_function_type(self): def run(self): print("my car can run!") obj = ObjTest({"brand": "a", "function": run}) self.assertEqual("<function run>", objstr(obj.function))
def test_multiline(self): m = Multiline() actual = objstr(m) self.assertEqual(actual.count("\n"), 9)
def test_exclude_indent(self): t = ObjTest({"pos1": "in", "pos2": "out", "pos3": "ex"}) expected = "<ObjTest\n .pos1 = 'in'\n >" self.assertEqual(objstr(t, indent_level=1, exclude=['pos2', 'pos3']), expected)
def test_str(self): s = objstr(A()) self.assertTrue(len(s) > 0)
def test_include_exclude_mix(self): t = ObjTest({"pos1": "in", "pos2": "out", "pos3": "ex"}) output = objstr(t, include=['pos1', 'pos2'], exclude=['pos2', 'pos3']) self.assertIn("pos1", output) self.assertNotIn("pos2", output) self.assertNotIn("pos3", output)