"""Tests for the unparse.py script in the Tools/parser directory.""" import unittest import test.support import io import os import random import tokenize import ast from test.test_tools import basepath, toolsdir, skip_if_missing skip_if_missing() parser_path = os.path.join(toolsdir, "parser") with test.support.DirsOnSysPath(parser_path): import unparse def read_pyfile(filename): """Read and return the contents of a Python source file (as a string), taking into account the file encoding.""" with open(filename, "rb") as pyfile: encoding = tokenize.detect_encoding(pyfile.readline)[0] with open(filename, "r", encoding=encoding) as pyfile: source = pyfile.read() return source for_else = """\ def f(): for x in range(10):
"""Tests for the md5sum script in the Tools directory.""" import os import unittest from test import support from test.support.script_helper import assert_python_ok, assert_python_failure from test.test_tools import scriptsdir, skip_if_missing skip_if_missing() class MD5SumTests(unittest.TestCase): @classmethod def setUpClass(cls): cls.script = os.path.join(scriptsdir, 'md5sum.py') os.mkdir(support.TESTFN) cls.fodder = os.path.join(support.TESTFN, 'md5sum.fodder') with open(cls.fodder, 'wb') as f: f.write(b'md5sum\r\ntest file\r\n') cls.fodder_md5 = b'd38dae2eb1ab346a292ef6850f9e1a0d' cls.fodder_textmode_md5 = b'a8b07894e2ca3f2a4c3094065fa6e0a5' @classmethod def tearDownClass(cls): support.rmtree(support.TESTFN) def test_noargs(self): rc, out, err = assert_python_ok(self.script) self.assertEqual(rc, 0) self.assertTrue( out.startswith(b'd41d8cd98f00b204e9800998ecf8427e <stdin>'))
# Argument Clinic # Copyright 2012-2013 by Larry Hastings. # Licensed to the PSF under a contributor agreement. from test import support, test_tools from test.support import os_helper from unittest import TestCase import collections import inspect import os.path import sys import unittest test_tools.skip_if_missing('clinic') with test_tools.imports_under_tool('clinic'): import clinic from clinic import DSLParser class FakeConverter: def __init__(self, name, args): self.name = name self.args = args class FakeConverterFactory: def __init__(self, name): self.name = name def __call__(self, name, default, **kwargs): return FakeConverter(self.name, kwargs)
import io import textwrap import unittest from test import test_tools from typing import Dict, Any from tokenize import TokenInfo, NAME, NEWLINE, NUMBER, OP test_tools.skip_if_missing('peg_generator') with test_tools.imports_under_tool('peg_generator'): from pegen.grammar_parser import GeneratedParser as GrammarParser from pegen.testutil import (parse_string, generate_parser, make_parser) from pegen.grammar import GrammarVisitor, GrammarError, Grammar from pegen.grammar_visualizer import ASTGrammarPrinter from pegen.parser import Parser from pegen.python_generator import PythonParserGenerator class TestPegen(unittest.TestCase): def test_parse_grammar(self) -> None: grammar_source = """ start: sum NEWLINE sum: t1=term '+' t2=term { action } | term term: NUMBER """ expected = """ start: sum NEWLINE sum: term '+' term | term term: NUMBER """ grammar: Grammar = parse_string(grammar_source, GrammarParser)
import os import shutil import tempfile from pathlib import Path from test import test_tools from test import support from test.support import os_helper from test.support.script_helper import assert_python_ok _py_cflags_nodist = sysconfig.get_config_var("PY_CFLAGS_NODIST") _pgo_flag = sysconfig.get_config_var("PGO_PROF_USE_FLAG") if _pgo_flag and _py_cflags_nodist and _pgo_flag in _py_cflags_nodist: raise unittest.SkipTest("peg_generator test disabled under PGO build") test_tools.skip_if_missing("peg_generator") with test_tools.imports_under_tool("peg_generator"): from pegen.grammar_parser import GeneratedParser as GrammarParser from pegen.testutil import ( parse_string, generate_parser_c_extension, generate_c_parser_source, ) from pegen.ast_dump import ast_dump TEST_TEMPLATE = """ tmp_dir = {extension_path!r} import ast import traceback
"""Sanity-check tests for the "freeze" tool.""" import sys import textwrap import unittest from test import support from test.support import os_helper from test.test_tools import imports_under_tool, skip_if_missing skip_if_missing('freeze') with imports_under_tool('freeze', 'test'): import freeze as helper @support.requires_zlib() @unittest.skipIf(sys.platform.startswith('win'), 'not supported on Windows') @support.skip_if_buildbot('not all buildbots have enough space') class TestFreeze(unittest.TestCase): def test_freeze_simple_script(self): script = textwrap.dedent(""" import sys print('running...') sys.exit(0) """) with os_helper.temp_dir() as outdir: outdir, scriptfile, python = helper.prepare(script, outdir) executable = helper.freeze(python, scriptfile, outdir) text = helper.run(executable) self.assertEqual(text, 'running...')