How to use analyze_morf method in autotest

Best Python code snippet using autotest_python

coverage.py

Source:coverage.py Github

copy

Full Screen

...238 file = morf.__file__239 else:240 file = morf241 return self.canonical_filename(file)242 # analyze_morf(morf). Analyze the module or filename passed as243 # the argument. If the source code can't be found, raise an error.244 # Otherwise, return a pair of (1) the canonical filename of the245 # source code for the module, and (2) a list of lines of statements246 # in the source code.247 def analyze_morf(self, morf):248 if self.analysis_cache.has_key(morf):249 return self.analysis_cache[morf]250 filename = self.morf_filename(morf)251 ext = os.path.splitext(filename)[1]252 if ext == '.pyc':253 if not os.path.exists(filename[0:-1]):254 raise self.error, ("No source for compiled code '%s'."255 % filename)256 filename = filename[0:-1]257 elif ext != '.py':258 raise self.error, "File '%s' not Python source." % filename259 source = open(filename, 'r')260 import parser261 tree = parser.suite(source.read()).totuple(1)262 source.close()263 statements = {}264 self.find_statements(tree, statements)265 lines = statements.keys()266 lines.sort()267 result = filename, lines268 self.analysis_cache[morf] = result269 return result270 # find_statements(tree, dict). Find each statement in the parse271 # tree and record the line on which the statement starts in the272 # dictionary (by assigning it to 1).273 #274 # It works by walking the whole tree depth-first. Every time it275 # comes across a statement (symbol.stmt -- this includes compound276 # statements like 'if' and 'while') it calls find_statement, which277 # descends the tree below the statement to find the first terminal278 # token in that statement and record the lines on which that token279 # was found.280 #281 # This algorithm may find some lines several times (because of the282 # grammar production statement -> compound statement -> statement),283 # but that doesn't matter because we record lines as the keys of the284 # dictionary.285 #286 # See also [GDR 2001-12-04b, 3.2].287 def find_statements(self, tree, dict):288 import symbol, token289 if token.ISNONTERMINAL(tree[0]):290 for t in tree[1:]:291 self.find_statements(t, dict)292 if tree[0] == symbol.stmt:293 self.find_statement(tree[1], dict)294 elif (tree[0] == token.NAME295 and tree[1] in ['elif', 'except', 'finally']):296 dict[tree[2]] = 1297 def find_statement(self, tree, dict):298 import token299 while token.ISNONTERMINAL(tree[0]):300 tree = tree[1]301 dict[tree[2]] = 1302 # format_lines(statements, lines). Format a list of line numbers303 # for printing by coalescing groups of lines as long as the lines304 # represent consecutive statements. This will coalesce even if305 # there are gaps between statements, so if statements =306 # [1,2,3,4,5,10,11,12,13,14] and lines = [1,2,5,10,11,13,14] then307 # format_lines will return "1-2, 5-11, 13-14".308 def format_lines(self, statements, lines):309 pairs = []310 i = 0311 j = 0312 start = None313 pairs = []314 while i < len(statements) and j < len(lines):315 if statements[i] == lines[j]:316 if start == None:317 start = lines[j]318 end = lines[j]319 j = j + 1320 elif start:321 pairs.append((start, end))322 start = None323 i = i + 1324 if start:325 pairs.append((start, end))326 def stringify(pair):327 start, end = pair328 if start == end:329 return "%d" % start330 else:331 return "%d-%d" % (start, end)332 import string333 return string.join(map(stringify, pairs), ", ")334 def analysis(self, morf):335 filename, statements = self.analyze_morf(morf)336 self.canonicalize_filenames()337 if not self.cexecuted.has_key(filename):338 self.cexecuted[filename] = {}339 missing = []340 for line in statements:341 if not self.cexecuted[filename].has_key(line):342 missing.append(line)343 return (filename, statements, missing,344 self.format_lines(statements, missing))345 def morf_name(self, morf):346 if isinstance(morf, types.ModuleType):347 return morf.__name__348 else:349 return os.path.splitext(os.path.basename(morf))[0]...

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 autotest 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