File: string_description_test.py

package info (click to toggle)
pyhamcrest 2.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 844 kB
  • sloc: python: 4,081; makefile: 114; sh: 15
file content (59 lines) | stat: -rw-r--r-- 1,899 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import re
import unittest

import pytest
from hamcrest.core.selfdescribing import SelfDescribing
from hamcrest.core.string_description import StringDescription

__author__ = "Jon Reid"
__copyright__ = "Copyright 2011 hamcrest.org"
__license__ = "BSD, see License.txt"


class FakeSelfDescribing(SelfDescribing):
    def describe_to(self, description):
        description.append_text("DESCRIPTION")


class StringDescriptionTest(unittest.TestCase):
    def setUp(self):
        self.description = StringDescription()

    def testLetsSelfDescribingObjectDescribeItself(self):
        self.description.append_description_of(FakeSelfDescribing())
        self.assertEqual("DESCRIPTION", str(self.description))

    def testDescribesStringInQuotes(self):
        self.description.append_description_of("FOO")
        self.assertEqual("'FOO'", str(self.description))

    def testWrapsNonSelfDescribingObjectInAngleBrackets(self):
        self.description.append_description_of(42)
        self.assertEqual("<42>", str(self.description))

    def testShouldNotAddAngleBracketsIfObjectDescriptionAlreadyHasThem(self):
        self.description.append_description_of(object())
        expected = re.compile("<object object at 0x[0-9a-fA-F]+>")
        self.assertTrue(expected.match(str(self.description)))

    def testDescribeUnicodeStringAsUnicode(self):
        self.description.append_description_of("\u05d0")
        self.assertEqual("'\u05d0'", str(self.description))


# below is a set of things that should append without error to string
# descriptions
@pytest.mark.parametrize("valid_input", (b"bytes", "unicode"))
def test_description_append_valid_input(valid_input):
    desc = StringDescription()
    desc.append(valid_input)
    str(desc)


def test_description_append_invalid_input():
    desc = StringDescription()
    desc.append(chr(239))


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