Skip to content

Commit 76bc595

Browse files
committed
Add unittest module python files.
1 parent 3c9045d commit 76bc595

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+20410
-0
lines changed

Lib/contextlib.py

Lines changed: 440 additions & 0 deletions
Large diffs are not rendered by default.

Lib/fnmatch.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
"""Filename matching with shell patterns.
2+
3+
fnmatch(FILENAME, PATTERN) matches according to the local convention.
4+
fnmatchcase(FILENAME, PATTERN) always takes case in account.
5+
6+
The functions operate by translating the pattern into a regular
7+
expression. They cache the compiled regular expressions for speed.
8+
9+
The function translate(PATTERN) returns a regular expression
10+
corresponding to PATTERN. (It does not compile it.)
11+
"""
12+
import os
13+
import posixpath
14+
import re
15+
import functools
16+
17+
__all__ = ["filter", "fnmatch", "fnmatchcase", "translate"]
18+
19+
def fnmatch(name, pat):
20+
"""Test whether FILENAME matches PATTERN.
21+
22+
Patterns are Unix shell style:
23+
24+
* matches everything
25+
? matches any single character
26+
[seq] matches any character in seq
27+
[!seq] matches any char not in seq
28+
29+
An initial period in FILENAME is not special.
30+
Both FILENAME and PATTERN are first case-normalized
31+
if the operating system requires it.
32+
If you don't want this, use fnmatchcase(FILENAME, PATTERN).
33+
"""
34+
name = os.path.normcase(name)
35+
pat = os.path.normcase(pat)
36+
return fnmatchcase(name, pat)
37+
38+
@functools.lru_cache(maxsize=256, typed=True)
39+
def _compile_pattern(pat):
40+
if isinstance(pat, bytes):
41+
pat_str = str(pat, 'ISO-8859-1')
42+
res_str = translate(pat_str)
43+
res = bytes(res_str, 'ISO-8859-1')
44+
else:
45+
res = translate(pat)
46+
return re.compile(res).match
47+
48+
def filter(names, pat):
49+
"""Return the subset of the list NAMES that match PAT."""
50+
result = []
51+
pat = os.path.normcase(pat)
52+
match = _compile_pattern(pat)
53+
if os.path is posixpath:
54+
# normcase on posix is NOP. Optimize it away from the loop.
55+
for name in names:
56+
if match(name):
57+
result.append(name)
58+
else:
59+
for name in names:
60+
if match(os.path.normcase(name)):
61+
result.append(name)
62+
return result
63+
64+
def fnmatchcase(name, pat):
65+
"""Test whether FILENAME matches PATTERN, including case.
66+
67+
This is a version of fnmatch() which doesn't case-normalize
68+
its arguments.
69+
"""
70+
match = _compile_pattern(pat)
71+
return match(name) is not None
72+
73+
74+
def translate(pat):
75+
"""Translate a shell PATTERN to a regular expression.
76+
77+
There is no way to quote meta-characters.
78+
"""
79+
80+
i, n = 0, len(pat)
81+
res = ''
82+
while i < n:
83+
c = pat[i]
84+
i = i+1
85+
if c == '*':
86+
res = res + '.*'
87+
elif c == '?':
88+
res = res + '.'
89+
elif c == '[':
90+
j = i
91+
if j < n and pat[j] == '!':
92+
j = j+1
93+
if j < n and pat[j] == ']':
94+
j = j+1
95+
while j < n and pat[j] != ']':
96+
j = j+1
97+
if j >= n:
98+
res = res + '\\['
99+
else:
100+
stuff = pat[i:j]
101+
if '--' not in stuff:
102+
stuff = stuff.replace('\\', r'\\')
103+
else:
104+
chunks = []
105+
k = i+2 if pat[i] == '!' else i+1
106+
while True:
107+
k = pat.find('-', k, j)
108+
if k < 0:
109+
break
110+
chunks.append(pat[i:k])
111+
i = k+1
112+
k = k+3
113+
chunks.append(pat[i:j])
114+
# Escape backslashes and hyphens for set difference (--).
115+
# Hyphens that create ranges shouldn't be escaped.
116+
stuff = '-'.join(s.replace('\\', r'\\').replace('-', r'\-')
117+
for s in chunks)
118+
# Escape set operations (&&, ~~ and ||).
119+
stuff = re.sub(r'([&~|])', r'\\\1', stuff)
120+
i = j+1
121+
if stuff[0] == '!':
122+
stuff = '^' + stuff[1:]
123+
elif stuff[0] in ('^', '['):
124+
stuff = '\\' + stuff
125+
res = '%s[%s]' % (res, stuff)
126+
else:
127+
res = res + re.escape(c)
128+
return r'(?s:%s)\Z' % res

Lib/unittest/__init__.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""
2+
Python unit testing framework, based on Erich Gamma's JUnit and Kent Beck's
3+
Smalltalk testing framework (used with permission).
4+
5+
This module contains the core framework classes that form the basis of
6+
specific test cases and suites (TestCase, TestSuite etc.), and also a
7+
text-based utility class for running the tests and reporting the results
8+
(TextTestRunner).
9+
10+
Simple usage:
11+
12+
import unittest
13+
14+
class IntegerArithmeticTestCase(unittest.TestCase):
15+
def testAdd(self): # test method names begin with 'test'
16+
self.assertEqual((1 + 2), 3)
17+
self.assertEqual(0 + 1, 1)
18+
def testMultiply(self):
19+
self.assertEqual((0 * 10), 0)
20+
self.assertEqual((5 * 8), 40)
21+
22+
if __name__ == '__main__':
23+
unittest.main()
24+
25+
Further information is available in the bundled documentation, and from
26+
27+
http://docs.python.org/library/unittest.html
28+
29+
Copyright (c) 1999-2003 Steve Purcell
30+
Copyright (c) 2003-2010 Python Software Foundation
31+
This module is free software, and you may redistribute it and/or modify
32+
it under the same terms as Python itself, so long as this copyright message
33+
and disclaimer are retained in their original form.
34+
35+
IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
36+
SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF
37+
THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
38+
DAMAGE.
39+
40+
THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
41+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
42+
PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS,
43+
AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
44+
SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
45+
"""
46+
47+
__all__ = ['TestResult', 'TestCase', 'TestSuite',
48+
'TextTestRunner', 'TestLoader', 'FunctionTestCase', 'main',
49+
'defaultTestLoader', 'SkipTest', 'skip', 'skipIf', 'skipUnless',
50+
'expectedFailure', 'TextTestResult', 'installHandler',
51+
'registerResult', 'removeResult', 'removeHandler']
52+
53+
# Expose obsolete functions for backwards compatibility
54+
__all__.extend(['getTestCaseNames', 'makeSuite', 'findTestCases'])
55+
56+
__unittest = True
57+
58+
from .result import TestResult
59+
from .case import (TestCase, FunctionTestCase, SkipTest, skip, skipIf,
60+
skipUnless, expectedFailure)
61+
from .suite import BaseTestSuite, TestSuite
62+
from .loader import (TestLoader, defaultTestLoader, makeSuite, getTestCaseNames,
63+
findTestCases)
64+
from .main import TestProgram, main
65+
from .runner import TextTestRunner, TextTestResult
66+
from .signals import installHandler, registerResult, removeResult, removeHandler
67+
68+
# deprecated
69+
_TextTestResult = TextTestResult
70+
71+
# There are no tests here, so don't try to run anything discovered from
72+
# introspecting the symbols (e.g. FunctionTestCase). Instead, all our
73+
# tests come from within unittest.test.
74+
def load_tests(loader, tests, pattern):
75+
import os.path
76+
# top level directory cached on loader instance
77+
this_dir = os.path.dirname(__file__)
78+
return loader.discover(start_dir=this_dir, pattern=pattern)

Lib/unittest/__main__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""Main entry point"""
2+
3+
import sys
4+
if sys.argv[0].endswith("__main__.py"):
5+
import os.path
6+
# We change sys.argv[0] to make help message more useful
7+
# use executable without path, unquoted
8+
# (it's just a hint anyway)
9+
# (if you have spaces in your executable you get what you deserve!)
10+
executable = os.path.basename(sys.executable)
11+
sys.argv[0] = executable + " -m unittest"
12+
del os
13+
14+
__unittest = True
15+
16+
from .main import main
17+
18+
main(module=None)

0 commit comments

Comments
 (0)