コード例 #1
0
 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)
コード例 #2
0
ファイル: main.py プロジェクト: JohnLunzer/py1
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)
コード例 #3
0
ファイル: main.py プロジェクト: unbrice/py1
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)
コード例 #4
0
 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)
コード例 #5
0
 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)
コード例 #6
0
ファイル: test_tty.py プロジェクト: unbrice/py1
 def setUp(self):
     self.test_code = template_reader.build_code(
         ['c = 0'], ['c += 1'], ['P(c)'], concise=False)
コード例 #7
0
 def setUp(self):
     self.test_code = template_reader.build_code(['c = 0'], ['c += 1'],
                                                 ['P(c)'],
                                                 concise=False)