Paul Kehrer | 874445a | 2016-12-13 11:09:35 -0600 | [diff] [blame] | 1 | # This file is dual licensed under the terms of the Apache License, Version |
| 2 | # 2.0, and the BSD License. See the LICENSE file in the root of this repository |
| 3 | # for complete details. |
| 4 | |
| 5 | from __future__ import absolute_import, division, print_function |
| 6 | |
Alex Gaynor | 0c9aed9 | 2017-07-08 21:50:01 -0400 | [diff] [blame^] | 7 | import pytest |
| 8 | |
Paul Kehrer | 874445a | 2016-12-13 11:09:35 -0600 | [diff] [blame] | 9 | from cryptography import utils |
| 10 | |
| 11 | |
| 12 | def test_int_from_bytes_bytearray(): |
| 13 | assert utils.int_from_bytes(bytearray(b"\x02\x10"), "big") == 528 |
Alex Gaynor | 0c9aed9 | 2017-07-08 21:50:01 -0400 | [diff] [blame^] | 14 | |
| 15 | |
| 16 | class TestCachedProperty(object): |
| 17 | def test_simple(self): |
| 18 | accesses = [] |
| 19 | |
| 20 | class T(object): |
| 21 | @utils.cached_property |
| 22 | def t(self): |
| 23 | accesses.append(None) |
| 24 | return 14 |
| 25 | |
| 26 | assert T.t |
| 27 | t = T() |
| 28 | assert t.t == 14 |
| 29 | assert len(accesses) == 1 |
| 30 | assert t.t == 14 |
| 31 | assert len(accesses) == 1 |
| 32 | |
| 33 | t = T() |
| 34 | assert t.t == 14 |
| 35 | assert len(accesses) == 2 |
| 36 | assert t.t == 14 |
| 37 | assert len(accesses) == 2 |
| 38 | |
| 39 | def test_set(self): |
| 40 | accesses = [] |
| 41 | |
| 42 | class T(object): |
| 43 | @utils.cached_property |
| 44 | def t(self): |
| 45 | accesses.append(None) |
| 46 | return 14 |
| 47 | |
| 48 | t = T() |
| 49 | with pytest.raises(AttributeError): |
| 50 | t.t = None |
| 51 | assert len(accesses) == 0 |
| 52 | assert t.t == 14 |
| 53 | assert len(accesses) == 1 |
| 54 | with pytest.raises(AttributeError): |
| 55 | t.t = None |
| 56 | assert len(accesses) == 1 |
| 57 | assert t.t == 14 |
| 58 | assert len(accesses) == 1 |