Created
November 21, 2015 10:23
-
-
Save behrtam/2894facca0f35f642300 to your computer and use it in GitHub Desktop.
Add support for caseless assertions that also work with unicode through mixin.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import unittest | |
import unicodedata | |
''' There is text for which text.lower() != text.upper().lower(), such as "ß": | |
"ß".lower() | |
#>>> 'ß' | |
"ß".upper().lower() | |
#>>> 'ss' | |
http://stackoverflow.com/a/29247821 | |
''' | |
def normalize_caseless(text): | |
return unicodedata.normalize("NFKD", text.casefold()) | |
def caseless_equal(left, right): | |
return normalize_caseless(left) == normalize_caseless(right) | |
class CaselessAssertions: | |
def assertCaselessEqual(self, left, right): | |
self.assertEqual(normalize_caseless(left), normalize_caseless(right)) | |
def assertCaselessDictEqual(self, left, right): | |
left = {normalize_caseless(k): v for k, v in left.items()} | |
right = {normalize_caseless(k): v for k, v in right.items()} | |
self.assertDictEqual(left, right) | |
class CaselessTests(unittest.TestCase, CaselessAssertions): | |
def test_equal(self): | |
self.assertCaselessEqual("Test", "test") | |
def test_equal_fail(self): | |
self.assertCaselessEqual("Test", "testx") | |
def test_dict(self): | |
d1 = {'X': 3, 'Y': 2} | |
d2 = {'y': 2, 'x': 3} | |
self.assertCaselessDictEqual(d1, d2) | |
def test_dict_fail_value(self): | |
d1 = {'X': 3, 'Y': 2} | |
d2 = {'y': 3, 'x': 3} | |
self.assertCaselessDictEqual(d1, d2) | |
def test_dict_fail_key(self): | |
d1 = {'X': 3, 'Y': 2} | |
d2 = {'y': 2, 'a': 3} | |
self.assertCaselessDictEqual(d1, d2) | |
def test_unicode(self): | |
d1 = {'ß': 42, '💾💿': 11} | |
d2 = {'ß': 42, '💾💿': 11} | |
self.assertCaselessDictEqual(d1, d2) | |
if __name__ == '__main__': | |
unittest.main() | |
''' OUTPUT | |
.FF.F. | |
====================================================================== | |
FAIL: test_dict_fail_key (__main__.CaselessTests) | |
---------------------------------------------------------------------- | |
Traceback (most recent call last): | |
File "Spielwiese/caseless.py", line 44, in test_dict_fail_key | |
self.assertCaselessDictEqual(d1, d2) | |
File "Spielwiese/caseless.py", line 21, in assertCaselessDictEqual | |
self.assertDictEqual(left, right) | |
AssertionError: {'x': 3, 'y': 2} != {'a': 3, 'y': 2} | |
- {'x': 3, 'y': 2} | |
? ^ | |
+ {'a': 3, 'y': 2} | |
? ^ | |
====================================================================== | |
FAIL: test_dict_fail_value (__main__.CaselessTests) | |
---------------------------------------------------------------------- | |
Traceback (most recent call last): | |
File "Spielwiese/caseless.py", line 39, in test_dict_fail_value | |
self.assertCaselessDictEqual(d1, d2) | |
File "Spielwiese/caseless.py", line 21, in assertCaselessDictEqual | |
self.assertDictEqual(left, right) | |
AssertionError: {'x': 3, 'y': 2} != {'x': 3, 'y': 3} | |
- {'x': 3, 'y': 2} | |
? ^ | |
+ {'x': 3, 'y': 3} | |
? ^ | |
====================================================================== | |
FAIL: test_equal_fail (__main__.CaselessTests) | |
---------------------------------------------------------------------- | |
Traceback (most recent call last): | |
File "Spielwiese/caseless.py", line 29, in test_equal_fail | |
self.assertCaselessEqual("Test", "testx") | |
File "Spielwiese/caseless.py", line 16, in assertCaselessEqual | |
self.assertEqual(normalize_caseless(left), normalize_caseless(right)) | |
AssertionError: 'test' != 'testx' | |
- test | |
+ testx | |
? + | |
---------------------------------------------------------------------- | |
Ran 6 tests in 0.005s | |
FAILED (failures=3) | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment