|
| 1 | +import unittest |
| 2 | +import re |
| 3 | + |
| 4 | +from fassert import FuzzyAssert |
| 5 | + |
| 6 | + |
| 7 | +class ConfigurationTest(unittest.TestCase): |
| 8 | + def test_eval_function(self): |
| 9 | + data = "test" |
| 10 | + template = lambda x: len(x) == 4 |
| 11 | + |
| 12 | + fasserter = FuzzyAssert() |
| 13 | + |
| 14 | + self.assertIs(fasserter.eval_functions, True) |
| 15 | + fasserter.match(data, template) |
| 16 | + |
| 17 | + fasserter.eval_functions = False |
| 18 | + self.assertRaises(AssertionError, fasserter.match, data, template) |
| 19 | + |
| 20 | + def test_fuzzy_sequence_types(self): |
| 21 | + test_data = ( |
| 22 | + ((), []), |
| 23 | + (set(), ()), |
| 24 | + (set(), []), |
| 25 | + ) |
| 26 | + |
| 27 | + fasserter = FuzzyAssert() |
| 28 | + self.assertIs(fasserter.fuzzy_sequence_types, False) |
| 29 | + |
| 30 | + for data, template in test_data: |
| 31 | + with self.subTest(data=data, template=template): |
| 32 | + self.assertRaises(AssertionError, fasserter.match, data, template) |
| 33 | + |
| 34 | + with self.subTest(data=template, template=data): |
| 35 | + self.assertRaises(AssertionError, fasserter.match, template, data) |
| 36 | + |
| 37 | + def test_check_minimum_sequence_length(self): |
| 38 | + test_data = ( |
| 39 | + ([""], ["", ""]), |
| 40 | + (["test"], [re.compile(".{4}"), "test", re.compile("^test$")]) |
| 41 | + ) |
| 42 | + |
| 43 | + fasserter = FuzzyAssert() |
| 44 | + |
| 45 | + |
| 46 | + for data, template in test_data: |
| 47 | + with self.subTest(data=data, template=template): |
| 48 | + fasserter.check_minimum_sequence_length = True |
| 49 | + self.assertRaises(AssertionError, fasserter.match, data, template) |
| 50 | + |
| 51 | + fasserter.check_minimum_sequence_length = False |
| 52 | + self.assertIs(fasserter.match(data, template), True) |
| 53 | + |
| 54 | + |
| 55 | + def test_regex_allowed(self): |
| 56 | + test_data = ( |
| 57 | + ("", re.compile("")), |
| 58 | + ("test", re.compile("^test$")), |
| 59 | + ("test", re.compile("test")), |
| 60 | + ("test", re.compile(".{4}")) |
| 61 | + ) |
| 62 | + |
| 63 | + fasserter = FuzzyAssert() |
| 64 | + |
| 65 | + for data, template in test_data: |
| 66 | + with self.subTest(data=data, template=template): |
| 67 | + fasserter.regex_allowed = False |
| 68 | + self.assertRaises(AssertionError, fasserter.match, data, template) |
| 69 | + |
| 70 | + fasserter.regex_allowed = True |
| 71 | + self.assertIs(fasserter.match(data, template), True) |
| 72 | + |
| 73 | + with self.subTest(data=template, template=data): |
| 74 | + fasserter.regex_allowed = True |
| 75 | + # a string would never match regex, e.g. the regex matches are one way only from template to data |
| 76 | + self.assertRaises(AssertionError, fasserter.match, template, data) |
0 commit comments