How to use reflect method in avocado

Best Python code snippet using avocado_python

test_reflect.py

Source:test_reflect.py Github

copy

Full Screen

1# Copyright (c) 2001-2007 Twisted Matrix Laboratories.2# See LICENSE for details.3"""4Test cases for twisted.reflect module.5"""6import weakref, os7from ihooks import ModuleImporter8try:9 from collections import deque10except ImportError:11 deque = None12# Twisted Imports13from twisted.trial import unittest14from twisted.python import reflect15class SettableTest(unittest.TestCase):16 def setUp(self):17 self.setter = reflect.Settable()18 def tearDown(self):19 del self.setter20 def testSet(self):21 self.setter(a=1, b=2)22 self.failUnlessEqual(self.setter.a, 1)23 self.failUnlessEqual(self.setter.b, 2)24class AccessorTester(reflect.Accessor):25 def set_x(self, x):26 self.y = x27 self.reallySet('x',x)28 def get_z(self):29 self.q = 130 return 131 def del_z(self):32 self.reallyDel("q")33class AccessorTest(unittest.TestCase):34 def setUp(self):35 self.tester = AccessorTester()36 def testSet(self):37 self.tester.x = 138 self.failUnlessEqual(self.tester.x, 1)39 self.failUnlessEqual(self.tester.y, 1)40 def testGet(self):41 self.failUnlessEqual(self.tester.z, 1)42 self.failUnlessEqual(self.tester.q, 1)43 def testDel(self):44 self.tester.z45 self.failUnlessEqual(self.tester.q, 1)46 del self.tester.z47 self.failUnlessEqual(hasattr(self.tester, "q"), 0)48 self.tester.x = 149 del self.tester.x50 self.failUnlessEqual(hasattr(self.tester, "x"), 0)51class LookupsTestCase(unittest.TestCase):52 """53 Tests for L{namedClass}, L{namedModule}, and L{namedAny}.54 """55 def test_namedClassLookup(self):56 """57 L{namedClass} should return the class object for the name it is passed.58 """59 self.assertIdentical(60 reflect.namedClass("twisted.python.reflect.Summer"),61 reflect.Summer)62 def test_namedModuleLookup(self):63 """64 L{namedModule} should return the module object for the name it is65 passed.66 """67 self.assertIdentical(68 reflect.namedModule("twisted.python.reflect"), reflect)69 def test_namedAnyPackageLookup(self):70 """71 L{namedAny} should return the package object for the name it is passed.72 """73 import twisted.python74 self.assertIdentical(75 reflect.namedAny("twisted.python"), twisted.python)76 def test_namedAnyModuleLookup(self):77 """78 L{namedAny} should return the module object for the name it is passed.79 """80 self.assertIdentical(81 reflect.namedAny("twisted.python.reflect"), reflect)82 def test_namedAnyClassLookup(self):83 """84 L{namedAny} should return the class object for the name it is passed.85 """86 self.assertIdentical(87 reflect.namedAny("twisted.python.reflect.Summer"), reflect.Summer)88 def test_namedAnyAttributeLookup(self):89 """90 L{namedAny} should return the object an attribute of a non-module,91 non-package object is bound to for the name it is passed.92 """93 # Note - not assertEqual because unbound method lookup creates a new94 # object every time. This is a foolishness of Python's object95 # implementation, not a bug in Twisted.96 self.assertEqual(97 reflect.namedAny("twisted.python.reflect.Summer.reallySet"),98 reflect.Summer.reallySet)99 def test_namedAnySecondAttributeLookup(self):100 """101 L{namedAny} should return the object an attribute of an object which102 itself was an attribute of a non-module, non-package object is bound to103 for the name it is passed.104 """105 self.assertIdentical(106 reflect.namedAny(107 "twisted.python.reflect.Summer.reallySet.__doc__"),108 reflect.Summer.reallySet.__doc__)109 def test_importExceptions(self):110 """111 Exceptions raised by modules which L{namedAny} causes to be imported112 should pass through L{namedAny} to the caller.113 """114 self.assertRaises(115 ZeroDivisionError,116 reflect.namedAny, "twisted.test.reflect_helper_ZDE")117 # Make sure that this behavior is *consistent* for 2.3, where there is118 # no post-failed-import cleanup119 self.assertRaises(120 ZeroDivisionError,121 reflect.namedAny, "twisted.test.reflect_helper_ZDE")122 self.assertRaises(123 ValueError,124 reflect.namedAny, "twisted.test.reflect_helper_VE")125 # Modules which themselves raise ImportError when imported should result in an ImportError126 self.assertRaises(127 ImportError,128 reflect.namedAny, "twisted.test.reflect_helper_IE")129 def test_attributeExceptions(self):130 """131 If segments on the end of a fully-qualified Python name represents132 attributes which aren't actually present on the object represented by133 the earlier segments, L{namedAny} should raise an L{AttributeError}.134 """135 self.assertRaises(136 AttributeError,137 reflect.namedAny, "twisted.nosuchmoduleintheworld")138 # ImportError behaves somewhat differently between "import139 # extant.nonextant" and "import extant.nonextant.nonextant", so test140 # the latter as well.141 self.assertRaises(142 AttributeError,143 reflect.namedAny, "twisted.nosuch.modulein.theworld")144 self.assertRaises(145 AttributeError,146 reflect.namedAny, "twisted.python.reflect.Summer.nosuchattributeintheworld")147 def test_invalidNames(self):148 """149 Passing a name which isn't a fully-qualified Python name to L{namedAny}150 should result in a L{ValueError}.151 """152 # Finally, invalid module names should raise a ValueError153 self.assertRaises(154 ValueError,155 reflect.namedAny, "")156 self.assertRaises(157 ValueError,158 reflect.namedAny, "12345")159 self.assertRaises(160 ValueError,161 reflect.namedAny, "@#$@(#.!@(#!@#")162 # This case is kind of stupid and is mostly a historical accident.163 self.assertRaises(164 ValueError,165 reflect.namedAny, "tcelfer.nohtyp.detsiwt")166class ImportHooksLookupTests(LookupsTestCase):167 """168 Tests for lookup methods in the presence of L{ihooks}-style import hooks.169 Runs all of the tests from L{LookupsTestCase} after installing a custom170 import hook.171 """172 def setUp(self):173 """174 Perturb the normal import behavior subtly by installing an import175 hook. No custom behavior is provided, but this adds some extra176 frames to the call stack, which L{namedAny} must be able to account177 for.178 """179 self.importer = ModuleImporter()180 self.importer.install()181 def tearDown(self):182 """183 Uninstall the custom import hook.184 """185 self.importer.uninstall()186class ObjectGrep(unittest.TestCase):187 def test_dictionary(self):188 """189 Test references search through a dictionnary, as a key or as a value.190 """191 o = object()192 d1 = {None: o}193 d2 = {o: None}194 self.assertIn("[None]", reflect.objgrep(d1, o, reflect.isSame))195 self.assertIn("{None}", reflect.objgrep(d2, o, reflect.isSame))196 def test_list(self):197 """198 Test references search through a list.199 """200 o = object()201 L = [None, o]202 self.assertIn("[1]", reflect.objgrep(L, o, reflect.isSame))203 def test_tuple(self):204 """205 Test references search through a tuple.206 """207 o = object()208 T = (o, None)209 self.assertIn("[0]", reflect.objgrep(T, o, reflect.isSame))210 def test_instance(self):211 """212 Test references search through an object attribute.213 """214 class Dummy:215 pass216 o = object()217 d = Dummy()218 d.o = o219 self.assertIn(".o", reflect.objgrep(d, o, reflect.isSame))220 def test_weakref(self):221 """222 Test references search through a weakref object.223 """224 class Dummy:225 pass226 o = Dummy()227 w1 = weakref.ref(o)228 self.assertIn("()", reflect.objgrep(w1, o, reflect.isSame))229 def test_boundMethod(self):230 """231 Test references search through method special attributes.232 """233 class Dummy:234 def dummy(self):235 pass236 o = Dummy()237 m = o.dummy238 self.assertIn(".im_self", reflect.objgrep(m, m.im_self, reflect.isSame))239 self.assertIn(".im_class", reflect.objgrep(m, m.im_class, reflect.isSame))240 self.assertIn(".im_func", reflect.objgrep(m, m.im_func, reflect.isSame))241 def test_everything(self):242 """243 Test references search using complex set of objects.244 """245 class Dummy:246 def method(self):247 pass248 o = Dummy()249 D1 = {(): "baz", None: "Quux", o: "Foosh"}250 L = [None, (), D1, 3]251 T = (L, {}, Dummy())252 D2 = {0: "foo", 1: "bar", 2: T}253 i = Dummy()254 i.attr = D2255 m = i.method256 w = weakref.ref(m)257 self.assertIn("().im_self.attr[2][0][2]{'Foosh'}", reflect.objgrep(w, o, reflect.isSame))258 def test_depthLimit(self):259 """260 Test the depth of references search.261 """262 a = []263 b = [a]264 c = [a, b]265 d = [a, c]266 self.assertEquals(['[0]'], reflect.objgrep(d, a, reflect.isSame, maxDepth=1))267 self.assertEquals(['[0]', '[1][0]'], reflect.objgrep(d, a, reflect.isSame, maxDepth=2))268 self.assertEquals(['[0]', '[1][0]', '[1][1][0]'], reflect.objgrep(d, a, reflect.isSame, maxDepth=3))269 def test_deque(self):270 """271 Test references search through a deque object. Only for Python > 2.3.272 """273 o = object()274 D = deque()275 D.append(None)276 D.append(o)277 self.assertIn("[1]", reflect.objgrep(D, o, reflect.isSame))278 if deque is None:279 test_deque.skip = "Deque not available"280class GetClass(unittest.TestCase):281 def testOld(self):282 class OldClass:283 pass284 old = OldClass()285 self.assertIn(reflect.getClass(OldClass).__name__, ('class', 'classobj'))286 self.assertEquals(reflect.getClass(old).__name__, 'OldClass')287 def testNew(self):288 class NewClass(object):289 pass290 new = NewClass()291 self.assertEquals(reflect.getClass(NewClass).__name__, 'type')292 self.assertEquals(reflect.getClass(new).__name__, 'NewClass')293class Breakable(object):294 breakRepr = False295 breakStr = False296 def __str__(self):297 if self.breakStr:298 raise self299 else:300 return '<Breakable>'301 def __repr__(self):302 if self.breakRepr:303 raise self304 else:305 return 'Breakable()'306class BrokenType(Breakable, type):307 breakName = False308 def get___name__(self):309 if self.breakName:310 raise RuntimeError("no name")311 return 'BrokenType'312 __name__ = property(get___name__)313class BTBase(Breakable):314 __metaclass__ = BrokenType315 breakRepr = True316 breakStr = True317class NoClassAttr(object):318 __class__ = property(lambda x: x.not_class)319class SafeRepr(unittest.TestCase):320 def testWorkingRepr(self):321 x = [1,2,3]322 self.assertEquals(reflect.safe_repr(x), repr(x))323 def testBrokenRepr(self):324 b = Breakable()325 b.breakRepr = True326 reflect.safe_repr(b)327 def testBrokenStr(self):328 b = Breakable()329 b.breakStr = True330 reflect.safe_repr(b)331 def testBrokenClassRepr(self):332 class X(BTBase):333 breakRepr = True334 reflect.safe_repr(X)335 reflect.safe_repr(X())336 def testBrokenClassStr(self):337 class X(BTBase):338 breakStr = True339 reflect.safe_repr(X)340 reflect.safe_repr(X())341 def testBroken__Class__Attr(self):342 reflect.safe_repr(NoClassAttr())343 def testBroken__Class__Name__Attr(self):344 class X(BTBase):345 breakName = True346 reflect.safe_repr(X())347class SafeStr(unittest.TestCase):348 def testWorkingStr(self):349 x = [1,2,3]350 self.assertEquals(reflect.safe_str(x), str(x))351 def testBrokenStr(self):352 b = Breakable()353 b.breakStr = True354 reflect.safe_str(b)355 def testBrokenRepr(self):356 b = Breakable()357 b.breakRepr = True358 reflect.safe_str(b)359 def testBrokenClassStr(self):360 class X(BTBase):361 breakStr = True362 reflect.safe_str(X)363 reflect.safe_str(X())364 def testBrokenClassRepr(self):365 class X(BTBase):366 breakRepr = True367 reflect.safe_str(X)368 reflect.safe_str(X())369 def testBroken__Class__Attr(self):370 reflect.safe_str(NoClassAttr())371 def testBroken__Class__Name__Attr(self):372 class X(BTBase):373 breakName = True374 reflect.safe_str(X())375class FilenameToModule(unittest.TestCase):376 """377 Test L{reflect.filenameToModuleName} detection.378 """379 def test_directory(self):380 """381 Tests it finds good name for directories/packages.382 """383 module = reflect.filenameToModuleName(os.path.join('twisted', 'test'))384 self.assertEquals(module, 'test')385 module = reflect.filenameToModuleName(os.path.join('twisted', 'test')386 + os.path.sep)387 self.assertEquals(module, 'test')388 def test_file(self):389 """390 Test it finds good name for files.391 """392 module = reflect.filenameToModuleName(393 os.path.join('twisted', 'test', 'test_reflect.py'))...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run avocado automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful