|
|
e8d769 |
From 37a4d0dbeb9a92b959edfb9b1aceba4eaacf9f78 Mon Sep 17 00:00:00 2001
|
|
|
e8d769 |
From: Alex Panov <thealexpanov@gmail.com>
|
|
|
e8d769 |
Date: Sun, 15 May 2016 23:36:18 -0400
|
|
|
e8d769 |
Subject: [PATCH] Add boolean matchers
|
|
|
e8d769 |
|
|
|
e8d769 |
---
|
|
|
e8d769 |
README.rst | 5 ++++
|
|
|
e8d769 |
src/hamcrest/library/__init__.py | 3 ++
|
|
|
e8d769 |
src/hamcrest/library/bool/__init__.py | 1 +
|
|
|
e8d769 |
src/hamcrest/library/bool/bool_comparison.py | 22 ++++++++++++++
|
|
|
e8d769 |
tests/hamcrest_unit_test/bool/__init__.py | 0
|
|
|
e8d769 |
.../bool/bool_comparison_test.py | 34 ++++++++++++++++++++++
|
|
|
e8d769 |
6 files changed, 65 insertions(+)
|
|
|
e8d769 |
create mode 100644 src/hamcrest/library/bool/__init__.py
|
|
|
e8d769 |
create mode 100644 src/hamcrest/library/bool/bool_comparison.py
|
|
|
e8d769 |
create mode 100644 tests/hamcrest_unit_test/bool/__init__.py
|
|
|
e8d769 |
create mode 100644 tests/hamcrest_unit_test/bool/bool_comparison_test.py
|
|
|
e8d769 |
|
|
|
e8d769 |
diff --git a/README.rst b/README.rst
|
|
|
e8d769 |
index 8ef46bb..d2200f8 100644
|
|
|
e8d769 |
--- a/README.rst
|
|
|
e8d769 |
+++ b/README.rst
|
|
|
e8d769 |
@@ -148,6 +148,11 @@ PyHamcrest comes with a library of useful matchers:
|
|
|
e8d769 |
* ``greater_than``, ``greater_than_or_equal_to``, ``less_than``,
|
|
|
e8d769 |
``less_than_or_equal_to`` - match numeric ordering
|
|
|
e8d769 |
|
|
|
e8d769 |
+* Boolean
|
|
|
e8d769 |
+
|
|
|
e8d769 |
+ * ``is_true`` - verify the value is True
|
|
|
e8d769 |
+ * ``is_false`` - verify the value is False
|
|
|
e8d769 |
+
|
|
|
e8d769 |
* Text
|
|
|
e8d769 |
|
|
|
e8d769 |
* ``contains_string`` - match part of a string
|
|
|
e8d769 |
diff --git a/src/hamcrest/library/__init__.py b/src/hamcrest/library/__init__.py
|
|
|
e8d769 |
index a5a7963..55dfcda 100644
|
|
|
e8d769 |
--- a/src/hamcrest/library/__init__.py
|
|
|
e8d769 |
+++ b/src/hamcrest/library/__init__.py
|
|
|
e8d769 |
@@ -7,6 +7,7 @@ from hamcrest.library.integration import *
|
|
|
e8d769 |
from hamcrest.library.number import *
|
|
|
e8d769 |
from hamcrest.library.object import *
|
|
|
e8d769 |
from hamcrest.library.text import *
|
|
|
e8d769 |
+from hamcrest.library.bool import *
|
|
|
e8d769 |
|
|
|
e8d769 |
__author__ = "Jon Reid"
|
|
|
e8d769 |
__copyright__ = "Copyright 2011 hamcrest.org"
|
|
|
e8d769 |
@@ -41,4 +42,6 @@ __all__ = [
|
|
|
e8d769 |
'ends_with',
|
|
|
e8d769 |
'starts_with',
|
|
|
e8d769 |
'string_contains_in_order',
|
|
|
e8d769 |
+ 'is_true',
|
|
|
e8d769 |
+ 'is_false'
|
|
|
e8d769 |
]
|
|
|
e8d769 |
diff --git a/src/hamcrest/library/bool/__init__.py b/src/hamcrest/library/bool/__init__.py
|
|
|
e8d769 |
new file mode 100644
|
|
|
e8d769 |
index 0000000..7cf13a3
|
|
|
e8d769 |
--- /dev/null
|
|
|
e8d769 |
+++ b/src/hamcrest/library/bool/__init__.py
|
|
|
e8d769 |
@@ -0,0 +1 @@
|
|
|
e8d769 |
+from .bool_comparison import is_true, is_false
|
|
|
e8d769 |
diff --git a/src/hamcrest/library/bool/bool_comparison.py b/src/hamcrest/library/bool/bool_comparison.py
|
|
|
e8d769 |
new file mode 100644
|
|
|
e8d769 |
index 0000000..af7e1b6
|
|
|
e8d769 |
--- /dev/null
|
|
|
e8d769 |
+++ b/src/hamcrest/library/bool/bool_comparison.py
|
|
|
e8d769 |
@@ -0,0 +1,22 @@
|
|
|
e8d769 |
+from hamcrest.core.base_matcher import BaseMatcher
|
|
|
e8d769 |
+
|
|
|
e8d769 |
+
|
|
|
e8d769 |
+class IsABool(BaseMatcher):
|
|
|
e8d769 |
+ def __init__(self, boolean_value):
|
|
|
e8d769 |
+ self.boolean_value = boolean_value
|
|
|
e8d769 |
+
|
|
|
e8d769 |
+ def describe_to(self, description):
|
|
|
e8d769 |
+ description.append_text(str(self.boolean_value))
|
|
|
e8d769 |
+
|
|
|
e8d769 |
+ def _matches(self, item):
|
|
|
e8d769 |
+ if not isinstance(item, bool):
|
|
|
e8d769 |
+ return False
|
|
|
e8d769 |
+ return item == self.boolean_value
|
|
|
e8d769 |
+
|
|
|
e8d769 |
+
|
|
|
e8d769 |
+def is_true():
|
|
|
e8d769 |
+ return IsABool(True)
|
|
|
e8d769 |
+
|
|
|
e8d769 |
+
|
|
|
e8d769 |
+def is_false():
|
|
|
e8d769 |
+ return IsABool(False)
|
|
|
e8d769 |
diff --git a/tests/hamcrest_unit_test/bool/__init__.py b/tests/hamcrest_unit_test/bool/__init__.py
|
|
|
e8d769 |
new file mode 100644
|
|
|
e8d769 |
index 0000000..e69de29
|
|
|
e8d769 |
diff --git a/tests/hamcrest_unit_test/bool/bool_comparison_test.py b/tests/hamcrest_unit_test/bool/bool_comparison_test.py
|
|
|
e8d769 |
new file mode 100644
|
|
|
e8d769 |
index 0000000..e865365
|
|
|
e8d769 |
--- /dev/null
|
|
|
e8d769 |
+++ b/tests/hamcrest_unit_test/bool/bool_comparison_test.py
|
|
|
e8d769 |
@@ -0,0 +1,34 @@
|
|
|
e8d769 |
+from hamcrest import assert_that, equal_to
|
|
|
e8d769 |
+from hamcrest.core.string_description import StringDescription
|
|
|
e8d769 |
+from hamcrest.library.bool import is_false, is_true
|
|
|
e8d769 |
+from hamcrest_unit_test.matcher_test import MatcherTest
|
|
|
e8d769 |
+
|
|
|
e8d769 |
+
|
|
|
e8d769 |
+class BoolComparisonTest(MatcherTest):
|
|
|
e8d769 |
+ def test_true_is_true(self):
|
|
|
e8d769 |
+ self.assert_matches('Is True', is_true(), True)
|
|
|
e8d769 |
+
|
|
|
e8d769 |
+ def test_false_is_not_true(self):
|
|
|
e8d769 |
+ self.assert_does_not_match('False', is_true(), False)
|
|
|
e8d769 |
+
|
|
|
e8d769 |
+ def test_false_is_false(self):
|
|
|
e8d769 |
+ self.assert_matches('False', is_false(), False)
|
|
|
e8d769 |
+
|
|
|
e8d769 |
+ def test_true_is_not_false(self):
|
|
|
e8d769 |
+ self.assert_does_not_match('True', is_false(), True)
|
|
|
e8d769 |
+
|
|
|
e8d769 |
+ def test_number_is_not_true(self):
|
|
|
e8d769 |
+ self.assert_does_not_match('True', is_true(), 1)
|
|
|
e8d769 |
+
|
|
|
e8d769 |
+ def test_number_is_not_false(self):
|
|
|
e8d769 |
+ self.assert_does_not_match('False', is_false(), 1)
|
|
|
e8d769 |
+
|
|
|
e8d769 |
+ def test_is_true_description(self):
|
|
|
e8d769 |
+ description = StringDescription()
|
|
|
e8d769 |
+ is_true().describe_to(description)
|
|
|
e8d769 |
+ assert_that(str(description), equal_to('True'))
|
|
|
e8d769 |
+
|
|
|
e8d769 |
+ def test_is_false_description(self):
|
|
|
e8d769 |
+ description = StringDescription()
|
|
|
e8d769 |
+ is_false().describe_to(description)
|
|
|
e8d769 |
+ assert_that(str(description), equal_to('False'))
|
|
|
e8d769 |
--
|
|
|
e8d769 |
2.9.3
|
|
|
e8d769 |
|