Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
dcdac1f
fnmatch: Add ure compatibility.
pi-anl Mar 18, 2022
7259f0f
fnmatch: Remove dependency on os.path.
pi-anl Apr 11, 2022
d64557a
fnmatch: Release 0.6.0.
pi-anl Mar 18, 2022
a9cd99c
unittest: Allow passing module name or instance into unittest.main()
andrewleech Nov 1, 2018
7d4d02e
unittest: Log failure tracebacks at test end.
andrewleech Oct 31, 2018
663a3d6
unittest: test_unittest.py: Fix typo in method name.
pfalcon Dec 18, 2018
669d343
unittest: Allow to catch AssertionError with assertRaises().
Oct 31, 2018
fca89f6
unittest: test_unittest: Add test for .assertRaises(AssertionError).
pfalcon Jan 25, 2019
965e25c
unittest: test_unittest: Typo fix.
pfalcon Mar 3, 2019
a57b575
unittest: AssertRaisesContext: Store exception value as self.exception.
pfalcon Jul 27, 2019
dedfe2d
unittest: Add assertLessEqual, assertGreaterEqual methods.
pfalcon Aug 13, 2019
dc788f4
unittest: Reinstate useful debugger helper.
pfalcon Dec 14, 2019
c72ec5c
unittest: TestSuite: Add undescore to internal field, self._tests.
pfalcon Dec 15, 2019
d747b21
unittest: Only treat callable fields as test methods.
pfalcon Dec 15, 2019
f09d2ec
unittest: Support both test classes and class instances.
pfalcon Feb 26, 2020
5d3a44c
unittest: TestCase: Add (dummy) __init__.
pfalcon Feb 27, 2020
7d77774
unittest: Add TestCase.skipTest() method.
pfalcon Aug 8, 2020
555f28c
unittest: Add dummy TestCase.subTest() context manager.
pfalcon Aug 9, 2020
04dce89
unittest: Add dummy TestCase.assertWarns() context manager.
pfalcon Aug 10, 2020
2c0b508
unittest: TestSuite: Add run() method.
pfalcon Nov 15, 2020
1b46612
unittest: Implement basic addCleanup()/doCleanup().
pfalcon May 12, 2021
e582666
unittest: Properly handle failures vs errors.
pfalcon May 14, 2021
8e82f3d
unittest: Support recursive TestSuite's.
pfalcon May 15, 2021
377ebbf
unittest: Add expectedFailure decorator.
pfalcon Jul 19, 2021
01fcd42
unittest: test_unittest: Add tests for expectedFailure decorator.
pfalcon Jul 20, 2021
5a53a75
unittest: Print no. of skipped tests in a way compatible with CPython.
pfalcon Oct 12, 2021
ac282d8
unittest: Add TextTestRunner as alias for TestRunner.
pfalcon Oct 13, 2021
f92833b
unittest: Support TestCase subclasses with own runTest() method.
pfalcon Oct 23, 2021
c7eb3de
unittest: Print module name on result lines.
pi-anl Mar 18, 2022
9d9ca3d
unittest: Run test_* functions as well as TestCase classes.
pi-anl Mar 18, 2022
a7b2f63
unittest: Add discover function.
pi-anl Mar 18, 2022
cb8d108
unittest: Add test for environment isolation.
pi-anl May 3, 2022
9f6f211
unittest: Reset python env between tests.
pi-anl Apr 11, 2022
9b6315a
unittest: Add exception capturing for subTest.
pi-anl May 3, 2022
ddeb9a7
unittest: Improve failure text consistency with cpython.
pi-anl May 3, 2022
2d61dbd
unittest: Add setUpClass and tearDownClass handling.
Apr 5, 2022
959115d
unittest: Add support for specifying custom TestRunner.
pi-anl May 5, 2022
db4c739
unittest: Version 0.9.0
pi-anl Oct 24, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions python-stdlib/fnmatch/fnmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,26 @@
The function translate(PATTERN) returns a regular expression
corresponding to PATTERN. (It does not compile it.)
"""
import os
import os.path
import re

# import functools
try:
from os.path import normcase
except ImportError:

def normcase(s):
"""
From os.path.normcase
Normalize the case of a pathname. On Windows, convert all characters
in the pathname to lowercase, and also convert forward slashes to
backward slashes. On other operating systems, return the path unchanged.
"""
return s


__all__ = ["filter", "fnmatch", "fnmatchcase", "translate"]

COMPAT = re.__name__ == "ure"


def fnmatch(name, pat):
"""Test whether FILENAME matches PATTERN.
Expand All @@ -33,8 +45,8 @@ def fnmatch(name, pat):
if the operating system requires it.
If you don't want this, use fnmatchcase(FILENAME, PATTERN).
"""
name = os.path.normcase(name)
pat = os.path.normcase(pat)
name = normcase(name)
pat = normcase(pat)
return fnmatchcase(name, pat)


Expand All @@ -46,16 +58,21 @@ def _compile_pattern(pat):
res = bytes(res_str, "ISO-8859-1")
else:
res = translate(pat)
if COMPAT:
if res.startswith("(?ms)"):
res = res[5:]
if res.endswith("\\Z"):
res = res[:-2] + "$"
return re.compile(res).match


def filter(names, pat):
"""Return the subset of the list NAMES that match PAT."""
result = []
pat = os.path.normcase(pat)
pat = normcase(pat)
match = _compile_pattern(pat)
for name in names:
if match(os.path.normcase(name)):
if match(normcase(name)):
result.append(name)
return result

Expand Down Expand Up @@ -104,6 +121,15 @@ def translate(pat):
stuff = "\\" + stuff
res = "%s[%s]" % (res, stuff)
else:
res = res + re.escape(c)
try:
res = res + re.escape(c)
except AttributeError:
# Using ure rather than re-pcre
res = res + re_escape(c)
# Original patterns is undefined, see http://bugs.python.org/issue21464
return "(?ms)" + res + "\Z"


def re_escape(pattern):
# Replacement minimal re.escape for ure compatibility
return re.sub(r"([\^\$\.\|\?\*\+\(\)\[\\])", r"\\\1", pattern)
3 changes: 1 addition & 2 deletions python-stdlib/fnmatch/metadata.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
srctype = cpython
type = module
version = 0.5.2
depends = os, os.path, re-pcre
version = 0.6.0
3 changes: 1 addition & 2 deletions python-stdlib/fnmatch/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name="micropython-fnmatch",
version="0.5.2",
version="0.6.0",
description="CPython fnmatch module ported to MicroPython",
long_description="This is a module ported from CPython standard library to be compatible with\nMicroPython interpreter. Usually, this means applying small patches for\nfeatures not supported (yet, or at all) in MicroPython. Sometimes, heavier\nchanges are required. Note that CPython modules are written with availability\nof vast resources in mind, and may not work for MicroPython ports with\nlimited heap. If you are affected by such a case, please help reimplement\nthe module from scratch.",
url="https://github.com/micropython/micropython-lib",
Expand All @@ -21,5 +21,4 @@
license="Python",
cmdclass={"sdist": sdist_upip.sdist},
py_modules=["fnmatch"],
install_requires=["micropython-os", "micropython-os.path", "micropython-re-pcre"],
)
7 changes: 4 additions & 3 deletions python-stdlib/fnmatch/test_fnmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class FnmatchTestCase(unittest.TestCase):
def check_match(self, filename, pattern, should_match=1, fn=fnmatch):
if should_match:
self.assertTrue(
fn(filename, pattern), "expected %r to match pattern %r" % (filename, pattern)
fn(filename, pattern),
"expected %r to match pattern %r" % (filename, pattern),
)
else:
self.assertTrue(
Expand Down Expand Up @@ -80,9 +81,9 @@ def test_filter(self):
self.assertEqual(filter(["a", "b"], "a"), ["a"])


def test_main():
def main():
support.run_unittest(FnmatchTestCase, TranslateTestCase, FilterTestCase)


if __name__ == "__main__":
test_main()
main()
3 changes: 2 additions & 1 deletion python-stdlib/unittest/metadata.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
srctype = micropython-lib
type = module
version = 0.3.2
version = 0.9.0
depends = argparse, fnmatch
3 changes: 2 additions & 1 deletion python-stdlib/unittest/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name="micropython-unittest",
version="0.3.2",
version="0.9.0",
description="unittest module for MicroPython",
long_description="This is a module reimplemented specifically for MicroPython standard library,\nwith efficient and lean design in mind. Note that this module is likely work\nin progress and likely supports just a subset of CPython's corresponding\nmodule. Please help with the development if you are interested in this\nmodule.",
url="https://github.com/micropython/micropython-lib",
Expand All @@ -21,4 +21,5 @@
license="MIT",
cmdclass={"sdist": sdist_upip.sdist},
py_modules=["unittest"],
install_requires=["micropython-argparse", "micropython-fnmatch"],
)
68 changes: 66 additions & 2 deletions python-stdlib/unittest/test_unittest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
from test_unittest_isolated import global_context


class TestUnittestAssertions(unittest.TestCase):
Expand Down Expand Up @@ -37,7 +38,7 @@ def test_AlmostEqual(self):
with self.assertRaises(AssertionError):
self.assertNotAlmostEqual(float("inf"), float("inf"))

def test_AmostEqualWithDelta(self):
def test_AlmostEqualWithDelta(self):
self.assertAlmostEqual(1.1, 1.0, delta=0.5)
self.assertAlmostEqual(1.0, 1.1, delta=0.5)
self.assertNotAlmostEqual(1.1, 1.0, delta=0.05)
Expand Down Expand Up @@ -109,7 +110,70 @@ def testRaises(self):

@unittest.skip("test of skipping")
def testSkip(self):
self.assertFail("this should be skipped")
self.fail("this should be skipped")

def testAssert(self):

e1 = None
try:

def func_under_test(a):
assert a > 10

self.assertRaises(AssertionError, func_under_test, 20)
except AssertionError as e:
e1 = e

if not e1 or "not raised" not in e1.args[0]:
self.fail("Expected to catch lack of AssertionError from assert in func_under_test")

@unittest.expectedFailure
def testExpectedFailure(self):
self.assertEqual(1, 0)

def testExpectedFailureNot(self):
@unittest.expectedFailure
def testInner():
self.assertEqual(1, 1)

try:
testInner()
except:
pass
else:
self.fail("Unexpected success was not detected")

def test_NotChangedByOtherTest(self):
global global_context
assert global_context is None
global_context = True

def test_subtest_even(self):
"""
Test that numbers between 0 and 5 are all even.
"""
for i in range(0, 10, 2):
with self.subTest("Should only pass for even numbers", i=i):
self.assertEqual(i % 2, 0)


class TestUnittestSetup(unittest.TestCase):
class_setup_var = 0

def setUpClass(self):
TestUnittestSetup.class_setup_var += 1

def tearDownClass(self):
# Not sure how to actually test this, but we can check (in the test case below)
# that it hasn't been run already at least.
TestUnittestSetup.class_setup_var = -1

def testSetUpTearDownClass_1(self):
assert TestUnittestSetup.class_setup_var == 1, TestUnittestSetup.class_setup_var

def testSetUpTearDownClass_2(self):
# Test this twice, as if setUpClass() gets run like setUp() it would be run twice
assert TestUnittestSetup.class_setup_var == 1, TestUnittestSetup.class_setup_var


if __name__ == "__main__":
Expand Down
15 changes: 15 additions & 0 deletions python-stdlib/unittest/test_unittest_isolated.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import unittest


global_context = None


class TestUnittestIsolated(unittest.TestCase):
def test_NotChangedByOtherTest(self):
global global_context
assert global_context is None
global_context = True


if __name__ == "__main__":
unittest.main()
Loading