|
| 1 | +import unittest |
| 2 | +from fluentcheck.check import Check, CheckError |
| 3 | + |
| 4 | + |
| 5 | +class BooleanObject: |
| 6 | + def __init__(self, is_true): |
| 7 | + self.is_true = is_true |
| 8 | + def __bool__(self): |
| 9 | + return self.is_true |
| 10 | + |
| 11 | + |
| 12 | +class BooleanByLengthObject: |
| 13 | + |
| 14 | + def __init__(self, items = None): |
| 15 | + self.items = [] if items is None else items |
| 16 | + def __len__(self): |
| 17 | + return len(self.items) |
| 18 | + |
| 19 | + |
| 20 | +class TestBooleansAssertions(unittest.TestCase): |
| 21 | + |
| 22 | + def setUp(self): |
| 23 | + self.falsy_values = ([], (), {}, set(), "", '', |
| 24 | + range(0), 0, 0.0, 0j, None, False, |
| 25 | + BooleanObject(False), BooleanByLengthObject()) |
| 26 | + self.truthy_values = ([1,], ('a',), {1:'one'}, set([1,1,]), "1", '2', |
| 27 | + range(1), 1, 1.0, 1j, not None, True, |
| 28 | + BooleanObject(True), BooleanByLengthObject([1,2])) |
| 29 | + |
| 30 | + def test_is_boolean(self): |
| 31 | + res = Check(1 == 2).is_boolean() |
| 32 | + self.assertIsInstance(res, Check) |
| 33 | + try: |
| 34 | + Check(1).is_boolean() |
| 35 | + self.fail() |
| 36 | + except CheckError: |
| 37 | + pass |
| 38 | + |
| 39 | + def test_is_not_boolean(self): |
| 40 | + res = Check(1).is_not_boolean() |
| 41 | + self.assertIsInstance(res, Check) |
| 42 | + try: |
| 43 | + Check(1 == 2).is_not_boolean() |
| 44 | + self.fail() |
| 45 | + except CheckError: |
| 46 | + pass |
| 47 | + |
| 48 | + def test_is_true(self): |
| 49 | + res = Check(1 == 1).is_true() |
| 50 | + self.assertIsInstance(res, Check) |
| 51 | + try: |
| 52 | + Check(1 == 2).is_true() |
| 53 | + self.fail() |
| 54 | + except CheckError: |
| 55 | + pass |
| 56 | + |
| 57 | + def test_is_not_true(self): |
| 58 | + res = Check(1 == 2).is_not_true() |
| 59 | + self.assertIsInstance(res, Check) |
| 60 | + try: |
| 61 | + Check(1 == 1).is_not_true() |
| 62 | + self.fail() |
| 63 | + except CheckError: |
| 64 | + pass |
| 65 | + |
| 66 | + def test_is_false(self): |
| 67 | + res = Check(1 == 2).is_false() |
| 68 | + self.assertIsInstance(res, Check) |
| 69 | + try: |
| 70 | + Check(1 == 1).is_false() |
| 71 | + self.fail() |
| 72 | + except CheckError: |
| 73 | + pass |
| 74 | + |
| 75 | + def test_is_not_false(self): |
| 76 | + res = Check(1 == 1).is_not_false() |
| 77 | + self.assertIsInstance(res, Check) |
| 78 | + try: |
| 79 | + Check(1 == 2).is_not_false() |
| 80 | + self.fail() |
| 81 | + except CheckError: |
| 82 | + pass |
| 83 | + |
| 84 | + def test_is_falsy(self): |
| 85 | + for item in self.falsy_values: |
| 86 | + res = Check(item).is_falsy() |
| 87 | + self.assertIsInstance(res, Check) |
| 88 | + try: |
| 89 | + Check(not item).is_falsy() |
| 90 | + self.fail() |
| 91 | + except CheckError: |
| 92 | + pass |
| 93 | + |
| 94 | + def test_is_not_falsy(self): |
| 95 | + for item in self.falsy_values: |
| 96 | + res = Check(not item).is_not_falsy() |
| 97 | + self.assertIsInstance(res, Check) |
| 98 | + try: |
| 99 | + Check(item).is_not_falsy() |
| 100 | + self.fail() |
| 101 | + except CheckError: |
| 102 | + pass |
| 103 | + |
| 104 | + def test_is_truthy(self): |
| 105 | + for item in self.truthy_values: |
| 106 | + res = Check(item).is_truthy() |
| 107 | + self.assertIsInstance(res, Check) |
| 108 | + try: |
| 109 | + Check(not item).is_truthy() |
| 110 | + self.fail() |
| 111 | + except CheckError: |
| 112 | + pass |
| 113 | + |
| 114 | + def test_is_not_truthy(self): |
| 115 | + for item in self.truthy_values: |
| 116 | + res = Check(not item).is_not_truthy() |
| 117 | + self.assertIsInstance(res, Check) |
| 118 | + try: |
| 119 | + Check(item).is_not_truthy() |
| 120 | + self.fail() |
| 121 | + except CheckError: |
| 122 | + pass |
| 123 | + |
| 124 | + def test_has_same_truth_of(self): |
| 125 | + res = Check(BooleanObject(True)).has_same_truth_of(BooleanByLengthObject([1,2])) |
| 126 | + self.assertIsInstance(res, Check) |
| 127 | + try: |
| 128 | + Check(BooleanObject(False)).has_same_truth_of(BooleanByLengthObject([1,2])) |
| 129 | + self.fail() |
| 130 | + except CheckError: |
| 131 | + pass |
| 132 | + |
| 133 | + def test_has_opposite_truth_of(self): |
| 134 | + res = Check(BooleanObject(False)).has_opposite_truth_of(BooleanByLengthObject([1,])) |
| 135 | + self.assertIsInstance(res, Check) |
| 136 | + try: |
| 137 | + Check(BooleanObject(False)).has_opposite_truth_of(BooleanByLengthObject()) |
| 138 | + self.fail() |
| 139 | + except CheckError: |
| 140 | + pass |
0 commit comments