1 | """ |
---|
2 | Tests for allmydata.util.humanreadable. |
---|
3 | |
---|
4 | This module has been ported to Python 3. |
---|
5 | """ |
---|
6 | |
---|
7 | from twisted.trial import unittest |
---|
8 | |
---|
9 | from allmydata.util import humanreadable |
---|
10 | |
---|
11 | |
---|
12 | |
---|
13 | def foo(): pass # FYI foo()'s line number is used in the test below |
---|
14 | |
---|
15 | |
---|
16 | class NoArgumentException(Exception): |
---|
17 | def __init__(self): |
---|
18 | pass |
---|
19 | |
---|
20 | class HumanReadable(unittest.TestCase): |
---|
21 | def test_repr(self): |
---|
22 | hr = humanreadable.hr |
---|
23 | # we match on regex so this test isn't fragile about line-numbers |
---|
24 | self.assertRegex(hr(foo), r"<foo\(\) at test_humanreadable.py:\d+>") |
---|
25 | self.failUnlessEqual(hr(self.test_repr), |
---|
26 | "<bound method HumanReadable.test_repr of <allmydata.test.test_humanreadable.HumanReadable testMethod=test_repr>>") |
---|
27 | self.failUnlessEqual(hr(1), "1") |
---|
28 | self.assertIn(hr(10**40), |
---|
29 | ["100000000000000000...000000000000000000", |
---|
30 | "100000000000000000...0000000000000000000"]) |
---|
31 | self.failUnlessEqual(hr(self), "<allmydata.test.test_humanreadable.HumanReadable testMethod=test_repr>") |
---|
32 | self.failUnlessEqual(hr([1,2]), "[1, 2]") |
---|
33 | self.failUnlessEqual(hr({1:2}), "{1:2}") |
---|
34 | try: |
---|
35 | raise ValueError |
---|
36 | except Exception as e: |
---|
37 | self.failUnless( |
---|
38 | hr(e) == "<ValueError: ()>" # python-2.4 |
---|
39 | or hr(e) == "ValueError()") # python-2.5 |
---|
40 | try: |
---|
41 | raise ValueError("oops") |
---|
42 | except Exception as e: |
---|
43 | self.failUnless( |
---|
44 | hr(e) == "<ValueError: 'oops'>" # python-2.4 |
---|
45 | or hr(e) == "ValueError('oops',)" # python-2.5 |
---|
46 | or hr(e) == "ValueError(u'oops',)" # python 2 during py3 transition |
---|
47 | ) |
---|
48 | try: |
---|
49 | raise NoArgumentException |
---|
50 | except Exception as e: |
---|
51 | self.failUnless( |
---|
52 | hr(e) == "<NoArgumentException>" # python-2.4 |
---|
53 | or hr(e) == "NoArgumentException()" # python-2.5 |
---|
54 | or hr(e) == "<NoArgumentException: ()>", hr(e)) # python-3 |
---|