def testHasCode(self): """Ensure the generated code contains the user code.""" begin = 'begin = %s' % uuid.uuid4() for_each = 'for_each = %s' % uuid.uuid4() end = 'end = %s' % uuid.uuid4() generated = build_code([begin], [for_each], [end], concise=True) for code in [begin, for_each, end]: self.assertIn(code, generated)
def main(args=None): parser = _get_option_parser() args = parser.parse_args(args) begin = [imports.expand_short(i) for i in args.import_list] if args.single_snippet: # We expect no additional code if given a single snippet. for opt in ('begin', 'end', 'each_line'): if getattr(args, opt): snippet = _abreviate(args.single_snippet) parser.error( '--%s is specified yet there is a lonely code snippet, try' ' fixing quotes or adding --begin/-b before: "%s"' % (opt, snippet)) begin.append(args.single_snippet) else: begin += args.begin code = template_reader.build_code( begin=_uncurl_list_or_die(begin), each_line=_uncurl_list_or_die(args.each_line), end=_uncurl_list_or_die(args.end), concise=args.dump_code == _ARG_CONCISE, ) if args.dump_code: if sys.stdout.isatty(): try: colors = tty.count_ansi_colors() code = tty.ansi_highlight_code(code, colors) except: pass # Never fail on highlighting. print(code) return 0 code_globals = runner.build_globals() try: runner.run(code, code_globals) except runner.RunFailed as e: print('Running the one-liner failed:', file=sys.stderr) print(e.message, file=sys.stderr) print('You can see the unescaped code with --code=full', file=sys.stderr) sys.exit(1)
def testHasForLoop(self): """Ensure there is no for loop if there is no each_line code.""" no_foreach = build_code(['x=1'], [], ['x=3'], concise=False) foreach = build_code(['x=1'], ['x=2'], ['x=3'], concise=False) self.assertNotIn('for', no_foreach) self.assertIn('for', foreach)
def testHasReturns(self): """Ensure concise code does not contain full function body.""" concise = build_code([], [], [], concise=True) full = build_code([], [], [], concise=False) self.assertNotIn('return', concise) self.assertIn('return', full)
def setUp(self): self.test_code = template_reader.build_code( ['c = 0'], ['c += 1'], ['P(c)'], concise=False)
def setUp(self): self.test_code = template_reader.build_code(['c = 0'], ['c += 1'], ['P(c)'], concise=False)